index.js 3.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. "use strict";
  2. var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault").default;
  3. Object.defineProperty(exports, "__esModule", {
  4. value: true
  5. });
  6. exports.default = lastDayOfWeek;
  7. var _index = _interopRequireDefault(require("../toDate/index.js"));
  8. var _index2 = _interopRequireDefault(require("../_lib/toInteger/index.js"));
  9. var _index3 = _interopRequireDefault(require("../_lib/requiredArgs/index.js"));
  10. var _index4 = require("../_lib/defaultOptions/index.js");
  11. /**
  12. * @name lastDayOfWeek
  13. * @category Week Helpers
  14. * @summary Return the last day of a week for the given date.
  15. *
  16. * @description
  17. * Return the last day of a week for the given date.
  18. * The result will be in the local timezone.
  19. *
  20. * @param {Date|Number} date - the original date
  21. * @param {Object} [options] - an object with options.
  22. * @param {Locale} [options.locale=defaultLocale] - the locale object. See [Locale]{@link https://date-fns.org/docs/Locale}
  23. * @param {0|1|2|3|4|5|6} [options.weekStartsOn=0] - the index of the first day of the week (0 - Sunday)
  24. * @returns {Date} the last day of a week
  25. * @throws {TypeError} 1 argument required
  26. * @throws {RangeError} `options.weekStartsOn` must be between 0 and 6
  27. *
  28. * @example
  29. * // The last day of a week for 2 September 2014 11:55:00:
  30. * const result = lastDayOfWeek(new Date(2014, 8, 2, 11, 55, 0))
  31. * //=> Sat Sep 06 2014 00:00:00
  32. *
  33. * @example
  34. * // If the week starts on Monday, the last day of the week for 2 September 2014 11:55:00:
  35. * const result = lastDayOfWeek(new Date(2014, 8, 2, 11, 55, 0), { weekStartsOn: 1 })
  36. * //=> Sun Sep 07 2014 00:00:00
  37. */
  38. function lastDayOfWeek(dirtyDate, options) {
  39. var _ref, _ref2, _ref3, _options$weekStartsOn, _options$locale, _options$locale$optio, _defaultOptions$local, _defaultOptions$local2;
  40. (0, _index3.default)(1, arguments);
  41. var defaultOptions = (0, _index4.getDefaultOptions)();
  42. var weekStartsOn = (0, _index2.default)((_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);
  43. // Test if weekStartsOn is between 0 and 6 _and_ is not NaN
  44. if (!(weekStartsOn >= 0 && weekStartsOn <= 6)) {
  45. throw new RangeError('weekStartsOn must be between 0 and 6');
  46. }
  47. var date = (0, _index.default)(dirtyDate);
  48. var day = date.getDay();
  49. var diff = (day < weekStartsOn ? -7 : 0) + 6 - (day - weekStartsOn);
  50. date.setHours(0, 0, 0, 0);
  51. date.setDate(date.getDate() + diff);
  52. return date;
  53. }
  54. module.exports = exports.default;