foldl.js 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. 'use strict';
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports.default = reduce;
  6. var _eachOfSeries = require('./eachOfSeries');
  7. var _eachOfSeries2 = _interopRequireDefault(_eachOfSeries);
  8. var _noop = require('lodash/noop');
  9. var _noop2 = _interopRequireDefault(_noop);
  10. var _once = require('./internal/once');
  11. var _once2 = _interopRequireDefault(_once);
  12. var _wrapAsync = require('./internal/wrapAsync');
  13. var _wrapAsync2 = _interopRequireDefault(_wrapAsync);
  14. function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
  15. /**
  16. * Reduces `coll` into a single value using an async `iteratee` to return each
  17. * successive step. `memo` is the initial state of the reduction. This function
  18. * only operates in series.
  19. *
  20. * For performance reasons, it may make sense to split a call to this function
  21. * into a parallel map, and then use the normal `Array.prototype.reduce` on the
  22. * results. This function is for situations where each step in the reduction
  23. * needs to be async; if you can get the data before reducing it, then it's
  24. * probably a good idea to do so.
  25. *
  26. * @name reduce
  27. * @static
  28. * @memberOf module:Collections
  29. * @method
  30. * @alias inject
  31. * @alias foldl
  32. * @category Collection
  33. * @param {Array|Iterable|Object} coll - A collection to iterate over.
  34. * @param {*} memo - The initial state of the reduction.
  35. * @param {AsyncFunction} iteratee - A function applied to each item in the
  36. * array to produce the next step in the reduction.
  37. * The `iteratee` should complete with the next state of the reduction.
  38. * If the iteratee complete with an error, the reduction is stopped and the
  39. * main `callback` is immediately called with the error.
  40. * Invoked with (memo, item, callback).
  41. * @param {Function} [callback] - A callback which is called after all the
  42. * `iteratee` functions have finished. Result is the reduced value. Invoked with
  43. * (err, result).
  44. * @example
  45. *
  46. * async.reduce([1,2,3], 0, function(memo, item, callback) {
  47. * // pointless async:
  48. * process.nextTick(function() {
  49. * callback(null, memo + item)
  50. * });
  51. * }, function(err, result) {
  52. * // result is now equal to the last value of memo, which is 6
  53. * });
  54. */
  55. function reduce(coll, memo, iteratee, callback) {
  56. callback = (0, _once2.default)(callback || _noop2.default);
  57. var _iteratee = (0, _wrapAsync2.default)(iteratee);
  58. (0, _eachOfSeries2.default)(coll, function (x, i, callback) {
  59. _iteratee(memo, x, function (err, v) {
  60. memo = v;
  61. callback(err);
  62. });
  63. }, function (err) {
  64. callback(err, memo);
  65. });
  66. }
  67. module.exports = exports['default'];