Software zum Installieren eines Smart-Mirror Frameworks , zum Nutzen von hochschulrelevanten Informationen, auf einem Raspberry-Pi.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

sortBy.js 2.7KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. 'use strict';
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. var _map = require('./map');
  6. var _map2 = _interopRequireDefault(_map);
  7. var _wrapAsync = require('./internal/wrapAsync');
  8. var _wrapAsync2 = _interopRequireDefault(_wrapAsync);
  9. var _awaitify = require('./internal/awaitify');
  10. var _awaitify2 = _interopRequireDefault(_awaitify);
  11. function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
  12. /**
  13. * Sorts a list by the results of running each `coll` value through an async
  14. * `iteratee`.
  15. *
  16. * @name sortBy
  17. * @static
  18. * @memberOf module:Collections
  19. * @method
  20. * @category Collection
  21. * @param {Array|Iterable|AsyncIterable|Object} coll - A collection to iterate over.
  22. * @param {AsyncFunction} iteratee - An async function to apply to each item in
  23. * `coll`.
  24. * The iteratee should complete with a value to use as the sort criteria as
  25. * its `result`.
  26. * 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. * @returns {Promise} a promise, if no callback passed
  32. * @example
  33. *
  34. * async.sortBy(['file1','file2','file3'], function(file, callback) {
  35. * fs.stat(file, function(err, stats) {
  36. * callback(err, stats.mtime);
  37. * });
  38. * }, function(err, results) {
  39. * // results is now the original array of files sorted by
  40. * // modified date
  41. * });
  42. *
  43. * // By modifying the callback parameter the
  44. * // sorting order can be influenced:
  45. *
  46. * // ascending order
  47. * async.sortBy([1,9,3,5], function(x, callback) {
  48. * callback(null, x);
  49. * }, function(err,result) {
  50. * // result callback
  51. * });
  52. *
  53. * // descending order
  54. * async.sortBy([1,9,3,5], function(x, callback) {
  55. * callback(null, x*-1); //<- x*-1 instead of x, turns the order around
  56. * }, function(err,result) {
  57. * // result callback
  58. * });
  59. */
  60. function sortBy(coll, iteratee, callback) {
  61. var _iteratee = (0, _wrapAsync2.default)(iteratee);
  62. return (0, _map2.default)(coll, (x, iterCb) => {
  63. _iteratee(x, (err, criteria) => {
  64. if (err) return iterCb(err);
  65. iterCb(err, { value: x, criteria });
  66. });
  67. }, (err, results) => {
  68. if (err) return callback(err);
  69. callback(null, results.sort(comparator).map(v => v.value));
  70. });
  71. function comparator(left, right) {
  72. var a = left.criteria,
  73. b = right.criteria;
  74. return a < b ? -1 : a > b ? 1 : 0;
  75. }
  76. }
  77. exports.default = (0, _awaitify2.default)(sortBy, 3);
  78. module.exports = exports['default'];