3 Object.defineProperty(exports, "__esModule", {
6 exports.default = transform;
8 var _isArray = require('lodash/isArray');
10 var _isArray2 = _interopRequireDefault(_isArray);
12 var _noop = require('lodash/noop');
14 var _noop2 = _interopRequireDefault(_noop);
16 var _eachOf = require('./eachOf');
18 var _eachOf2 = _interopRequireDefault(_eachOf);
20 var _once = require('./internal/once');
22 var _once2 = _interopRequireDefault(_once);
24 function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
27 * A relative of `reduce`. Takes an Object or Array, and iterates over each
28 * element in series, each step potentially mutating an `accumulator` value.
29 * The type of the accumulator defaults to the type of collection passed in.
33 * @memberOf module:Collections
35 * @category Collection
36 * @param {Array|Iterable|Object} coll - A collection to iterate over.
37 * @param {*} [accumulator] - The initial state of the transform. If omitted,
38 * it will default to an empty Object or Array, depending on the type of `coll`
39 * @param {Function} iteratee - A function applied to each item in the
40 * collection that potentially modifies the accumulator. The `iteratee` is
41 * passed a `callback(err)` which accepts an optional error as its first
42 * argument. If an error is passed to the callback, the transform is stopped
43 * and the main `callback` is immediately called with the error.
44 * Invoked with (accumulator, item, key, callback).
45 * @param {Function} [callback] - A callback which is called after all the
46 * `iteratee` functions have finished. Result is the transformed accumulator.
47 * Invoked with (err, result).
50 * async.transform([1,2,3], function(acc, item, index, callback) {
52 * process.nextTick(function() {
56 * }, function(err, result) {
57 * // result is now equal to [2, 4, 6]
62 * async.transform({a: 1, b: 2, c: 3}, function (obj, val, key, callback) {
63 * setImmediate(function () {
67 * }, function (err, result) {
68 * // result is equal to {a: 2, b: 4, c: 6}
71 function transform(coll, accumulator, iteratee, callback) {
72 if (arguments.length === 3) {
74 iteratee = accumulator;
75 accumulator = (0, _isArray2.default)(coll) ? [] : {};
77 callback = (0, _once2.default)(callback || _noop2.default);
79 (0, _eachOf2.default)(coll, function (v, k, cb) {
80 iteratee(accumulator, v, k, cb);
82 callback(err, accumulator);
85 module.exports = exports['default'];