eachWeekendOfInterval.js 1.3 KB

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