UNPKG

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