Skip to content

Commit 5fc7613

Browse files
committed
refac(fuzzy): iterative solution
1 parent f46dff8 commit 5fc7613

File tree

1 file changed

+17
-9
lines changed

1 file changed

+17
-9
lines changed

src/_common.js

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -60,15 +60,23 @@ function objectContains(partial, object) {
6060
* @returns {*}
6161
*/
6262
function hasApproxPattern(word, pattern) {
63-
if(pattern === '')
64-
return word;
65-
66-
var index = word.indexOf(pattern.charAt(0));
67-
68-
if(index === -1)
69-
return false;
70-
71-
return hasApproxPattern(word.substr(index+1), pattern.substr(1))
63+
// cheaper version of indexOf; instead of creating each
64+
// iteration new str.
65+
function indexOf(word, p, c) {
66+
var j = 0;
67+
while ((p + j) <= word.length) {
68+
if (word.charAt(p + j) == c) return j;
69+
j++;
70+
}
71+
return -1;
72+
}
73+
var p = 0;
74+
for (var i = 0; i <= pattern.length; i++) {
75+
var index = indexOf(word, p, pattern.charAt(i));
76+
if (index == -1) return false;
77+
p += index;
78+
}
79+
return true
7280
}
7381

7482
/**

0 commit comments

Comments
 (0)