_createIndexFinder.js 977 B

12345678910111213141516171819202122232425262728
  1. import getLength from './_getLength.js';
  2. import { slice } from './_setup.js';
  3. import isNaN from './isNaN.js';
  4. // Internal function to generate the `_.indexOf` and `_.lastIndexOf` functions.
  5. export default function createIndexFinder(dir, predicateFind, sortedIndex) {
  6. return function(array, item, idx) {
  7. var i = 0, length = getLength(array);
  8. if (typeof idx == 'number') {
  9. if (dir > 0) {
  10. i = idx >= 0 ? idx : Math.max(idx + length, i);
  11. } else {
  12. length = idx >= 0 ? Math.min(idx + 1, length) : idx + length + 1;
  13. }
  14. } else if (sortedIndex && idx && length) {
  15. idx = sortedIndex(array, item);
  16. return array[idx] === item ? idx : -1;
  17. }
  18. if (item !== item) {
  19. idx = predicateFind(slice.call(array, i, length), isNaN);
  20. return idx >= 0 ? idx + i : -1;
  21. }
  22. for (idx = dir > 0 ? i : length - 1; idx >= 0 && idx < length; idx += dir) {
  23. if (array[idx] === item) return idx;
  24. }
  25. return -1;
  26. };
  27. }