UNPKG

8.32 kBJavaScriptView Raw
1import { u } from '../localizedFormat/utils';
2var formattingTokens = /(\[[^[]*\])|([-_:/.,()\s]+)|(A|a|Q|YYYY|YY?|ww?|MM?M?M?|Do|DD?|hh?|HH?|mm?|ss?|S{1,3}|z|ZZ?)/g;
3var match1 = /\d/; // 0 - 9
4
5var match2 = /\d\d/; // 00 - 99
6
7var match3 = /\d{3}/; // 000 - 999
8
9var match4 = /\d{4}/; // 0000 - 9999
10
11var match1to2 = /\d\d?/; // 0 - 99
12
13var matchSigned = /[+-]?\d+/; // -inf - inf
14
15var matchOffset = /[+-]\d\d:?(\d\d)?|Z/; // +00:00 -00:00 +0000 or -0000 +00 or Z
16
17var matchWord = /\d*[^-_:/,()\s\d]+/; // Word
18
19var locale = {};
20
21var parseTwoDigitYear = function parseTwoDigitYear(input) {
22 input = +input;
23 return input + (input > 68 ? 1900 : 2000);
24};
25
26function offsetFromString(string) {
27 if (!string) return 0;
28 if (string === 'Z') return 0;
29 var parts = string.match(/([+-]|\d\d)/g);
30 var minutes = +(parts[1] * 60) + (+parts[2] || 0);
31 return minutes === 0 ? 0 : parts[0] === '+' ? -minutes : minutes; // eslint-disable-line no-nested-ternary
32}
33
34var addInput = function addInput(property) {
35 return function (input) {
36 this[property] = +input;
37 };
38};
39
40var zoneExpressions = [matchOffset, function (input) {
41 var zone = this.zone || (this.zone = {});
42 zone.offset = offsetFromString(input);
43}];
44
45var getLocalePart = function getLocalePart(name) {
46 var part = locale[name];
47 return part && (part.indexOf ? part : part.s.concat(part.f));
48};
49
50var meridiemMatch = function meridiemMatch(input, isLowerCase) {
51 var isAfternoon;
52 var _locale = locale,
53 meridiem = _locale.meridiem;
54
55 if (!meridiem) {
56 isAfternoon = input === (isLowerCase ? 'pm' : 'PM');
57 } else {
58 for (var i = 1; i <= 24; i += 1) {
59 // todo: fix input === meridiem(i, 0, isLowerCase)
60 if (input.indexOf(meridiem(i, 0, isLowerCase)) > -1) {
61 isAfternoon = i > 12;
62 break;
63 }
64 }
65 }
66
67 return isAfternoon;
68};
69
70var expressions = {
71 A: [matchWord, function (input) {
72 this.afternoon = meridiemMatch(input, false);
73 }],
74 a: [matchWord, function (input) {
75 this.afternoon = meridiemMatch(input, true);
76 }],
77 Q: [match1, function (input) {
78 this.month = (input - 1) * 3 + 1;
79 }],
80 S: [match1, function (input) {
81 this.milliseconds = +input * 100;
82 }],
83 SS: [match2, function (input) {
84 this.milliseconds = +input * 10;
85 }],
86 SSS: [match3, function (input) {
87 this.milliseconds = +input;
88 }],
89 s: [match1to2, addInput('seconds')],
90 ss: [match1to2, addInput('seconds')],
91 m: [match1to2, addInput('minutes')],
92 mm: [match1to2, addInput('minutes')],
93 H: [match1to2, addInput('hours')],
94 h: [match1to2, addInput('hours')],
95 HH: [match1to2, addInput('hours')],
96 hh: [match1to2, addInput('hours')],
97 D: [match1to2, addInput('day')],
98 DD: [match2, addInput('day')],
99 Do: [matchWord, function (input) {
100 var _locale2 = locale,
101 ordinal = _locale2.ordinal;
102
103 var _input$match = input.match(/\d+/);
104
105 this.day = _input$match[0];
106 if (!ordinal) return;
107
108 for (var i = 1; i <= 31; i += 1) {
109 if (ordinal(i).replace(/\[|\]/g, '') === input) {
110 this.day = i;
111 }
112 }
113 }],
114 w: [match1to2, addInput('week')],
115 ww: [match2, addInput('week')],
116 M: [match1to2, addInput('month')],
117 MM: [match2, addInput('month')],
118 MMM: [matchWord, function (input) {
119 var months = getLocalePart('months');
120 var monthsShort = getLocalePart('monthsShort');
121 var matchIndex = (monthsShort || months.map(function (_) {
122 return _.slice(0, 3);
123 })).indexOf(input) + 1;
124
125 if (matchIndex < 1) {
126 throw new Error();
127 }
128
129 this.month = matchIndex % 12 || matchIndex;
130 }],
131 MMMM: [matchWord, function (input) {
132 var months = getLocalePart('months');
133 var matchIndex = months.indexOf(input) + 1;
134
135 if (matchIndex < 1) {
136 throw new Error();
137 }
138
139 this.month = matchIndex % 12 || matchIndex;
140 }],
141 Y: [matchSigned, addInput('year')],
142 YY: [match2, function (input) {
143 this.year = parseTwoDigitYear(input);
144 }],
145 YYYY: [match4, addInput('year')],
146 Z: zoneExpressions,
147 ZZ: zoneExpressions
148};
149
150function correctHours(time) {
151 var afternoon = time.afternoon;
152
153 if (afternoon !== undefined) {
154 var hours = time.hours;
155
156 if (afternoon) {
157 if (hours < 12) {
158 time.hours += 12;
159 }
160 } else if (hours === 12) {
161 time.hours = 0;
162 }
163
164 delete time.afternoon;
165 }
166}
167
168function makeParser(format) {
169 format = u(format, locale && locale.formats);
170 var array = format.match(formattingTokens);
171 var length = array.length;
172
173 for (var i = 0; i < length; i += 1) {
174 var token = array[i];
175 var parseTo = expressions[token];
176 var regex = parseTo && parseTo[0];
177 var parser = parseTo && parseTo[1];
178
179 if (parser) {
180 array[i] = {
181 regex: regex,
182 parser: parser
183 };
184 } else {
185 array[i] = token.replace(/^\[|\]$/g, '');
186 }
187 }
188
189 return function (input) {
190 var time = {};
191
192 for (var _i = 0, start = 0; _i < length; _i += 1) {
193 var _token = array[_i];
194
195 if (typeof _token === 'string') {
196 start += _token.length;
197 } else {
198 var _regex = _token.regex,
199 _parser = _token.parser;
200 var part = input.slice(start);
201
202 var match = _regex.exec(part);
203
204 var value = match[0];
205
206 _parser.call(time, value);
207
208 input = input.replace(value, '');
209 }
210 }
211
212 correctHours(time);
213 return time;
214 };
215}
216
217var parseFormattedInput = function parseFormattedInput(input, format, utc, dayjs) {
218 try {
219 if (['x', 'X'].indexOf(format) > -1) return new Date((format === 'X' ? 1000 : 1) * input);
220 var parser = makeParser(format);
221
222 var _parser2 = parser(input),
223 year = _parser2.year,
224 month = _parser2.month,
225 day = _parser2.day,
226 hours = _parser2.hours,
227 minutes = _parser2.minutes,
228 seconds = _parser2.seconds,
229 milliseconds = _parser2.milliseconds,
230 zone = _parser2.zone,
231 week = _parser2.week;
232
233 var now = new Date();
234 var d = day || (!year && !month ? now.getDate() : 1);
235 var y = year || now.getFullYear();
236 var M = 0;
237
238 if (!(year && !month)) {
239 M = month > 0 ? month - 1 : now.getMonth();
240 }
241
242 var h = hours || 0;
243 var m = minutes || 0;
244 var s = seconds || 0;
245 var ms = milliseconds || 0;
246
247 if (zone) {
248 return new Date(Date.UTC(y, M, d, h, m, s, ms + zone.offset * 60 * 1000));
249 }
250
251 if (utc) {
252 return new Date(Date.UTC(y, M, d, h, m, s, ms));
253 }
254
255 var newDate;
256 newDate = new Date(y, M, d, h, m, s, ms);
257
258 if (week) {
259 newDate = dayjs(newDate).week(week).toDate();
260 }
261
262 return newDate;
263 } catch (e) {
264 return new Date(''); // Invalid Date
265 }
266};
267
268export default (function (o, C, d) {
269 d.p.customParseFormat = true;
270
271 if (o && o.parseTwoDigitYear) {
272 parseTwoDigitYear = o.parseTwoDigitYear;
273 }
274
275 var proto = C.prototype;
276 var oldParse = proto.parse;
277
278 proto.parse = function (cfg) {
279 var date = cfg.date,
280 utc = cfg.utc,
281 args = cfg.args;
282 this.$u = utc;
283 var format = args[1];
284
285 if (typeof format === 'string') {
286 var isStrictWithoutLocale = args[2] === true;
287 var isStrictWithLocale = args[3] === true;
288 var isStrict = isStrictWithoutLocale || isStrictWithLocale;
289 var pl = args[2];
290
291 if (isStrictWithLocale) {
292 pl = args[2];
293 }
294
295 locale = this.$locale();
296
297 if (!isStrictWithoutLocale && pl) {
298 locale = d.Ls[pl];
299 }
300
301 this.$d = parseFormattedInput(date, format, utc, d);
302 this.init();
303 if (pl && pl !== true) this.$L = this.locale(pl).$L; // use != to treat
304 // input number 1410715640579 and format string '1410715640579' equal
305 // eslint-disable-next-line eqeqeq
306
307 if (isStrict && date != this.format(format)) {
308 this.$d = new Date('');
309 } // reset global locale to make parallel unit test
310
311
312 locale = {};
313 } else if (format instanceof Array) {
314 var len = format.length;
315
316 for (var i = 1; i <= len; i += 1) {
317 args[1] = format[i - 1];
318 var result = d.apply(this, args);
319
320 if (result.isValid()) {
321 this.$d = result.$d;
322 this.$L = result.$L;
323 this.init();
324 break;
325 }
326
327 if (i === len) this.$d = new Date('');
328 }
329 } else {
330 oldParse.call(this, cfg);
331 }
332 };
333});
\No newline at end of file