sugar
Version:
A Javascript utility library for working with native objects.
26 lines (23 loc) • 658 B
JavaScript
;
var isArrayIndex = require('./isArrayIndex');
function getSparseArrayIndexes(arr, fromIndex, loop, fromRight) {
var indexes = [], i;
for (i in arr) {
// istanbul ignore next
if (isArrayIndex(i) && (loop || (fromRight ? i <= fromIndex : i >= fromIndex))) {
indexes.push(+i);
}
}
indexes.sort(function(a, b) {
var aLoop = a > fromIndex;
var bLoop = b > fromIndex;
// This block cannot be reached unless ES5 methods are being shimmed.
// istanbul ignore if
if (aLoop !== bLoop) {
return aLoop ? -1 : 1;
}
return a - b;
});
return indexes;
}
module.exports = getSparseArrayIndexes;