| 1 | 'use strict';
|
|---|
| 2 |
|
|---|
| 3 | Object.defineProperty(exports, "__esModule", {
|
|---|
| 4 | value: true
|
|---|
| 5 | });
|
|---|
| 6 |
|
|---|
| 7 | var _map = require('./map.js');
|
|---|
| 8 |
|
|---|
| 9 | var _map2 = _interopRequireDefault(_map);
|
|---|
| 10 |
|
|---|
| 11 | var _wrapAsync = require('./internal/wrapAsync.js');
|
|---|
| 12 |
|
|---|
| 13 | var _wrapAsync2 = _interopRequireDefault(_wrapAsync);
|
|---|
| 14 |
|
|---|
| 15 | var _awaitify = require('./internal/awaitify.js');
|
|---|
| 16 |
|
|---|
| 17 | var _awaitify2 = _interopRequireDefault(_awaitify);
|
|---|
| 18 |
|
|---|
| 19 | function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
|---|
| 20 |
|
|---|
| 21 | /**
|
|---|
| 22 | * Sorts a list by the results of running each `coll` value through an async
|
|---|
| 23 | * `iteratee`.
|
|---|
| 24 | *
|
|---|
| 25 | * @name sortBy
|
|---|
| 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 value to use as the sort criteria as
|
|---|
| 34 | * its `result`.
|
|---|
| 35 | * Invoked with (item, callback).
|
|---|
| 36 | * @param {Function} callback - A callback which is called after all the
|
|---|
| 37 | * `iteratee` functions have finished, or an error occurs. Results is the items
|
|---|
| 38 | * from the original `coll` sorted by the values returned by the `iteratee`
|
|---|
| 39 | * calls. Invoked with (err, results).
|
|---|
| 40 | * @returns {Promise} a promise, if no callback passed
|
|---|
| 41 | * @example
|
|---|
| 42 | *
|
|---|
| 43 | * // bigfile.txt is a file that is 251100 bytes in size
|
|---|
| 44 | * // mediumfile.txt is a file that is 11000 bytes in size
|
|---|
| 45 | * // smallfile.txt is a file that is 121 bytes in size
|
|---|
| 46 | *
|
|---|
| 47 | * // asynchronous function that returns the file size in bytes
|
|---|
| 48 | * function getFileSizeInBytes(file, callback) {
|
|---|
| 49 | * fs.stat(file, function(err, stat) {
|
|---|
| 50 | * if (err) {
|
|---|
| 51 | * return callback(err);
|
|---|
| 52 | * }
|
|---|
| 53 | * callback(null, stat.size);
|
|---|
| 54 | * });
|
|---|
| 55 | * }
|
|---|
| 56 | *
|
|---|
| 57 | * // Using callbacks
|
|---|
| 58 | * async.sortBy(['mediumfile.txt','smallfile.txt','bigfile.txt'], getFileSizeInBytes,
|
|---|
| 59 | * function(err, results) {
|
|---|
| 60 | * if (err) {
|
|---|
| 61 | * console.log(err);
|
|---|
| 62 | * } else {
|
|---|
| 63 | * console.log(results);
|
|---|
| 64 | * // results is now the original array of files sorted by
|
|---|
| 65 | * // file size (ascending by default), e.g.
|
|---|
| 66 | * // [ 'smallfile.txt', 'mediumfile.txt', 'bigfile.txt']
|
|---|
| 67 | * }
|
|---|
| 68 | * }
|
|---|
| 69 | * );
|
|---|
| 70 | *
|
|---|
| 71 | * // By modifying the callback parameter the
|
|---|
| 72 | * // sorting order can be influenced:
|
|---|
| 73 | *
|
|---|
| 74 | * // ascending order
|
|---|
| 75 | * async.sortBy(['mediumfile.txt','smallfile.txt','bigfile.txt'], function(file, callback) {
|
|---|
| 76 | * getFileSizeInBytes(file, function(getFileSizeErr, fileSize) {
|
|---|
| 77 | * if (getFileSizeErr) return callback(getFileSizeErr);
|
|---|
| 78 | * callback(null, fileSize);
|
|---|
| 79 | * });
|
|---|
| 80 | * }, function(err, results) {
|
|---|
| 81 | * if (err) {
|
|---|
| 82 | * console.log(err);
|
|---|
| 83 | * } else {
|
|---|
| 84 | * console.log(results);
|
|---|
| 85 | * // results is now the original array of files sorted by
|
|---|
| 86 | * // file size (ascending by default), e.g.
|
|---|
| 87 | * // [ 'smallfile.txt', 'mediumfile.txt', 'bigfile.txt']
|
|---|
| 88 | * }
|
|---|
| 89 | * }
|
|---|
| 90 | * );
|
|---|
| 91 | *
|
|---|
| 92 | * // descending order
|
|---|
| 93 | * async.sortBy(['bigfile.txt','mediumfile.txt','smallfile.txt'], function(file, callback) {
|
|---|
| 94 | * getFileSizeInBytes(file, function(getFileSizeErr, fileSize) {
|
|---|
| 95 | * if (getFileSizeErr) {
|
|---|
| 96 | * return callback(getFileSizeErr);
|
|---|
| 97 | * }
|
|---|
| 98 | * callback(null, fileSize * -1);
|
|---|
| 99 | * });
|
|---|
| 100 | * }, function(err, results) {
|
|---|
| 101 | * if (err) {
|
|---|
| 102 | * console.log(err);
|
|---|
| 103 | * } else {
|
|---|
| 104 | * console.log(results);
|
|---|
| 105 | * // results is now the original array of files sorted by
|
|---|
| 106 | * // file size (ascending by default), e.g.
|
|---|
| 107 | * // [ 'bigfile.txt', 'mediumfile.txt', 'smallfile.txt']
|
|---|
| 108 | * }
|
|---|
| 109 | * }
|
|---|
| 110 | * );
|
|---|
| 111 | *
|
|---|
| 112 | * // Error handling
|
|---|
| 113 | * async.sortBy(['mediumfile.txt','smallfile.txt','missingfile.txt'], getFileSizeInBytes,
|
|---|
| 114 | * function(err, results) {
|
|---|
| 115 | * if (err) {
|
|---|
| 116 | * console.log(err);
|
|---|
| 117 | * // [ Error: ENOENT: no such file or directory ]
|
|---|
| 118 | * } else {
|
|---|
| 119 | * console.log(results);
|
|---|
| 120 | * }
|
|---|
| 121 | * }
|
|---|
| 122 | * );
|
|---|
| 123 | *
|
|---|
| 124 | * // Using Promises
|
|---|
| 125 | * async.sortBy(['mediumfile.txt','smallfile.txt','bigfile.txt'], getFileSizeInBytes)
|
|---|
| 126 | * .then( results => {
|
|---|
| 127 | * console.log(results);
|
|---|
| 128 | * // results is now the original array of files sorted by
|
|---|
| 129 | * // file size (ascending by default), e.g.
|
|---|
| 130 | * // [ 'smallfile.txt', 'mediumfile.txt', 'bigfile.txt']
|
|---|
| 131 | * }).catch( err => {
|
|---|
| 132 | * console.log(err);
|
|---|
| 133 | * });
|
|---|
| 134 | *
|
|---|
| 135 | * // Error handling
|
|---|
| 136 | * async.sortBy(['mediumfile.txt','smallfile.txt','missingfile.txt'], getFileSizeInBytes)
|
|---|
| 137 | * .then( results => {
|
|---|
| 138 | * console.log(results);
|
|---|
| 139 | * }).catch( err => {
|
|---|
| 140 | * console.log(err);
|
|---|
| 141 | * // [ Error: ENOENT: no such file or directory ]
|
|---|
| 142 | * });
|
|---|
| 143 | *
|
|---|
| 144 | * // Using async/await
|
|---|
| 145 | * (async () => {
|
|---|
| 146 | * try {
|
|---|
| 147 | * let results = await async.sortBy(['bigfile.txt','mediumfile.txt','smallfile.txt'], getFileSizeInBytes);
|
|---|
| 148 | * console.log(results);
|
|---|
| 149 | * // results is now the original array of files sorted by
|
|---|
| 150 | * // file size (ascending by default), e.g.
|
|---|
| 151 | * // [ 'smallfile.txt', 'mediumfile.txt', 'bigfile.txt']
|
|---|
| 152 | * }
|
|---|
| 153 | * catch (err) {
|
|---|
| 154 | * console.log(err);
|
|---|
| 155 | * }
|
|---|
| 156 | * })();
|
|---|
| 157 | *
|
|---|
| 158 | * // Error handling
|
|---|
| 159 | * async () => {
|
|---|
| 160 | * try {
|
|---|
| 161 | * let results = await async.sortBy(['missingfile.txt','mediumfile.txt','smallfile.txt'], getFileSizeInBytes);
|
|---|
| 162 | * console.log(results);
|
|---|
| 163 | * }
|
|---|
| 164 | * catch (err) {
|
|---|
| 165 | * console.log(err);
|
|---|
| 166 | * // [ Error: ENOENT: no such file or directory ]
|
|---|
| 167 | * }
|
|---|
| 168 | * }
|
|---|
| 169 | *
|
|---|
| 170 | */
|
|---|
| 171 | function sortBy(coll, iteratee, callback) {
|
|---|
| 172 | var _iteratee = (0, _wrapAsync2.default)(iteratee);
|
|---|
| 173 | return (0, _map2.default)(coll, (x, iterCb) => {
|
|---|
| 174 | _iteratee(x, (err, criteria) => {
|
|---|
| 175 | if (err) return iterCb(err);
|
|---|
| 176 | iterCb(err, { value: x, criteria });
|
|---|
| 177 | });
|
|---|
| 178 | }, (err, results) => {
|
|---|
| 179 | if (err) return callback(err);
|
|---|
| 180 | callback(null, results.sort(comparator).map(v => v.value));
|
|---|
| 181 | });
|
|---|
| 182 |
|
|---|
| 183 | function comparator(left, right) {
|
|---|
| 184 | var a = left.criteria,
|
|---|
| 185 | b = right.criteria;
|
|---|
| 186 | return a < b ? -1 : a > b ? 1 : 0;
|
|---|
| 187 | }
|
|---|
| 188 | }
|
|---|
| 189 | exports.default = (0, _awaitify2.default)(sortBy, 3);
|
|---|
| 190 | module.exports = exports.default; |
|---|