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