Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add typo-resistency to filter graceful #4745

Open
wants to merge 9 commits into
base: master
Choose a base branch
from
40 changes: 33 additions & 7 deletions src/util/filter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,21 +81,47 @@
// permutations of the pattern to find a better match. The
// permutations only swap neighbouring characters, e.g
// `cnoso` becomes `conso`, `cnsoo`, `cnoos`.
const tries = Math.min(7, pattern.length - 1)
for (let movingPatternPos = patternPos + 1; movingPatternPos < tries; movingPatternPos++) {
const newPattern = nextTypoPermutation(pattern, movingPatternPos)
//
// For the last 2 permutations try to remove the last characters,
// maybe the last few letters are typos. For example,
// `conson` could be a typo for `console`
const maxTriesPermutation = Math.min(7, pattern.length - 1);

Check failure on line 88 in src/util/filter.ts

View workflow job for this annotation

GitHub Actions / Lint

Extra semicolon
const maxTries = maxTriesPermutation + pattern.length;

Check failure on line 89 in src/util/filter.ts

View workflow job for this annotation

GitHub Actions / Lint

Extra semicolon

// Loop for existing permutations
for (let i = 1; i <= maxTriesPermutation; i++) {
const newPattern = nextTypoPermutation(pattern, patternPos + i);

Check failure on line 93 in src/util/filter.ts

View workflow job for this annotation

GitHub Actions / Lint

Extra semicolon
checkAndUpdateTopCandidate(newPattern, 3);

Check failure on line 94 in src/util/filter.ts

View workflow job for this annotation

GitHub Actions / Lint

Extra semicolon
}

const lastChar = pattern.charAt(pattern.length - 1);

Check failure on line 97 in src/util/filter.ts

View workflow job for this annotation

GitHub Actions / Lint

Extra semicolon

if ((/[a-zA-Z0-9]/.test(lastChar))) {
// Loop for removing characters at all positions
for (let i = 0; i < pattern.length; i++) {
const newPattern = pattern.slice(0, i) + pattern.slice(i + 1);

Check failure on line 102 in src/util/filter.ts

View workflow job for this annotation

GitHub Actions / Lint

Extra semicolon
checkAndUpdateTopCandidate(newPattern, 4);

Check failure on line 103 in src/util/filter.ts

View workflow job for this annotation

GitHub Actions / Lint

Extra semicolon
}

// Loop for removing last characters
for (let i = 1; i < 3; i++) {
const newPattern = pattern.slice(0, pattern.length - i);

Check failure on line 108 in src/util/filter.ts

View workflow job for this annotation

GitHub Actions / Lint

Extra semicolon
checkAndUpdateTopCandidate(newPattern, i+4);

Check failure on line 109 in src/util/filter.ts

View workflow job for this annotation

GitHub Actions / Lint

Extra semicolon
}
}

function checkAndUpdateTopCandidate(newPattern: string, penalty: number) {

Check failure on line 113 in src/util/filter.ts

View workflow job for this annotation

GitHub Actions / Lint

Move function declaration to function body root
if (newPattern) {
const candidate = fuzzyScore(newPattern, newPattern.toLowerCase(), patternPos, word, lowWord, wordPos, options)
const candidate = fuzzyScore(newPattern, newPattern.toLowerCase(), patternPos, word, lowWord, wordPos, options);
if (candidate) {
candidate[0] -= 3 // permutation penalty
candidate[0] -= penalty; // permutation penalty
if (!top || candidate[0] > top[0]) {
top = candidate
top = candidate;
}
}
}
}
}

return top
}

Expand Down
Loading