-
Notifications
You must be signed in to change notification settings - Fork 22
/
test.ts
67 lines (59 loc) · 1.58 KB
/
test.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
const { closest, distance } = require("./mod.js");
const levenshtein = (a, b) => {
if (a.length === 0) {
return b.length;
}
if (b.length === 0) {
return a.length;
}
if (a.length > b.length) {
const tmp = a;
a = b;
b = tmp;
}
const row = [];
for (let i = 0; i <= a.length; i++) {
row[i] = i;
}
for (let i = 1; i <= b.length; i++) {
let prev = i;
for (let j = 1; j <= a.length; j++) {
let val = 0;
if (b.charAt(i - 1) === a.charAt(j - 1)) {
val = row[j - 1];
} else {
val = Math.min(row[j - 1] + 1, prev + 1, row[j] + 1);
}
row[j - 1] = prev;
prev = val;
}
row[a.length] = prev;
}
return row[a.length];
};
const makeid = (length) => {
let result = "";
const characters =
"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
const charactersLength = characters.length;
for (let i = 0; i < length; i++) {
result += characters.charAt(Math.floor(Math.random() * charactersLength));
}
return result;
};
test("test compare", () => {
for (let i = 0; i < 1000; i++) {
const rnd_num1 = (Math.random() * 1000) | 0;
const rnd_num2 = (Math.random() * 1000) | 0;
const rnd_string1 = makeid(rnd_num1);
const rnd_string2 = makeid(rnd_num2);
const actual = distance(rnd_string1, rnd_string2);
const expected = levenshtein(rnd_string1, rnd_string2);
expect(actual).toBe(expected);
}
});
test("test find", () => {
const actual = closest("fast", ["slow", "faster", "fastest"]);
const expected = "faster";
expect(actual).toBe(expected);
});