ISODayParser.js 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  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 { mapValue, parseNDigits } from "../utils.js";
  9. import setUTCISODay from "../../../_lib/setUTCISODay/index.js"; // ISO day of week
  10. export var ISODayParser = /*#__PURE__*/function (_Parser) {
  11. _inherits(ISODayParser, _Parser);
  12. var _super = _createSuper(ISODayParser);
  13. function ISODayParser() {
  14. var _this;
  15. _classCallCheck(this, ISODayParser);
  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), "priority", 90);
  21. _defineProperty(_assertThisInitialized(_this), "incompatibleTokens", ['y', 'Y', 'u', 'q', 'Q', 'M', 'L', 'w', 'd', 'D', 'E', 'e', 'c', 't', 'T']);
  22. return _this;
  23. }
  24. _createClass(ISODayParser, [{
  25. key: "parse",
  26. value: function parse(dateString, token, match) {
  27. var valueCallback = function valueCallback(value) {
  28. if (value === 0) {
  29. return 7;
  30. }
  31. return value;
  32. };
  33. switch (token) {
  34. // 2
  35. case 'i':
  36. case 'ii':
  37. // 02
  38. return parseNDigits(token.length, dateString);
  39. // 2nd
  40. case 'io':
  41. return match.ordinalNumber(dateString, {
  42. unit: 'day'
  43. });
  44. // Tue
  45. case 'iii':
  46. return mapValue(match.day(dateString, {
  47. width: 'abbreviated',
  48. context: 'formatting'
  49. }) || match.day(dateString, {
  50. width: 'short',
  51. context: 'formatting'
  52. }) || match.day(dateString, {
  53. width: 'narrow',
  54. context: 'formatting'
  55. }), valueCallback);
  56. // T
  57. case 'iiiii':
  58. return mapValue(match.day(dateString, {
  59. width: 'narrow',
  60. context: 'formatting'
  61. }), valueCallback);
  62. // Tu
  63. case 'iiiiii':
  64. return mapValue(match.day(dateString, {
  65. width: 'short',
  66. context: 'formatting'
  67. }) || match.day(dateString, {
  68. width: 'narrow',
  69. context: 'formatting'
  70. }), valueCallback);
  71. // Tuesday
  72. case 'iiii':
  73. default:
  74. return mapValue(match.day(dateString, {
  75. width: 'wide',
  76. context: 'formatting'
  77. }) || match.day(dateString, {
  78. width: 'abbreviated',
  79. context: 'formatting'
  80. }) || match.day(dateString, {
  81. width: 'short',
  82. context: 'formatting'
  83. }) || match.day(dateString, {
  84. width: 'narrow',
  85. context: 'formatting'
  86. }), valueCallback);
  87. }
  88. }
  89. }, {
  90. key: "validate",
  91. value: function validate(_date, value) {
  92. return value >= 1 && value <= 7;
  93. }
  94. }, {
  95. key: "set",
  96. value: function set(date, _flags, value) {
  97. date = setUTCISODay(date, value);
  98. date.setUTCHours(0, 0, 0, 0);
  99. return date;
  100. }
  101. }]);
  102. return ISODayParser;
  103. }(Parser);