eachWeekendOfInterval.cjs 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. "use strict";
  2. exports.eachWeekendOfInterval = eachWeekendOfInterval;
  3. var _index = require("./_lib/normalizeInterval.cjs");
  4. var _index2 = require("./constructFrom.cjs");
  5. var _index3 = require("./eachDayOfInterval.cjs");
  6. var _index4 = require("./isWeekend.cjs");
  7. /**
  8. * The {@link eachWeekendOfInterval} function options.
  9. */
  10. /**
  11. * The {@link eachWeekendOfInterval} function result type.
  12. */
  13. /**
  14. * @name eachWeekendOfInterval
  15. * @category Interval Helpers
  16. * @summary List all the Saturdays and Sundays in the given date interval.
  17. *
  18. * @description
  19. * Get all the Saturdays and Sundays in the given date interval.
  20. *
  21. * @typeParam IntervalType - Interval type.
  22. * @typeParam Options - Options type.
  23. *
  24. * @param interval - The given interval
  25. * @param options - An object with options
  26. *
  27. * @returns An array containing all the Saturdays and Sundays
  28. *
  29. * @example
  30. * // Lists all Saturdays and Sundays in the given date interval
  31. * const result = eachWeekendOfInterval({
  32. * start: new Date(2018, 8, 17),
  33. * end: new Date(2018, 8, 30)
  34. * })
  35. * //=> [
  36. * // Sat Sep 22 2018 00:00:00,
  37. * // Sun Sep 23 2018 00:00:00,
  38. * // Sat Sep 29 2018 00:00:00,
  39. * // Sun Sep 30 2018 00:00:00
  40. * // ]
  41. */
  42. function eachWeekendOfInterval(interval, options) {
  43. const { start, end } = (0, _index.normalizeInterval)(options?.in, interval);
  44. const dateInterval = (0, _index3.eachDayOfInterval)({ start, end }, options);
  45. const weekends = [];
  46. let index = 0;
  47. while (index < dateInterval.length) {
  48. const date = dateInterval[index++];
  49. if ((0, _index4.isWeekend)(date))
  50. weekends.push((0, _index2.constructFrom)(start, date));
  51. }
  52. return weekends;
  53. }