-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathphrase.js
68 lines (58 loc) · 1.68 KB
/
phrase.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
// Phrase detector
module.exports = function (phrase) {
var words = phrase.split(" ");
var found = false;
var nextWord = 0;
var index = 0;
var first = -1;
var last = -1;
return {
accept: function (word) {
// If we've already found our phrase, nothing more to do
if (found) {
return;
}
var lookingFor = words[nextWord].toLowerCase().trim();
var got = word.toLowerCase().trim();
if (lookingFor === got) {
// Found the next word, update first/last index
if (nextWord === 0) {
first = index;
} else if (nextWord === words.length - 1) {
last = index;
found = true;
}
// Start looking for the next word
nextWord++;
} else {
// Reset our search if we find an out-of-phrase word
nextWord = 0;
first = -1;
last = -1;
}
// Regardless, advance our search index
index++;
},
acceptAll: function (phrase) {
var words = phrase.split(" ");
words.forEach(this.accept);
},
reset: function () {
index = 0;
found = false;
first = -1;
last = -1;
nextWord = 0;
},
getResult: function () {
if (found) {
return {
first: first,
last: last
};
} else {
return null;
}
}
};
};