From b513ae5e780f488e68eceb32bac0068f3f2e5f34 Mon Sep 17 00:00:00 2001 From: Stephen Shaw Date: Mon, 4 Nov 2024 09:45:58 -0600 Subject: [PATCH] Revert "feat: support finding data with typos (#153)" This reverts commit 8fc0645af4b2dfdbb53dd9c1c088ab52cd997f5f. --- src/__tests__/index.ts | 7 ------- src/index.ts | 29 +++++++++++------------------ 2 files changed, 11 insertions(+), 25 deletions(-) diff --git a/src/__tests__/index.ts b/src/__tests__/index.ts index 50225b3..bcb380e 100644 --- a/src/__tests__/index.ts +++ b/src/__tests__/index.ts @@ -54,13 +54,6 @@ const tests: Record = { '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: [], diff --git a/src/index.ts b/src/index.ts index f5bdd73..7972c0e 100644 --- a/src/index.ts +++ b/src/index.ts @@ -264,6 +264,7 @@ function getClosenessRanking( stringToRank: string, ): Ranking { let matchingInOrderCharCount = 0 + let charNumber = 0 function findMatchingCharacter( matchChar: string, string: string, @@ -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 } }