interval.cjs 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. "use strict";
  2. exports.interval = interval;
  3. var _index = require("./_lib/normalizeDates.cjs");
  4. /**
  5. * The {@link interval} function options.
  6. */
  7. /**
  8. * The {@link interval} function result type. It resolves the proper data type.
  9. * It uses the first argument date object type, starting from the start argument,
  10. * then the end interval date. If a context function is passed, it uses the context
  11. * function return type.
  12. */
  13. /**
  14. * @name interval
  15. * @category Interval Helpers
  16. * @summary Creates an interval object and validates its values.
  17. *
  18. * @description
  19. * Creates a normalized interval object and validates its values. If the interval is invalid, an exception is thrown.
  20. *
  21. * @typeParam StartDate - Start date type.
  22. * @typeParam EndDate - End date type.
  23. * @typeParam Options - Options type.
  24. *
  25. * @param start - The start of the interval.
  26. * @param end - The end of the interval.
  27. * @param options - The options object.
  28. *
  29. * @throws `Start date is invalid` when `start` is invalid.
  30. * @throws `End date is invalid` when `end` is invalid.
  31. * @throws `End date must be after start date` when end is before `start` and `options.assertPositive` is true.
  32. *
  33. * @returns The normalized and validated interval object.
  34. */
  35. function interval(start, end, options) {
  36. const [_start, _end] = (0, _index.normalizeDates)(options?.in, start, end);
  37. if (isNaN(+_start)) throw new TypeError("Start date is invalid");
  38. if (isNaN(+_end)) throw new TypeError("End date is invalid");
  39. if (options?.assertPositive && +_start > +_end)
  40. throw new TypeError("End date must be after start date");
  41. return { start: _start, end: _end };
  42. }