clamp.js 942 B

123456789101112131415161718192021222324252627282930313233
  1. import _curry3 from "./internal/_curry3.js";
  2. /**
  3. * Restricts a number to be within a range.
  4. *
  5. * Also works for other ordered types such as Strings and Dates.
  6. *
  7. * @func
  8. * @memberOf R
  9. * @since v0.20.0
  10. * @category Relation
  11. * @sig Ord a => a -> a -> a -> a
  12. * @param {Number} minimum The lower limit of the clamp (inclusive)
  13. * @param {Number} maximum The upper limit of the clamp (inclusive)
  14. * @param {Number} value Value to be clamped
  15. * @return {Number} Returns `minimum` when `val < minimum`, `maximum` when `val > maximum`, returns `val` otherwise
  16. * @example
  17. *
  18. * R.clamp(1, 10, -5) // => 1
  19. * R.clamp(1, 10, 15) // => 10
  20. * R.clamp(1, 10, 4) // => 4
  21. */
  22. var clamp =
  23. /*#__PURE__*/
  24. _curry3(function clamp(min, max, value) {
  25. if (min > max) {
  26. throw new Error('min must not be greater than max in clamp(min, max, value)');
  27. }
  28. return value < min ? min : value > max ? max : value;
  29. });
  30. export default clamp;