UNPKG

1.34 kBJavaScriptView Raw
1import { eachDayOfInterval } from "./eachDayOfInterval.mjs";
2import { isWeekend } from "./isWeekend.mjs";
3
4/**
5 * @name eachWeekendOfInterval
6 * @category Interval Helpers
7 * @summary List all the Saturdays and Sundays in the given date interval.
8 *
9 * @description
10 * Get all the Saturdays and Sundays in the given date interval.
11 *
12 * @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).
13 *
14 * @param interval - The given interval
15 *
16 * @returns An array containing all the Saturdays and Sundays
17 *
18 * @example
19 * // Lists all Saturdays and Sundays in the given date interval
20 * const result = eachWeekendOfInterval({
21 * start: new Date(2018, 8, 17),
22 * end: new Date(2018, 8, 30)
23 * })
24 * //=> [
25 * // Sat Sep 22 2018 00:00:00,
26 * // Sun Sep 23 2018 00:00:00,
27 * // Sat Sep 29 2018 00:00:00,
28 * // Sun Sep 30 2018 00:00:00
29 * // ]
30 */
31export function eachWeekendOfInterval(interval) {
32 const dateInterval = eachDayOfInterval(interval);
33 const weekends = [];
34 let index = 0;
35 while (index < dateInterval.length) {
36 const date = dateInterval[index++];
37 if (isWeekend(date)) weekends.push(date);
38 }
39 return weekends;
40}
41
42// Fallback for modularized imports:
43export default eachWeekendOfInterval;