UNPKG

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