aboutsummaryrefslogtreecommitdiff
path: root/src/lib/Error/path.ts
diff options
context:
space:
mode:
Diffstat (limited to 'src/lib/Error/path.ts')
-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;
};