suggestSimilar.js
This file
Path
lib/suggestSimilar.js
99 lines · 2.7 KiB · JavaScript
Last changed by
Update dependecies and lint (#2489)John Geebefore adoption0ea3bb3
History of this fileTake it
The walk stopped after 200 commits: lines older than 471ec40 point at it.
Add copyInheritedSettings to README (#1798)John Geebefore adoption471ec40
lines 1–8why, one step back1const maxDistance = 3;23function editDistance(a, b) {4 // https://en.wikipedia.org/wiki/Damerau–Levenshtein_distance5 // Calculating optimal string alignment distance, no substring is edited more than once.6 // (Simple implementation.)78 // Quick early exit, return worst case.Reformat code with Prettier (#2180)John Geebefore adoption1bdc749
lines 9–10why, one step back9 if (Math.abs(a.length - b.length) > maxDistance)10 return Math.max(a.length, b.length);Add copyInheritedSettings to README (#1798)John Geebefore adoption471ec40
lines 11–26why, one step back1112 // distance between prefix substrings of a and b13 const d = [];1415 // pure deletions turn a into empty string16 for (let i = 0; i <= a.length; i++) {17 d[i] = [i];18 }19 // pure insertions turn empty string into b20 for (let j = 0; j <= b.length; j++) {21 d[0][j] = j;22 }2324 // fill matrix25 for (let j = 1; j <= b.length; j++) {26 for (let i = 1; i <= a.length; i++) {Update dependecies and lint (#2489)John Geebefore adoption0ea3bb3
line 27why, one step back27 let cost;Add copyInheritedSettings to README (#1798)John Geebefore adoption471ec40
lines 28–35why, one step back28 if (a[i - 1] === b[j - 1]) {29 cost = 0;30 } else {31 cost = 1;32 }33 d[i][j] = Math.min(34 d[i - 1][j] + 1, // deletion35 d[i][j - 1] + 1, // insertionReformat code with Prettier (#2180)John Geebefore adoption1bdc749
line 36why, one step back36 d[i - 1][j - 1] + cost, // substitutionAdd copyInheritedSettings to README (#1798)John Geebefore adoption471ec40
lines 37–55why, one step back37 );38 // transposition39 if (i > 1 && j > 1 && a[i - 1] === b[j - 2] && a[i - 2] === b[j - 1]) {40 d[i][j] = Math.min(d[i][j], d[i - 2][j - 2] + 1);41 }42 }43 }4445 return d[a.length][b.length];46}4748/**49 * Find close matches, restricted to same number of edits.50 *51 * @param {string} word52 * @param {string[]} candidates53 * @returns {string}54 */55Switching to esm-only (#2464)John Geebefore adoptionf4bd470
line 56why, one step back56export function suggestSimilar(word, candidates) {Add copyInheritedSettings to README (#1798)John Geebefore adoption471ec40
lines 57–63why, one step back57 if (!candidates || candidates.length === 0) return '';58 // remove possible duplicates59 candidates = Array.from(new Set(candidates));6061 const searchingOptions = word.startsWith('--');62 if (searchingOptions) {63 word = word.slice(2);Reformat code with Prettier (#2180)John Geebefore adoption1bdc749
line 64why, one step back64 candidates = candidates.map((candidate) => candidate.slice(2));Add copyInheritedSettings to README (#1798)John Geebefore adoption471ec40
lines 65–88why, one step back65 }6667 let similar = [];68 let bestDistance = maxDistance;69 const minSimilarity = 0.4;70 candidates.forEach((candidate) => {71 if (candidate.length <= 1) return; // no one character guesses7273 const distance = editDistance(word, candidate);74 const length = Math.max(word.length, candidate.length);75 const similarity = (length - distance) / length;76 if (similarity > minSimilarity) {77 if (distance < bestDistance) {78 // better edit distance, throw away previous worse matches79 bestDistance = distance;80 similar = [candidate];81 } else if (distance === bestDistance) {82 similar.push(candidate);83 }84 }85 });8687 similar.sort((a, b) => a.localeCompare(b));88 if (searchingOptions) {Reformat code with Prettier (#2180)John Geebefore adoption1bdc749
line 89why, one step back89 similar = similar.map((candidate) => `--${candidate}`);Add copyInheritedSettings to README (#1798)John Geebefore adoption471ec40
lines 90–99why, one step back90 }9192 if (similar.length > 1) {93 return `\n(Did you mean one of ${similar.join(', ')}?)`;94 }95 if (similar.length === 1) {96 return `\n(Did you mean ${similar[0]}?)`;97 }98 return '';99}