| 'use strict'; |
| var bind = require('../internals/function-bind-context'); |
| var IndexedObject = require('../internals/indexed-object'); |
| var toObject = require('../internals/to-object'); |
| var lengthOfArrayLike = require('../internals/length-of-array-like'); |
| var arraySpeciesCreate = require('../internals/array-species-create'); |
| var createProperty = require('../internals/create-property'); |
|
|
| |
| var createMethod = function (TYPE) { |
| var IS_MAP = TYPE === 1; |
| var IS_FILTER = TYPE === 2; |
| var IS_SOME = TYPE === 3; |
| var IS_EVERY = TYPE === 4; |
| var IS_FIND_INDEX = TYPE === 6; |
| var IS_FILTER_REJECT = TYPE === 7; |
| var NO_HOLES = TYPE === 5 || IS_FIND_INDEX; |
| return function ($this, callbackfn, that) { |
| var O = toObject($this); |
| var self = IndexedObject(O); |
| var length = lengthOfArrayLike(self); |
| var boundFunction = bind(callbackfn, that); |
| var index = 0; |
| var resIndex = 0; |
| var target = IS_MAP ? arraySpeciesCreate($this, length) : IS_FILTER || IS_FILTER_REJECT ? arraySpeciesCreate($this, 0) : undefined; |
| var value, result; |
| for (;length > index; index++) if (NO_HOLES || index in self) { |
| value = self[index]; |
| result = boundFunction(value, index, O); |
| if (TYPE) { |
| if (IS_MAP) createProperty(target, index, result); |
| else if (result) switch (TYPE) { |
| case 3: return true; |
| case 5: return value; |
| case 6: return index; |
| case 2: createProperty(target, resIndex++, value); |
| } else switch (TYPE) { |
| case 4: return false; |
| case 7: createProperty(target, resIndex++, value); |
| } |
| } |
| } |
| return IS_FIND_INDEX ? -1 : IS_SOME || IS_EVERY ? IS_EVERY : target; |
| }; |
| }; |
|
|
| module.exports = { |
| |
| |
| forEach: createMethod(0), |
| |
| |
| map: createMethod(1), |
| |
| |
| filter: createMethod(2), |
| |
| |
| some: createMethod(3), |
| |
| |
| every: createMethod(4), |
| |
| |
| find: createMethod(5), |
| |
| |
| findIndex: createMethod(6), |
| |
| |
| filterReject: createMethod(7) |
| }; |
|
|