-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
180 lines (143 loc) · 3.8 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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
'use strict';
/**
* Get and compare edit distance
* @param {string} input guessing value
* @param {string} candidate candidate to match against
* @param {number} max threshold limit
* @return {string} final matrix value
*/
const getEditDistance = (input, candidate, max) => {
const MAX_INT = Math.pow(2, 32) - 1;
max = max || max === 0 ? max : MAX_INT;
const inputLength = input.length;
const candidateLength = candidate.length;
if (inputLength === 0) {
return Math.min(max + 1, candidateLength);
}
if (candidateLength === 0) {
return Math.min(max + 1, inputLength);
}
if (Math.abs(inputLength - candidateLength) > max) {
return max + 1;
}
const matrix = [];
let colMin = MAX_INT;
let i = 0;
let j = 0;
let minJ = 1;
let maxJ = 1;
for (; i <= candidateLength; i++) {
matrix[i] = [i];
}
for (; j <= inputLength; j++) {
matrix[0][j] = j;
}
for (i = 1; i <= candidateLength; i++) {
if (i > max) {
minJ = i - max;
}
maxJ = candidateLength + 1;
if (maxJ > max + i) {
maxJ = max + i;
}
for (j = 1; j <= inputLength; j++) {
if (j < minJ || j > maxJ) {
matrix[i][j] = max + 1;
} else if (candidate.charAt(i - 1) === input.charAt(j - 1)) {
matrix[i][j] = matrix[i - 1][j - 1];
} else {
matrix[i][j] = Math.min(
matrix[i - 1][j - 1] + 1,
Math.min(matrix[i][j - 1] + 1, matrix[i - 1][j] + 1)
);
}
if (matrix[i][j] < colMin) {
colMin = matrix[i][j];
}
}
if (colMin > max) {
return max + 1;
}
}
return matrix[candidateLength][inputLength];
};
/**
* Matching against a list of strings
* @param {string} input The string input to match
* @param {array} list array of strings or objects to match against
* @param {object} options pptions are set on the sayWhat function object
* @return {string} matching value
*/
const sayWhat = (input, list, options) => {
const _options = {
key: null,
threshold: 0.4,
thresholdAbsolute: 20,
caseSensitive: false,
nullResultValue: null,
returnWinningObject: null,
returnFirstMatch: false,
...options
};
let _input = input;
const _list = list;
if (typeof _input !== 'string') {
throw new TypeError('The `input` argument must be a string');
}
if (!_options.caseSensitive) {
_input = _input.toLowerCase();
}
const thresholdRelative =
_options.threshold === null ? null : _options.threshold * _input.length;
const {thresholdAbsolute} = _options;
let winningVal;
if (thresholdRelative !== null && thresholdAbsolute !== null) {
winningVal = Math.min(thresholdRelative, thresholdAbsolute);
} else if (thresholdRelative !== null) {
winningVal = thresholdRelative;
} else if (thresholdAbsolute !== null) {
winningVal = thresholdAbsolute;
} else {
winningVal = null;
}
const len = _list.length;
let i = 0;
let candidate;
let testCandidate;
let val;
let winner;
for (; i < len; i++) {
candidate = _list[i];
if (_options.key) {
candidate = candidate[_options.key];
}
if (!candidate) {
continue;
}
if (!_options.caseSensitive) {
testCandidate = candidate.toLowerCase();
} else {
testCandidate = candidate;
}
val = getEditDistance(input, testCandidate, winningVal);
if (winningVal === null || val < winningVal) {
winningVal = val;
if (_options.key && _options.returnWinningObject) {
winner = list[i];
} else {
winner = candidate;
}
if (_options.returnFirstMatch) {
return winner;
}
}
}
return winner || _options.nullResultValue;
};
(function (root, factory) {
if (typeof exports === 'object') {
module.exports = factory;
} else {
root = factory;
}
})(this, sayWhat);