1 | import { normalizeInterval } from "./_lib/normalizeInterval.js";
|
2 | import { addQuarters } from "./addQuarters.js";
|
3 | import { constructFrom } from "./constructFrom.js";
|
4 | import { startOfQuarter } from "./startOfQuarter.js";
|
5 |
|
6 | /**
|
7 | * The {@link eachQuarterOfInterval} function options.
|
8 | */
|
9 |
|
10 | /**
|
11 | * The {@link eachQuarterOfInterval} function result type. It resolves the proper data type.
|
12 | * It uses the first argument date object type, starting from the date argument,
|
13 | * then the start interval date, and finally the end interval date. If
|
14 | * a context function is passed, it uses the context function return type.
|
15 | */
|
16 |
|
17 | /**
|
18 | * @name eachQuarterOfInterval
|
19 | * @category Interval Helpers
|
20 | * @summary Return the array of quarters within the specified time interval.
|
21 | *
|
22 | * @description
|
23 | * Return the array of quarters within the specified time interval.
|
24 | *
|
25 | * @typeParam IntervalType - Interval type.
|
26 | * @typeParam Options - Options type.
|
27 | *
|
28 | * @param interval - The interval
|
29 | * @param options - An object with options
|
30 | *
|
31 | * @returns The array with starts of quarters from the quarter of the interval start to the quarter of the interval end
|
32 | *
|
33 | * @example
|
34 | * // Each quarter within interval 6 February 2014 - 10 August 2014:
|
35 | * const result = eachQuarterOfInterval({
|
36 | * start: new Date(2014, 1, 6),
|
37 | * end: new Date(2014, 7, 10),
|
38 | * })
|
39 | * //=> [
|
40 | * // Wed Jan 01 2014 00:00:00,
|
41 | * // Tue Apr 01 2014 00:00:00,
|
42 | * // Tue Jul 01 2014 00:00:00,
|
43 | * // ]
|
44 | */
|
45 | export function eachQuarterOfInterval(interval, options) {
|
46 | const { start, end } = normalizeInterval(options?.in, interval);
|
47 |
|
48 | let reversed = +start > +end;
|
49 | const endTime = reversed ? +startOfQuarter(start) : +startOfQuarter(end);
|
50 | let date = reversed ? startOfQuarter(end) : startOfQuarter(start);
|
51 |
|
52 | let step = options?.step ?? 1;
|
53 | if (!step) return [];
|
54 | if (step < 0) {
|
55 | step = -step;
|
56 | reversed = !reversed;
|
57 | }
|
58 |
|
59 | const dates = [];
|
60 |
|
61 | while (+date <= endTime) {
|
62 | dates.push(constructFrom(start, date));
|
63 | date = addQuarters(date, step);
|
64 | }
|
65 |
|
66 | return reversed ? dates.reverse() : dates;
|
67 | }
|
68 |
|
69 | // Fallback for modularized imports:
|
70 | export default eachQuarterOfInterval;
|