sortBy.js 2.8KB

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