isMatch.js 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297
  1. import { isValid } from "./isValid.js";
  2. import { parse } from "./parse.js";
  3. /**
  4. * The {@link isMatch} function options.
  5. */
  6. /**
  7. * @name isMatch
  8. * @category Common Helpers
  9. * @summary validates the date string against given formats
  10. *
  11. * @description
  12. * Return the true if given date is string correct against the given format else
  13. * will return false.
  14. *
  15. * > ⚠️ Please note that the `format` tokens differ from Moment.js and other libraries.
  16. * > See: https://github.com/date-fns/date-fns/blob/master/docs/unicodeTokens.md
  17. *
  18. * The characters in the format string wrapped between two single quotes characters (') are escaped.
  19. * Two single quotes in a row, whether inside or outside a quoted sequence, represent a 'real' single quote.
  20. *
  21. * Format of the format string is based on Unicode Technical Standard #35:
  22. * https://www.unicode.org/reports/tr35/tr35-dates.html#Date_Field_Symbol_Table
  23. * with a few additions (see note 5 below the table).
  24. *
  25. * Not all tokens are compatible. Combinations that don't make sense or could lead to bugs are prohibited
  26. * and will throw `RangeError`. For example usage of 24-hour format token with AM/PM token will throw an exception:
  27. *
  28. * ```javascript
  29. * isMatch('23 AM', 'HH a')
  30. * //=> RangeError: The format string mustn't contain `HH` and `a` at the same time
  31. * ```
  32. *
  33. * See the compatibility table: https://docs.google.com/spreadsheets/d/e/2PACX-1vQOPU3xUhplll6dyoMmVUXHKl_8CRDs6_ueLmex3SoqwhuolkuN3O05l4rqx5h1dKX8eb46Ul-CCSrq/pubhtml?gid=0&single=true
  34. *
  35. * Accepted format string patterns:
  36. * | Unit |Prior| Pattern | Result examples | Notes |
  37. * |---------------------------------|-----|---------|-----------------------------------|-------|
  38. * | Era | 140 | G..GGG | AD, BC | |
  39. * | | | GGGG | Anno Domini, Before Christ | 2 |
  40. * | | | GGGGG | A, B | |
  41. * | Calendar year | 130 | y | 44, 1, 1900, 2017, 9999 | 4 |
  42. * | | | yo | 44th, 1st, 1900th, 9999999th | 4,5 |
  43. * | | | yy | 44, 01, 00, 17 | 4 |
  44. * | | | yyy | 044, 001, 123, 999 | 4 |
  45. * | | | yyyy | 0044, 0001, 1900, 2017 | 4 |
  46. * | | | yyyyy | ... | 2,4 |
  47. * | Local week-numbering year | 130 | Y | 44, 1, 1900, 2017, 9000 | 4 |
  48. * | | | Yo | 44th, 1st, 1900th, 9999999th | 4,5 |
  49. * | | | YY | 44, 01, 00, 17 | 4,6 |
  50. * | | | YYY | 044, 001, 123, 999 | 4 |
  51. * | | | YYYY | 0044, 0001, 1900, 2017 | 4,6 |
  52. * | | | YYYYY | ... | 2,4 |
  53. * | ISO week-numbering year | 130 | R | -43, 1, 1900, 2017, 9999, -9999 | 4,5 |
  54. * | | | RR | -43, 01, 00, 17 | 4,5 |
  55. * | | | RRR | -043, 001, 123, 999, -999 | 4,5 |
  56. * | | | RRRR | -0043, 0001, 2017, 9999, -9999 | 4,5 |
  57. * | | | RRRRR | ... | 2,4,5 |
  58. * | Extended year | 130 | u | -43, 1, 1900, 2017, 9999, -999 | 4 |
  59. * | | | uu | -43, 01, 99, -99 | 4 |
  60. * | | | uuu | -043, 001, 123, 999, -999 | 4 |
  61. * | | | uuuu | -0043, 0001, 2017, 9999, -9999 | 4 |
  62. * | | | uuuuu | ... | 2,4 |
  63. * | Quarter (formatting) | 120 | Q | 1, 2, 3, 4 | |
  64. * | | | Qo | 1st, 2nd, 3rd, 4th | 5 |
  65. * | | | QQ | 01, 02, 03, 04 | |
  66. * | | | QQQ | Q1, Q2, Q3, Q4 | |
  67. * | | | QQQQ | 1st quarter, 2nd quarter, ... | 2 |
  68. * | | | QQQQQ | 1, 2, 3, 4 | 4 |
  69. * | Quarter (stand-alone) | 120 | q | 1, 2, 3, 4 | |
  70. * | | | qo | 1st, 2nd, 3rd, 4th | 5 |
  71. * | | | qq | 01, 02, 03, 04 | |
  72. * | | | qqq | Q1, Q2, Q3, Q4 | |
  73. * | | | qqqq | 1st quarter, 2nd quarter, ... | 2 |
  74. * | | | qqqqq | 1, 2, 3, 4 | 3 |
  75. * | Month (formatting) | 110 | M | 1, 2, ..., 12 | |
  76. * | | | Mo | 1st, 2nd, ..., 12th | 5 |
  77. * | | | MM | 01, 02, ..., 12 | |
  78. * | | | MMM | Jan, Feb, ..., Dec | |
  79. * | | | MMMM | January, February, ..., December | 2 |
  80. * | | | MMMMM | J, F, ..., D | |
  81. * | Month (stand-alone) | 110 | L | 1, 2, ..., 12 | |
  82. * | | | Lo | 1st, 2nd, ..., 12th | 5 |
  83. * | | | LL | 01, 02, ..., 12 | |
  84. * | | | LLL | Jan, Feb, ..., Dec | |
  85. * | | | LLLL | January, February, ..., December | 2 |
  86. * | | | LLLLL | J, F, ..., D | |
  87. * | Local week of year | 100 | w | 1, 2, ..., 53 | |
  88. * | | | wo | 1st, 2nd, ..., 53th | 5 |
  89. * | | | ww | 01, 02, ..., 53 | |
  90. * | ISO week of year | 100 | I | 1, 2, ..., 53 | 5 |
  91. * | | | Io | 1st, 2nd, ..., 53th | 5 |
  92. * | | | II | 01, 02, ..., 53 | 5 |
  93. * | Day of month | 90 | d | 1, 2, ..., 31 | |
  94. * | | | do | 1st, 2nd, ..., 31st | 5 |
  95. * | | | dd | 01, 02, ..., 31 | |
  96. * | Day of year | 90 | D | 1, 2, ..., 365, 366 | 7 |
  97. * | | | Do | 1st, 2nd, ..., 365th, 366th | 5 |
  98. * | | | DD | 01, 02, ..., 365, 366 | 7 |
  99. * | | | DDD | 001, 002, ..., 365, 366 | |
  100. * | | | DDDD | ... | 2 |
  101. * | Day of week (formatting) | 90 | E..EEE | Mon, Tue, Wed, ..., Su | |
  102. * | | | EEEE | Monday, Tuesday, ..., Sunday | 2 |
  103. * | | | EEEEE | M, T, W, T, F, S, S | |
  104. * | | | EEEEEE | Mo, Tu, We, Th, Fr, Sa, Su | |
  105. * | ISO day of week (formatting) | 90 | i | 1, 2, 3, ..., 7 | 5 |
  106. * | | | io | 1st, 2nd, ..., 7th | 5 |
  107. * | | | ii | 01, 02, ..., 07 | 5 |
  108. * | | | iii | Mon, Tue, Wed, ..., Su | 5 |
  109. * | | | iiii | Monday, Tuesday, ..., Sunday | 2,5 |
  110. * | | | iiiii | M, T, W, T, F, S, S | 5 |
  111. * | | | iiiiii | Mo, Tu, We, Th, Fr, Sa, Su | 5 |
  112. * | Local day of week (formatting) | 90 | e | 2, 3, 4, ..., 1 | |
  113. * | | | eo | 2nd, 3rd, ..., 1st | 5 |
  114. * | | | ee | 02, 03, ..., 01 | |
  115. * | | | eee | Mon, Tue, Wed, ..., Su | |
  116. * | | | eeee | Monday, Tuesday, ..., Sunday | 2 |
  117. * | | | eeeee | M, T, W, T, F, S, S | |
  118. * | | | eeeeee | Mo, Tu, We, Th, Fr, Sa, Su | |
  119. * | Local day of week (stand-alone) | 90 | c | 2, 3, 4, ..., 1 | |
  120. * | | | co | 2nd, 3rd, ..., 1st | 5 |
  121. * | | | cc | 02, 03, ..., 01 | |
  122. * | | | ccc | Mon, Tue, Wed, ..., Su | |
  123. * | | | cccc | Monday, Tuesday, ..., Sunday | 2 |
  124. * | | | ccccc | M, T, W, T, F, S, S | |
  125. * | | | cccccc | Mo, Tu, We, Th, Fr, Sa, Su | |
  126. * | AM, PM | 80 | a..aaa | AM, PM | |
  127. * | | | aaaa | a.m., p.m. | 2 |
  128. * | | | aaaaa | a, p | |
  129. * | AM, PM, noon, midnight | 80 | b..bbb | AM, PM, noon, midnight | |
  130. * | | | bbbb | a.m., p.m., noon, midnight | 2 |
  131. * | | | bbbbb | a, p, n, mi | |
  132. * | Flexible day period | 80 | B..BBB | at night, in the morning, ... | |
  133. * | | | BBBB | at night, in the morning, ... | 2 |
  134. * | | | BBBBB | at night, in the morning, ... | |
  135. * | Hour [1-12] | 70 | h | 1, 2, ..., 11, 12 | |
  136. * | | | ho | 1st, 2nd, ..., 11th, 12th | 5 |
  137. * | | | hh | 01, 02, ..., 11, 12 | |
  138. * | Hour [0-23] | 70 | H | 0, 1, 2, ..., 23 | |
  139. * | | | Ho | 0th, 1st, 2nd, ..., 23rd | 5 |
  140. * | | | HH | 00, 01, 02, ..., 23 | |
  141. * | Hour [0-11] | 70 | K | 1, 2, ..., 11, 0 | |
  142. * | | | Ko | 1st, 2nd, ..., 11th, 0th | 5 |
  143. * | | | KK | 01, 02, ..., 11, 00 | |
  144. * | Hour [1-24] | 70 | k | 24, 1, 2, ..., 23 | |
  145. * | | | ko | 24th, 1st, 2nd, ..., 23rd | 5 |
  146. * | | | kk | 24, 01, 02, ..., 23 | |
  147. * | Minute | 60 | m | 0, 1, ..., 59 | |
  148. * | | | mo | 0th, 1st, ..., 59th | 5 |
  149. * | | | mm | 00, 01, ..., 59 | |
  150. * | Second | 50 | s | 0, 1, ..., 59 | |
  151. * | | | so | 0th, 1st, ..., 59th | 5 |
  152. * | | | ss | 00, 01, ..., 59 | |
  153. * | Seconds timestamp | 40 | t | 512969520 | |
  154. * | | | tt | ... | 2 |
  155. * | Fraction of second | 30 | S | 0, 1, ..., 9 | |
  156. * | | | SS | 00, 01, ..., 99 | |
  157. * | | | SSS | 000, 001, ..., 999 | |
  158. * | | | SSSS | ... | 2 |
  159. * | Milliseconds timestamp | 20 | T | 512969520900 | |
  160. * | | | TT | ... | 2 |
  161. * | Timezone (ISO-8601 w/ Z) | 10 | X | -08, +0530, Z | |
  162. * | | | XX | -0800, +0530, Z | |
  163. * | | | XXX | -08:00, +05:30, Z | |
  164. * | | | XXXX | -0800, +0530, Z, +123456 | 2 |
  165. * | | | XXXXX | -08:00, +05:30, Z, +12:34:56 | |
  166. * | Timezone (ISO-8601 w/o Z) | 10 | x | -08, +0530, +00 | |
  167. * | | | xx | -0800, +0530, +0000 | |
  168. * | | | xxx | -08:00, +05:30, +00:00 | 2 |
  169. * | | | xxxx | -0800, +0530, +0000, +123456 | |
  170. * | | | xxxxx | -08:00, +05:30, +00:00, +12:34:56 | |
  171. * | Long localized date | NA | P | 05/29/1453 | 5,8 |
  172. * | | | PP | May 29, 1453 | |
  173. * | | | PPP | May 29th, 1453 | |
  174. * | | | PPPP | Sunday, May 29th, 1453 | 2,5,8 |
  175. * | Long localized time | NA | p | 12:00 AM | 5,8 |
  176. * | | | pp | 12:00:00 AM | |
  177. * | Combination of date and time | NA | Pp | 05/29/1453, 12:00 AM | |
  178. * | | | PPpp | May 29, 1453, 12:00:00 AM | |
  179. * | | | PPPpp | May 29th, 1453 at ... | |
  180. * | | | PPPPpp | Sunday, May 29th, 1453 at ... | 2,5,8 |
  181. * Notes:
  182. * 1. "Formatting" units (e.g. formatting quarter) in the default en-US locale
  183. * are the same as "stand-alone" units, but are different in some languages.
  184. * "Formatting" units are declined according to the rules of the language
  185. * in the context of a date. "Stand-alone" units are always nominative singular.
  186. * In `format` function, they will produce different result:
  187. *
  188. * `format(new Date(2017, 10, 6), 'do LLLL', {locale: cs}) //=> '6. listopad'`
  189. *
  190. * `format(new Date(2017, 10, 6), 'do MMMM', {locale: cs}) //=> '6. listopadu'`
  191. *
  192. * `isMatch` will try to match both formatting and stand-alone units interchangeably.
  193. *
  194. * 2. Any sequence of the identical letters is a pattern, unless it is escaped by
  195. * the single quote characters (see below).
  196. * If the sequence is longer than listed in table:
  197. * - for numerical units (`yyyyyyyy`) `isMatch` will try to match a number
  198. * as wide as the sequence
  199. * - for text units (`MMMMMMMM`) `isMatch` will try to match the widest variation of the unit.
  200. * These variations are marked with "2" in the last column of the table.
  201. *
  202. * 3. `QQQQQ` and `qqqqq` could be not strictly numerical in some locales.
  203. * These tokens represent the shortest form of the quarter.
  204. *
  205. * 4. The main difference between `y` and `u` patterns are B.C. years:
  206. *
  207. * | Year | `y` | `u` |
  208. * |------|-----|-----|
  209. * | AC 1 | 1 | 1 |
  210. * | BC 1 | 1 | 0 |
  211. * | BC 2 | 2 | -1 |
  212. *
  213. * Also `yy` will try to guess the century of two digit year by proximity with `referenceDate`:
  214. *
  215. * `isMatch('50', 'yy') //=> true`
  216. *
  217. * `isMatch('75', 'yy') //=> true`
  218. *
  219. * while `uu` will use the year as is:
  220. *
  221. * `isMatch('50', 'uu') //=> true`
  222. *
  223. * `isMatch('75', 'uu') //=> true`
  224. *
  225. * The same difference is true for local and ISO week-numbering years (`Y` and `R`),
  226. * except local week-numbering years are dependent on `options.weekStartsOn`
  227. * and `options.firstWeekContainsDate` (compare [setISOWeekYear](https://date-fns.org/docs/setISOWeekYear)
  228. * and [setWeekYear](https://date-fns.org/docs/setWeekYear)).
  229. *
  230. * 5. These patterns are not in the Unicode Technical Standard #35:
  231. * - `i`: ISO day of week
  232. * - `I`: ISO week of year
  233. * - `R`: ISO week-numbering year
  234. * - `o`: ordinal number modifier
  235. * - `P`: long localized date
  236. * - `p`: long localized time
  237. *
  238. * 6. `YY` and `YYYY` tokens represent week-numbering years but they are often confused with years.
  239. * You should enable `options.useAdditionalWeekYearTokens` to use them. See: https://github.com/date-fns/date-fns/blob/master/docs/unicodeTokens.md
  240. *
  241. * 7. `D` and `DD` tokens represent days of the year but they are often confused with days of the month.
  242. * You should enable `options.useAdditionalDayOfYearTokens` to use them. See: https://github.com/date-fns/date-fns/blob/master/docs/unicodeTokens.md
  243. *
  244. * 8. `P+` tokens do not have a defined priority since they are merely aliases to other tokens based
  245. * on the given locale.
  246. *
  247. * using `en-US` locale: `P` => `MM/dd/yyyy`
  248. * using `en-US` locale: `p` => `hh:mm a`
  249. * using `pt-BR` locale: `P` => `dd/MM/yyyy`
  250. * using `pt-BR` locale: `p` => `HH:mm`
  251. *
  252. * Values will be checked in the descending order of its unit's priority.
  253. * Units of an equal priority overwrite each other in the order of appearance.
  254. *
  255. * If no values of higher priority are matched (e.g. when matching string 'January 1st' without a year),
  256. * the values will be taken from today's using `new Date()` date which works as a context of parsing.
  257. *
  258. * The result may vary by locale.
  259. *
  260. * If `formatString` matches with `dateString` but does not provides tokens, `referenceDate` will be returned.
  261. *
  262. * @param dateStr - The date string to verify
  263. * @param format - The string of tokens
  264. * @param options - An object with options.
  265. * see: https://github.com/date-fns/date-fns/blob/master/docs/unicodeTokens.md
  266. * see: https://github.com/date-fns/date-fns/blob/master/docs/unicodeTokens.md
  267. *
  268. * @returns Is format string a match for date string?
  269. *
  270. * @throws `options.locale` must contain `match` property
  271. * @throws use `yyyy` instead of `YYYY` for formatting years; see: https://github.com/date-fns/date-fns/blob/master/docs/unicodeTokens.md
  272. * @throws use `yy` instead of `YY` for formatting years; see: https://github.com/date-fns/date-fns/blob/master/docs/unicodeTokens.md
  273. * @throws use `d` instead of `D` for formatting days of the month; see: https://github.com/date-fns/date-fns/blob/master/docs/unicodeTokens.md
  274. * @throws use `dd` instead of `DD` for formatting days of the month; see: https://github.com/date-fns/date-fns/blob/master/docs/unicodeTokens.md
  275. * @throws format string contains an unescaped latin alphabet character
  276. *
  277. * @example
  278. * // Match 11 February 2014 from middle-endian format:
  279. * const result = isMatch('02/11/2014', 'MM/dd/yyyy')
  280. * //=> true
  281. *
  282. * @example
  283. * // Match 28th of February in Esperanto locale in the context of 2010 year:
  284. * import eo from 'date-fns/locale/eo'
  285. * const result = isMatch('28-a de februaro', "do 'de' MMMM", {
  286. * locale: eo
  287. * })
  288. * //=> true
  289. */
  290. export function isMatch(dateStr, formatStr, options) {
  291. return isValid(parse(dateStr, formatStr, new Date(), options));
  292. }
  293. // Fallback for modularized imports:
  294. export default isMatch;