1 | 'use strict';
|
---|
2 | var bind = require('../internals/function-bind-context');
|
---|
3 | var IndexedObject = require('../internals/indexed-object');
|
---|
4 | var toObject = require('../internals/to-object');
|
---|
5 | var lengthOfArrayLike = require('../internals/length-of-array-like');
|
---|
6 |
|
---|
7 | // `Array.prototype.{ findLast, findLastIndex }` methods implementation
|
---|
8 | var 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 |
|
---|
28 | module.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 | };
|
---|