aboutsummaryrefslogtreecommitdiff
path: root/node_modules/find-index/last.js
blob: 186739a68db9d80a38d92e5e89271762784b3343 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
function findLastIndex(array, predicate, self) {
  var len = array.length;
  var i;
  if (len === 0) return -1;
  if (typeof predicate !== 'function') {
    throw new TypeError(predicate + ' must be a function');
  }

  if (self) {
    for (i = len - 1; i >= 0; i--) {
      if (predicate.call(self, array[i], i, array)) {
        return i;
      }
    }
  } else {
    for (i = len - 1; i >= 0; i--) {
      if (predicate(array[i], i, array)) {
        return i;
      }
    }
  }

  return -1;
}

module.exports = findLastIndex