Skip to content
Open
39 changes: 32 additions & 7 deletions src/util/filter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,21 +81,46 @@
// 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

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

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

View workflow job for this annotation

GitHub Actions / Lint

Extra semicolon
checkAndUpdateTopCandidate(newPattern, 3);

Check failure on line 93 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 96 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 101 in src/util/filter.ts

View workflow job for this annotation

GitHub Actions / Lint

Extra semicolon
checkAndUpdateTopCandidate(newPattern, 4);

Check failure on line 102 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 107 in src/util/filter.ts

View workflow job for this annotation

GitHub Actions / Lint

Extra semicolon
checkAndUpdateTopCandidate(newPattern, i + 4);

Check failure on line 108 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 112 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);

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

View workflow job for this annotation

GitHub Actions / Lint

Extra semicolon
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