index.js 22 KB

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