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