transform.js 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  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. function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
  15. /**
  16. * A relative of `reduce`. Takes an Object or Array, and iterates over each
  17. * element in series, each step potentially mutating an `accumulator` value.
  18. * The type of the accumulator defaults to the type of collection passed in.
  19. *
  20. * @name transform
  21. * @static
  22. * @memberOf module:Collections
  23. * @method
  24. * @category Collection
  25. * @param {Array|Iterable|Object} coll - A collection to iterate over.
  26. * @param {*} [accumulator] - The initial state of the transform. If omitted,
  27. * it will default to an empty Object or Array, depending on the type of `coll`
  28. * @param {Function} iteratee - A function applied to each item in the
  29. * collection that potentially modifies the accumulator. The `iteratee` is
  30. * passed a `callback(err)` which accepts an optional error as its first
  31. * argument. If an error is passed to the callback, the transform is stopped
  32. * and the main `callback` is immediately called with the error.
  33. * Invoked with (accumulator, item, key, callback).
  34. * @param {Function} [callback] - A callback which is called after all the
  35. * `iteratee` functions have finished. Result is the transformed accumulator.
  36. * Invoked with (err, result).
  37. * @example
  38. *
  39. * async.transform([1,2,3], function(acc, item, index, callback) {
  40. * // pointless async:
  41. * process.nextTick(function() {
  42. * acc.push(item * 2)
  43. * callback(null)
  44. * });
  45. * }, function(err, result) {
  46. * // result is now equal to [2, 4, 6]
  47. * });
  48. *
  49. * @example
  50. *
  51. * async.transform({a: 1, b: 2, c: 3}, function (obj, val, key, callback) {
  52. * setImmediate(function () {
  53. * obj[key] = val * 2;
  54. * callback();
  55. * })
  56. * }, function (err, result) {
  57. * // result is equal to {a: 2, b: 4, c: 6}
  58. * })
  59. */
  60. function transform(coll, accumulator, iteratee, callback) {
  61. if (arguments.length === 3) {
  62. callback = iteratee;
  63. iteratee = accumulator;
  64. accumulator = (0, _isArray2.default)(coll) ? [] : {};
  65. }
  66. callback = (0, _once2.default)(callback || _noop2.default);
  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'];