index.js 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. "use strict";
  2. var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault").default;
  3. Object.defineProperty(exports, "__esModule", {
  4. value: true
  5. });
  6. exports.default = eachWeekendOfInterval;
  7. var _index = _interopRequireDefault(require("../eachDayOfInterval/index.js"));
  8. var _index2 = _interopRequireDefault(require("../isSunday/index.js"));
  9. var _index3 = _interopRequireDefault(require("../isWeekend/index.js"));
  10. var _index4 = _interopRequireDefault(require("../_lib/requiredArgs/index.js"));
  11. /**
  12. * @name eachWeekendOfInterval
  13. * @category Interval Helpers
  14. * @summary List all the Saturdays and Sundays in the given date interval.
  15. *
  16. * @description
  17. * Get all the Saturdays and Sundays in the given date interval.
  18. *
  19. * @param {Interval} interval - the given interval. See [Interval]{@link https://date-fns.org/docs/Interval}
  20. * @returns {Date[]} an array containing all the Saturdays and Sundays
  21. * @throws {TypeError} 1 argument required
  22. * @throws {RangeError} The start of an interval cannot be after its end
  23. * @throws {RangeError} Date in interval cannot be `Invalid Date`
  24. *
  25. * @example
  26. * // Lists all Saturdays and Sundays in the given date interval
  27. * const result = eachWeekendOfInterval({
  28. * start: new Date(2018, 8, 17),
  29. * end: new Date(2018, 8, 30)
  30. * })
  31. * //=> [
  32. * // Sat Sep 22 2018 00:00:00,
  33. * // Sun Sep 23 2018 00:00:00,
  34. * // Sat Sep 29 2018 00:00:00,
  35. * // Sun Sep 30 2018 00:00:00
  36. * // ]
  37. */
  38. function eachWeekendOfInterval(interval) {
  39. (0, _index4.default)(1, arguments);
  40. var dateInterval = (0, _index.default)(interval);
  41. var weekends = [];
  42. var index = 0;
  43. while (index < dateInterval.length) {
  44. var date = dateInterval[index++];
  45. if ((0, _index3.default)(date)) {
  46. weekends.push(date);
  47. if ((0, _index2.default)(date)) index = index + 5;
  48. }
  49. }
  50. return weekends;
  51. }
  52. module.exports = exports.default;