react / wstein / node_modules / jest-cli / node_modules / istanbul / node_modules / js-yaml / node_modules / argparse / node_modules / lodash / object / transform.js
80742 viewsvar arrayEach = require('../internal/arrayEach'),1baseCallback = require('../internal/baseCallback'),2baseCreate = require('../internal/baseCreate'),3baseForOwn = require('../internal/baseForOwn'),4isArray = require('../lang/isArray'),5isFunction = require('../lang/isFunction'),6isObject = require('../lang/isObject'),7isTypedArray = require('../lang/isTypedArray');89/**10* An alternative to `_.reduce`; this method transforms `object` to a new11* `accumulator` object which is the result of running each of its own enumerable12* properties through `iteratee`, with each invocation potentially mutating13* the `accumulator` object. The `iteratee` is bound to `thisArg` and invoked14* with four arguments: (accumulator, value, key, object). Iteratee functions15* may exit iteration early by explicitly returning `false`.16*17* @static18* @memberOf _19* @category Object20* @param {Array|Object} object The object to iterate over.21* @param {Function} [iteratee=_.identity] The function invoked per iteration.22* @param {*} [accumulator] The custom accumulator value.23* @param {*} [thisArg] The `this` binding of `iteratee`.24* @returns {*} Returns the accumulated value.25* @example26*27* _.transform([2, 3, 4], function(result, n) {28* result.push(n *= n);29* return n % 2 == 0;30* });31* // => [4, 9]32*33* _.transform({ 'a': 1, 'b': 2 }, function(result, n, key) {34* result[key] = n * 3;35* });36* // => { 'a': 3, 'b': 6 }37*/38function transform(object, iteratee, accumulator, thisArg) {39var isArr = isArray(object) || isTypedArray(object);40iteratee = baseCallback(iteratee, thisArg, 4);4142if (accumulator == null) {43if (isArr || isObject(object)) {44var Ctor = object.constructor;45if (isArr) {46accumulator = isArray(object) ? new Ctor : [];47} else {48accumulator = baseCreate(isFunction(Ctor) ? Ctor.prototype : null);49}50} else {51accumulator = {};52}53}54(isArr ? arrayEach : baseForOwn)(object, function(value, index, object) {55return iteratee(accumulator, value, index, object);56});57return accumulator;58}5960module.exports = transform;616263