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 |
|
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;
|
34 | else if (result) switch (TYPE) {
|
35 | case 3: return true;
|
36 | case 5: return value;
|
37 | case 6: return index;
|
38 | case 2: push(target, value);
|
39 | } else switch (TYPE) {
|
40 | case 4: return false;
|
41 | case 7: push(target, value);
|
42 | }
|
43 | }
|
44 | }
|
45 | return IS_FIND_INDEX ? -1 : IS_SOME || IS_EVERY ? IS_EVERY : target;
|
46 | };
|
47 | };
|
48 |
|
49 | module.exports = {
|
50 |
|
51 |
|
52 | forEach: createMethod(0),
|
53 |
|
54 |
|
55 | map: createMethod(1),
|
56 |
|
57 |
|
58 | filter: createMethod(2),
|
59 |
|
60 |
|
61 | some: createMethod(3),
|
62 |
|
63 |
|
64 | every: createMethod(4),
|
65 |
|
66 |
|
67 | find: createMethod(5),
|
68 |
|
69 |
|
70 | findIndex: createMethod(6),
|
71 |
|
72 |
|
73 | filterReject: createMethod(7)
|
74 | };
|