sortBy.js
 1  'use strict';
 2  
 3  Object.defineProperty(exports, "__esModule", {
 4      value: true
 5  });
 6  
 7  var _map = require('./map');
 8  
 9  var _map2 = _interopRequireDefault(_map);
10  
11  var _wrapAsync = require('./internal/wrapAsync');
12  
13  var _wrapAsync2 = _interopRequireDefault(_wrapAsync);
14  
15  var _awaitify = require('./internal/awaitify');
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   * async.sortBy(['file1','file2','file3'], function(file, callback) {
44   *     fs.stat(file, function(err, stats) {
45   *         callback(err, stats.mtime);
46   *     });
47   * }, function(err, results) {
48   *     // results is now the original array of files sorted by
49   *     // modified date
50   * });
51   *
52   * // By modifying the callback parameter the
53   * // sorting order can be influenced:
54   *
55   * // ascending order
56   * async.sortBy([1,9,3,5], function(x, callback) {
57   *     callback(null, x);
58   * }, function(err,result) {
59   *     // result callback
60   * });
61   *
62   * // descending order
63   * async.sortBy([1,9,3,5], function(x, callback) {
64   *     callback(null, x*-1);    //<- x*-1 instead of x, turns the order around
65   * }, function(err,result) {
66   *     // result callback
67   * });
68   */
69  function sortBy(coll, iteratee, callback) {
70      var _iteratee = (0, _wrapAsync2.default)(iteratee);
71      return (0, _map2.default)(coll, (x, iterCb) => {
72          _iteratee(x, (err, criteria) => {
73              if (err) return iterCb(err);
74              iterCb(err, { value: x, criteria });
75          });
76      }, (err, results) => {
77          if (err) return callback(err);
78          callback(null, results.sort(comparator).map(v => v.value));
79      });
80  
81      function comparator(left, right) {
82          var a = left.criteria,
83              b = right.criteria;
84          return a < b ? -1 : a > b ? 1 : 0;
85      }
86  }
87  exports.default = (0, _awaitify2.default)(sortBy, 3);
88  module.exports = exports['default'];