transform.js 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. 'use strict';
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports.default = transform;
  6. var _eachOf = require('./eachOf.js');
  7. var _eachOf2 = _interopRequireDefault(_eachOf);
  8. var _once = require('./internal/once.js');
  9. var _once2 = _interopRequireDefault(_once);
  10. var _wrapAsync = require('./internal/wrapAsync.js');
  11. var _wrapAsync2 = _interopRequireDefault(_wrapAsync);
  12. var _promiseCallback = require('./internal/promiseCallback.js');
  13. function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
  14. /**
  15. * A relative of `reduce`. Takes an Object or Array, and iterates over each
  16. * element in parallel, each step potentially mutating an `accumulator` value.
  17. * The type of the accumulator defaults to the type of collection passed in.
  18. *
  19. * @name transform
  20. * @static
  21. * @memberOf module:Collections
  22. * @method
  23. * @category Collection
  24. * @param {Array|Iterable|AsyncIterable|Object} coll - A collection to iterate over.
  25. * @param {*} [accumulator] - The initial state of the transform. If omitted,
  26. * it will default to an empty Object or Array, depending on the type of `coll`
  27. * @param {AsyncFunction} iteratee - A function applied to each item in the
  28. * collection that potentially modifies the accumulator.
  29. * Invoked with (accumulator, item, key, callback).
  30. * @param {Function} [callback] - A callback which is called after all the
  31. * `iteratee` functions have finished. Result is the transformed accumulator.
  32. * Invoked with (err, result).
  33. * @returns {Promise} a promise, if no callback provided
  34. * @example
  35. *
  36. * // file1.txt is a file that is 1000 bytes in size
  37. * // file2.txt is a file that is 2000 bytes in size
  38. * // file3.txt is a file that is 3000 bytes in size
  39. *
  40. * // helper function that returns human-readable size format from bytes
  41. * function formatBytes(bytes, decimals = 2) {
  42. * // implementation not included for brevity
  43. * return humanReadbleFilesize;
  44. * }
  45. *
  46. * const fileList = ['file1.txt','file2.txt','file3.txt'];
  47. *
  48. * // asynchronous function that returns the file size, transformed to human-readable format
  49. * // e.g. 1024 bytes = 1KB, 1234 bytes = 1.21 KB, 1048576 bytes = 1MB, etc.
  50. * function transformFileSize(acc, value, key, callback) {
  51. * fs.stat(value, function(err, stat) {
  52. * if (err) {
  53. * return callback(err);
  54. * }
  55. * acc[key] = formatBytes(stat.size);
  56. * callback(null);
  57. * });
  58. * }
  59. *
  60. * // Using callbacks
  61. * async.transform(fileList, transformFileSize, function(err, result) {
  62. * if(err) {
  63. * console.log(err);
  64. * } else {
  65. * console.log(result);
  66. * // [ '1000 Bytes', '1.95 KB', '2.93 KB' ]
  67. * }
  68. * });
  69. *
  70. * // Using Promises
  71. * async.transform(fileList, transformFileSize)
  72. * .then(result => {
  73. * console.log(result);
  74. * // [ '1000 Bytes', '1.95 KB', '2.93 KB' ]
  75. * }).catch(err => {
  76. * console.log(err);
  77. * });
  78. *
  79. * // Using async/await
  80. * (async () => {
  81. * try {
  82. * let result = await async.transform(fileList, transformFileSize);
  83. * console.log(result);
  84. * // [ '1000 Bytes', '1.95 KB', '2.93 KB' ]
  85. * }
  86. * catch (err) {
  87. * console.log(err);
  88. * }
  89. * })();
  90. *
  91. * @example
  92. *
  93. * // file1.txt is a file that is 1000 bytes in size
  94. * // file2.txt is a file that is 2000 bytes in size
  95. * // file3.txt is a file that is 3000 bytes in size
  96. *
  97. * // helper function that returns human-readable size format from bytes
  98. * function formatBytes(bytes, decimals = 2) {
  99. * // implementation not included for brevity
  100. * return humanReadbleFilesize;
  101. * }
  102. *
  103. * const fileMap = { f1: 'file1.txt', f2: 'file2.txt', f3: 'file3.txt' };
  104. *
  105. * // asynchronous function that returns the file size, transformed to human-readable format
  106. * // e.g. 1024 bytes = 1KB, 1234 bytes = 1.21 KB, 1048576 bytes = 1MB, etc.
  107. * function transformFileSize(acc, value, key, callback) {
  108. * fs.stat(value, function(err, stat) {
  109. * if (err) {
  110. * return callback(err);
  111. * }
  112. * acc[key] = formatBytes(stat.size);
  113. * callback(null);
  114. * });
  115. * }
  116. *
  117. * // Using callbacks
  118. * async.transform(fileMap, transformFileSize, function(err, result) {
  119. * if(err) {
  120. * console.log(err);
  121. * } else {
  122. * console.log(result);
  123. * // { f1: '1000 Bytes', f2: '1.95 KB', f3: '2.93 KB' }
  124. * }
  125. * });
  126. *
  127. * // Using Promises
  128. * async.transform(fileMap, transformFileSize)
  129. * .then(result => {
  130. * console.log(result);
  131. * // { f1: '1000 Bytes', f2: '1.95 KB', f3: '2.93 KB' }
  132. * }).catch(err => {
  133. * console.log(err);
  134. * });
  135. *
  136. * // Using async/await
  137. * async () => {
  138. * try {
  139. * let result = await async.transform(fileMap, transformFileSize);
  140. * console.log(result);
  141. * // { f1: '1000 Bytes', f2: '1.95 KB', f3: '2.93 KB' }
  142. * }
  143. * catch (err) {
  144. * console.log(err);
  145. * }
  146. * }
  147. *
  148. */
  149. function transform(coll, accumulator, iteratee, callback) {
  150. if (arguments.length <= 3 && typeof accumulator === 'function') {
  151. callback = iteratee;
  152. iteratee = accumulator;
  153. accumulator = Array.isArray(coll) ? [] : {};
  154. }
  155. callback = (0, _once2.default)(callback || (0, _promiseCallback.promiseCallback)());
  156. var _iteratee = (0, _wrapAsync2.default)(iteratee);
  157. (0, _eachOf2.default)(coll, (v, k, cb) => {
  158. _iteratee(accumulator, v, k, cb);
  159. }, err => callback(err, accumulator));
  160. return callback[_promiseCallback.PROMISE_SYMBOL];
  161. }
  162. module.exports = exports.default;