1 | 'use strict';
|
---|
2 | var bind = require('../internals/function-bind-context');
|
---|
3 | var uncurryThis = require('../internals/function-uncurry-this');
|
---|
4 | var IndexedObject = require('../internals/indexed-object');
|
---|
5 | var toObject = require('../internals/to-object');
|
---|
6 | var lengthOfArrayLike = require('../internals/length-of-array-like');
|
---|
7 | var arraySpeciesCreate = require('../internals/array-species-create');
|
---|
8 |
|
---|
9 | var push = uncurryThis([].push);
|
---|
10 |
|
---|
11 | // `Array.prototype.{ forEach, map, filter, some, every, find, findIndex, filterReject }` methods implementation
|
---|
12 | var createMethod = function (TYPE) {
|
---|
13 | var IS_MAP = TYPE === 1;
|
---|
14 | var IS_FILTER = TYPE === 2;
|
---|
15 | var IS_SOME = TYPE === 3;
|
---|
16 | var IS_EVERY = TYPE === 4;
|
---|
17 | var IS_FIND_INDEX = TYPE === 6;
|
---|
18 | var IS_FILTER_REJECT = TYPE === 7;
|
---|
19 | var NO_HOLES = TYPE === 5 || IS_FIND_INDEX;
|
---|
20 | return function ($this, callbackfn, that, specificCreate) {
|
---|
21 | var O = toObject($this);
|
---|
22 | var self = IndexedObject(O);
|
---|
23 | var length = lengthOfArrayLike(self);
|
---|
24 | var boundFunction = bind(callbackfn, that);
|
---|
25 | var index = 0;
|
---|
26 | var create = specificCreate || arraySpeciesCreate;
|
---|
27 | var target = IS_MAP ? create($this, length) : IS_FILTER || IS_FILTER_REJECT ? create($this, 0) : undefined;
|
---|
28 | var value, result;
|
---|
29 | for (;length > index; index++) if (NO_HOLES || index in self) {
|
---|
30 | value = self[index];
|
---|
31 | result = boundFunction(value, index, O);
|
---|
32 | if (TYPE) {
|
---|
33 | if (IS_MAP) target[index] = result; // map
|
---|
34 | else if (result) switch (TYPE) {
|
---|
35 | case 3: return true; // some
|
---|
36 | case 5: return value; // find
|
---|
37 | case 6: return index; // findIndex
|
---|
38 | case 2: push(target, value); // filter
|
---|
39 | } else switch (TYPE) {
|
---|
40 | case 4: return false; // every
|
---|
41 | case 7: push(target, value); // filterReject
|
---|
42 | }
|
---|
43 | }
|
---|
44 | }
|
---|
45 | return IS_FIND_INDEX ? -1 : IS_SOME || IS_EVERY ? IS_EVERY : target;
|
---|
46 | };
|
---|
47 | };
|
---|
48 |
|
---|
49 | module.exports = {
|
---|
50 | // `Array.prototype.forEach` method
|
---|
51 | // https://tc39.es/ecma262/#sec-array.prototype.foreach
|
---|
52 | forEach: createMethod(0),
|
---|
53 | // `Array.prototype.map` method
|
---|
54 | // https://tc39.es/ecma262/#sec-array.prototype.map
|
---|
55 | map: createMethod(1),
|
---|
56 | // `Array.prototype.filter` method
|
---|
57 | // https://tc39.es/ecma262/#sec-array.prototype.filter
|
---|
58 | filter: createMethod(2),
|
---|
59 | // `Array.prototype.some` method
|
---|
60 | // https://tc39.es/ecma262/#sec-array.prototype.some
|
---|
61 | some: createMethod(3),
|
---|
62 | // `Array.prototype.every` method
|
---|
63 | // https://tc39.es/ecma262/#sec-array.prototype.every
|
---|
64 | every: createMethod(4),
|
---|
65 | // `Array.prototype.find` method
|
---|
66 | // https://tc39.es/ecma262/#sec-array.prototype.find
|
---|
67 | find: createMethod(5),
|
---|
68 | // `Array.prototype.findIndex` method
|
---|
69 | // https://tc39.es/ecma262/#sec-array.prototype.findIndex
|
---|
70 | findIndex: createMethod(6),
|
---|
71 | // `Array.prototype.filterReject` method
|
---|
72 | // https://github.com/tc39/proposal-array-filtering
|
---|
73 | filterReject: createMethod(7)
|
---|
74 | };
|
---|