@acusti/matchmaking
Advanced tools
Comparing version 0.4.0 to 0.5.0
@@ -19,5 +19,11 @@ const FIRST_NUMBER = '0'.charCodeAt(0); | ||
const shortestLength = Math.min(strALength, strBLength); | ||
// Exact partial match is next best score to exact match | ||
if (strA.slice(0, shortestLength) === strB.slice(0, shortestLength)) { | ||
return MAX_PARTIAL_EXACT_SCORE; | ||
// Exact partial match is the next best score to an exact match with | ||
// relative length from the beginning applying a penalty to total score | ||
const [strLonger, strShorter] = strALength > strBLength ? [strA, strB] : [strB, strA]; | ||
const matchStart = strLonger.indexOf(strShorter); | ||
if (matchStart > -1) { | ||
// Maximum penalty for distance from beginning is 0.25 | ||
const penaltyPerStep = 0.25 / (strLonger.length - 2); | ||
const penalty = penaltyPerStep * matchStart; | ||
return MAX_PARTIAL_EXACT_SCORE - penalty; | ||
} | ||
@@ -54,6 +60,5 @@ // To proportionally weight consecutive exact matches, increase bonus relative to total length | ||
} | ||
// If score came out at 0 or less, give it best possible score for an inexact match | ||
if (score <= 0) { | ||
// If score came to 0 or less, use best possible score for an inexact match | ||
if (score <= 0) | ||
return MAX_INEXACT_SCORE; | ||
} | ||
score = (worstPossibleScore - score) / worstPossibleScore; | ||
@@ -60,0 +65,0 @@ // Don’t allow an inexact match to get a score of 1 (reserved for an exact match) |
{ | ||
"name": "@acusti/matchmaking", | ||
"version": "0.4.0", | ||
"version": "0.5.0", | ||
"description": "Intuitive approximate string matching (i.e. fuzzy searches)", | ||
@@ -41,4 +41,4 @@ "keywords": [ | ||
"devDependencies": { | ||
"typescript": "^4.9.3" | ||
"typescript": "^5.1.6" | ||
} | ||
} |
@@ -27,3 +27,3 @@ # @acusti/matchmaking | ||
```js | ||
import { getBestMatch, sortByBestMatch } from '@acusti/textual'; | ||
import { getBestMatch, sortByBestMatch } from '@acusti/matchmaking'; | ||
``` | ||
@@ -30,0 +30,0 @@ |
@@ -60,2 +60,14 @@ import { describe, expect, it } from 'vitest'; | ||
const FONT_WEIGHT_VALUES = [ | ||
'Font Weight - 100', | ||
'Font Weight - 200', | ||
'Font Weight - 300', | ||
'Font Weight - 400', | ||
'Font Weight - 500', | ||
'Font Weight - 600', | ||
'Font Weight - 700', | ||
'Font Weight - 800', | ||
'Font Weight - 900', | ||
]; | ||
describe('@acusti/matchmaking', () => { | ||
@@ -69,4 +81,4 @@ describe('sortByBestMatch', () => { | ||
'Wyoming', | ||
'Vermont', | ||
'Virginia', | ||
'New Hampshire', | ||
'New Jersey', | ||
]); | ||
@@ -131,2 +143,8 @@ | ||
expect(getBestMatch({ items: CSS_VALUES, text: '6' })).toEqual('7px'); | ||
expect(getBestMatch({ items: FONT_WEIGHT_VALUES, text: '300' })).toBe( | ||
'Font Weight - 300', | ||
); | ||
expect(getBestMatch({ items: FONT_WEIGHT_VALUES, text: '9' })).toBe( | ||
'Font Weight - 900', | ||
); | ||
// 'rpyrg' is close to 'south', so best match is the first “south *” item | ||
@@ -133,0 +151,0 @@ expect(getBestMatch({ items: STATES, text: 'rpyrg' })).toBe('South Carolina'); |
@@ -22,5 +22,11 @@ const FIRST_NUMBER = '0'.charCodeAt(0); | ||
// Exact partial match is next best score to exact match | ||
if (strA.slice(0, shortestLength) === strB.slice(0, shortestLength)) { | ||
return MAX_PARTIAL_EXACT_SCORE; | ||
// Exact partial match is the next best score to an exact match with | ||
// relative length from the beginning applying a penalty to total score | ||
const [strLonger, strShorter] = strALength > strBLength ? [strA, strB] : [strB, strA]; | ||
const matchStart = strLonger.indexOf(strShorter); | ||
if (matchStart > -1) { | ||
// Maximum penalty for distance from beginning is 0.25 | ||
const penaltyPerStep = 0.25 / (strLonger.length - 2); | ||
const penalty = penaltyPerStep * matchStart; | ||
return MAX_PARTIAL_EXACT_SCORE - penalty; | ||
} | ||
@@ -60,7 +66,6 @@ | ||
} | ||
// If score came out at 0 or less, give it best possible score for an inexact match | ||
if (score <= 0) { | ||
return MAX_INEXACT_SCORE; | ||
} | ||
// If score came to 0 or less, use best possible score for an inexact match | ||
if (score <= 0) return MAX_INEXACT_SCORE; | ||
score = (worstPossibleScore - score) / worstPossibleScore; | ||
@@ -67,0 +72,0 @@ // Don’t allow an inexact match to get a score of 1 (reserved for an exact match) |
Sorry, the diff of this file is not supported yet
19057
324