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