sortBy.js 2.7 KB

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