index.js 4.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. "use strict";
  2. var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault").default;
  3. Object.defineProperty(exports, "__esModule", {
  4. value: true
  5. });
  6. exports.default = getWeekYear;
  7. var _index = _interopRequireDefault(require("../startOfWeek/index.js"));
  8. var _index2 = _interopRequireDefault(require("../toDate/index.js"));
  9. var _index3 = _interopRequireDefault(require("../_lib/toInteger/index.js"));
  10. var _index4 = _interopRequireDefault(require("../_lib/requiredArgs/index.js"));
  11. var _index5 = require("../_lib/defaultOptions/index.js");
  12. /**
  13. * @name getWeekYear
  14. * @category Week-Numbering Year Helpers
  15. * @summary Get the local week-numbering year of the given date.
  16. *
  17. * @description
  18. * Get the local week-numbering year of the given date.
  19. * The exact calculation depends on the values of
  20. * `options.weekStartsOn` (which is the index of the first day of the week)
  21. * and `options.firstWeekContainsDate` (which is the day of January, which is always in
  22. * the first week of the week-numbering year)
  23. *
  24. * Week numbering: https://en.wikipedia.org/wiki/Week#Week_numbering
  25. *
  26. * @param {Date|Number} date - the given date
  27. * @param {Object} [options] - an object with options.
  28. * @param {Locale} [options.locale=defaultLocale] - the locale object. See [Locale]{@link https://date-fns.org/docs/Locale}
  29. * @param {0|1|2|3|4|5|6} [options.weekStartsOn=0] - the index of the first day of the week (0 - Sunday)
  30. * @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
  31. * @returns {Number} the local week-numbering year
  32. * @throws {TypeError} 1 argument required
  33. * @throws {RangeError} `options.weekStartsOn` must be between 0 and 6
  34. * @throws {RangeError} `options.firstWeekContainsDate` must be between 1 and 7
  35. *
  36. * @example
  37. * // Which week numbering year is 26 December 2004 with the default settings?
  38. * const result = getWeekYear(new Date(2004, 11, 26))
  39. * //=> 2005
  40. *
  41. * @example
  42. * // Which week numbering year is 26 December 2004 if week starts on Saturday?
  43. * const result = getWeekYear(new Date(2004, 11, 26), { weekStartsOn: 6 })
  44. * //=> 2004
  45. *
  46. * @example
  47. * // Which week numbering year is 26 December 2004 if the first week contains 4 January?
  48. * const result = getWeekYear(new Date(2004, 11, 26), { firstWeekContainsDate: 4 })
  49. * //=> 2004
  50. */
  51. function getWeekYear(dirtyDate, options) {
  52. var _ref, _ref2, _ref3, _options$firstWeekCon, _options$locale, _options$locale$optio, _defaultOptions$local, _defaultOptions$local2;
  53. (0, _index4.default)(1, arguments);
  54. var date = (0, _index2.default)(dirtyDate);
  55. var year = date.getFullYear();
  56. var defaultOptions = (0, _index5.getDefaultOptions)();
  57. var firstWeekContainsDate = (0, _index3.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);
  58. // Test if weekStartsOn is between 1 and 7 _and_ is not NaN
  59. if (!(firstWeekContainsDate >= 1 && firstWeekContainsDate <= 7)) {
  60. throw new RangeError('firstWeekContainsDate must be between 1 and 7 inclusively');
  61. }
  62. var firstWeekOfNextYear = new Date(0);
  63. firstWeekOfNextYear.setFullYear(year + 1, 0, firstWeekContainsDate);
  64. firstWeekOfNextYear.setHours(0, 0, 0, 0);
  65. var startOfNextYear = (0, _index.default)(firstWeekOfNextYear, options);
  66. var firstWeekOfThisYear = new Date(0);
  67. firstWeekOfThisYear.setFullYear(year, 0, firstWeekContainsDate);
  68. firstWeekOfThisYear.setHours(0, 0, 0, 0);
  69. var startOfThisYear = (0, _index.default)(firstWeekOfThisYear, options);
  70. if (date.getTime() >= startOfNextYear.getTime()) {
  71. return year + 1;
  72. } else if (date.getTime() >= startOfThisYear.getTime()) {
  73. return year;
  74. } else {
  75. return year - 1;
  76. }
  77. }
  78. module.exports = exports.default;