index.js 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports.default = eachWeekendOfInterval;
  6. var _index = _interopRequireDefault(require("../eachDayOfInterval/index.js"));
  7. var _index2 = _interopRequireDefault(require("../isSunday/index.js"));
  8. var _index3 = _interopRequireDefault(require("../isWeekend/index.js"));
  9. var _index4 = _interopRequireDefault(require("../_lib/requiredArgs/index.js"));
  10. function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
  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;