index.js 33 KB

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