groupBy.js 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. 'use strict';
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports.default = groupBy;
  6. var _groupByLimit = require('./groupByLimit.js');
  7. var _groupByLimit2 = _interopRequireDefault(_groupByLimit);
  8. function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
  9. /**
  10. * Returns a new object, where each value corresponds to an array of items, from
  11. * `coll`, that returned the corresponding key. That is, the keys of the object
  12. * correspond to the values passed to the `iteratee` callback.
  13. *
  14. * Note: Since this function applies the `iteratee` to each item in parallel,
  15. * there is no guarantee that the `iteratee` functions will complete in order.
  16. * However, the values for each key in the `result` will be in the same order as
  17. * the original `coll`. For Objects, the values will roughly be in the order of
  18. * the original Objects' keys (but this can vary across JavaScript engines).
  19. *
  20. * @name groupBy
  21. * @static
  22. * @memberOf module:Collections
  23. * @method
  24. * @category Collection
  25. * @param {Array|Iterable|AsyncIterable|Object} coll - A collection to iterate over.
  26. * @param {AsyncFunction} iteratee - An async function to apply to each item in
  27. * `coll`.
  28. * The iteratee should complete with a `key` to group the value under.
  29. * Invoked with (value, callback).
  30. * @param {Function} [callback] - A callback which is called when all `iteratee`
  31. * functions have finished, or an error occurs. Result is an `Object` whoses
  32. * properties are arrays of values which returned the corresponding key.
  33. * @returns {Promise} a promise, if no callback is passed
  34. * @example
  35. *
  36. * // dir1 is a directory that contains file1.txt, file2.txt
  37. * // dir2 is a directory that contains file3.txt, file4.txt
  38. * // dir3 is a directory that contains file5.txt
  39. * // dir4 does not exist
  40. *
  41. * const files = ['dir1/file1.txt','dir2','dir4']
  42. *
  43. * // asynchronous function that detects file type as none, file, or directory
  44. * function detectFile(file, callback) {
  45. * fs.stat(file, function(err, stat) {
  46. * if (err) {
  47. * return callback(null, 'none');
  48. * }
  49. * callback(null, stat.isDirectory() ? 'directory' : 'file');
  50. * });
  51. * }
  52. *
  53. * //Using callbacks
  54. * async.groupBy(files, detectFile, function(err, result) {
  55. * if(err) {
  56. * console.log(err);
  57. * } else {
  58. * console.log(result);
  59. * // {
  60. * // file: [ 'dir1/file1.txt' ],
  61. * // none: [ 'dir4' ],
  62. * // directory: [ 'dir2']
  63. * // }
  64. * // result is object containing the files grouped by type
  65. * }
  66. * });
  67. *
  68. * // Using Promises
  69. * async.groupBy(files, detectFile)
  70. * .then( result => {
  71. * console.log(result);
  72. * // {
  73. * // file: [ 'dir1/file1.txt' ],
  74. * // none: [ 'dir4' ],
  75. * // directory: [ 'dir2']
  76. * // }
  77. * // result is object containing the files grouped by type
  78. * }).catch( err => {
  79. * console.log(err);
  80. * });
  81. *
  82. * // Using async/await
  83. * async () => {
  84. * try {
  85. * let result = await async.groupBy(files, detectFile);
  86. * console.log(result);
  87. * // {
  88. * // file: [ 'dir1/file1.txt' ],
  89. * // none: [ 'dir4' ],
  90. * // directory: [ 'dir2']
  91. * // }
  92. * // result is object containing the files grouped by type
  93. * }
  94. * catch (err) {
  95. * console.log(err);
  96. * }
  97. * }
  98. *
  99. */
  100. function groupBy(coll, iteratee, callback) {
  101. return (0, _groupByLimit2.default)(coll, Infinity, iteratee, callback);
  102. }
  103. module.exports = exports['default'];