index.js 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. import eachWeekendOfInterval from "../eachWeekendOfInterval/index.js";
  2. import startOfMonth from "../startOfMonth/index.js";
  3. import endOfMonth from "../endOfMonth/index.js";
  4. import requiredArgs from "../_lib/requiredArgs/index.js";
  5. /**
  6. * @name eachWeekendOfMonth
  7. * @category Month Helpers
  8. * @summary List all the Saturdays and Sundays in the given month.
  9. *
  10. * @description
  11. * Get all the Saturdays and Sundays in the given month.
  12. *
  13. * @param {Date|Number} date - the given month
  14. * @returns {Date[]} an array containing all the Saturdays and Sundays
  15. * @throws {TypeError} 1 argument required
  16. * @throws {RangeError} The passed date is invalid
  17. *
  18. * @example
  19. * // Lists all Saturdays and Sundays in the given month
  20. * const result = eachWeekendOfMonth(new Date(2022, 1, 1))
  21. * //=> [
  22. * // Sat Feb 05 2022 00:00:00,
  23. * // Sun Feb 06 2022 00:00:00,
  24. * // Sat Feb 12 2022 00:00:00,
  25. * // Sun Feb 13 2022 00:00:00,
  26. * // Sat Feb 19 2022 00:00:00,
  27. * // Sun Feb 20 2022 00:00:00,
  28. * // Sat Feb 26 2022 00:00:00,
  29. * // Sun Feb 27 2022 00:00:00
  30. * // ]
  31. */
  32. export default function eachWeekendOfMonth(dirtyDate) {
  33. requiredArgs(1, arguments);
  34. var startDate = startOfMonth(dirtyDate);
  35. if (isNaN(startDate.getTime())) throw new RangeError('The passed date is invalid');
  36. var endDate = endOfMonth(dirtyDate);
  37. return eachWeekendOfInterval({
  38. start: startDate,
  39. end: endDate
  40. });
  41. }