1 | import { normalizeDates } from "./_lib/normalizeDates.js";
|
2 |
|
3 | /**
|
4 | * The {@link interval} function options.
|
5 | */
|
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 | /**
|
15 | * @name interval
|
16 | * @category Interval Helpers
|
17 | * @summary Creates an interval object and validates its values.
|
18 | *
|
19 | * @description
|
20 | * Creates a normalized interval object and validates its values. If the interval is invalid, an exception is thrown.
|
21 | *
|
22 | * @typeParam StartDate - Start date type.
|
23 | * @typeParam EndDate - End date type.
|
24 | * @typeParam Options - Options type.
|
25 | *
|
26 | * @param start - The start of the interval.
|
27 | * @param end - The end of the interval.
|
28 | * @param options - The options object.
|
29 | *
|
30 | * @throws `Start date is invalid` when `start` is invalid.
|
31 | * @throws `End date is invalid` when `end` is invalid.
|
32 | * @throws `End date must be after start date` when end is before `start` and `options.assertPositive` is true.
|
33 | *
|
34 | * @returns The normalized and validated interval object.
|
35 | */
|
36 | export function interval(start, end, options) {
|
37 | const [_start, _end] = normalizeDates(options?.in, start, end);
|
38 |
|
39 | if (isNaN(+_start)) throw new TypeError("Start date is invalid");
|
40 | if (isNaN(+_end)) throw new TypeError("End date is invalid");
|
41 |
|
42 | if (options?.assertPositive && +_start > +_end)
|
43 | throw new TypeError("End date must be after start date");
|
44 |
|
45 | return { start: _start, end: _end };
|
46 | }
|
47 |
|
48 | // Fallback for modularized imports:
|
49 | export default interval;
|