1 | ;
|
2 |
|
3 | Object.defineProperty(exports, "__esModule", {
|
4 | value: true
|
5 | });
|
6 | exports.default = lightFormat;
|
7 |
|
8 | var _index = _interopRequireDefault(require("../toDate/index.js"));
|
9 |
|
10 | var _index2 = _interopRequireDefault(require("../_lib/format/lightFormatters/index.js"));
|
11 |
|
12 | var _index3 = _interopRequireDefault(require("../_lib/getTimezoneOffsetInMilliseconds/index.js"));
|
13 |
|
14 | var _index4 = _interopRequireDefault(require("../isValid/index.js"));
|
15 |
|
16 | var _index5 = _interopRequireDefault(require("../subMilliseconds/index.js"));
|
17 |
|
18 | var _index6 = _interopRequireDefault(require("../_lib/requiredArgs/index.js"));
|
19 |
|
20 | function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
21 |
|
22 | // This RegExp consists of three parts separated by `|`:
|
23 | // - (\w)\1* matches any sequences of the same letter
|
24 | // - '' matches two quote characters in a row
|
25 | // - '(''|[^'])+('|$) matches anything surrounded by two quote characters ('),
|
26 | // except a single quote symbol, which ends the sequence.
|
27 | // Two quote characters do not end the sequence.
|
28 | // If there is no matching single quote
|
29 | // then the sequence will continue until the end of the string.
|
30 | // - . matches any single character unmatched by previous parts of the RegExps
|
31 | var formattingTokensRegExp = /(\w)\1*|''|'(''|[^'])+('|$)|./g;
|
32 | var escapedStringRegExp = /^'([^]*?)'?$/;
|
33 | var doubleQuoteRegExp = /''/g;
|
34 | var unescapedLatinCharacterRegExp = /[a-zA-Z]/;
|
35 | /**
|
36 | * @name lightFormat
|
37 | * @category Common Helpers
|
38 | * @summary Format the date.
|
39 | *
|
40 | * @description
|
41 | * Return the formatted date string in the given format. Unlike `format`,
|
42 | * `lightFormat` doesn't use locales and outputs date using the most popular tokens.
|
43 | *
|
44 | * > ⚠️ Please note that the `lightFormat` tokens differ from Moment.js and other libraries.
|
45 | * > See: https://git.io/fxCyr
|
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 | *
|
50 | * Format of the string is based on Unicode Technical Standard #35:
|
51 | * https://www.unicode.org/reports/tr35/tr35-dates.html#Date_Field_Symbol_Table
|
52 | *
|
53 | * Accepted patterns:
|
54 | * | Unit | Pattern | Result examples |
|
55 | * |---------------------------------|---------|-----------------------------------|
|
56 | * | AM, PM | a..aaa | AM, PM |
|
57 | * | | aaaa | a.m., p.m. |
|
58 | * | | aaaaa | a, p |
|
59 | * | Calendar year | y | 44, 1, 1900, 2017 |
|
60 | * | | yy | 44, 01, 00, 17 |
|
61 | * | | yyy | 044, 001, 000, 017 |
|
62 | * | | yyyy | 0044, 0001, 1900, 2017 |
|
63 | * | Month (formatting) | M | 1, 2, ..., 12 |
|
64 | * | | MM | 01, 02, ..., 12 |
|
65 | * | Day of month | d | 1, 2, ..., 31 |
|
66 | * | | dd | 01, 02, ..., 31 |
|
67 | * | Hour [1-12] | h | 1, 2, ..., 11, 12 |
|
68 | * | | hh | 01, 02, ..., 11, 12 |
|
69 | * | Hour [0-23] | H | 0, 1, 2, ..., 23 |
|
70 | * | | HH | 00, 01, 02, ..., 23 |
|
71 | * | Minute | m | 0, 1, ..., 59 |
|
72 | * | | mm | 00, 01, ..., 59 |
|
73 | * | Second | s | 0, 1, ..., 59 |
|
74 | * | | ss | 00, 01, ..., 59 |
|
75 | * | Fraction of second | S | 0, 1, ..., 9 |
|
76 | * | | SS | 00, 01, ..., 99 |
|
77 | * | | SSS | 000, 0001, ..., 999 |
|
78 | * | | SSSS | ... |
|
79 | *
|
80 | * @param {Date|Number} date - the original date
|
81 | * @param {String} format - the string of tokens
|
82 | * @returns {String} the formatted date string
|
83 | * @throws {TypeError} 2 arguments required
|
84 | * @throws {RangeError} format string contains an unescaped latin alphabet character
|
85 | *
|
86 | * @example
|
87 | * var result = lightFormat(new Date(2014, 1, 11), 'yyyy-MM-dd')
|
88 | * //=> '1987-02-11'
|
89 | */
|
90 |
|
91 | function lightFormat(dirtyDate, dirtyFormatStr) {
|
92 | (0, _index6.default)(2, arguments);
|
93 | var formatStr = String(dirtyFormatStr);
|
94 | var originalDate = (0, _index.default)(dirtyDate);
|
95 |
|
96 | if (!(0, _index4.default)(originalDate)) {
|
97 | throw new RangeError('Invalid time value');
|
98 | } // Convert the date in system timezone to the same date in UTC+00:00 timezone.
|
99 | // This ensures that when UTC functions will be implemented, locales will be compatible with them.
|
100 | // See an issue about UTC functions: https://github.com/date-fns/date-fns/issues/376
|
101 |
|
102 |
|
103 | var timezoneOffset = (0, _index3.default)(originalDate);
|
104 | var utcDate = (0, _index5.default)(originalDate, timezoneOffset);
|
105 | var result = formatStr.match(formattingTokensRegExp).map(function (substring) {
|
106 | // Replace two single quote characters with one single quote character
|
107 | if (substring === "''") {
|
108 | return "'";
|
109 | }
|
110 |
|
111 | var firstCharacter = substring[0];
|
112 |
|
113 | if (firstCharacter === "'") {
|
114 | return cleanEscapedString(substring);
|
115 | }
|
116 |
|
117 | var formatter = _index2.default[firstCharacter];
|
118 |
|
119 | if (formatter) {
|
120 | return formatter(utcDate, substring, null, {});
|
121 | }
|
122 |
|
123 | if (firstCharacter.match(unescapedLatinCharacterRegExp)) {
|
124 | throw new RangeError('Format string contains an unescaped latin alphabet character `' + firstCharacter + '`');
|
125 | }
|
126 |
|
127 | return substring;
|
128 | }).join('');
|
129 | return result;
|
130 | }
|
131 |
|
132 | function cleanEscapedString(input) {
|
133 | return input.match(escapedStringRegExp)[1].replace(doubleQuoteRegExp, "'");
|
134 | }
|
135 |
|
136 | module.exports = exports.default; |
\ | No newline at end of file |