transform.js
 1  'use strict';
 2  
 3  Object.defineProperty(exports, "__esModule", {
 4      value: true
 5  });
 6  exports.default = transform;
 7  
 8  var _eachOf = require('./eachOf');
 9  
10  var _eachOf2 = _interopRequireDefault(_eachOf);
11  
12  var _once = require('./internal/once');
13  
14  var _once2 = _interopRequireDefault(_once);
15  
16  var _wrapAsync = require('./internal/wrapAsync');
17  
18  var _wrapAsync2 = _interopRequireDefault(_wrapAsync);
19  
20  var _promiseCallback = require('./internal/promiseCallback');
21  
22  function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
23  
24  /**
25   * A relative of `reduce`.  Takes an Object or Array, and iterates over each
26   * element in parallel, each step potentially mutating an `accumulator` value.
27   * The type of the accumulator defaults to the type of collection passed in.
28   *
29   * @name transform
30   * @static
31   * @memberOf module:Collections
32   * @method
33   * @category Collection
34   * @param {Array|Iterable|AsyncIterable|Object} coll - A collection to iterate over.
35   * @param {*} [accumulator] - The initial state of the transform.  If omitted,
36   * it will default to an empty Object or Array, depending on the type of `coll`
37   * @param {AsyncFunction} iteratee - A function applied to each item in the
38   * collection that potentially modifies the accumulator.
39   * Invoked with (accumulator, item, key, callback).
40   * @param {Function} [callback] - A callback which is called after all the
41   * `iteratee` functions have finished. Result is the transformed accumulator.
42   * Invoked with (err, result).
43   * @returns {Promise} a promise, if no callback provided
44   * @example
45   *
46   * async.transform([1,2,3], function(acc, item, index, callback) {
47   *     // pointless async:
48   *     process.nextTick(function() {
49   *         acc[index] = item * 2
50   *         callback(null)
51   *     });
52   * }, function(err, result) {
53   *     // result is now equal to [2, 4, 6]
54   * });
55   *
56   * @example
57   *
58   * async.transform({a: 1, b: 2, c: 3}, function (obj, val, key, callback) {
59   *     setImmediate(function () {
60   *         obj[key] = val * 2;
61   *         callback();
62   *     })
63   * }, function (err, result) {
64   *     // result is equal to {a: 2, b: 4, c: 6}
65   * })
66   */
67  function transform(coll, accumulator, iteratee, callback) {
68      if (arguments.length <= 3 && typeof accumulator === 'function') {
69          callback = iteratee;
70          iteratee = accumulator;
71          accumulator = Array.isArray(coll) ? [] : {};
72      }
73      callback = (0, _once2.default)(callback || (0, _promiseCallback.promiseCallback)());
74      var _iteratee = (0, _wrapAsync2.default)(iteratee);
75  
76      (0, _eachOf2.default)(coll, (v, k, cb) => {
77          _iteratee(accumulator, v, k, cb);
78      }, err => callback(err, accumulator));
79      return callback[_promiseCallback.PROMISE_SYMBOL];
80  }
81  module.exports = exports['default'];