UNPKG

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