main
Last change
on this file was d565449, checked in by stefan toskovski <stefantoska84@…>, 4 weeks ago |
Update repo after prototype presentation
|
-
Property mode
set to
100644
|
File size:
743 bytes
|
Rev | Line | |
---|
[d565449] | 1 | "use strict";
|
---|
| 2 |
|
---|
| 3 | Object.defineProperty(exports, "__esModule", {
|
---|
| 4 | value: true
|
---|
| 5 | });
|
---|
| 6 | exports.findSuggestion = findSuggestion;
|
---|
| 7 | const {
|
---|
| 8 | min
|
---|
| 9 | } = Math;
|
---|
| 10 | function levenshtein(a, b) {
|
---|
| 11 | let t = [],
|
---|
| 12 | u = [],
|
---|
| 13 | i,
|
---|
| 14 | j;
|
---|
| 15 | const m = a.length,
|
---|
| 16 | n = b.length;
|
---|
| 17 | if (!m) {
|
---|
| 18 | return n;
|
---|
| 19 | }
|
---|
| 20 | if (!n) {
|
---|
| 21 | return m;
|
---|
| 22 | }
|
---|
| 23 | for (j = 0; j <= n; j++) {
|
---|
| 24 | t[j] = j;
|
---|
| 25 | }
|
---|
| 26 | for (i = 1; i <= m; i++) {
|
---|
| 27 | for (u = [i], j = 1; j <= n; j++) {
|
---|
| 28 | u[j] = a[i - 1] === b[j - 1] ? t[j - 1] : min(t[j - 1], t[j], u[j - 1]) + 1;
|
---|
| 29 | }
|
---|
| 30 | t = u;
|
---|
| 31 | }
|
---|
| 32 | return u[n];
|
---|
| 33 | }
|
---|
| 34 | function findSuggestion(str, arr) {
|
---|
| 35 | const distances = arr.map(el => levenshtein(el, str));
|
---|
| 36 | return arr[distances.indexOf(min(...distances))];
|
---|
| 37 | }
|
---|
| 38 |
|
---|
| 39 | //# sourceMappingURL=find-suggestion.js.map
|
---|
Note:
See
TracBrowser
for help on using the repository browser.