1 var arrayEach = require('./internal/arrayEach'),
2 baseCreate = require('./internal/baseCreate'),
3 baseForOwn = require('./internal/baseForOwn'),
4 baseIteratee = require('./internal/baseIteratee'),
5 isArray = require('./isArray'),
6 isFunction = require('./isFunction'),
7 isObject = require('./isObject'),
8 isTypedArray = require('./isTypedArray');
11 * An alternative to `_.reduce`; this method transforms `object` to a new
12 * `accumulator` object which is the result of running each of its own enumerable
13 * properties through `iteratee`, with each invocation potentially mutating
14 * the `accumulator` object. The iteratee is invoked with four arguments:
15 * (accumulator, value, key, object). Iteratee functions may exit iteration
16 * early by explicitly returning `false`.
21 * @param {Array|Object} object The object to iterate over.
22 * @param {Function} [iteratee=_.identity] The function invoked per iteration.
23 * @param {*} [accumulator] The custom accumulator value.
24 * @returns {*} Returns the accumulated value.
27 * _.transform([2, 3, 4], function(result, n) {
28 * result.push(n *= n);
33 * _.transform({ 'a': 1, 'b': 2, 'c': 1 }, function(result, value, key) {
34 * (result[value] || (result[value] = [])).push(key);
36 * // => { '1': ['a', 'c'], '2': ['b'] }
38 function transform(object, iteratee, accumulator) {
39 var isArr = isArray(object) || isTypedArray(object);
40 iteratee = baseIteratee(iteratee, 4);
42 if (accumulator == null) {
43 if (isArr || isObject(object)) {
44 var Ctor = object.constructor;
46 accumulator = isArray(object) ? new Ctor : [];
48 accumulator = baseCreate(isFunction(Ctor) ? Ctor.prototype : undefined);
54 (isArr ? arrayEach : baseForOwn)(object, function(value, index, object) {
55 return iteratee(accumulator, value, index, object);
60 module.exports = transform;