1 line
2.6 KiB
Text
1 line
2.6 KiB
Text
|
{"version":3,"names":["min","Math","levenshtein","a","b","t","u","i","j","m","length","n","findSuggestion","str","arr","distances","map","el","indexOf"],"sources":["../src/find-suggestion.ts"],"sourcesContent":["const { min } = Math;\n\n// a minimal leven distance implementation\n// balanced maintainability with code size\n// It is not blazingly fast but should be okay for Babel user case\n// where it will be run for at most tens of time on strings\n// that have less than 20 ASCII characters\n\n// https://rosettacode.org/wiki/Levenshtein_distance#ES5\nfunction levenshtein(a: string, b: string): number {\n let t = [],\n u: number[] = [],\n i,\n j;\n const m = a.length,\n n = b.length;\n if (!m) {\n return n;\n }\n if (!n) {\n return m;\n }\n for (j = 0; j <= n; j++) {\n t[j] = j;\n }\n for (i = 1; i <= m; i++) {\n for (u = [i], j = 1; j <= n; j++) {\n u[j] =\n a[i - 1] === b[j - 1] ? t[j - 1] : min(t[j - 1], t[j], u[j - 1]) + 1;\n }\n t = u;\n }\n return u[n];\n}\n\n/**\n * Given a string `str` and an array of candidates `arr`,\n * return the first of elements in candidates that has minimal\n * Levenshtein distance with `str`.\n * @export\n * @param {string} str\n * @param {string[]} arr\n * @returns {string}\n */\nexport function findSuggestion(str: string, arr: readonly string[]): string {\n const distances = arr.map<number>(el => levenshtein(el, str));\n return arr[distances.indexOf(min(...distances))];\n}\n"],"mappings":";;;;;;AAAA,MAAM;EAAEA;AAAI,CAAC,GAAGC,IAAI;AASpB,SAASC,WAAWA,CAACC,CAAS,EAAEC,CAAS,EAAU;EACjD,IAAIC,CAAC,GAAG,EAAE;IACRC,CAAW,GAAG,EAAE;IAChBC,CAAC;IACDC,CAAC;EACH,MAAMC,CAAC,GAAGN,CAAC,CAACO,MAAM;IAChBC,CAAC,GAAGP,CAAC,CAACM,MAAM;EACd,IAAI,CAACD,CAAC,EAAE;IACN,OAAOE,CAAC;EACV;EACA,IAAI,CAACA,CAAC,EAAE;IACN,OAAOF,CAAC;EACV;EACA,KAAKD,CAAC,GAAG,CAAC,EAAEA,CAA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