LocalWeekYearParser.js 3.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. "use strict";
  2. var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault").default;
  3. Object.defineProperty(exports, "__esModule", {
  4. value: true
  5. });
  6. exports.LocalWeekYearParser = void 0;
  7. var _classCallCheck2 = _interopRequireDefault(require("@babel/runtime/helpers/classCallCheck"));
  8. var _createClass2 = _interopRequireDefault(require("@babel/runtime/helpers/createClass"));
  9. var _assertThisInitialized2 = _interopRequireDefault(require("@babel/runtime/helpers/assertThisInitialized"));
  10. var _inherits2 = _interopRequireDefault(require("@babel/runtime/helpers/inherits"));
  11. var _createSuper2 = _interopRequireDefault(require("@babel/runtime/helpers/createSuper"));
  12. var _defineProperty2 = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty"));
  13. var _Parser2 = require("../Parser.js");
  14. var _utils = require("../utils.js");
  15. var _index = _interopRequireDefault(require("../../../_lib/getUTCWeekYear/index.js"));
  16. var _index2 = _interopRequireDefault(require("../../../_lib/startOfUTCWeek/index.js"));
  17. // Local week-numbering year
  18. var LocalWeekYearParser = /*#__PURE__*/function (_Parser) {
  19. (0, _inherits2.default)(LocalWeekYearParser, _Parser);
  20. var _super = (0, _createSuper2.default)(LocalWeekYearParser);
  21. function LocalWeekYearParser() {
  22. var _this;
  23. (0, _classCallCheck2.default)(this, LocalWeekYearParser);
  24. for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) {
  25. args[_key] = arguments[_key];
  26. }
  27. _this = _super.call.apply(_super, [this].concat(args));
  28. (0, _defineProperty2.default)((0, _assertThisInitialized2.default)(_this), "priority", 130);
  29. (0, _defineProperty2.default)((0, _assertThisInitialized2.default)(_this), "incompatibleTokens", ['y', 'R', 'u', 'Q', 'q', 'M', 'L', 'I', 'd', 'D', 'i', 't', 'T']);
  30. return _this;
  31. }
  32. (0, _createClass2.default)(LocalWeekYearParser, [{
  33. key: "parse",
  34. value: function parse(dateString, token, match) {
  35. var valueCallback = function valueCallback(year) {
  36. return {
  37. year: year,
  38. isTwoDigitYear: token === 'YY'
  39. };
  40. };
  41. switch (token) {
  42. case 'Y':
  43. return (0, _utils.mapValue)((0, _utils.parseNDigits)(4, dateString), valueCallback);
  44. case 'Yo':
  45. return (0, _utils.mapValue)(match.ordinalNumber(dateString, {
  46. unit: 'year'
  47. }), valueCallback);
  48. default:
  49. return (0, _utils.mapValue)((0, _utils.parseNDigits)(token.length, dateString), valueCallback);
  50. }
  51. }
  52. }, {
  53. key: "validate",
  54. value: function validate(_date, value) {
  55. return value.isTwoDigitYear || value.year > 0;
  56. }
  57. }, {
  58. key: "set",
  59. value: function set(date, flags, value, options) {
  60. var currentYear = (0, _index.default)(date, options);
  61. if (value.isTwoDigitYear) {
  62. var normalizedTwoDigitYear = (0, _utils.normalizeTwoDigitYear)(value.year, currentYear);
  63. date.setUTCFullYear(normalizedTwoDigitYear, 0, options.firstWeekContainsDate);
  64. date.setUTCHours(0, 0, 0, 0);
  65. return (0, _index2.default)(date, options);
  66. }
  67. var year = !('era' in flags) || flags.era === 1 ? value.year : 1 - value.year;
  68. date.setUTCFullYear(year, 0, options.firstWeekContainsDate);
  69. date.setUTCHours(0, 0, 0, 0);
  70. return (0, _index2.default)(date, options);
  71. }
  72. }]);
  73. return LocalWeekYearParser;
  74. }(_Parser2.Parser);
  75. exports.LocalWeekYearParser = LocalWeekYearParser;