match.js 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. import { buildMatchFn } from "../../_lib/buildMatchFn.js";
  2. import { buildMatchPatternFn } from "../../_lib/buildMatchPatternFn.js";
  3. const matchOrdinalNumberPattern = /^(\d+)/i;
  4. const parseOrdinalNumberPattern = /\d+/i;
  5. const matchEraPatterns = {
  6. narrow: /^(tcn|scn)/i,
  7. abbreviated: /^(trước CN|sau CN)/i,
  8. wide: /^(trước Công Nguyên|sau Công Nguyên)/i,
  9. };
  10. const parseEraPatterns = {
  11. any: [/^t/i, /^s/i],
  12. };
  13. const matchQuarterPatterns = {
  14. narrow: /^([1234]|i{1,3}v?)/i,
  15. abbreviated: /^q([1234]|i{1,3}v?)/i,
  16. wide: /^quý ([1234]|i{1,3}v?)/i,
  17. };
  18. const parseQuarterPatterns = {
  19. any: [/(1|i)$/i, /(2|ii)$/i, /(3|iii)$/i, /(4|iv)$/i],
  20. };
  21. const matchMonthPatterns = {
  22. // month number may contain leading 0, 'thg' prefix may have space, underscore or empty before number
  23. // note the order of '1' since it is a sub-string of '10', so must be lower priority
  24. narrow: /^(0?[2-9]|10|11|12|0?1)/i,
  25. // note the order of 'thg 1' since it is sub-string of 'thg 10', so must be lower priority
  26. abbreviated: /^thg[ _]?(0?[1-9](?!\d)|10|11|12)/i,
  27. // note the order of 'Mười' since it is sub-string of Mười Một, so must be lower priority
  28. wide: /^tháng ?(Một|Hai|Ba|Tư|Năm|Sáu|Bảy|Tám|Chín|Mười|Mười ?Một|Mười ?Hai|0?[1-9](?!\d)|10|11|12)/i,
  29. };
  30. const parseMonthPatterns = {
  31. narrow: [
  32. /0?1$/i,
  33. /0?2/i,
  34. /3/,
  35. /4/,
  36. /5/,
  37. /6/,
  38. /7/,
  39. /8/,
  40. /9/,
  41. /10/,
  42. /11/,
  43. /12/,
  44. ],
  45. abbreviated: [
  46. /^thg[ _]?0?1(?!\d)/i,
  47. /^thg[ _]?0?2/i,
  48. /^thg[ _]?0?3/i,
  49. /^thg[ _]?0?4/i,
  50. /^thg[ _]?0?5/i,
  51. /^thg[ _]?0?6/i,
  52. /^thg[ _]?0?7/i,
  53. /^thg[ _]?0?8/i,
  54. /^thg[ _]?0?9/i,
  55. /^thg[ _]?10/i,
  56. /^thg[ _]?11/i,
  57. /^thg[ _]?12/i,
  58. ],
  59. wide: [
  60. /^tháng ?(Một|0?1(?!\d))/i,
  61. /^tháng ?(Hai|0?2)/i,
  62. /^tháng ?(Ba|0?3)/i,
  63. /^tháng ?(Tư|0?4)/i,
  64. /^tháng ?(Năm|0?5)/i,
  65. /^tháng ?(Sáu|0?6)/i,
  66. /^tháng ?(Bảy|0?7)/i,
  67. /^tháng ?(Tám|0?8)/i,
  68. /^tháng ?(Chín|0?9)/i,
  69. /^tháng ?(Mười|10)/i,
  70. /^tháng ?(Mười ?Một|11)/i,
  71. /^tháng ?(Mười ?Hai|12)/i,
  72. ],
  73. };
  74. const matchDayPatterns = {
  75. narrow: /^(CN|T2|T3|T4|T5|T6|T7)/i,
  76. short: /^(CN|Th ?2|Th ?3|Th ?4|Th ?5|Th ?6|Th ?7)/i,
  77. abbreviated: /^(CN|Th ?2|Th ?3|Th ?4|Th ?5|Th ?6|Th ?7)/i,
  78. wide: /^(Chủ ?Nhật|Chúa ?Nhật|thứ ?Hai|thứ ?Ba|thứ ?Tư|thứ ?Năm|thứ ?Sáu|thứ ?Bảy)/i,
  79. };
  80. const parseDayPatterns = {
  81. narrow: [/CN/i, /2/i, /3/i, /4/i, /5/i, /6/i, /7/i],
  82. short: [/CN/i, /2/i, /3/i, /4/i, /5/i, /6/i, /7/i],
  83. abbreviated: [/CN/i, /2/i, /3/i, /4/i, /5/i, /6/i, /7/i],
  84. wide: [/(Chủ|Chúa) ?Nhật/i, /Hai/i, /Ba/i, /Tư/i, /Năm/i, /Sáu/i, /Bảy/i],
  85. };
  86. const matchDayPeriodPatterns = {
  87. narrow: /^(a|p|nửa đêm|trưa|(giờ) (sáng|chiều|tối|đêm))/i,
  88. abbreviated: /^(am|pm|nửa đêm|trưa|(giờ) (sáng|chiều|tối|đêm))/i,
  89. wide: /^(ch[^i]*|sa|nửa đêm|trưa|(giờ) (sáng|chiều|tối|đêm))/i,
  90. };
  91. const parseDayPeriodPatterns = {
  92. any: {
  93. am: /^(a|sa)/i,
  94. pm: /^(p|ch[^i]*)/i,
  95. midnight: /nửa đêm/i,
  96. noon: /trưa/i,
  97. morning: /sáng/i,
  98. afternoon: /chiều/i,
  99. evening: /tối/i,
  100. night: /^đêm/i,
  101. },
  102. };
  103. export const match = {
  104. ordinalNumber: buildMatchPatternFn({
  105. matchPattern: matchOrdinalNumberPattern,
  106. parsePattern: parseOrdinalNumberPattern,
  107. valueCallback: (value) => parseInt(value, 10),
  108. }),
  109. era: buildMatchFn({
  110. matchPatterns: matchEraPatterns,
  111. defaultMatchWidth: "wide",
  112. parsePatterns: parseEraPatterns,
  113. defaultParseWidth: "any",
  114. }),
  115. quarter: buildMatchFn({
  116. matchPatterns: matchQuarterPatterns,
  117. defaultMatchWidth: "wide",
  118. parsePatterns: parseQuarterPatterns,
  119. defaultParseWidth: "any",
  120. valueCallback: (index) => index + 1,
  121. }),
  122. month: buildMatchFn({
  123. matchPatterns: matchMonthPatterns,
  124. defaultMatchWidth: "wide",
  125. parsePatterns: parseMonthPatterns,
  126. defaultParseWidth: "wide",
  127. }),
  128. day: buildMatchFn({
  129. matchPatterns: matchDayPatterns,
  130. defaultMatchWidth: "wide",
  131. parsePatterns: parseDayPatterns,
  132. defaultParseWidth: "wide",
  133. }),
  134. dayPeriod: buildMatchFn({
  135. matchPatterns: matchDayPeriodPatterns,
  136. defaultMatchWidth: "wide",
  137. parsePatterns: parseDayPeriodPatterns,
  138. defaultParseWidth: "any",
  139. }),
  140. };