arr-indexes-of
Version:
Like Array#indexOf but return all indexes
36 lines (30 loc) • 883 B
JavaScript
/*!
* arr-indexes-of | MIT (c) Shinnosuke Watanabe
* https://github.com/shinnn/arr-indexes-of
*/
import appendType from 'append-type';
export default function arrIndexesOf(arr, searchValue, fromIndex) {
if (!Array.isArray(arr)) {
throw new TypeError('Expected an array, but got ' +
(arr === '' ? 'an empty string' : appendType(arr)) +
'.');
}
if (fromIndex !== undefined) {
if (typeof fromIndex !== 'number') {
throw new TypeError('Expected an index where to start the searching forwards in the array, but got ' +
(fromIndex === '' ? 'an empty string' : appendType(fromIndex)) +
'.');
}
if (fromIndex < 0) {
fromIndex = 0;
}
} else {
fromIndex = 0;
}
return arr.slice(fromIndex).reduce(function(results, current, index) {
if (current === searchValue) {
results.push(fromIndex + index);
}
return results;
}, []);
}