QuarterParser.js 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  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 { Parser } from "../Parser.js";
  8. import { parseNDigits } from "../utils.js";
  9. export var QuarterParser = /*#__PURE__*/function (_Parser) {
  10. _inherits(QuarterParser, _Parser);
  11. var _super = _createSuper(QuarterParser);
  12. function QuarterParser() {
  13. var _this;
  14. _classCallCheck(this, QuarterParser);
  15. for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) {
  16. args[_key] = arguments[_key];
  17. }
  18. _this = _super.call.apply(_super, [this].concat(args));
  19. _defineProperty(_assertThisInitialized(_this), "priority", 120);
  20. _defineProperty(_assertThisInitialized(_this), "incompatibleTokens", ['Y', 'R', 'q', 'M', 'L', 'w', 'I', 'd', 'D', 'i', 'e', 'c', 't', 'T']);
  21. return _this;
  22. }
  23. _createClass(QuarterParser, [{
  24. key: "parse",
  25. value: function parse(dateString, token, match) {
  26. switch (token) {
  27. // 1, 2, 3, 4
  28. case 'Q':
  29. case 'QQ':
  30. // 01, 02, 03, 04
  31. return parseNDigits(token.length, dateString);
  32. // 1st, 2nd, 3rd, 4th
  33. case 'Qo':
  34. return match.ordinalNumber(dateString, {
  35. unit: 'quarter'
  36. });
  37. // Q1, Q2, Q3, Q4
  38. case 'QQQ':
  39. return match.quarter(dateString, {
  40. width: 'abbreviated',
  41. context: 'formatting'
  42. }) || match.quarter(dateString, {
  43. width: 'narrow',
  44. context: 'formatting'
  45. });
  46. // 1, 2, 3, 4 (narrow quarter; could be not numerical)
  47. case 'QQQQQ':
  48. return match.quarter(dateString, {
  49. width: 'narrow',
  50. context: 'formatting'
  51. });
  52. // 1st quarter, 2nd quarter, ...
  53. case 'QQQQ':
  54. default:
  55. return match.quarter(dateString, {
  56. width: 'wide',
  57. context: 'formatting'
  58. }) || match.quarter(dateString, {
  59. width: 'abbreviated',
  60. context: 'formatting'
  61. }) || match.quarter(dateString, {
  62. width: 'narrow',
  63. context: 'formatting'
  64. });
  65. }
  66. }
  67. }, {
  68. key: "validate",
  69. value: function validate(_date, value) {
  70. return value >= 1 && value <= 4;
  71. }
  72. }, {
  73. key: "set",
  74. value: function set(date, _flags, value) {
  75. date.setUTCMonth((value - 1) * 3, 1);
  76. date.setUTCHours(0, 0, 0, 0);
  77. return date;
  78. }
  79. }]);
  80. return QuarterParser;
  81. }(Parser);