index.js 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. import { getDefaultOptions } from "../_lib/defaultOptions/index.js";
  2. import toDate from "../toDate/index.js";
  3. import toInteger from "../_lib/toInteger/index.js";
  4. import requiredArgs from "../_lib/requiredArgs/index.js";
  5. /**
  6. * @name endOfWeek
  7. * @category Week Helpers
  8. * @summary Return the end of a week for the given date.
  9. *
  10. * @description
  11. * Return the end of a week for the given date.
  12. * The result will be in the local timezone.
  13. *
  14. * @param {Date|Number} date - the original date
  15. * @param {Object} [options] - an object with options.
  16. * @param {Locale} [options.locale=defaultLocale] - the locale object. See [Locale]{@link https://date-fns.org/docs/Locale}
  17. * @param {0|1|2|3|4|5|6} [options.weekStartsOn=0] - the index of the first day of the week (0 - Sunday)
  18. * @returns {Date} the end of a week
  19. * @throws {TypeError} 1 argument required
  20. * @throws {RangeError} `options.weekStartsOn` must be between 0 and 6
  21. *
  22. * @example
  23. * // The end of a week for 2 September 2014 11:55:00:
  24. * const result = endOfWeek(new Date(2014, 8, 2, 11, 55, 0))
  25. * //=> Sat Sep 06 2014 23:59:59.999
  26. *
  27. * @example
  28. * // If the week starts on Monday, the end of the week for 2 September 2014 11:55:00:
  29. * const result = endOfWeek(new Date(2014, 8, 2, 11, 55, 0), { weekStartsOn: 1 })
  30. * //=> Sun Sep 07 2014 23:59:59.999
  31. */
  32. export default function endOfWeek(dirtyDate, options) {
  33. var _ref, _ref2, _ref3, _options$weekStartsOn, _options$locale, _options$locale$optio, _defaultOptions$local, _defaultOptions$local2;
  34. requiredArgs(1, arguments);
  35. var defaultOptions = getDefaultOptions();
  36. var weekStartsOn = toInteger((_ref = (_ref2 = (_ref3 = (_options$weekStartsOn = options === null || options === void 0 ? void 0 : options.weekStartsOn) !== null && _options$weekStartsOn !== void 0 ? _options$weekStartsOn : options === null || options === void 0 ? void 0 : (_options$locale = options.locale) === null || _options$locale === void 0 ? void 0 : (_options$locale$optio = _options$locale.options) === null || _options$locale$optio === void 0 ? void 0 : _options$locale$optio.weekStartsOn) !== null && _ref3 !== void 0 ? _ref3 : defaultOptions.weekStartsOn) !== null && _ref2 !== void 0 ? _ref2 : (_defaultOptions$local = defaultOptions.locale) === null || _defaultOptions$local === void 0 ? void 0 : (_defaultOptions$local2 = _defaultOptions$local.options) === null || _defaultOptions$local2 === void 0 ? void 0 : _defaultOptions$local2.weekStartsOn) !== null && _ref !== void 0 ? _ref : 0);
  37. // Test if weekStartsOn is between 0 and 6 _and_ is not NaN
  38. if (!(weekStartsOn >= 0 && weekStartsOn <= 6)) {
  39. throw new RangeError('weekStartsOn must be between 0 and 6 inclusively');
  40. }
  41. var date = toDate(dirtyDate);
  42. var day = date.getDay();
  43. var diff = (day < weekStartsOn ? -7 : 0) + 6 - (day - weekStartsOn);
  44. date.setDate(date.getDate() + diff);
  45. date.setHours(23, 59, 59, 999);
  46. return date;
  47. }