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