Software zum Installieren eines Smart-Mirror Frameworks , zum Nutzen von hochschulrelevanten Informationen, auf einem Raspberry-Pi.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

reduce.js 2.7KB

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