1 | 'use strict';
|
---|
2 |
|
---|
3 | Object.defineProperty(exports, "__esModule", {
|
---|
4 | value: true
|
---|
5 | });
|
---|
6 | exports.default = sortBy;
|
---|
7 |
|
---|
8 | var _arrayMap = require('lodash/_arrayMap');
|
---|
9 |
|
---|
10 | var _arrayMap2 = _interopRequireDefault(_arrayMap);
|
---|
11 |
|
---|
12 | var _baseProperty = require('lodash/_baseProperty');
|
---|
13 |
|
---|
14 | var _baseProperty2 = _interopRequireDefault(_baseProperty);
|
---|
15 |
|
---|
16 | var _map = require('./map');
|
---|
17 |
|
---|
18 | var _map2 = _interopRequireDefault(_map);
|
---|
19 |
|
---|
20 | var _wrapAsync = require('./internal/wrapAsync');
|
---|
21 |
|
---|
22 | var _wrapAsync2 = _interopRequireDefault(_wrapAsync);
|
---|
23 |
|
---|
24 | function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
---|
25 |
|
---|
26 | /**
|
---|
27 | * Sorts a list by the results of running each `coll` value through an async
|
---|
28 | * `iteratee`.
|
---|
29 | *
|
---|
30 | * @name sortBy
|
---|
31 | * @static
|
---|
32 | * @memberOf module:Collections
|
---|
33 | * @method
|
---|
34 | * @category Collection
|
---|
35 | * @param {Array|Iterable|Object} coll - A collection to iterate over.
|
---|
36 | * @param {AsyncFunction} iteratee - An async function to apply to each item in
|
---|
37 | * `coll`.
|
---|
38 | * The iteratee should complete with a value to use as the sort criteria as
|
---|
39 | * its `result`.
|
---|
40 | * Invoked with (item, callback).
|
---|
41 | * @param {Function} callback - A callback which is called after all the
|
---|
42 | * `iteratee` functions have finished, or an error occurs. Results is the items
|
---|
43 | * from the original `coll` sorted by the values returned by the `iteratee`
|
---|
44 | * calls. Invoked with (err, results).
|
---|
45 | * @example
|
---|
46 | *
|
---|
47 | * async.sortBy(['file1','file2','file3'], function(file, callback) {
|
---|
48 | * fs.stat(file, function(err, stats) {
|
---|
49 | * callback(err, stats.mtime);
|
---|
50 | * });
|
---|
51 | * }, function(err, results) {
|
---|
52 | * // results is now the original array of files sorted by
|
---|
53 | * // modified date
|
---|
54 | * });
|
---|
55 | *
|
---|
56 | * // By modifying the callback parameter the
|
---|
57 | * // sorting order can be influenced:
|
---|
58 | *
|
---|
59 | * // ascending order
|
---|
60 | * async.sortBy([1,9,3,5], function(x, callback) {
|
---|
61 | * callback(null, x);
|
---|
62 | * }, function(err,result) {
|
---|
63 | * // result callback
|
---|
64 | * });
|
---|
65 | *
|
---|
66 | * // descending order
|
---|
67 | * async.sortBy([1,9,3,5], function(x, callback) {
|
---|
68 | * callback(null, x*-1); //<- x*-1 instead of x, turns the order around
|
---|
69 | * }, function(err,result) {
|
---|
70 | * // result callback
|
---|
71 | * });
|
---|
72 | */
|
---|
73 | function sortBy(coll, iteratee, callback) {
|
---|
74 | var _iteratee = (0, _wrapAsync2.default)(iteratee);
|
---|
75 | (0, _map2.default)(coll, function (x, callback) {
|
---|
76 | _iteratee(x, function (err, criteria) {
|
---|
77 | if (err) return callback(err);
|
---|
78 | callback(null, { value: x, criteria: criteria });
|
---|
79 | });
|
---|
80 | }, function (err, results) {
|
---|
81 | if (err) return callback(err);
|
---|
82 | callback(null, (0, _arrayMap2.default)(results.sort(comparator), (0, _baseProperty2.default)('value')));
|
---|
83 | });
|
---|
84 |
|
---|
85 | function comparator(left, right) {
|
---|
86 | var a = left.criteria,
|
---|
87 | b = right.criteria;
|
---|
88 | return a < b ? -1 : a > b ? 1 : 0;
|
---|
89 | }
|
---|
90 | }
|
---|
91 | module.exports = exports['default']; |
---|