index.js 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports.default = getUTCWeekYear;
  6. var _index = _interopRequireDefault(require("../toInteger/index.js"));
  7. var _index2 = _interopRequireDefault(require("../../toDate/index.js"));
  8. var _index3 = _interopRequireDefault(require("../startOfUTCWeek/index.js"));
  9. var _index4 = _interopRequireDefault(require("../requiredArgs/index.js"));
  10. function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
  11. // This function will be a part of public API when UTC function will be implemented.
  12. // See issue: https://github.com/date-fns/date-fns/issues/376
  13. function getUTCWeekYear(dirtyDate, dirtyOptions) {
  14. (0, _index4.default)(1, arguments);
  15. var date = (0, _index2.default)(dirtyDate, dirtyOptions);
  16. var year = date.getUTCFullYear();
  17. var options = dirtyOptions || {};
  18. var locale = options.locale;
  19. var localeFirstWeekContainsDate = locale && locale.options && locale.options.firstWeekContainsDate;
  20. var defaultFirstWeekContainsDate = localeFirstWeekContainsDate == null ? 1 : (0, _index.default)(localeFirstWeekContainsDate);
  21. var firstWeekContainsDate = options.firstWeekContainsDate == null ? defaultFirstWeekContainsDate : (0, _index.default)(options.firstWeekContainsDate); // Test if weekStartsOn is between 1 and 7 _and_ is not NaN
  22. if (!(firstWeekContainsDate >= 1 && firstWeekContainsDate <= 7)) {
  23. throw new RangeError('firstWeekContainsDate must be between 1 and 7 inclusively');
  24. }
  25. var firstWeekOfNextYear = new Date(0);
  26. firstWeekOfNextYear.setUTCFullYear(year + 1, 0, firstWeekContainsDate);
  27. firstWeekOfNextYear.setUTCHours(0, 0, 0, 0);
  28. var startOfNextYear = (0, _index3.default)(firstWeekOfNextYear, dirtyOptions);
  29. var firstWeekOfThisYear = new Date(0);
  30. firstWeekOfThisYear.setUTCFullYear(year, 0, firstWeekContainsDate);
  31. firstWeekOfThisYear.setUTCHours(0, 0, 0, 0);
  32. var startOfThisYear = (0, _index3.default)(firstWeekOfThisYear, dirtyOptions);
  33. if (date.getTime() >= startOfNextYear.getTime()) {
  34. return year + 1;
  35. } else if (date.getTime() >= startOfThisYear.getTime()) {
  36. return year;
  37. } else {
  38. return year - 1;
  39. }
  40. }
  41. module.exports = exports.default;