index.js 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. "use strict";
  2. var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault").default;
  3. Object.defineProperty(exports, "__esModule", {
  4. value: true
  5. });
  6. exports.default = clamp;
  7. var _index = _interopRequireDefault(require("../max/index.js"));
  8. var _index2 = _interopRequireDefault(require("../min/index.js"));
  9. var _index3 = _interopRequireDefault(require("../_lib/requiredArgs/index.js"));
  10. /**
  11. * @name clamp
  12. * @category Interval Helpers
  13. * @summary Return a date bounded by the start and the end of the given interval
  14. *
  15. * @description
  16. * Clamps a date to the lower bound with the start of the interval and the upper
  17. * bound with the end of the interval.
  18. *
  19. * - When the date is less than the start of the interval, the start is returned.
  20. * - When the date is greater than the end of the interval, the end is returned.
  21. * - Otherwise the date is returned.
  22. *
  23. * @example
  24. * // What is Mar, 21, 2021 bounded to an interval starting at Mar, 22, 2021 and ending at Apr, 01, 2021
  25. * const result = clamp(new Date(2021, 2, 21), {
  26. * start: new Date(2021, 2, 22),
  27. * end: new Date(2021, 3, 1),
  28. * })
  29. * //=> Mon Mar 22 2021 00:00:00
  30. *
  31. * @param {Date | Number} date - the date to be bounded
  32. * @param {Interval} interval - the interval to bound to
  33. * @returns {Date} the date bounded by the start and the end of the interval
  34. * @throws {TypeError} 2 arguments required
  35. */
  36. function clamp(date, _ref) {
  37. var start = _ref.start,
  38. end = _ref.end;
  39. (0, _index3.default)(2, arguments);
  40. return (0, _index2.default)([(0, _index.default)([date, start]), end]);
  41. }
  42. module.exports = exports.default;