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