comparator.js 1.1 KB

12345678910111213141516171819202122232425262728293031323334
  1. import _curry1 from "./internal/_curry1.js";
  2. /**
  3. * Makes a comparator function out of a function that reports whether the first
  4. * element is less than the second.
  5. *
  6. * @func
  7. * @memberOf R
  8. * @since v0.1.0
  9. * @category Function
  10. * @sig ((a, b) -> Boolean) -> ((a, b) -> Number)
  11. * @param {Function} pred A predicate function of arity two which will return `true` if the first argument
  12. * is less than the second, `false` otherwise
  13. * @return {Function} A Function :: a -> b -> Int that returns `-1` if a < b, `1` if b < a, otherwise `0`
  14. * @example
  15. *
  16. * const byAge = R.comparator((a, b) => a.age < b.age);
  17. * const people = [
  18. * { name: 'Emma', age: 70 },
  19. * { name: 'Peter', age: 78 },
  20. * { name: 'Mikhail', age: 62 },
  21. * ];
  22. * const peopleByIncreasingAge = R.sort(byAge, people);
  23. * //=> [{ name: 'Mikhail', age: 62 },{ name: 'Emma', age: 70 }, { name: 'Peter', age: 78 }]
  24. */
  25. var comparator =
  26. /*#__PURE__*/
  27. _curry1(function comparator(pred) {
  28. return function (a, b) {
  29. return pred(a, b) ? -1 : pred(b, a) ? 1 : 0;
  30. };
  31. });
  32. export default comparator;