mapValuesLimit.js 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. 'use strict';
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports.default = mapValuesLimit;
  6. var _eachOfLimit = require('./eachOfLimit');
  7. var _eachOfLimit2 = _interopRequireDefault(_eachOfLimit);
  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. * The same as [`mapValues`]{@link module:Collections.mapValues} but runs a maximum of `limit` async operations at a
  17. * time.
  18. *
  19. * @name mapValuesLimit
  20. * @static
  21. * @memberOf module:Collections
  22. * @method
  23. * @see [async.mapValues]{@link module:Collections.mapValues}
  24. * @category Collection
  25. * @param {Object} obj - A collection to iterate over.
  26. * @param {number} limit - The maximum number of async operations at a time.
  27. * @param {AsyncFunction} iteratee - A function to apply to each value and key
  28. * in `coll`.
  29. * The iteratee should complete with the transformed value as its result.
  30. * Invoked with (value, key, callback).
  31. * @param {Function} [callback] - A callback which is called when all `iteratee`
  32. * functions have finished, or an error occurs. `result` is a new object consisting
  33. * of each key from `obj`, with each transformed value on the right-hand side.
  34. * Invoked with (err, result).
  35. */
  36. function mapValuesLimit(obj, limit, iteratee, callback) {
  37. callback = (0, _once2.default)(callback || _noop2.default);
  38. var newObj = {};
  39. var _iteratee = (0, _wrapAsync2.default)(iteratee);
  40. (0, _eachOfLimit2.default)(obj, limit, function (val, key, next) {
  41. _iteratee(val, key, function (err, result) {
  42. if (err) return next(err);
  43. newObj[key] = result;
  44. next();
  45. });
  46. }, function (err) {
  47. callback(err, newObj);
  48. });
  49. }
  50. module.exports = exports['default'];