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.

35 lines
1.1 KiB

4 years ago
  1. import _curry3 from "./internal/_curry3.js";
  2. /**
  3. * Makes an ascending comparator function out of a function that returns a value
  4. * that can be compared with `<` and `>`.
  5. *
  6. * @func
  7. * @memberOf R
  8. * @since v0.23.0
  9. * @category Function
  10. * @sig Ord b => (a -> b) -> a -> a -> Number
  11. * @param {Function} fn A function of arity one that returns a value that can be compared
  12. * @param {*} a The first item to be compared.
  13. * @param {*} b The second item to be compared.
  14. * @return {Number} `-1` if fn(a) < fn(b), `1` if fn(b) < fn(a), otherwise `0`
  15. * @see R.descend
  16. * @example
  17. *
  18. * const byAge = R.ascend(R.prop('age'));
  19. * const people = [
  20. * { name: 'Emma', age: 70 },
  21. * { name: 'Peter', age: 78 },
  22. * { name: 'Mikhail', age: 62 },
  23. * ];
  24. * const peopleByYoungestFirst = R.sort(byAge, people);
  25. * //=> [{ name: 'Mikhail', age: 62 },{ name: 'Emma', age: 70 }, { name: 'Peter', age: 78 }]
  26. */
  27. var ascend =
  28. /*#__PURE__*/
  29. _curry3(function ascend(fn, a, b) {
  30. var aa = fn(a);
  31. var bb = fn(b);
  32. return aa < bb ? -1 : aa > bb ? 1 : 0;
  33. });
  34. export default ascend;