_createPredicateIndexFinder.js 550 B

123456789101112131415161718
  1. var _cb = require('./_cb.js');
  2. var _getLength = require('./_getLength.js');
  3. // Internal function to generate `_.findIndex` and `_.findLastIndex`.
  4. function createPredicateIndexFinder(dir) {
  5. return function(array, predicate, context) {
  6. predicate = _cb(predicate, context);
  7. var length = _getLength(array);
  8. var index = dir > 0 ? 0 : length - 1;
  9. for (; index >= 0 && index < length; index += dir) {
  10. if (predicate(array[index], index, array)) return index;
  11. }
  12. return -1;
  13. };
  14. }
  15. module.exports = createPredicateIndexFinder;