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