index.js 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports.default = eachWeekOfInterval;
  6. var _index = _interopRequireDefault(require("../addWeeks/index.js"));
  7. var _index2 = _interopRequireDefault(require("../startOfWeek/index.js"));
  8. var _index3 = _interopRequireDefault(require("../toDate/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 eachWeekOfInterval
  13. * @category Interval Helpers
  14. * @summary Return the array of weeks within the specified time interval.
  15. *
  16. * @description
  17. * Return the array of weeks within the specified time interval.
  18. *
  19. * ### v2.0.0 breaking changes:
  20. *
  21. * - [Changes that are common for the whole library](https://github.com/date-fns/date-fns/blob/master/docs/upgradeGuide.md#Common-Changes).
  22. *
  23. * @param {Interval} interval - the interval. See [Interval]{@link https://date-fns.org/docs/Interval}
  24. * @param {Object} [options] - an object with options.
  25. * @param {Locale} [options.locale=defaultLocale] - the locale object. See [Locale]{@link https://date-fns.org/docs/Locale}
  26. * @param {0|1|2|3|4|5|6} [options.weekStartsOn=0] - the index of the first day of the week (0 - Sunday)
  27. * @returns {Date[]} the array with starts of weeks from the week of the interval start to the week of the interval end
  28. * @throws {TypeError} 1 argument required
  29. * @throws {RangeError} `options.weekStartsOn` must be 0, 1, ..., 6
  30. * @throws {RangeError} The start of an interval cannot be after its end
  31. * @throws {RangeError} Date in interval cannot be `Invalid Date`
  32. *
  33. * @example
  34. * // Each week within interval 6 October 2014 - 23 November 2014:
  35. * var result = eachWeekOfInterval({
  36. * start: new Date(2014, 9, 6),
  37. * end: new Date(2014, 10, 23)
  38. * })
  39. * //=> [
  40. * // Sun Oct 05 2014 00:00:00,
  41. * // Sun Oct 12 2014 00:00:00,
  42. * // Sun Oct 19 2014 00:00:00,
  43. * // Sun Oct 26 2014 00:00:00,
  44. * // Sun Nov 02 2014 00:00:00,
  45. * // Sun Nov 09 2014 00:00:00,
  46. * // Sun Nov 16 2014 00:00:00,
  47. * // Sun Nov 23 2014 00:00:00
  48. * // ]
  49. */
  50. function eachWeekOfInterval(dirtyInterval, options) {
  51. (0, _index4.default)(1, arguments);
  52. var interval = dirtyInterval || {};
  53. var startDate = (0, _index3.default)(interval.start);
  54. var endDate = (0, _index3.default)(interval.end);
  55. var endTime = endDate.getTime(); // Throw an exception if start date is after end date or if any date is `Invalid Date`
  56. if (!(startDate.getTime() <= endTime)) {
  57. throw new RangeError('Invalid interval');
  58. }
  59. var startDateWeek = (0, _index2.default)(startDate, options);
  60. var endDateWeek = (0, _index2.default)(endDate, options); // Some timezones switch DST at midnight, making start of day unreliable in these timezones, 3pm is a safe bet
  61. startDateWeek.setHours(15);
  62. endDateWeek.setHours(15);
  63. endTime = endDateWeek.getTime();
  64. var weeks = [];
  65. var currentWeek = startDateWeek;
  66. while (currentWeek.getTime() <= endTime) {
  67. currentWeek.setHours(0);
  68. weeks.push((0, _index3.default)(currentWeek));
  69. currentWeek = (0, _index.default)(currentWeek, 1);
  70. currentWeek.setHours(15);
  71. }
  72. return weeks;
  73. }
  74. module.exports = exports.default;