DayParser.js 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. import { setDay } from "../../../setDay.js";
  2. import { Parser } from "../Parser.js";
  3. // Day of week
  4. export class DayParser extends Parser {
  5. priority = 90;
  6. parse(dateString, token, match) {
  7. switch (token) {
  8. // Tue
  9. case "E":
  10. case "EE":
  11. case "EEE":
  12. return (
  13. match.day(dateString, {
  14. width: "abbreviated",
  15. context: "formatting",
  16. }) ||
  17. match.day(dateString, { width: "short", context: "formatting" }) ||
  18. match.day(dateString, { width: "narrow", context: "formatting" })
  19. );
  20. // T
  21. case "EEEEE":
  22. return match.day(dateString, {
  23. width: "narrow",
  24. context: "formatting",
  25. });
  26. // Tu
  27. case "EEEEEE":
  28. return (
  29. match.day(dateString, { width: "short", context: "formatting" }) ||
  30. match.day(dateString, { width: "narrow", context: "formatting" })
  31. );
  32. // Tuesday
  33. case "EEEE":
  34. default:
  35. return (
  36. match.day(dateString, { width: "wide", context: "formatting" }) ||
  37. match.day(dateString, {
  38. width: "abbreviated",
  39. context: "formatting",
  40. }) ||
  41. match.day(dateString, { width: "short", context: "formatting" }) ||
  42. match.day(dateString, { width: "narrow", context: "formatting" })
  43. );
  44. }
  45. }
  46. validate(_date, value) {
  47. return value >= 0 && value <= 6;
  48. }
  49. set(date, _flags, value, options) {
  50. date = setDay(date, value, options);
  51. date.setHours(0, 0, 0, 0);
  52. return date;
  53. }
  54. incompatibleTokens = ["D", "i", "e", "c", "t", "T"];
  55. }