|
603 | 603 | return result; |
604 | 604 | }; |
605 | 605 |
|
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 | | - |
638 | 606 | // Generator function to create the findIndex and findLastIndex functions |
639 | | - function createIndexFinder(dir) { |
| 607 | + function createPredicateIndexFinder(dir) { |
640 | 608 | return function(array, predicate, context) { |
641 | 609 | predicate = cb(predicate, context); |
642 | 610 | var length = array != null && array.length; |
|
649 | 617 | } |
650 | 618 |
|
651 | 619 | // 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); |
655 | 622 |
|
656 | 623 | // Use a comparator function to figure out the smallest index at which |
657 | 624 | // an object should be inserted so as to maintain order. Uses binary search. |
|
666 | 633 | return low; |
667 | 634 | }; |
668 | 635 |
|
| 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 | + |
669 | 668 | // Generate an integer Array containing an arithmetic progression. A port of |
670 | 669 | // the native Python `range()` function. See |
671 | 670 | // [the Python documentation](http://docs.python.org/library/functions.html#range). |
|
0 commit comments