UNPKG

29 kBJavaScriptView Raw
1import { constructFrom } from "./constructFrom.mjs";
2import { getDefaultOptions } from "./getDefaultOptions.mjs";
3import { defaultLocale } from "./_lib/defaultLocale.mjs";
4import { toDate } from "./toDate.mjs";
5import { longFormatters } from "./_lib/format/longFormatters.mjs";
6import {
7 isProtectedDayOfYearToken,
8 isProtectedWeekYearToken,
9 warnOrThrowProtectedError,
10} from "./_lib/protectedTokens.mjs";
11import { parsers } from "./parse/_lib/parsers.mjs";
12import { DateToSystemTimezoneSetter } from "./parse/_lib/Setter.mjs";
13
14// Rexports of internal for libraries to use.
15// See: https://github.com/date-fns/date-fns/issues/3638#issuecomment-1877082874
16export { longFormatters, parsers };
17
18/**
19 * The {@link parse} function options.
20 */
21
22// This RegExp consists of three parts separated by `|`:
23// - [yYQqMLwIdDecihHKkms]o matches any available ordinal number token
24// (one of the certain letters followed by `o`)
25// - (\w)\1* matches any sequences of the same letter
26// - '' matches two quote characters in a row
27// - '(''|[^'])+('|$) matches anything surrounded by two quote characters ('),
28// except a single quote symbol, which ends the sequence.
29// Two quote characters do not end the sequence.
30// If there is no matching single quote
31// then the sequence will continue until the end of the string.
32// - . matches any single character unmatched by previous parts of the RegExps
33const formattingTokensRegExp =
34 /[yYQqMLwIdDecihHKkms]o|(\w)\1*|''|'(''|[^'])+('|$)|./g;
35
36// This RegExp catches symbols escaped by quotes, and also
37// sequences of symbols P, p, and the combinations like `PPPPPPPppppp`
38const longFormattingTokensRegExp = /P+p+|P+|p+|''|'(''|[^'])+('|$)|./g;
39
40const escapedStringRegExp = /^'([^]*?)'?$/;
41const doubleQuoteRegExp = /''/g;
42
43const notWhitespaceRegExp = /\S/;
44const unescapedLatinCharacterRegExp = /[a-zA-Z]/;
45
46/**
47 * @name parse
48 * @category Common Helpers
49 * @summary Parse the date.
50 *
51 * @description
52 * Return the date parsed from string using the given format string.
53 *
54 * > ⚠️ Please note that the `format` tokens differ from Moment.js and other libraries.
55 * > See: https://github.com/date-fns/date-fns/blob/master/docs/unicodeTokens.md
56 *
57 * The characters in the format string wrapped between two single quotes characters (') are escaped.
58 * Two single quotes in a row, whether inside or outside a quoted sequence, represent a 'real' single quote.
59 *
60 * Format of the format string is based on Unicode Technical Standard #35:
61 * https://www.unicode.org/reports/tr35/tr35-dates.html#Date_Field_Symbol_Table
62 * with a few additions (see note 5 below the table).
63 *
64 * Not all tokens are compatible. Combinations that don't make sense or could lead to bugs are prohibited
65 * and will throw `RangeError`. For example usage of 24-hour format token with AM/PM token will throw an exception:
66 *
67 * ```javascript
68 * parse('23 AM', 'HH a', new Date())
69 * //=> RangeError: The format string mustn't contain `HH` and `a` at the same time
70 * ```
71 *
72 * See the compatibility table: https://docs.google.com/spreadsheets/d/e/2PACX-1vQOPU3xUhplll6dyoMmVUXHKl_8CRDs6_ueLmex3SoqwhuolkuN3O05l4rqx5h1dKX8eb46Ul-CCSrq/pubhtml?gid=0&single=true
73 *
74 * Accepted format string patterns:
75 * | Unit |Prior| Pattern | Result examples | Notes |
76 * |---------------------------------|-----|---------|-----------------------------------|-------|
77 * | Era | 140 | G..GGG | AD, BC | |
78 * | | | GGGG | Anno Domini, Before Christ | 2 |
79 * | | | GGGGG | A, B | |
80 * | Calendar year | 130 | y | 44, 1, 1900, 2017, 9999 | 4 |
81 * | | | yo | 44th, 1st, 1900th, 9999999th | 4,5 |
82 * | | | yy | 44, 01, 00, 17 | 4 |
83 * | | | yyy | 044, 001, 123, 999 | 4 |
84 * | | | yyyy | 0044, 0001, 1900, 2017 | 4 |
85 * | | | yyyyy | ... | 2,4 |
86 * | Local week-numbering year | 130 | Y | 44, 1, 1900, 2017, 9000 | 4 |
87 * | | | Yo | 44th, 1st, 1900th, 9999999th | 4,5 |
88 * | | | YY | 44, 01, 00, 17 | 4,6 |
89 * | | | YYY | 044, 001, 123, 999 | 4 |
90 * | | | YYYY | 0044, 0001, 1900, 2017 | 4,6 |
91 * | | | YYYYY | ... | 2,4 |
92 * | ISO week-numbering year | 130 | R | -43, 1, 1900, 2017, 9999, -9999 | 4,5 |
93 * | | | RR | -43, 01, 00, 17 | 4,5 |
94 * | | | RRR | -043, 001, 123, 999, -999 | 4,5 |
95 * | | | RRRR | -0043, 0001, 2017, 9999, -9999 | 4,5 |
96 * | | | RRRRR | ... | 2,4,5 |
97 * | Extended year | 130 | u | -43, 1, 1900, 2017, 9999, -999 | 4 |
98 * | | | uu | -43, 01, 99, -99 | 4 |
99 * | | | uuu | -043, 001, 123, 999, -999 | 4 |
100 * | | | uuuu | -0043, 0001, 2017, 9999, -9999 | 4 |
101 * | | | uuuuu | ... | 2,4 |
102 * | Quarter (formatting) | 120 | Q | 1, 2, 3, 4 | |
103 * | | | Qo | 1st, 2nd, 3rd, 4th | 5 |
104 * | | | QQ | 01, 02, 03, 04 | |
105 * | | | QQQ | Q1, Q2, Q3, Q4 | |
106 * | | | QQQQ | 1st quarter, 2nd quarter, ... | 2 |
107 * | | | QQQQQ | 1, 2, 3, 4 | 4 |
108 * | Quarter (stand-alone) | 120 | q | 1, 2, 3, 4 | |
109 * | | | qo | 1st, 2nd, 3rd, 4th | 5 |
110 * | | | qq | 01, 02, 03, 04 | |
111 * | | | qqq | Q1, Q2, Q3, Q4 | |
112 * | | | qqqq | 1st quarter, 2nd quarter, ... | 2 |
113 * | | | qqqqq | 1, 2, 3, 4 | 3 |
114 * | Month (formatting) | 110 | M | 1, 2, ..., 12 | |
115 * | | | Mo | 1st, 2nd, ..., 12th | 5 |
116 * | | | MM | 01, 02, ..., 12 | |
117 * | | | MMM | Jan, Feb, ..., Dec | |
118 * | | | MMMM | January, February, ..., December | 2 |
119 * | | | MMMMM | J, F, ..., D | |
120 * | Month (stand-alone) | 110 | L | 1, 2, ..., 12 | |
121 * | | | Lo | 1st, 2nd, ..., 12th | 5 |
122 * | | | LL | 01, 02, ..., 12 | |
123 * | | | LLL | Jan, Feb, ..., Dec | |
124 * | | | LLLL | January, February, ..., December | 2 |
125 * | | | LLLLL | J, F, ..., D | |
126 * | Local week of year | 100 | w | 1, 2, ..., 53 | |
127 * | | | wo | 1st, 2nd, ..., 53th | 5 |
128 * | | | ww | 01, 02, ..., 53 | |
129 * | ISO week of year | 100 | I | 1, 2, ..., 53 | 5 |
130 * | | | Io | 1st, 2nd, ..., 53th | 5 |
131 * | | | II | 01, 02, ..., 53 | 5 |
132 * | Day of month | 90 | d | 1, 2, ..., 31 | |
133 * | | | do | 1st, 2nd, ..., 31st | 5 |
134 * | | | dd | 01, 02, ..., 31 | |
135 * | Day of year | 90 | D | 1, 2, ..., 365, 366 | 7 |
136 * | | | Do | 1st, 2nd, ..., 365th, 366th | 5 |
137 * | | | DD | 01, 02, ..., 365, 366 | 7 |
138 * | | | DDD | 001, 002, ..., 365, 366 | |
139 * | | | DDDD | ... | 2 |
140 * | Day of week (formatting) | 90 | E..EEE | Mon, Tue, Wed, ..., Sun | |
141 * | | | EEEE | Monday, Tuesday, ..., Sunday | 2 |
142 * | | | EEEEE | M, T, W, T, F, S, S | |
143 * | | | EEEEEE | Mo, Tu, We, Th, Fr, Sa, Su | |
144 * | ISO day of week (formatting) | 90 | i | 1, 2, 3, ..., 7 | 5 |
145 * | | | io | 1st, 2nd, ..., 7th | 5 |
146 * | | | ii | 01, 02, ..., 07 | 5 |
147 * | | | iii | Mon, Tue, Wed, ..., Sun | 5 |
148 * | | | iiii | Monday, Tuesday, ..., Sunday | 2,5 |
149 * | | | iiiii | M, T, W, T, F, S, S | 5 |
150 * | | | iiiiii | Mo, Tu, We, Th, Fr, Sa, Su | 5 |
151 * | Local day of week (formatting) | 90 | e | 2, 3, 4, ..., 1 | |
152 * | | | eo | 2nd, 3rd, ..., 1st | 5 |
153 * | | | ee | 02, 03, ..., 01 | |
154 * | | | eee | Mon, Tue, Wed, ..., Sun | |
155 * | | | eeee | Monday, Tuesday, ..., Sunday | 2 |
156 * | | | eeeee | M, T, W, T, F, S, S | |
157 * | | | eeeeee | Mo, Tu, We, Th, Fr, Sa, Su | |
158 * | Local day of week (stand-alone) | 90 | c | 2, 3, 4, ..., 1 | |
159 * | | | co | 2nd, 3rd, ..., 1st | 5 |
160 * | | | cc | 02, 03, ..., 01 | |
161 * | | | ccc | Mon, Tue, Wed, ..., Sun | |
162 * | | | cccc | Monday, Tuesday, ..., Sunday | 2 |
163 * | | | ccccc | M, T, W, T, F, S, S | |
164 * | | | cccccc | Mo, Tu, We, Th, Fr, Sa, Su | |
165 * | AM, PM | 80 | a..aaa | AM, PM | |
166 * | | | aaaa | a.m., p.m. | 2 |
167 * | | | aaaaa | a, p | |
168 * | AM, PM, noon, midnight | 80 | b..bbb | AM, PM, noon, midnight | |
169 * | | | bbbb | a.m., p.m., noon, midnight | 2 |
170 * | | | bbbbb | a, p, n, mi | |
171 * | Flexible day period | 80 | B..BBB | at night, in the morning, ... | |
172 * | | | BBBB | at night, in the morning, ... | 2 |
173 * | | | BBBBB | at night, in the morning, ... | |
174 * | Hour [1-12] | 70 | h | 1, 2, ..., 11, 12 | |
175 * | | | ho | 1st, 2nd, ..., 11th, 12th | 5 |
176 * | | | hh | 01, 02, ..., 11, 12 | |
177 * | Hour [0-23] | 70 | H | 0, 1, 2, ..., 23 | |
178 * | | | Ho | 0th, 1st, 2nd, ..., 23rd | 5 |
179 * | | | HH | 00, 01, 02, ..., 23 | |
180 * | Hour [0-11] | 70 | K | 1, 2, ..., 11, 0 | |
181 * | | | Ko | 1st, 2nd, ..., 11th, 0th | 5 |
182 * | | | KK | 01, 02, ..., 11, 00 | |
183 * | Hour [1-24] | 70 | k | 24, 1, 2, ..., 23 | |
184 * | | | ko | 24th, 1st, 2nd, ..., 23rd | 5 |
185 * | | | kk | 24, 01, 02, ..., 23 | |
186 * | Minute | 60 | m | 0, 1, ..., 59 | |
187 * | | | mo | 0th, 1st, ..., 59th | 5 |
188 * | | | mm | 00, 01, ..., 59 | |
189 * | Second | 50 | s | 0, 1, ..., 59 | |
190 * | | | so | 0th, 1st, ..., 59th | 5 |
191 * | | | ss | 00, 01, ..., 59 | |
192 * | Seconds timestamp | 40 | t | 512969520 | |
193 * | | | tt | ... | 2 |
194 * | Fraction of second | 30 | S | 0, 1, ..., 9 | |
195 * | | | SS | 00, 01, ..., 99 | |
196 * | | | SSS | 000, 001, ..., 999 | |
197 * | | | SSSS | ... | 2 |
198 * | Milliseconds timestamp | 20 | T | 512969520900 | |
199 * | | | TT | ... | 2 |
200 * | Timezone (ISO-8601 w/ Z) | 10 | X | -08, +0530, Z | |
201 * | | | XX | -0800, +0530, Z | |
202 * | | | XXX | -08:00, +05:30, Z | |
203 * | | | XXXX | -0800, +0530, Z, +123456 | 2 |
204 * | | | XXXXX | -08:00, +05:30, Z, +12:34:56 | |
205 * | Timezone (ISO-8601 w/o Z) | 10 | x | -08, +0530, +00 | |
206 * | | | xx | -0800, +0530, +0000 | |
207 * | | | xxx | -08:00, +05:30, +00:00 | 2 |
208 * | | | xxxx | -0800, +0530, +0000, +123456 | |
209 * | | | xxxxx | -08:00, +05:30, +00:00, +12:34:56 | |
210 * | Long localized date | NA | P | 05/29/1453 | 5,8 |
211 * | | | PP | May 29, 1453 | |
212 * | | | PPP | May 29th, 1453 | |
213 * | | | PPPP | Sunday, May 29th, 1453 | 2,5,8 |
214 * | Long localized time | NA | p | 12:00 AM | 5,8 |
215 * | | | pp | 12:00:00 AM | |
216 * | Combination of date and time | NA | Pp | 05/29/1453, 12:00 AM | |
217 * | | | PPpp | May 29, 1453, 12:00:00 AM | |
218 * | | | PPPpp | May 29th, 1453 at ... | |
219 * | | | PPPPpp | Sunday, May 29th, 1453 at ... | 2,5,8 |
220 * Notes:
221 * 1. "Formatting" units (e.g. formatting quarter) in the default en-US locale
222 * are the same as "stand-alone" units, but are different in some languages.
223 * "Formatting" units are declined according to the rules of the language
224 * in the context of a date. "Stand-alone" units are always nominative singular.
225 * In `format` function, they will produce different result:
226 *
227 * `format(new Date(2017, 10, 6), 'do LLLL', {locale: cs}) //=> '6. listopad'`
228 *
229 * `format(new Date(2017, 10, 6), 'do MMMM', {locale: cs}) //=> '6. listopadu'`
230 *
231 * `parse` will try to match both formatting and stand-alone units interchangably.
232 *
233 * 2. Any sequence of the identical letters is a pattern, unless it is escaped by
234 * the single quote characters (see below).
235 * If the sequence is longer than listed in table:
236 * - for numerical units (`yyyyyyyy`) `parse` will try to match a number
237 * as wide as the sequence
238 * - for text units (`MMMMMMMM`) `parse` will try to match the widest variation of the unit.
239 * These variations are marked with "2" in the last column of the table.
240 *
241 * 3. `QQQQQ` and `qqqqq` could be not strictly numerical in some locales.
242 * These tokens represent the shortest form of the quarter.
243 *
244 * 4. The main difference between `y` and `u` patterns are B.C. years:
245 *
246 * | Year | `y` | `u` |
247 * |------|-----|-----|
248 * | AC 1 | 1 | 1 |
249 * | BC 1 | 1 | 0 |
250 * | BC 2 | 2 | -1 |
251 *
252 * Also `yy` will try to guess the century of two digit year by proximity with `referenceDate`:
253 *
254 * `parse('50', 'yy', new Date(2018, 0, 1)) //=> Sat Jan 01 2050 00:00:00`
255 *
256 * `parse('75', 'yy', new Date(2018, 0, 1)) //=> Wed Jan 01 1975 00:00:00`
257 *
258 * while `uu` will just assign the year as is:
259 *
260 * `parse('50', 'uu', new Date(2018, 0, 1)) //=> Sat Jan 01 0050 00:00:00`
261 *
262 * `parse('75', 'uu', new Date(2018, 0, 1)) //=> Tue Jan 01 0075 00:00:00`
263 *
264 * The same difference is true for local and ISO week-numbering years (`Y` and `R`),
265 * except local week-numbering years are dependent on `options.weekStartsOn`
266 * and `options.firstWeekContainsDate` (compare [setISOWeekYear](https://date-fns.org/docs/setISOWeekYear)
267 * and [setWeekYear](https://date-fns.org/docs/setWeekYear)).
268 *
269 * 5. These patterns are not in the Unicode Technical Standard #35:
270 * - `i`: ISO day of week
271 * - `I`: ISO week of year
272 * - `R`: ISO week-numbering year
273 * - `o`: ordinal number modifier
274 * - `P`: long localized date
275 * - `p`: long localized time
276 *
277 * 6. `YY` and `YYYY` tokens represent week-numbering years but they are often confused with years.
278 * You should enable `options.useAdditionalWeekYearTokens` to use them. See: https://github.com/date-fns/date-fns/blob/master/docs/unicodeTokens.md
279 *
280 * 7. `D` and `DD` tokens represent days of the year but they are ofthen confused with days of the month.
281 * You should enable `options.useAdditionalDayOfYearTokens` to use them. See: https://github.com/date-fns/date-fns/blob/master/docs/unicodeTokens.md
282 *
283 * 8. `P+` tokens do not have a defined priority since they are merely aliases to other tokens based
284 * on the given locale.
285 *
286 * using `en-US` locale: `P` => `MM/dd/yyyy`
287 * using `en-US` locale: `p` => `hh:mm a`
288 * using `pt-BR` locale: `P` => `dd/MM/yyyy`
289 * using `pt-BR` locale: `p` => `HH:mm`
290 *
291 * Values will be assigned to the date in the descending order of its unit's priority.
292 * Units of an equal priority overwrite each other in the order of appearance.
293 *
294 * If no values of higher priority are parsed (e.g. when parsing string 'January 1st' without a year),
295 * the values will be taken from 3rd argument `referenceDate` which works as a context of parsing.
296 *
297 * `referenceDate` must be passed for correct work of the function.
298 * If you're not sure which `referenceDate` to supply, create a new instance of Date:
299 * `parse('02/11/2014', 'MM/dd/yyyy', new Date())`
300 * In this case parsing will be done in the context of the current date.
301 * If `referenceDate` is `Invalid Date` or a value not convertible to valid `Date`,
302 * then `Invalid Date` will be returned.
303 *
304 * The result may vary by locale.
305 *
306 * If `formatString` matches with `dateString` but does not provides tokens, `referenceDate` will be returned.
307 *
308 * If parsing failed, `Invalid Date` will be returned.
309 * Invalid Date is a Date, whose time value is NaN.
310 * Time value of Date: http://es5.github.io/#x15.9.1.1
311 *
312 * @typeParam DateType - The `Date` type, the function operates on. Gets inferred from passed arguments. Allows to use extensions like [`UTCDate`](https://github.com/date-fns/utc).
313 *
314 * @param dateStr - The string to parse
315 * @param formatStr - The string of tokens
316 * @param referenceDate - defines values missing from the parsed dateString
317 * @param options - An object with options.
318 * see: https://github.com/date-fns/date-fns/blob/master/docs/unicodeTokens.md
319 * see: https://github.com/date-fns/date-fns/blob/master/docs/unicodeTokens.md
320 *
321 * @returns The parsed date
322 *
323 * @throws `options.locale` must contain `match` property
324 * @throws use `yyyy` instead of `YYYY` for formatting years using [format provided] to the input [input provided]; see: https://github.com/date-fns/date-fns/blob/master/docs/unicodeTokens.md
325 * @throws use `yy` instead of `YY` for formatting years using [format provided] to the input [input provided]; see: https://github.com/date-fns/date-fns/blob/master/docs/unicodeTokens.md
326 * @throws use `d` instead of `D` for formatting days of the month using [format provided] to the input [input provided]; see: https://github.com/date-fns/date-fns/blob/master/docs/unicodeTokens.md
327 * @throws use `dd` instead of `DD` for formatting days of the month using [format provided] to the input [input provided]; see: https://github.com/date-fns/date-fns/blob/master/docs/unicodeTokens.md
328 * @throws format string contains an unescaped latin alphabet character
329 *
330 * @example
331 * // Parse 11 February 2014 from middle-endian format:
332 * var result = parse('02/11/2014', 'MM/dd/yyyy', new Date())
333 * //=> Tue Feb 11 2014 00:00:00
334 *
335 * @example
336 * // Parse 28th of February in Esperanto locale in the context of 2010 year:
337 * import eo from 'date-fns/locale/eo'
338 * var result = parse('28-a de februaro', "do 'de' MMMM", new Date(2010, 0, 1), {
339 * locale: eo
340 * })
341 * //=> Sun Feb 28 2010 00:00:00
342 */
343export function parse(dateStr, formatStr, referenceDate, options) {
344 const defaultOptions = getDefaultOptions();
345 const locale = options?.locale ?? defaultOptions.locale ?? defaultLocale;
346
347 const firstWeekContainsDate =
348 options?.firstWeekContainsDate ??
349 options?.locale?.options?.firstWeekContainsDate ??
350 defaultOptions.firstWeekContainsDate ??
351 defaultOptions.locale?.options?.firstWeekContainsDate ??
352 1;
353
354 const weekStartsOn =
355 options?.weekStartsOn ??
356 options?.locale?.options?.weekStartsOn ??
357 defaultOptions.weekStartsOn ??
358 defaultOptions.locale?.options?.weekStartsOn ??
359 0;
360
361 if (formatStr === "") {
362 if (dateStr === "") {
363 return toDate(referenceDate);
364 } else {
365 return constructFrom(referenceDate, NaN);
366 }
367 }
368
369 const subFnOptions = {
370 firstWeekContainsDate,
371 weekStartsOn,
372 locale,
373 };
374
375 // If timezone isn't specified, it will be set to the system timezone
376 const setters = [new DateToSystemTimezoneSetter()];
377
378 const tokens = formatStr
379 .match(longFormattingTokensRegExp)
380 .map((substring) => {
381 const firstCharacter = substring[0];
382 if (firstCharacter in longFormatters) {
383 const longFormatter = longFormatters[firstCharacter];
384 return longFormatter(substring, locale.formatLong);
385 }
386 return substring;
387 })
388 .join("")
389 .match(formattingTokensRegExp);
390
391 const usedTokens = [];
392
393 for (let token of tokens) {
394 if (
395 !options?.useAdditionalWeekYearTokens &&
396 isProtectedWeekYearToken(token)
397 ) {
398 warnOrThrowProtectedError(token, formatStr, dateStr);
399 }
400 if (
401 !options?.useAdditionalDayOfYearTokens &&
402 isProtectedDayOfYearToken(token)
403 ) {
404 warnOrThrowProtectedError(token, formatStr, dateStr);
405 }
406
407 const firstCharacter = token[0];
408 const parser = parsers[firstCharacter];
409 if (parser) {
410 const { incompatibleTokens } = parser;
411 if (Array.isArray(incompatibleTokens)) {
412 const incompatibleToken = usedTokens.find(
413 (usedToken) =>
414 incompatibleTokens.includes(usedToken.token) ||
415 usedToken.token === firstCharacter,
416 );
417 if (incompatibleToken) {
418 throw new RangeError(
419 `The format string mustn't contain \`${incompatibleToken.fullToken}\` and \`${token}\` at the same time`,
420 );
421 }
422 } else if (parser.incompatibleTokens === "*" && usedTokens.length > 0) {
423 throw new RangeError(
424 `The format string mustn't contain \`${token}\` and any other token at the same time`,
425 );
426 }
427
428 usedTokens.push({ token: firstCharacter, fullToken: token });
429
430 const parseResult = parser.run(
431 dateStr,
432 token,
433 locale.match,
434 subFnOptions,
435 );
436
437 if (!parseResult) {
438 return constructFrom(referenceDate, NaN);
439 }
440
441 setters.push(parseResult.setter);
442
443 dateStr = parseResult.rest;
444 } else {
445 if (firstCharacter.match(unescapedLatinCharacterRegExp)) {
446 throw new RangeError(
447 "Format string contains an unescaped latin alphabet character `" +
448 firstCharacter +
449 "`",
450 );
451 }
452
453 // Replace two single quote characters with one single quote character
454 if (token === "''") {
455 token = "'";
456 } else if (firstCharacter === "'") {
457 token = cleanEscapedString(token);
458 }
459
460 // Cut token from string, or, if string doesn't match the token, return Invalid Date
461 if (dateStr.indexOf(token) === 0) {
462 dateStr = dateStr.slice(token.length);
463 } else {
464 return constructFrom(referenceDate, NaN);
465 }
466 }
467 }
468
469 // Check if the remaining input contains something other than whitespace
470 if (dateStr.length > 0 && notWhitespaceRegExp.test(dateStr)) {
471 return constructFrom(referenceDate, NaN);
472 }
473
474 const uniquePrioritySetters = setters
475 .map((setter) => setter.priority)
476 .sort((a, b) => b - a)
477 .filter((priority, index, array) => array.indexOf(priority) === index)
478 .map((priority) =>
479 setters
480 .filter((setter) => setter.priority === priority)
481 .sort((a, b) => b.subPriority - a.subPriority),
482 )
483 .map((setterArray) => setterArray[0]);
484
485 let date = toDate(referenceDate);
486
487 if (isNaN(date.getTime())) {
488 return constructFrom(referenceDate, NaN);
489 }
490
491 const flags = {};
492 for (const setter of uniquePrioritySetters) {
493 if (!setter.validate(date, subFnOptions)) {
494 return constructFrom(referenceDate, NaN);
495 }
496
497 const result = setter.set(date, flags, subFnOptions);
498 // Result is tuple (date, flags)
499 if (Array.isArray(result)) {
500 date = result[0];
501 Object.assign(flags, result[1]);
502 // Result is date
503 } else {
504 date = result;
505 }
506 }
507
508 return constructFrom(referenceDate, date);
509}
510
511function cleanEscapedString(input) {
512 return input.match(escapedStringRegExp)[1].replace(doubleQuoteRegExp, "'");
513}
514
515// Fallback for modularized imports:
516export default parse;