eachWeekendOfInterval.mjs 1.3 KB

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