index.js 4.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. "use strict";
  2. var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault").default;
  3. Object.defineProperty(exports, "__esModule", {
  4. value: true
  5. });
  6. exports.default = setWeekYear;
  7. var _index = _interopRequireDefault(require("../differenceInCalendarDays/index.js"));
  8. var _index2 = _interopRequireDefault(require("../startOfWeekYear/index.js"));
  9. var _index3 = _interopRequireDefault(require("../toDate/index.js"));
  10. var _index4 = _interopRequireDefault(require("../_lib/toInteger/index.js"));
  11. var _index5 = _interopRequireDefault(require("../_lib/requiredArgs/index.js"));
  12. var _index6 = require("../_lib/defaultOptions/index.js");
  13. /**
  14. * @name setWeekYear
  15. * @category Week-Numbering Year Helpers
  16. * @summary Set the local week-numbering year to the given date.
  17. *
  18. * @description
  19. * Set the local week-numbering year to the given date,
  20. * saving the week number and the weekday number.
  21. * The exact calculation depends on the values of
  22. * `options.weekStartsOn` (which is the index of the first day of the week)
  23. * and `options.firstWeekContainsDate` (which is the day of January, which is always in
  24. * the first week of the week-numbering year)
  25. *
  26. * Week numbering: https://en.wikipedia.org/wiki/Week#Week_numbering
  27. *
  28. * @param {Date|Number} date - the date to be changed
  29. * @param {Number} weekYear - the local week-numbering year of the new date
  30. * @param {Object} [options] - an object with options.
  31. * @param {Locale} [options.locale=defaultLocale] - the locale object. See [Locale]{@link https://date-fns.org/docs/Locale}
  32. * @param {0|1|2|3|4|5|6} [options.weekStartsOn=0] - the index of the first day of the week (0 - Sunday)
  33. * @param {1|2|3|4|5|6|7} [options.firstWeekContainsDate=1] - the day of January, which is always in the first week of the year
  34. * @returns {Date} the new date with the local week-numbering year set
  35. * @throws {TypeError} 2 arguments required
  36. * @throws {RangeError} `options.weekStartsOn` must be between 0 and 6
  37. * @throws {RangeError} `options.firstWeekContainsDate` must be between 1 and 7
  38. *
  39. * @example
  40. * // Set the local week-numbering year 2004 to 2 January 2010 with default options:
  41. * const result = setWeekYear(new Date(2010, 0, 2), 2004)
  42. * //=> Sat Jan 03 2004 00:00:00
  43. *
  44. * @example
  45. * // Set the local week-numbering year 2004 to 2 January 2010,
  46. * // if Monday is the first day of week
  47. * // and 4 January is always in the first week of the year:
  48. * const result = setWeekYear(new Date(2010, 0, 2), 2004, {
  49. * weekStartsOn: 1,
  50. * firstWeekContainsDate: 4
  51. * })
  52. * //=> Sat Jan 01 2005 00:00:00
  53. */
  54. function setWeekYear(dirtyDate, dirtyWeekYear, options) {
  55. var _ref, _ref2, _ref3, _options$firstWeekCon, _options$locale, _options$locale$optio, _defaultOptions$local, _defaultOptions$local2;
  56. (0, _index5.default)(2, arguments);
  57. var defaultOptions = (0, _index6.getDefaultOptions)();
  58. var firstWeekContainsDate = (0, _index4.default)((_ref = (_ref2 = (_ref3 = (_options$firstWeekCon = options === null || options === void 0 ? void 0 : options.firstWeekContainsDate) !== null && _options$firstWeekCon !== void 0 ? _options$firstWeekCon : 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.firstWeekContainsDate) !== null && _ref3 !== void 0 ? _ref3 : defaultOptions.firstWeekContainsDate) !== 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.firstWeekContainsDate) !== null && _ref !== void 0 ? _ref : 1);
  59. var date = (0, _index3.default)(dirtyDate);
  60. var weekYear = (0, _index4.default)(dirtyWeekYear);
  61. var diff = (0, _index.default)(date, (0, _index2.default)(date, options));
  62. var firstWeek = new Date(0);
  63. firstWeek.setFullYear(weekYear, 0, firstWeekContainsDate);
  64. firstWeek.setHours(0, 0, 0, 0);
  65. date = (0, _index2.default)(firstWeek, options);
  66. date.setDate(date.getDate() + diff);
  67. return date;
  68. }
  69. module.exports = exports.default;