-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
30 lines (25 loc) · 899 Bytes
/
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
var isFuzzyStringMatch = require('./lib/is-fuzzy-string-match');
var buildFuzzyRegExpString = require('./lib/build-fuzzy-regexp-string');
// note: this is the repetition count, so a threshld of `n` will
// engage on the `n + 1` successive call with the same query
var USE_REGEX_THRESHOLD = 2;
var prevQuery = '',
queryRepeatCount = 0,
reQuery;
module.exports = function fz(candidate, query) {
if (candidate === query) return true;
if (!query.length) return true;
if (query === prevQuery) {
if (++queryRepeatCount >= USE_REGEX_THRESHOLD) {
if (queryRepeatCount === USE_REGEX_THRESHOLD) {
var fuzzyRegExpString = buildFuzzyRegExpString(query);
reQuery = new RegExp(fuzzyRegExpString, 'i');
}
return reQuery.test(candidate);
}
} else {
prevQuery = query;
queryRepeatCount = 0;
}
return isFuzzyStringMatch(candidate, query);
};