-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfind-by-words.js
57 lines (46 loc) · 1.46 KB
/
find-by-words.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
/*
* find-by-words
* author: Evandro Leopoldino Gonçalves <evandrolgoncalves@gmail.com>
* https://github.com/evandrolg
* License: MIT
*/
(function (name, context, definition) {
if (typeof module != 'undefined' && module.exports) module.exports = definition();
else if (typeof define == 'function' && define.amd) define(definition)
else context[name] = definition()
})('findByWords', this, function (name, context) {
var _isEqual = function(word1, word2, caseSensitive) {
if (caseSensitive) {
return word1.toLowerCase() == word2.toLowerCase();
}
return word1 == word2;
};
var findByWords = function(a, word, caseSensitive) {
var size = word.length;
var first = 0;
var last = a.length - 1;
var found = false;
var output = [];
while(first <= last && !found) {
var middle = Math.ceil((first + last) / 2);
var wordPart = a[middle].slice(0, size);
if (_isEqual(wordPart, word, caseSensitive)) {
found = true;
output.push(a[middle]);
var _middle = middle;
while (a[--_middle] && _isEqual(a[_middle].slice(0, size), word, caseSensitive)) {
output.push(a[_middle]);
}
while (a[++middle] && _isEqual(a[middle].slice(0, size), word, caseSensitive)) {
output.push(a[middle]);
}
} else if (wordPart > word) {
last = middle - 1;
} else {
first = middle + 1;
}
}
return output;
};
return findByWords;
});