-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
47 lines (47 loc) · 1.39 KB
/
index.js
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
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
var similarity = function (s1, s2) {
var longer = s1;
var shorter = s2;
if (s1.length < s2.length) {
longer = s2;
shorter = s1;
}
var longerLength = longer.length;
if (longerLength === 0) {
return 1.0;
}
return (longerLength - distance(longer, shorter)) / parseFloat(longerLength.toString());
};
var similarityPercent = function (s1, s2) {
return Math.round(similarity(s1, s2) * 10000) / 100;
};
exports.similarityPercent = similarityPercent;
var distance = function (s1, s2) {
s1 = s1.toLowerCase();
s2 = s2.toLowerCase();
var costs = [];
for (var i = 0; i <= s1.length; i++) {
var lastValue = i;
for (var j = 0; j <= s2.length; j++) {
if (i == 0) {
costs[j] = j;
}
else {
if (j > 0) {
var newValue = costs[j - 1];
if (s1.charAt(i - 1) != s2.charAt(j - 1)) {
newValue = Math.min(Math.min(newValue, lastValue), costs[j]) + 1;
}
costs[j - 1] = lastValue;
lastValue = newValue;
}
}
}
if (i > 0) {
costs[s2.length] = lastValue;
}
}
return costs[s2.length];
};
//# sourceMappingURL=index.js.map