index.js 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411
  1. "use strict";
  2. var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault").default;
  3. Object.defineProperty(exports, "__esModule", {
  4. value: true
  5. });
  6. exports.default = format;
  7. var _index = _interopRequireDefault(require("../isValid/index.js"));
  8. var _index2 = _interopRequireDefault(require("../subMilliseconds/index.js"));
  9. var _index3 = _interopRequireDefault(require("../toDate/index.js"));
  10. var _index4 = _interopRequireDefault(require("../_lib/format/formatters/index.js"));
  11. var _index5 = _interopRequireDefault(require("../_lib/format/longFormatters/index.js"));
  12. var _index6 = _interopRequireDefault(require("../_lib/getTimezoneOffsetInMilliseconds/index.js"));
  13. var _index7 = require("../_lib/protectedTokens/index.js");
  14. var _index8 = _interopRequireDefault(require("../_lib/toInteger/index.js"));
  15. var _index9 = _interopRequireDefault(require("../_lib/requiredArgs/index.js"));
  16. var _index10 = require("../_lib/defaultOptions/index.js");
  17. var _index11 = _interopRequireDefault(require("../_lib/defaultLocale/index.js"));
  18. // This RegExp consists of three parts separated by `|`:
  19. // - [yYQqMLwIdDecihHKkms]o matches any available ordinal number token
  20. // (one of the certain letters followed by `o`)
  21. // - (\w)\1* matches any sequences of the same letter
  22. // - '' matches two quote characters in a row
  23. // - '(''|[^'])+('|$) matches anything surrounded by two quote characters ('),
  24. // except a single quote symbol, which ends the sequence.
  25. // Two quote characters do not end the sequence.
  26. // If there is no matching single quote
  27. // then the sequence will continue until the end of the string.
  28. // - . matches any single character unmatched by previous parts of the RegExps
  29. var formattingTokensRegExp = /[yYQqMLwIdDecihHKkms]o|(\w)\1*|''|'(''|[^'])+('|$)|./g;
  30. // This RegExp catches symbols escaped by quotes, and also
  31. // sequences of symbols P, p, and the combinations like `PPPPPPPppppp`
  32. var longFormattingTokensRegExp = /P+p+|P+|p+|''|'(''|[^'])+('|$)|./g;
  33. var escapedStringRegExp = /^'([^]*?)'?$/;
  34. var doubleQuoteRegExp = /''/g;
  35. var unescapedLatinCharacterRegExp = /[a-zA-Z]/;
  36. /**
  37. * @name format
  38. * @category Common Helpers
  39. * @summary Format the date.
  40. *
  41. * @description
  42. * Return the formatted date string in the given format. The result may vary by locale.
  43. *
  44. * > ⚠️ Please note that the `format` tokens differ from Moment.js and other libraries.
  45. * > See: https://github.com/date-fns/date-fns/blob/master/docs/unicodeTokens.md
  46. *
  47. * The characters wrapped between two single quotes characters (') are escaped.
  48. * Two single quotes in a row, whether inside or outside a quoted sequence, represent a 'real' single quote.
  49. * (see the last example)
  50. *
  51. * Format of the string is based on Unicode Technical Standard #35:
  52. * https://www.unicode.org/reports/tr35/tr35-dates.html#Date_Field_Symbol_Table
  53. * with a few additions (see note 7 below the table).
  54. *
  55. * Accepted patterns:
  56. * | Unit | Pattern | Result examples | Notes |
  57. * |---------------------------------|---------|-----------------------------------|-------|
  58. * | Era | G..GGG | AD, BC | |
  59. * | | GGGG | Anno Domini, Before Christ | 2 |
  60. * | | GGGGG | A, B | |
  61. * | Calendar year | y | 44, 1, 1900, 2017 | 5 |
  62. * | | yo | 44th, 1st, 0th, 17th | 5,7 |
  63. * | | yy | 44, 01, 00, 17 | 5 |
  64. * | | yyy | 044, 001, 1900, 2017 | 5 |
  65. * | | yyyy | 0044, 0001, 1900, 2017 | 5 |
  66. * | | yyyyy | ... | 3,5 |
  67. * | Local week-numbering year | Y | 44, 1, 1900, 2017 | 5 |
  68. * | | Yo | 44th, 1st, 1900th, 2017th | 5,7 |
  69. * | | YY | 44, 01, 00, 17 | 5,8 |
  70. * | | YYY | 044, 001, 1900, 2017 | 5 |
  71. * | | YYYY | 0044, 0001, 1900, 2017 | 5,8 |
  72. * | | YYYYY | ... | 3,5 |
  73. * | ISO week-numbering year | R | -43, 0, 1, 1900, 2017 | 5,7 |
  74. * | | RR | -43, 00, 01, 1900, 2017 | 5,7 |
  75. * | | RRR | -043, 000, 001, 1900, 2017 | 5,7 |
  76. * | | RRRR | -0043, 0000, 0001, 1900, 2017 | 5,7 |
  77. * | | RRRRR | ... | 3,5,7 |
  78. * | Extended year | u | -43, 0, 1, 1900, 2017 | 5 |
  79. * | | uu | -43, 01, 1900, 2017 | 5 |
  80. * | | uuu | -043, 001, 1900, 2017 | 5 |
  81. * | | uuuu | -0043, 0001, 1900, 2017 | 5 |
  82. * | | uuuuu | ... | 3,5 |
  83. * | Quarter (formatting) | Q | 1, 2, 3, 4 | |
  84. * | | Qo | 1st, 2nd, 3rd, 4th | 7 |
  85. * | | QQ | 01, 02, 03, 04 | |
  86. * | | QQQ | Q1, Q2, Q3, Q4 | |
  87. * | | QQQQ | 1st quarter, 2nd quarter, ... | 2 |
  88. * | | QQQQQ | 1, 2, 3, 4 | 4 |
  89. * | Quarter (stand-alone) | q | 1, 2, 3, 4 | |
  90. * | | qo | 1st, 2nd, 3rd, 4th | 7 |
  91. * | | qq | 01, 02, 03, 04 | |
  92. * | | qqq | Q1, Q2, Q3, Q4 | |
  93. * | | qqqq | 1st quarter, 2nd quarter, ... | 2 |
  94. * | | qqqqq | 1, 2, 3, 4 | 4 |
  95. * | Month (formatting) | M | 1, 2, ..., 12 | |
  96. * | | Mo | 1st, 2nd, ..., 12th | 7 |
  97. * | | MM | 01, 02, ..., 12 | |
  98. * | | MMM | Jan, Feb, ..., Dec | |
  99. * | | MMMM | January, February, ..., December | 2 |
  100. * | | MMMMM | J, F, ..., D | |
  101. * | Month (stand-alone) | L | 1, 2, ..., 12 | |
  102. * | | Lo | 1st, 2nd, ..., 12th | 7 |
  103. * | | LL | 01, 02, ..., 12 | |
  104. * | | LLL | Jan, Feb, ..., Dec | |
  105. * | | LLLL | January, February, ..., December | 2 |
  106. * | | LLLLL | J, F, ..., D | |
  107. * | Local week of year | w | 1, 2, ..., 53 | |
  108. * | | wo | 1st, 2nd, ..., 53th | 7 |
  109. * | | ww | 01, 02, ..., 53 | |
  110. * | ISO week of year | I | 1, 2, ..., 53 | 7 |
  111. * | | Io | 1st, 2nd, ..., 53th | 7 |
  112. * | | II | 01, 02, ..., 53 | 7 |
  113. * | Day of month | d | 1, 2, ..., 31 | |
  114. * | | do | 1st, 2nd, ..., 31st | 7 |
  115. * | | dd | 01, 02, ..., 31 | |
  116. * | Day of year | D | 1, 2, ..., 365, 366 | 9 |
  117. * | | Do | 1st, 2nd, ..., 365th, 366th | 7 |
  118. * | | DD | 01, 02, ..., 365, 366 | 9 |
  119. * | | DDD | 001, 002, ..., 365, 366 | |
  120. * | | DDDD | ... | 3 |
  121. * | Day of week (formatting) | E..EEE | Mon, Tue, Wed, ..., Sun | |
  122. * | | EEEE | Monday, Tuesday, ..., Sunday | 2 |
  123. * | | EEEEE | M, T, W, T, F, S, S | |
  124. * | | EEEEEE | Mo, Tu, We, Th, Fr, Sa, Su | |
  125. * | ISO day of week (formatting) | i | 1, 2, 3, ..., 7 | 7 |
  126. * | | io | 1st, 2nd, ..., 7th | 7 |
  127. * | | ii | 01, 02, ..., 07 | 7 |
  128. * | | iii | Mon, Tue, Wed, ..., Sun | 7 |
  129. * | | iiii | Monday, Tuesday, ..., Sunday | 2,7 |
  130. * | | iiiii | M, T, W, T, F, S, S | 7 |
  131. * | | iiiiii | Mo, Tu, We, Th, Fr, Sa, Su | 7 |
  132. * | Local day of week (formatting) | e | 2, 3, 4, ..., 1 | |
  133. * | | eo | 2nd, 3rd, ..., 1st | 7 |
  134. * | | ee | 02, 03, ..., 01 | |
  135. * | | eee | Mon, Tue, Wed, ..., Sun | |
  136. * | | eeee | Monday, Tuesday, ..., Sunday | 2 |
  137. * | | eeeee | M, T, W, T, F, S, S | |
  138. * | | eeeeee | Mo, Tu, We, Th, Fr, Sa, Su | |
  139. * | Local day of week (stand-alone) | c | 2, 3, 4, ..., 1 | |
  140. * | | co | 2nd, 3rd, ..., 1st | 7 |
  141. * | | cc | 02, 03, ..., 01 | |
  142. * | | ccc | Mon, Tue, Wed, ..., Sun | |
  143. * | | cccc | Monday, Tuesday, ..., Sunday | 2 |
  144. * | | ccccc | M, T, W, T, F, S, S | |
  145. * | | cccccc | Mo, Tu, We, Th, Fr, Sa, Su | |
  146. * | AM, PM | a..aa | AM, PM | |
  147. * | | aaa | am, pm | |
  148. * | | aaaa | a.m., p.m. | 2 |
  149. * | | aaaaa | a, p | |
  150. * | AM, PM, noon, midnight | b..bb | AM, PM, noon, midnight | |
  151. * | | bbb | am, pm, noon, midnight | |
  152. * | | bbbb | a.m., p.m., noon, midnight | 2 |
  153. * | | bbbbb | a, p, n, mi | |
  154. * | Flexible day period | B..BBB | at night, in the morning, ... | |
  155. * | | BBBB | at night, in the morning, ... | 2 |
  156. * | | BBBBB | at night, in the morning, ... | |
  157. * | Hour [1-12] | h | 1, 2, ..., 11, 12 | |
  158. * | | ho | 1st, 2nd, ..., 11th, 12th | 7 |
  159. * | | hh | 01, 02, ..., 11, 12 | |
  160. * | Hour [0-23] | H | 0, 1, 2, ..., 23 | |
  161. * | | Ho | 0th, 1st, 2nd, ..., 23rd | 7 |
  162. * | | HH | 00, 01, 02, ..., 23 | |
  163. * | Hour [0-11] | K | 1, 2, ..., 11, 0 | |
  164. * | | Ko | 1st, 2nd, ..., 11th, 0th | 7 |
  165. * | | KK | 01, 02, ..., 11, 00 | |
  166. * | Hour [1-24] | k | 24, 1, 2, ..., 23 | |
  167. * | | ko | 24th, 1st, 2nd, ..., 23rd | 7 |
  168. * | | kk | 24, 01, 02, ..., 23 | |
  169. * | Minute | m | 0, 1, ..., 59 | |
  170. * | | mo | 0th, 1st, ..., 59th | 7 |
  171. * | | mm | 00, 01, ..., 59 | |
  172. * | Second | s | 0, 1, ..., 59 | |
  173. * | | so | 0th, 1st, ..., 59th | 7 |
  174. * | | ss | 00, 01, ..., 59 | |
  175. * | Fraction of second | S | 0, 1, ..., 9 | |
  176. * | | SS | 00, 01, ..., 99 | |
  177. * | | SSS | 000, 001, ..., 999 | |
  178. * | | SSSS | ... | 3 |
  179. * | Timezone (ISO-8601 w/ Z) | X | -08, +0530, Z | |
  180. * | | XX | -0800, +0530, Z | |
  181. * | | XXX | -08:00, +05:30, Z | |
  182. * | | XXXX | -0800, +0530, Z, +123456 | 2 |
  183. * | | XXXXX | -08:00, +05:30, Z, +12:34:56 | |
  184. * | Timezone (ISO-8601 w/o Z) | x | -08, +0530, +00 | |
  185. * | | xx | -0800, +0530, +0000 | |
  186. * | | xxx | -08:00, +05:30, +00:00 | 2 |
  187. * | | xxxx | -0800, +0530, +0000, +123456 | |
  188. * | | xxxxx | -08:00, +05:30, +00:00, +12:34:56 | |
  189. * | Timezone (GMT) | O...OOO | GMT-8, GMT+5:30, GMT+0 | |
  190. * | | OOOO | GMT-08:00, GMT+05:30, GMT+00:00 | 2 |
  191. * | Timezone (specific non-locat.) | z...zzz | GMT-8, GMT+5:30, GMT+0 | 6 |
  192. * | | zzzz | GMT-08:00, GMT+05:30, GMT+00:00 | 2,6 |
  193. * | Seconds timestamp | t | 512969520 | 7 |
  194. * | | tt | ... | 3,7 |
  195. * | Milliseconds timestamp | T | 512969520900 | 7 |
  196. * | | TT | ... | 3,7 |
  197. * | Long localized date | P | 04/29/1453 | 7 |
  198. * | | PP | Apr 29, 1453 | 7 |
  199. * | | PPP | April 29th, 1453 | 7 |
  200. * | | PPPP | Friday, April 29th, 1453 | 2,7 |
  201. * | Long localized time | p | 12:00 AM | 7 |
  202. * | | pp | 12:00:00 AM | 7 |
  203. * | | ppp | 12:00:00 AM GMT+2 | 7 |
  204. * | | pppp | 12:00:00 AM GMT+02:00 | 2,7 |
  205. * | Combination of date and time | Pp | 04/29/1453, 12:00 AM | 7 |
  206. * | | PPpp | Apr 29, 1453, 12:00:00 AM | 7 |
  207. * | | PPPppp | April 29th, 1453 at ... | 7 |
  208. * | | PPPPpppp| Friday, April 29th, 1453 at ... | 2,7 |
  209. * Notes:
  210. * 1. "Formatting" units (e.g. formatting quarter) in the default en-US locale
  211. * are the same as "stand-alone" units, but are different in some languages.
  212. * "Formatting" units are declined according to the rules of the language
  213. * in the context of a date. "Stand-alone" units are always nominative singular:
  214. *
  215. * `format(new Date(2017, 10, 6), 'do LLLL', {locale: cs}) //=> '6. listopad'`
  216. *
  217. * `format(new Date(2017, 10, 6), 'do MMMM', {locale: cs}) //=> '6. listopadu'`
  218. *
  219. * 2. Any sequence of the identical letters is a pattern, unless it is escaped by
  220. * the single quote characters (see below).
  221. * If the sequence is longer than listed in table (e.g. `EEEEEEEEEEE`)
  222. * the output will be the same as default pattern for this unit, usually
  223. * the longest one (in case of ISO weekdays, `EEEE`). Default patterns for units
  224. * are marked with "2" in the last column of the table.
  225. *
  226. * `format(new Date(2017, 10, 6), 'MMM') //=> 'Nov'`
  227. *
  228. * `format(new Date(2017, 10, 6), 'MMMM') //=> 'November'`
  229. *
  230. * `format(new Date(2017, 10, 6), 'MMMMM') //=> 'N'`
  231. *
  232. * `format(new Date(2017, 10, 6), 'MMMMMM') //=> 'November'`
  233. *
  234. * `format(new Date(2017, 10, 6), 'MMMMMMM') //=> 'November'`
  235. *
  236. * 3. Some patterns could be unlimited length (such as `yyyyyyyy`).
  237. * The output will be padded with zeros to match the length of the pattern.
  238. *
  239. * `format(new Date(2017, 10, 6), 'yyyyyyyy') //=> '00002017'`
  240. *
  241. * 4. `QQQQQ` and `qqqqq` could be not strictly numerical in some locales.
  242. * These tokens represent the shortest form of the quarter.
  243. *
  244. * 5. 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` always returns the last two digits of a year,
  253. * while `uu` pads single digit years to 2 characters and returns other years unchanged:
  254. *
  255. * | Year | `yy` | `uu` |
  256. * |------|------|------|
  257. * | 1 | 01 | 01 |
  258. * | 14 | 14 | 14 |
  259. * | 376 | 76 | 376 |
  260. * | 1453 | 53 | 1453 |
  261. *
  262. * The same difference is true for local and ISO week-numbering years (`Y` and `R`),
  263. * except local week-numbering years are dependent on `options.weekStartsOn`
  264. * and `options.firstWeekContainsDate` (compare [getISOWeekYear]{@link https://date-fns.org/docs/getISOWeekYear}
  265. * and [getWeekYear]{@link https://date-fns.org/docs/getWeekYear}).
  266. *
  267. * 6. Specific non-location timezones are currently unavailable in `date-fns`,
  268. * so right now these tokens fall back to GMT timezones.
  269. *
  270. * 7. These patterns are not in the Unicode Technical Standard #35:
  271. * - `i`: ISO day of week
  272. * - `I`: ISO week of year
  273. * - `R`: ISO week-numbering year
  274. * - `t`: seconds timestamp
  275. * - `T`: milliseconds timestamp
  276. * - `o`: ordinal number modifier
  277. * - `P`: long localized date
  278. * - `p`: long localized time
  279. *
  280. * 8. `YY` and `YYYY` tokens represent week-numbering years but they are often confused with years.
  281. * You should enable `options.useAdditionalWeekYearTokens` to use them. See: https://github.com/date-fns/date-fns/blob/master/docs/unicodeTokens.md
  282. *
  283. * 9. `D` and `DD` tokens represent days of the year but they are often confused with days of the month.
  284. * You should enable `options.useAdditionalDayOfYearTokens` to use them. See: https://github.com/date-fns/date-fns/blob/master/docs/unicodeTokens.md
  285. *
  286. * @param {Date|Number} date - the original date
  287. * @param {String} format - the string of tokens
  288. * @param {Object} [options] - an object with options.
  289. * @param {Locale} [options.locale=defaultLocale] - the locale object. See [Locale]{@link https://date-fns.org/docs/Locale}
  290. * @param {0|1|2|3|4|5|6} [options.weekStartsOn=0] - the index of the first day of the week (0 - Sunday)
  291. * @param {Number} [options.firstWeekContainsDate=1] - the day of January, which is
  292. * @param {Boolean} [options.useAdditionalWeekYearTokens=false] - if true, allows usage of the week-numbering year tokens `YY` and `YYYY`;
  293. * see: https://github.com/date-fns/date-fns/blob/master/docs/unicodeTokens.md
  294. * @param {Boolean} [options.useAdditionalDayOfYearTokens=false] - if true, allows usage of the day of year tokens `D` and `DD`;
  295. * see: https://github.com/date-fns/date-fns/blob/master/docs/unicodeTokens.md
  296. * @returns {String} the formatted date string
  297. * @throws {TypeError} 2 arguments required
  298. * @throws {RangeError} `date` must not be Invalid Date
  299. * @throws {RangeError} `options.locale` must contain `localize` property
  300. * @throws {RangeError} `options.locale` must contain `formatLong` property
  301. * @throws {RangeError} `options.weekStartsOn` must be between 0 and 6
  302. * @throws {RangeError} `options.firstWeekContainsDate` must be between 1 and 7
  303. * @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
  304. * @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
  305. * @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
  306. * @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
  307. * @throws {RangeError} format string contains an unescaped latin alphabet character
  308. *
  309. * @example
  310. * // Represent 11 February 2014 in middle-endian format:
  311. * const result = format(new Date(2014, 1, 11), 'MM/dd/yyyy')
  312. * //=> '02/11/2014'
  313. *
  314. * @example
  315. * // Represent 2 July 2014 in Esperanto:
  316. * import { eoLocale } from 'date-fns/locale/eo'
  317. * const result = format(new Date(2014, 6, 2), "do 'de' MMMM yyyy", {
  318. * locale: eoLocale
  319. * })
  320. * //=> '2-a de julio 2014'
  321. *
  322. * @example
  323. * // Escape string by single quote characters:
  324. * const result = format(new Date(2014, 6, 2, 15), "h 'o''clock'")
  325. * //=> "3 o'clock"
  326. */
  327. function format(dirtyDate, dirtyFormatStr, options) {
  328. 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;
  329. (0, _index9.default)(2, arguments);
  330. var formatStr = String(dirtyFormatStr);
  331. var defaultOptions = (0, _index10.getDefaultOptions)();
  332. 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 : _index11.default;
  333. 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);
  334. // Test if weekStartsOn is between 1 and 7 _and_ is not NaN
  335. if (!(firstWeekContainsDate >= 1 && firstWeekContainsDate <= 7)) {
  336. throw new RangeError('firstWeekContainsDate must be between 1 and 7 inclusively');
  337. }
  338. 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);
  339. // Test if weekStartsOn is between 0 and 6 _and_ is not NaN
  340. if (!(weekStartsOn >= 0 && weekStartsOn <= 6)) {
  341. throw new RangeError('weekStartsOn must be between 0 and 6 inclusively');
  342. }
  343. if (!locale.localize) {
  344. throw new RangeError('locale must contain localize property');
  345. }
  346. if (!locale.formatLong) {
  347. throw new RangeError('locale must contain formatLong property');
  348. }
  349. var originalDate = (0, _index3.default)(dirtyDate);
  350. if (!(0, _index.default)(originalDate)) {
  351. throw new RangeError('Invalid time value');
  352. }
  353. // Convert the date in system timezone to the same date in UTC+00:00 timezone.
  354. // This ensures that when UTC functions will be implemented, locales will be compatible with them.
  355. // See an issue about UTC functions: https://github.com/date-fns/date-fns/issues/376
  356. var timezoneOffset = (0, _index6.default)(originalDate);
  357. var utcDate = (0, _index2.default)(originalDate, timezoneOffset);
  358. var formatterOptions = {
  359. firstWeekContainsDate: firstWeekContainsDate,
  360. weekStartsOn: weekStartsOn,
  361. locale: locale,
  362. _originalDate: originalDate
  363. };
  364. var result = formatStr.match(longFormattingTokensRegExp).map(function (substring) {
  365. var firstCharacter = substring[0];
  366. if (firstCharacter === 'p' || firstCharacter === 'P') {
  367. var longFormatter = _index5.default[firstCharacter];
  368. return longFormatter(substring, locale.formatLong);
  369. }
  370. return substring;
  371. }).join('').match(formattingTokensRegExp).map(function (substring) {
  372. // Replace two single quote characters with one single quote character
  373. if (substring === "''") {
  374. return "'";
  375. }
  376. var firstCharacter = substring[0];
  377. if (firstCharacter === "'") {
  378. return cleanEscapedString(substring);
  379. }
  380. var formatter = _index4.default[firstCharacter];
  381. if (formatter) {
  382. if (!(options !== null && options !== void 0 && options.useAdditionalWeekYearTokens) && (0, _index7.isProtectedWeekYearToken)(substring)) {
  383. (0, _index7.throwProtectedError)(substring, dirtyFormatStr, String(dirtyDate));
  384. }
  385. if (!(options !== null && options !== void 0 && options.useAdditionalDayOfYearTokens) && (0, _index7.isProtectedDayOfYearToken)(substring)) {
  386. (0, _index7.throwProtectedError)(substring, dirtyFormatStr, String(dirtyDate));
  387. }
  388. return formatter(utcDate, substring, locale.localize, formatterOptions);
  389. }
  390. if (firstCharacter.match(unescapedLatinCharacterRegExp)) {
  391. throw new RangeError('Format string contains an unescaped latin alphabet character `' + firstCharacter + '`');
  392. }
  393. return substring;
  394. }).join('');
  395. return result;
  396. }
  397. function cleanEscapedString(input) {
  398. var matched = input.match(escapedStringRegExp);
  399. if (!matched) {
  400. return input;
  401. }
  402. return matched[1].replace(doubleQuoteRegExp, "'");
  403. }
  404. module.exports = exports.default;