mapValues.js 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. 'use strict';
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports.default = mapValues;
  6. var _mapValuesLimit = require('./mapValuesLimit.js');
  7. var _mapValuesLimit2 = _interopRequireDefault(_mapValuesLimit);
  8. function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
  9. /**
  10. * A relative of [`map`]{@link module:Collections.map}, designed for use with objects.
  11. *
  12. * Produces a new Object by mapping each value of `obj` through the `iteratee`
  13. * function. The `iteratee` is called each `value` and `key` from `obj` and a
  14. * callback for when it has finished processing. Each of these callbacks takes
  15. * two arguments: an `error`, and the transformed item from `obj`. If `iteratee`
  16. * passes an error to its callback, the main `callback` (for the `mapValues`
  17. * function) is immediately called with the error.
  18. *
  19. * Note, the order of the keys in the result is not guaranteed. The keys will
  20. * be roughly in the order they complete, (but this is very engine-specific)
  21. *
  22. * @name mapValues
  23. * @static
  24. * @memberOf module:Collections
  25. * @method
  26. * @category Collection
  27. * @param {Object} obj - A collection to iterate over.
  28. * @param {AsyncFunction} iteratee - A function to apply to each value and key
  29. * in `coll`.
  30. * The iteratee should complete with the transformed value as its result.
  31. * Invoked with (value, key, callback).
  32. * @param {Function} [callback] - A callback which is called when all `iteratee`
  33. * functions have finished, or an error occurs. `result` is a new object consisting
  34. * of each key from `obj`, with each transformed value on the right-hand side.
  35. * Invoked with (err, result).
  36. * @returns {Promise} a promise, if no callback is passed
  37. * @example
  38. *
  39. * // file1.txt is a file that is 1000 bytes in size
  40. * // file2.txt is a file that is 2000 bytes in size
  41. * // file3.txt is a file that is 3000 bytes in size
  42. * // file4.txt does not exist
  43. *
  44. * const fileMap = {
  45. * f1: 'file1.txt',
  46. * f2: 'file2.txt',
  47. * f3: 'file3.txt'
  48. * };
  49. *
  50. * const withMissingFileMap = {
  51. * f1: 'file1.txt',
  52. * f2: 'file2.txt',
  53. * f3: 'file4.txt'
  54. * };
  55. *
  56. * // asynchronous function that returns the file size in bytes
  57. * function getFileSizeInBytes(file, key, callback) {
  58. * fs.stat(file, function(err, stat) {
  59. * if (err) {
  60. * return callback(err);
  61. * }
  62. * callback(null, stat.size);
  63. * });
  64. * }
  65. *
  66. * // Using callbacks
  67. * async.mapValues(fileMap, getFileSizeInBytes, function(err, result) {
  68. * if (err) {
  69. * console.log(err);
  70. * } else {
  71. * console.log(result);
  72. * // result is now a map of file size in bytes for each file, e.g.
  73. * // {
  74. * // f1: 1000,
  75. * // f2: 2000,
  76. * // f3: 3000
  77. * // }
  78. * }
  79. * });
  80. *
  81. * // Error handling
  82. * async.mapValues(withMissingFileMap, getFileSizeInBytes, function(err, result) {
  83. * if (err) {
  84. * console.log(err);
  85. * // [ Error: ENOENT: no such file or directory ]
  86. * } else {
  87. * console.log(result);
  88. * }
  89. * });
  90. *
  91. * // Using Promises
  92. * async.mapValues(fileMap, getFileSizeInBytes)
  93. * .then( result => {
  94. * console.log(result);
  95. * // result is now a map of file size in bytes for each file, e.g.
  96. * // {
  97. * // f1: 1000,
  98. * // f2: 2000,
  99. * // f3: 3000
  100. * // }
  101. * }).catch (err => {
  102. * console.log(err);
  103. * });
  104. *
  105. * // Error Handling
  106. * async.mapValues(withMissingFileMap, getFileSizeInBytes)
  107. * .then( result => {
  108. * console.log(result);
  109. * }).catch (err => {
  110. * console.log(err);
  111. * // [ Error: ENOENT: no such file or directory ]
  112. * });
  113. *
  114. * // Using async/await
  115. * async () => {
  116. * try {
  117. * let result = await async.mapValues(fileMap, getFileSizeInBytes);
  118. * console.log(result);
  119. * // result is now a map of file size in bytes for each file, e.g.
  120. * // {
  121. * // f1: 1000,
  122. * // f2: 2000,
  123. * // f3: 3000
  124. * // }
  125. * }
  126. * catch (err) {
  127. * console.log(err);
  128. * }
  129. * }
  130. *
  131. * // Error Handling
  132. * async () => {
  133. * try {
  134. * let result = await async.mapValues(withMissingFileMap, getFileSizeInBytes);
  135. * console.log(result);
  136. * }
  137. * catch (err) {
  138. * console.log(err);
  139. * // [ Error: ENOENT: no such file or directory ]
  140. * }
  141. * }
  142. *
  143. */
  144. function mapValues(obj, iteratee, callback) {
  145. return (0, _mapValuesLimit2.default)(obj, Infinity, iteratee, callback);
  146. }
  147. module.exports = exports['default'];