Last change
on this file since 188ee53 was 6a3a178, checked in by Ema <ema_spirova@…>, 3 years ago |
initial commit
|
-
Property mode
set to
100644
|
File size:
715 bytes
|
Line | |
---|
1 | "use strict";
|
---|
2 |
|
---|
3 | Object.defineProperty(exports, "__esModule", {
|
---|
4 | value: true
|
---|
5 | });
|
---|
6 | exports.findSuggestion = findSuggestion;
|
---|
7 | const {
|
---|
8 | min
|
---|
9 | } = Math;
|
---|
10 |
|
---|
11 | function levenshtein(a, b) {
|
---|
12 | let t = [],
|
---|
13 | u = [],
|
---|
14 | i,
|
---|
15 | j;
|
---|
16 | const m = a.length,
|
---|
17 | n = b.length;
|
---|
18 |
|
---|
19 | if (!m) {
|
---|
20 | return n;
|
---|
21 | }
|
---|
22 |
|
---|
23 | if (!n) {
|
---|
24 | return m;
|
---|
25 | }
|
---|
26 |
|
---|
27 | for (j = 0; j <= n; j++) {
|
---|
28 | t[j] = j;
|
---|
29 | }
|
---|
30 |
|
---|
31 | for (i = 1; i <= m; i++) {
|
---|
32 | for (u = [i], j = 1; j <= n; j++) {
|
---|
33 | u[j] = a[i - 1] === b[j - 1] ? t[j - 1] : min(t[j - 1], t[j], u[j - 1]) + 1;
|
---|
34 | }
|
---|
35 |
|
---|
36 | t = u;
|
---|
37 | }
|
---|
38 |
|
---|
39 | return u[n];
|
---|
40 | }
|
---|
41 |
|
---|
42 | function findSuggestion(str, arr) {
|
---|
43 | const distances = arr.map(el => levenshtein(el, str));
|
---|
44 | return arr[distances.indexOf(min(...distances))];
|
---|
45 | } |
---|
Note:
See
TracBrowser
for help on using the repository browser.