Skip to content

Commit

Permalink
feat(revert): support finding data with typos (#153) (#155)
Browse files Browse the repository at this point in the history
This reverts commit 8fc0645.

BREAKING CHANGE: We've reverted the last release which added support for typos because it was too generous
  • Loading branch information
shshaw authored Nov 4, 2024
1 parent d9b7dab commit ec8e007
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 25 deletions.
7 changes: 0 additions & 7 deletions src/__tests__/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,13 +54,6 @@ const tests: Record<string, TestCase> = {
'The Tail of Forty Cities', // match2
],
},
'matches data that has minor typos': {
input: [
['juptyer', 'juppyter', 'jopytar', 'jupytor', 'jepytur'],
'jupyter',
],
output: ['juppyter', 'juptyer', 'jupytor'],
},
'no match for single character inputs that are not equal': {
input: [['abc'], 'd'],
output: [],
Expand Down
29 changes: 11 additions & 18 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -264,6 +264,7 @@ function getClosenessRanking(
stringToRank: string,
): Ranking {
let matchingInOrderCharCount = 0
let charNumber = 0
function findMatchingCharacter(
matchChar: string,
string: string,
Expand All @@ -278,31 +279,23 @@ function getClosenessRanking(
}
return -1
}
let skipped = 0
function getRanking(spread: number) {
const spreadPercentage = 1 / spread
const inOrderPercentage = matchingInOrderCharCount / stringToRank.length
const matchPercentage = (stringToRank.length - skipped) / stringToRank.length
const ranking = rankings.MATCHES + inOrderPercentage * spreadPercentage * matchPercentage
const ranking = rankings.MATCHES + inOrderPercentage * spreadPercentage
return ranking as Ranking
}
let firstIndex = 0
let charNumber = 0
let nextCharNumber = 0
for (let i = 0, I = stringToRank.length; i < I; i++) {
const firstIndex = findMatchingCharacter(stringToRank[0], testString, 0)
if (firstIndex < 0) {
return rankings.NO_MATCH
}
charNumber = firstIndex
for (let i = 1, I = stringToRank.length; i < I; i++) {
const matchChar = stringToRank[i]
nextCharNumber = findMatchingCharacter(matchChar, testString, charNumber)
const found = nextCharNumber > -1
if (found) {
charNumber = nextCharNumber
if (i === 0) {
firstIndex = charNumber
}
} else if (skipped > 0 || stringToRank.length <= 3) {
// if search term is short, require finding all characters
charNumber = findMatchingCharacter(matchChar, testString, charNumber)
const found = charNumber > -1
if (!found) {
return rankings.NO_MATCH
} else {
skipped += 1
}
}

Expand Down

0 comments on commit ec8e007

Please sign in to comment.