-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
40 lines (31 loc) · 950 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
31
32
33
34
35
36
37
38
39
40
'use strict';
var appendType = require('append-type');
var arrIndexesOf = require('arr-indexes-of');
/*!
* indexes-of-sequence | MIT (c) Shinnosuke Watanabe
* https://github.com/shinnn/indexes-of-sequence
*/
function indexesOfSequence(arr, searchValues, fromIndex) {
[arr, searchValues].forEach(function(arg) {
if (!Array.isArray(arg)) {
throw new TypeError('Expected an array, but got ' + appendType(arg) + '.');
}
});
if (searchValues.length === 0) {
return [];
}
var firstValueIndexes = arrIndexesOf(arr, searchValues[0], fromIndex);
if (firstValueIndexes.length === 0) {
return [];
}
if (searchValues.length === 1) {
return firstValueIndexes;
}
var restSearchValues = searchValues.slice(1);
return firstValueIndexes.filter(function(index) {
return restSearchValues.every(function(val, i) {
return arr[index + i + 1] === val;
});
});
}
module.exports = indexesOfSequence;