Skip to content

Commit 9249add

Browse files
committed
Combine _.indexOf and _.lastIndexOf
1 parent f77e099 commit 9249add

File tree

1 file changed

+35
-36
lines changed

1 file changed

+35
-36
lines changed

underscore.js

Lines changed: 35 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -603,40 +603,8 @@
603603
return result;
604604
};
605605

606-
// Return the position of the first occurrence of an item in an array,
607-
// or -1 if the item is not included in the array.
608-
// If the array is large and already in sort order, pass `true`
609-
// for **isSorted** to use binary search.
610-
_.indexOf = function(array, item, isSorted) {
611-
var i = 0, length = array && array.length;
612-
if (typeof isSorted == 'number') {
613-
i = isSorted < 0 ? Math.max(0, length + isSorted) : isSorted;
614-
} else if (isSorted && length) {
615-
i = _.sortedIndex(array, item);
616-
return array[i] === item ? i : -1;
617-
}
618-
if (item !== item) {
619-
var index = _.findIndex(slice.call(array, i), _.isNaN);
620-
return index >= 0 ? index + i : -1;
621-
}
622-
for (; i < length; i++) if (array[i] === item) return i;
623-
return -1;
624-
};
625-
626-
_.lastIndexOf = function(array, item, from) {
627-
var idx = array ? array.length : 0;
628-
if (typeof from == 'number') {
629-
idx = from < 0 ? idx + from + 1 : Math.min(idx, from + 1);
630-
}
631-
if (item !== item) {
632-
return _.findLastIndex(slice.call(array, 0, idx), _.isNaN);
633-
}
634-
while (--idx >= 0) if (array[idx] === item) return idx;
635-
return -1;
636-
};
637-
638606
// Generator function to create the findIndex and findLastIndex functions
639-
function createIndexFinder(dir) {
607+
function createPredicateIndexFinder(dir) {
640608
return function(array, predicate, context) {
641609
predicate = cb(predicate, context);
642610
var length = array != null && array.length;
@@ -649,9 +617,8 @@
649617
}
650618

651619
// Returns the first index on an array-like that passes a predicate test
652-
_.findIndex = createIndexFinder(1);
653-
654-
_.findLastIndex = createIndexFinder(-1);
620+
_.findIndex = createPredicateIndexFinder(1);
621+
_.findLastIndex = createPredicateIndexFinder(-1);
655622

656623
// Use a comparator function to figure out the smallest index at which
657624
// an object should be inserted so as to maintain order. Uses binary search.
@@ -666,6 +633,38 @@
666633
return low;
667634
};
668635

636+
// Generator function to create the indexOf and lastIndexOf functions
637+
function createIndexFinder(dir, predicateFind, sortedIndex) {
638+
return function(array, item, idx) {
639+
var i = 0, length = array ? array.length : -1;
640+
if (typeof idx == 'number') {
641+
if (dir > 0) {
642+
i = idx >= 0 ? idx : Math.max(idx + length, i);
643+
} else {
644+
length = idx >= 0 ? Math.min(idx + 1, length) : idx + length + 1;
645+
}
646+
} else if (sortedIndex && idx && length) {
647+
idx = _[sortedIndex](array, item);
648+
return array[idx] === item ? idx : -1;
649+
}
650+
if (item !== item) {
651+
idx = _[predicateFind](slice.call(array, i, length), _.isNaN);
652+
return idx >= 0 ? idx + i : -1;
653+
}
654+
for (idx = dir > 0 ? i : length - 1; idx >= 0 && idx < length; idx += dir) {
655+
if (array[idx] === item) return idx;
656+
}
657+
return -1;
658+
};
659+
}
660+
661+
// Return the position of the first occurrence of an item in an array,
662+
// or -1 if the item is not included in the array.
663+
// If the array is large and already in sort order, pass `true`
664+
// for **isSorted** to use binary search.
665+
_.indexOf = createIndexFinder(1, 'findIndex', 'sortedIndex');
666+
_.lastIndexOf = createIndexFinder(-1, 'findLastIndex');
667+
669668
// Generate an integer Array containing an arithmetic progression. A port of
670669
// the native Python `range()` function. See
671670
// [the Python documentation](http://docs.python.org/library/functions.html#range).

0 commit comments

Comments
 (0)