UNPKG

1.2 kBJavaScriptView Raw
1'use strict';
2var bind = require('../internals/function-bind-context');
3var IndexedObject = require('../internals/indexed-object');
4var toObject = require('../internals/to-object');
5var lengthOfArrayLike = require('../internals/length-of-array-like');
6
7// `Array.prototype.{ findLast, findLastIndex }` methods implementation
8var createMethod = function (TYPE) {
9 var IS_FIND_LAST_INDEX = TYPE === 1;
10 return function ($this, callbackfn, that) {
11 var O = toObject($this);
12 var self = IndexedObject(O);
13 var index = lengthOfArrayLike(self);
14 var boundFunction = bind(callbackfn, that);
15 var value, result;
16 while (index-- > 0) {
17 value = self[index];
18 result = boundFunction(value, index, O);
19 if (result) switch (TYPE) {
20 case 0: return value; // findLast
21 case 1: return index; // findLastIndex
22 }
23 }
24 return IS_FIND_LAST_INDEX ? -1 : undefined;
25 };
26};
27
28module.exports = {
29 // `Array.prototype.findLast` method
30 // https://github.com/tc39/proposal-array-find-from-last
31 findLast: createMethod(0),
32 // `Array.prototype.findLastIndex` method
33 // https://github.com/tc39/proposal-array-find-from-last
34 findLastIndex: createMethod(1)
35};