diff options
| author | Fuwn <[email protected]> | 2024-02-11 00:03:45 -0800 |
|---|---|---|
| committer | Fuwn <[email protected]> | 2024-02-11 00:03:47 -0800 |
| commit | 301cbbcd07015a436d190885171d4c34ba181d44 (patch) | |
| tree | 5e8b939a382c5ba206ff1960bf1138c1e4979517 /src/lib | |
| parent | feat(layout): new separator (diff) | |
| download | due.moe-301cbbcd07015a436d190885171d4c34ba181d44.tar.xz due.moe-301cbbcd07015a436d190885171d4c34ba181d44.zip | |
fix(error): levenshtein closest
Diffstat (limited to 'src/lib')
| -rw-r--r-- | src/lib/Error/path.ts | 14 |
1 files changed, 8 insertions, 6 deletions
diff --git a/src/lib/Error/path.ts b/src/lib/Error/path.ts index 40f50b7c..ea11fa69 100644 --- a/src/lib/Error/path.ts +++ b/src/lib/Error/path.ts @@ -1,11 +1,13 @@ +import levenshtein from 'fast-levenshtein'; + export const closest = (path: string, suggestions: string[]) => { - const partialMatch = suggestions.find((suggestion) => suggestion.includes(path)); + const suggestionsWithDistance = suggestions.map((suggestion) => { + const distance = levenshtein.get(path, suggestion); - if (partialMatch) return partialMatch; + return { suggestion, distance }; + }); - const closestMatch = suggestions.reduce((prev, curr) => { - return prev.length > curr.length ? prev : curr; - }, ''); + suggestionsWithDistance.sort((a, b) => a.distance - b.distance); - return closestMatch; + return suggestionsWithDistance[0].suggestion; }; |