MonthParser.js 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. import _classCallCheck from "@babel/runtime/helpers/esm/classCallCheck";
  2. import _createClass from "@babel/runtime/helpers/esm/createClass";
  3. import _assertThisInitialized from "@babel/runtime/helpers/esm/assertThisInitialized";
  4. import _inherits from "@babel/runtime/helpers/esm/inherits";
  5. import _createSuper from "@babel/runtime/helpers/esm/createSuper";
  6. import _defineProperty from "@babel/runtime/helpers/esm/defineProperty";
  7. import { mapValue, parseNDigits, parseNumericPattern } from "../utils.js";
  8. import { Parser } from "../Parser.js";
  9. import { numericPatterns } from "../constants.js";
  10. export var MonthParser = /*#__PURE__*/function (_Parser) {
  11. _inherits(MonthParser, _Parser);
  12. var _super = _createSuper(MonthParser);
  13. function MonthParser() {
  14. var _this;
  15. _classCallCheck(this, MonthParser);
  16. for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) {
  17. args[_key] = arguments[_key];
  18. }
  19. _this = _super.call.apply(_super, [this].concat(args));
  20. _defineProperty(_assertThisInitialized(_this), "incompatibleTokens", ['Y', 'R', 'q', 'Q', 'L', 'w', 'I', 'D', 'i', 'e', 'c', 't', 'T']);
  21. _defineProperty(_assertThisInitialized(_this), "priority", 110);
  22. return _this;
  23. }
  24. _createClass(MonthParser, [{
  25. key: "parse",
  26. value: function parse(dateString, token, match) {
  27. var valueCallback = function valueCallback(value) {
  28. return value - 1;
  29. };
  30. switch (token) {
  31. // 1, 2, ..., 12
  32. case 'M':
  33. return mapValue(parseNumericPattern(numericPatterns.month, dateString), valueCallback);
  34. // 01, 02, ..., 12
  35. case 'MM':
  36. return mapValue(parseNDigits(2, dateString), valueCallback);
  37. // 1st, 2nd, ..., 12th
  38. case 'Mo':
  39. return mapValue(match.ordinalNumber(dateString, {
  40. unit: 'month'
  41. }), valueCallback);
  42. // Jan, Feb, ..., Dec
  43. case 'MMM':
  44. return match.month(dateString, {
  45. width: 'abbreviated',
  46. context: 'formatting'
  47. }) || match.month(dateString, {
  48. width: 'narrow',
  49. context: 'formatting'
  50. });
  51. // J, F, ..., D
  52. case 'MMMMM':
  53. return match.month(dateString, {
  54. width: 'narrow',
  55. context: 'formatting'
  56. });
  57. // January, February, ..., December
  58. case 'MMMM':
  59. default:
  60. return match.month(dateString, {
  61. width: 'wide',
  62. context: 'formatting'
  63. }) || match.month(dateString, {
  64. width: 'abbreviated',
  65. context: 'formatting'
  66. }) || match.month(dateString, {
  67. width: 'narrow',
  68. context: 'formatting'
  69. });
  70. }
  71. }
  72. }, {
  73. key: "validate",
  74. value: function validate(_date, value) {
  75. return value >= 0 && value <= 11;
  76. }
  77. }, {
  78. key: "set",
  79. value: function set(date, _flags, value) {
  80. date.setUTCMonth(value, 1);
  81. date.setUTCHours(0, 0, 0, 0);
  82. return date;
  83. }
  84. }]);
  85. return MonthParser;
  86. }(Parser);