LocalDayParser.cjs 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. "use strict";
  2. exports.LocalDayParser = void 0;
  3. var _index = require("../../../setDay.cjs");
  4. var _Parser = require("../Parser.cjs");
  5. var _utils = require("../utils.cjs");
  6. // Local day of week
  7. class LocalDayParser extends _Parser.Parser {
  8. priority = 90;
  9. parse(dateString, token, match, options) {
  10. const valueCallback = (value) => {
  11. // We want here floor instead of trunc, so we get -7 for value 0 instead of 0
  12. const wholeWeekDays = Math.floor((value - 1) / 7) * 7;
  13. return ((value + options.weekStartsOn + 6) % 7) + wholeWeekDays;
  14. };
  15. switch (token) {
  16. // 3
  17. case "e":
  18. case "ee": // 03
  19. return (0, _utils.mapValue)(
  20. (0, _utils.parseNDigits)(token.length, dateString),
  21. valueCallback,
  22. );
  23. // 3rd
  24. case "eo":
  25. return (0, _utils.mapValue)(
  26. match.ordinalNumber(dateString, {
  27. unit: "day",
  28. }),
  29. valueCallback,
  30. );
  31. // Tue
  32. case "eee":
  33. return (
  34. match.day(dateString, {
  35. width: "abbreviated",
  36. context: "formatting",
  37. }) ||
  38. match.day(dateString, { width: "short", context: "formatting" }) ||
  39. match.day(dateString, { width: "narrow", context: "formatting" })
  40. );
  41. // T
  42. case "eeeee":
  43. return match.day(dateString, {
  44. width: "narrow",
  45. context: "formatting",
  46. });
  47. // Tu
  48. case "eeeeee":
  49. return (
  50. match.day(dateString, { width: "short", context: "formatting" }) ||
  51. match.day(dateString, { width: "narrow", context: "formatting" })
  52. );
  53. // Tuesday
  54. case "eeee":
  55. default:
  56. return (
  57. match.day(dateString, { width: "wide", context: "formatting" }) ||
  58. match.day(dateString, {
  59. width: "abbreviated",
  60. context: "formatting",
  61. }) ||
  62. match.day(dateString, { width: "short", context: "formatting" }) ||
  63. match.day(dateString, { width: "narrow", context: "formatting" })
  64. );
  65. }
  66. }
  67. validate(_date, value) {
  68. return value >= 0 && value <= 6;
  69. }
  70. set(date, _flags, value, options) {
  71. date = (0, _index.setDay)(date, value, options);
  72. date.setHours(0, 0, 0, 0);
  73. return date;
  74. }
  75. incompatibleTokens = [
  76. "y",
  77. "R",
  78. "u",
  79. "q",
  80. "Q",
  81. "M",
  82. "L",
  83. "I",
  84. "d",
  85. "D",
  86. "E",
  87. "i",
  88. "c",
  89. "t",
  90. "T",
  91. ];
  92. }
  93. exports.LocalDayParser = LocalDayParser;