index.js 3.6 KB

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