mapValuesLimit.js 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  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. function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
  13. /**
  14. * The same as [`mapValues`]{@link module:Collections.mapValues} but runs a maximum of `limit` async operations at a
  15. * time.
  16. *
  17. * @name mapValuesLimit
  18. * @static
  19. * @memberOf module:Collections
  20. * @method
  21. * @see [async.mapValues]{@link module:Collections.mapValues}
  22. * @category Collection
  23. * @param {Object} obj - A collection to iterate over.
  24. * @param {number} limit - The maximum number of async operations at a time.
  25. * @param {Function} iteratee - A function to apply to each value in `obj`.
  26. * The iteratee is passed a `callback(err, transformed)` which must be called
  27. * once it has completed with an error (which can be `null`) and a
  28. * transformed value. Invoked with (value, key, callback).
  29. * @param {Function} [callback] - A callback which is called when all `iteratee`
  30. * functions have finished, or an error occurs. `result` is a new object consisting
  31. * of each key from `obj`, with each transformed value on the right-hand side.
  32. * Invoked with (err, result).
  33. */
  34. function mapValuesLimit(obj, limit, iteratee, callback) {
  35. callback = (0, _once2.default)(callback || _noop2.default);
  36. var newObj = {};
  37. (0, _eachOfLimit2.default)(obj, limit, function (val, key, next) {
  38. iteratee(val, key, function (err, result) {
  39. if (err) return next(err);
  40. newObj[key] = result;
  41. next();
  42. });
  43. }, function (err) {
  44. callback(err, newObj);
  45. });
  46. }
  47. module.exports = exports['default'];