This repository has been archived by the owner on Dec 15, 2022. It is now read-only.
forked from hansonw/fuzzy-native
/
fuzzy-native-spec.js
343 lines (306 loc) · 9.57 KB
/
fuzzy-native-spec.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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
'use strict';
var fuzzyNative = require('../lib/main');
function values(results) {
return results.map(function(x) {
return x.value;
});
}
function genIds(values) {
return [Array.from({length: values.length}, (_, i) => i), values];
}
describe('fuzzy-native', function() {
var matcher;
beforeEach(function() {
const paths = [
'',
'a',
'ab',
'abC',
'abcd',
'alphabetacappa',
'AlphaBetaCappa',
'thisisatestdir',
'/////ThisIsATestDir',
'/this/is/a/test/dir',
'/test/tiatd',
'/zzz/path2/path3/path4',
'/path1/zzz/path3/path4',
'/path1/path2/zzz/path4',
'/path1/path2/path3/zzz',
];
matcher = new fuzzyNative.Matcher(...genIds(paths));
});
it('can match strings', function() {
var result = matcher.match('abc');
expect(values(result)).toEqual([
'abC',
'abcd',
'AlphaBetaCappa',
'alphabetacappa',
]);
result = matcher.match('t/i/a/t/d');
expect(values(result)).toEqual([
'/this/is/a/test/dir',
]);
// Make sure we prefer exact matches > abbreviations > others.
result = matcher.match('tiatd');
expect(values(result)).toEqual([
'/test/tiatd',
'/this/is/a/test/dir',
'/////ThisIsATestDir',
'thisisatestdir',
]);
// Defaults to case-insensitive matching (favouring the right case).
result = matcher.match('ABC', {smartCase: true});
expect(values(result)).toEqual([
'AlphaBetaCappa',
'abC',
'abcd',
'alphabetacappa',
]);
// Spaces should be ignored.
result = matcher.match('a b\tcappa');
expect(values(result)).toEqual([
'AlphaBetaCappa',
'alphabetacappa',
]);
// There was a bug where this would result in a match.
result = matcher.match('abcc');
expect(result).toEqual([]);
});
it('can do a case-sensitive search', function() {
var result = matcher.match('abc', {caseSensitive: true});
expect(values(result)).toEqual([
'abcd',
'alphabetacappa',
]);
result = matcher.match('C', {caseSensitive: true});
expect(values(result)).toEqual([
'abC',
'AlphaBetaCappa',
]);
});
it('uses smart case', function() {
var result = matcher.match('ThisIsATestDir', {smartCase: true});
expect(values(result)).toEqual([
'/////ThisIsATestDir',
'thisisatestdir',
'/this/is/a/test/dir',
]);
});
it('respects maxGap', function() {
var result = matcher.match('abc', {maxGap: 1});
expect(values(result)).toEqual([
'abC',
'abcd',
]);
});
it('favours shallow matches', function() {
var result = matcher.match('zzz', {caseSensitive: true});
expect(values(result)).toEqual([
'/path1/path2/path3/zzz',
'/path1/path2/zzz/path4',
'/path1/zzz/path3/path4',
'/zzz/path2/path3/path4',
]);
});
it('prefers whole words', function() {
matcher.setCandidates(...genIds([
'testa',
'testA',
'tes/A',
]));
var result = matcher.match('a');
expect(values(result)).toEqual([
'tes/A',
'testA',
'testa',
]);
});
it('breaks ties by length if root folder is not passed', function() {
matcher.setCandidates(...genIds(['123/a', '12/a', '1/a']));
var result = matcher.match('a');
expect(values(result)).toEqual(['1/a', '12/a', '123/a']);
});
it('breaks ties by distance to root folder', function() {
matcher.setCandidates(...genIds([
'/A/B/C/file.js',
'/A/B/file.js',
'/A/C/D/file.js',
'/A/REALLY_BIG_NAME/file.js',
'/A/file.js',
]));
var result = matcher.match('file', {rootPath: '/A/B/C/'});
expect(values(result)).toEqual([
'/A/B/C/file.js',
'/A/B/file.js',
'/A/file.js',
'/A/REALLY_BIG_NAME/file.js',
'/A/C/D/file.js',
]);
});
it('can limit to maxResults', function() {
var result = matcher.match('abc', {maxResults: 1});
expect(values(result)).toEqual([
'abC',
]);
result = matcher.match('ABC', {maxResults: 2, smartCase: true});
expect(values(result)).toEqual([
'AlphaBetaCappa',
'abC',
]);
});
it('can return matchIndexes', function() {
var result = matcher.match('abc', {recordMatchIndexes: true});
expect(result[0].matchIndexes).toEqual([0, 1, 2]);
expect(result[1].matchIndexes).toEqual([0, 1, 2]);
// alphabetacappa
// _ _ _
expect(result[2].matchIndexes).toEqual([0, 5, 9]);
expect(result[3].matchIndexes).toEqual([4, 5, 9]);
result = matcher.match('t/i/a/t/d', {recordMatchIndexes: true});
// /this/is/a/test/dir',
// _ __ ____ __
expect(result[0].matchIndexes).toEqual([1, 5, 6, 8, 9, 10, 11, 15, 16]);
});
it('supports modification', function() {
var result = matcher.match('abc', {maxResults: 1});
expect(values(result)).toEqual([
'abC',
]);
matcher.setCandidates([], []);
result = matcher.match('abc');
expect(values(result)).toEqual([]);
matcher.addCandidates([0, 1], ['abc', 'def']);
result = matcher.match('abc');
expect(values(result)).toEqual(['abc']);
matcher.removeCandidates([0]);
result = matcher.match('');
expect(values(result)).toEqual(['def']);
});
it('applies a variable gap penalty', function() {
const CANDIDATE = 'abcdefghijk/test'
matcher.setCandidates([0], [CANDIDATE]);
// Gap penalty caps out at 0.2.
let query = 'a/test';
expect(matcher.match(query)[0].score)
.toBeCloseTo(0.20 * query.length / CANDIDATE.length);
query = 'ab/test';
expect(matcher.match(query)[0].score)
.toBeCloseTo(0.20 * query.length / CANDIDATE.length);
// And then decreases by 0.05.
query = 'abc/test';
expect(matcher.match(query)[0].score)
.toBeCloseTo(0.25 * query.length / CANDIDATE.length);
query = 'abcdefghij/test';
expect(matcher.match(query)[0].score)
.toBeCloseTo(0.6 * query.length / CANDIDATE.length);
query = 'abcdefghijk/test';
expect(matcher.match(query)[0].score)
.toBe(1);
});
it('supports large strings', function() {
var longString = '';
var indexes = [];
for (var i = 0; i < 500; i++) {
longString += 'ab';
indexes.push(i * 2, i * 2 + 1);
}
matcher.setCandidates([0], [longString]);
expect(matcher.match(longString, {recordMatchIndexes: true})).toEqual([{
score: 1,
id: 0,
value: longString,
matchIndexes: indexes,
}]);
// Despite exceeding the scoring threshold, make sure penalties are applied.
const matches = matcher.match('aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa');
expect(matches.length).toBe(1);
expect(matches[0].score).toBeLessThan(1e-6);
});
it('works with non-alpha characters', function() {
matcher.setCandidates([0, 1], ['-_01234', '01234-']);
expect(values(matcher.match('-034'))).toEqual(['-_01234']);
});
it('ensures that the ids array and the values array are the same length when adding candidates', () => {
const expectedMessage = 'Expected ids array and values array to have the same length';
expect(() => matcher.addCandidates([1, 2, 3], ["a", "b"])).toThrow(expectedMessage)
expect(() => matcher.addCandidates([1, 2], ["a", "b", "c"])).toThrow(expectedMessage)
});
it('returns match results for duplicate values with different ids', () => {
matcher.setCandidates([0, 1], ['abc', 'abc']);
expect(matcher.match('ac').length).toBe(2);
matcher.setCandidates([0, 0], ['abc', 'abc']);
expect(matcher.match('ac').length).toBe(1);
});
it('returns matches when using different path separators', () => {
expect(
values(matcher.match('path1_path2_path3_zzz', {caseSensitive: true}))
).toEqual([
'/path1/path2/path3/zzz',
]);
expect(
values(matcher.match('path1_path2_path3_zzz', {caseSensitive: false}))
).toEqual([
'/path1/path2/path3/zzz',
]);
expect(
values(matcher.match('\\path1\\path2\\path3\\zzz', {caseSensitive: true}))
).toEqual([
'/path1/path2/path3/zzz',
]);
expect(
values(matcher.match('\\path1\\path2\\path3\\zzz', {caseSensitive: false}))
).toEqual([
'/path1/path2/path3/zzz',
]);
});
it('returns exact matches than normalized path separator matches', () => {
matcher.setCandidates(
[0, 1, 2],
['/path1/path2/path3/zzz', '/path1/path2/path3/zzz_ooo', '/path1/path2/path3/zzz/ooo']
);
expect(
values(matcher.match('path1/path2/path3/zzz', {caseSensitive: true}))
).toEqual([
'/path1/path2/path3/zzz',
'/path1/path2/path3/zzz/ooo',
'/path1/path2/path3/zzz_ooo'
]);
expect(
values(matcher.match('zzz_ooo', {caseSensitive: true}))
).toEqual([
'/path1/path2/path3/zzz_ooo',
'/path1/path2/path3/zzz/ooo'
]);
expect(
values(matcher.match('path1/path2/path3/zzz_ooo', {caseSensitive: true}))
).toEqual([
'/path1/path2/path3/zzz_ooo',
'/path1/path2/path3/zzz/ooo'
]);
expect(
values(matcher.match('path1/path2/path3/zzz_ooo', {caseSensitive: true}))
).toEqual([
'/path1/path2/path3/zzz_ooo',
'/path1/path2/path3/zzz/ooo'
]);
expect(
values(matcher.match('path1_path2_path3_zzz_ooo', {caseSensitive: true}))
).toEqual([
'/path1/path2/path3/zzz_ooo',
'/path1/path2/path3/zzz/ooo'
]);
expect(
values(matcher.match('path1/path2_path3/zzz_ooo', {caseSensitive: false}))
).toEqual([
'/path1/path2/path3/zzz_ooo',
'/path1/path2/path3/zzz/ooo'
]);
expect(
values(matcher.match('zzz/ooo', {caseSensitive: false}))
).toEqual([
'/path1/path2/path3/zzz/ooo'
]);
});
});