Skip to content

Commit

Permalink
fix: don't allow simple match to meet acronym threshold (#60)
Browse files Browse the repository at this point in the history
* Add test for issue #54

* Closes issue #54
  • Loading branch information
sdbrannum authored and Kent C. Dodds committed Jun 4, 2019
1 parent fa454c5 commit fcf34f4
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 4 deletions.
8 changes: 8 additions & 0 deletions src/__tests__/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -243,6 +243,14 @@ const tests = {
],
output: ['app', 'apple', 'apply', 'fiji apple'],
},
'when providing a rank threshold of ACRONYM, it returns only the items that meet the rank': {
input: [
['apple', 'atop', 'alpaca', 'vamped'],
'ap',
{threshold: rankings.ACRONYM},
],
output: ['apple'],
},
'defaults to ignore diacritics': {
input: [
['jalapeño', 'à la carte', 'café', 'papier-mâché', 'à la mode'],
Expand Down
11 changes: 7 additions & 4 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -293,27 +293,30 @@ function isCaseAcronym(testString, stringToRank, caseRank) {
* Returns a score based on how spread apart the
* characters from the stringToRank are within the testString.
* A number close to rankings.MATCHES represents a loose match. A number close
* to rankings.MATCHES + 1 represents a loose match.
* to rankings.MATCHES + 1 represents a tighter match.
* @param {String} testString - the string to test against
* @param {String} stringToRank - the string to rank
* @returns {Number} the number between rankings.MATCHES and
* rankings.MATCHES + 1 for how well stringToRank matches testString
*/
function getClosenessRanking(testString, stringToRank) {
let matchingInOrderCharCount = 0;
let charNumber = 0
function findMatchingCharacter(matchChar, string, index) {
for (let j = index; j < string.length; j++) {
const stringChar = string[j]
if (stringChar === matchChar) {
matchingInOrderCharCount += 1
return j + 1
}
}
return -1
}
function getRanking(spread) {
const matching = spread - stringToRank.length + 1
const ranking = rankings.MATCHES + 1 / matching
return ranking
const spreadPercentage = 1 / spread;
const inOrderPercentage = matchingInOrderCharCount / stringToRank.length;
const ranking = 1 + inOrderPercentage * spreadPercentage;
return ranking;
}
const firstIndex = findMatchingCharacter(stringToRank[0], testString, 0)
if (firstIndex < 0) {
Expand Down

0 comments on commit fcf34f4

Please sign in to comment.