aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorFuwn <[email protected]>2024-02-11 00:03:45 -0800
committerFuwn <[email protected]>2024-02-11 00:03:47 -0800
commit301cbbcd07015a436d190885171d4c34ba181d44 (patch)
tree5e8b939a382c5ba206ff1960bf1138c1e4979517 /src
parentfeat(layout): new separator (diff)
downloaddue.moe-301cbbcd07015a436d190885171d4c34ba181d44.tar.xz
due.moe-301cbbcd07015a436d190885171d4c34ba181d44.zip
fix(error): levenshtein closest
Diffstat (limited to 'src')
-rw-r--r--src/lib/Error/path.ts14
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;
};