1 | import { toDate } from "./toDate.mjs";
|
2 |
|
3 | /**
|
4 | * The {@link interval} function options.
|
5 | */
|
6 |
|
7 | /**
|
8 | * @name interval
|
9 | * @category Interval Helpers
|
10 | * @summary Creates an interval object and validates its values.
|
11 | *
|
12 | * @description
|
13 | * Creates a normalized interval object and validates its values. If the interval is invalid, an exception is thrown.
|
14 | *
|
15 | * @typeParam DateType - The `Date` type, the function operates on. Gets inferred from passed arguments. Allows to use extensions like [`UTCDate`](https://github.com/date-fns/utc).
|
16 | *
|
17 | * @param start - The start of the interval.
|
18 | * @param end - The end of the interval.
|
19 | * @param options - The options object.
|
20 | *
|
21 | * @throws `Start date is invalid` when `start` is invalid.
|
22 | * @throws `End date is invalid` when `end` is invalid.
|
23 | * @throws `End date must be after start date` when end is before `start` and `options.assertPositive` is true.
|
24 | *
|
25 | * @returns The normalized and validated interval object.
|
26 | */
|
27 | export function interval(start, end, options) {
|
28 | const _start = toDate(start);
|
29 | if (isNaN(+_start)) throw new TypeError("Start date is invalid");
|
30 |
|
31 | const _end = toDate(end);
|
32 | if (isNaN(+_end)) throw new TypeError("End date is invalid");
|
33 |
|
34 | if (options?.assertPositive && +_start > +_end)
|
35 | throw new TypeError("End date must be after start date");
|
36 |
|
37 | return { start: _start, end: _end };
|
38 | }
|
39 |
|
40 | // Fallback for modularized imports:
|
41 | export default interval;
|