UNPKG

12.5 kBJavaScriptView Raw
1import * as C from './constant';
2import en from './locale/en';
3import U from './utils';
4var L = 'en'; // global locale
5
6var Ls = {}; // global loaded locale
7
8Ls[L] = en;
9
10var isDayjs = function isDayjs(d) {
11 return d instanceof Dayjs;
12}; // eslint-disable-line no-use-before-define
13
14
15var parseLocale = function parseLocale(preset, object, isLocal) {
16 var l;
17 if (!preset) return L;
18
19 if (typeof preset === 'string') {
20 var presetLower = preset.toLowerCase();
21
22 if (Ls[presetLower]) {
23 l = presetLower;
24 }
25
26 if (object) {
27 Ls[presetLower] = object;
28 l = presetLower;
29 }
30
31 var presetSplit = preset.split('-');
32
33 if (!l && presetSplit.length > 1) {
34 return parseLocale(presetSplit[0]);
35 }
36 } else {
37 var name = preset.name;
38 Ls[name] = preset;
39 l = name;
40 }
41
42 if (!isLocal && l) L = l;
43 return l || !isLocal && L;
44};
45
46var dayjs = function dayjs(date, c) {
47 if (isDayjs(date)) {
48 return date.clone();
49 } // eslint-disable-next-line no-nested-ternary
50
51
52 var cfg = typeof c === 'object' ? c : {};
53 cfg.date = date;
54 cfg.args = arguments; // eslint-disable-line prefer-rest-params
55
56 return new Dayjs(cfg); // eslint-disable-line no-use-before-define
57};
58
59var wrapper = function wrapper(date, instance) {
60 return dayjs(date, {
61 locale: instance.$L,
62 utc: instance.$u,
63 x: instance.$x,
64 $offset: instance.$offset // todo: refactor; do not use this.$offset in you code
65
66 });
67};
68
69var Utils = U; // for plugin use
70
71Utils.l = parseLocale;
72Utils.i = isDayjs;
73Utils.w = wrapper;
74
75var parseDate = function parseDate(cfg) {
76 var date = cfg.date,
77 utc = cfg.utc;
78 if (date === null) return new Date(NaN); // null is invalid
79
80 if (Utils.u(date)) return new Date(); // today
81
82 if (date instanceof Date) return new Date(date);
83
84 if (typeof date === 'string' && !/Z$/i.test(date)) {
85 var d = date.match(C.REGEX_PARSE);
86
87 if (d) {
88 var m = d[2] - 1 || 0;
89 var ms = (d[7] || '0').substring(0, 3);
90
91 if (utc) {
92 return new Date(Date.UTC(d[1], m, d[3] || 1, d[4] || 0, d[5] || 0, d[6] || 0, ms));
93 }
94
95 return new Date(d[1], m, d[3] || 1, d[4] || 0, d[5] || 0, d[6] || 0, ms);
96 }
97 }
98
99 return new Date(date); // everything else
100};
101
102var Dayjs = /*#__PURE__*/function () {
103 function Dayjs(cfg) {
104 this.$L = parseLocale(cfg.locale, null, true);
105 this.parse(cfg); // for plugin
106 }
107
108 var _proto = Dayjs.prototype;
109
110 _proto.parse = function parse(cfg) {
111 this.$d = parseDate(cfg);
112 this.$x = cfg.x || {};
113 this.init();
114 };
115
116 _proto.init = function init() {
117 var $d = this.$d;
118 this.$y = $d.getFullYear();
119 this.$M = $d.getMonth();
120 this.$D = $d.getDate();
121 this.$W = $d.getDay();
122 this.$H = $d.getHours();
123 this.$m = $d.getMinutes();
124 this.$s = $d.getSeconds();
125 this.$ms = $d.getMilliseconds();
126 } // eslint-disable-next-line class-methods-use-this
127 ;
128
129 _proto.$utils = function $utils() {
130 return Utils;
131 };
132
133 _proto.isValid = function isValid() {
134 return !(this.$d.toString() === C.INVALID_DATE_STRING);
135 };
136
137 _proto.isSame = function isSame(that, units) {
138 var other = dayjs(that);
139 return this.startOf(units) <= other && other <= this.endOf(units);
140 };
141
142 _proto.isAfter = function isAfter(that, units) {
143 return dayjs(that) < this.startOf(units);
144 };
145
146 _proto.isBefore = function isBefore(that, units) {
147 return this.endOf(units) < dayjs(that);
148 };
149
150 _proto.$g = function $g(input, get, set) {
151 if (Utils.u(input)) return this[get];
152 return this.set(set, input);
153 };
154
155 _proto.unix = function unix() {
156 return Math.floor(this.valueOf() / 1000);
157 };
158
159 _proto.valueOf = function valueOf() {
160 // timezone(hour) * 60 * 60 * 1000 => ms
161 return this.$d.getTime();
162 };
163
164 _proto.startOf = function startOf(units, _startOf) {
165 var _this = this;
166
167 // startOf -> endOf
168 var isStartOf = !Utils.u(_startOf) ? _startOf : true;
169 var unit = Utils.p(units);
170
171 var instanceFactory = function instanceFactory(d, m) {
172 var ins = Utils.w(_this.$u ? Date.UTC(_this.$y, m, d) : new Date(_this.$y, m, d), _this);
173 return isStartOf ? ins : ins.endOf(C.D);
174 };
175
176 var instanceFactorySet = function instanceFactorySet(method, slice) {
177 var argumentStart = [0, 0, 0, 0];
178 var argumentEnd = [23, 59, 59, 999];
179 return Utils.w(_this.toDate()[method].apply( // eslint-disable-line prefer-spread
180 _this.toDate('s'), (isStartOf ? argumentStart : argumentEnd).slice(slice)), _this);
181 };
182
183 var $W = this.$W,
184 $M = this.$M,
185 $D = this.$D;
186 var utcPad = "set" + (this.$u ? 'UTC' : '');
187
188 switch (unit) {
189 case C.Y:
190 return isStartOf ? instanceFactory(1, 0) : instanceFactory(31, 11);
191
192 case C.M:
193 return isStartOf ? instanceFactory(1, $M) : instanceFactory(0, $M + 1);
194
195 case C.W:
196 {
197 var weekStart = this.$locale().weekStart || 0;
198 var gap = ($W < weekStart ? $W + 7 : $W) - weekStart;
199 return instanceFactory(isStartOf ? $D - gap : $D + (6 - gap), $M);
200 }
201
202 case C.D:
203 case C.DATE:
204 return instanceFactorySet(utcPad + "Hours", 0);
205
206 case C.H:
207 return instanceFactorySet(utcPad + "Minutes", 1);
208
209 case C.MIN:
210 return instanceFactorySet(utcPad + "Seconds", 2);
211
212 case C.S:
213 return instanceFactorySet(utcPad + "Milliseconds", 3);
214
215 default:
216 return this.clone();
217 }
218 };
219
220 _proto.endOf = function endOf(arg) {
221 return this.startOf(arg, false);
222 };
223
224 _proto.$set = function $set(units, _int) {
225 var _C$D$C$DATE$C$M$C$Y$C;
226
227 // private set
228 var unit = Utils.p(units);
229 var utcPad = "set" + (this.$u ? 'UTC' : '');
230 var name = (_C$D$C$DATE$C$M$C$Y$C = {}, _C$D$C$DATE$C$M$C$Y$C[C.D] = utcPad + "Date", _C$D$C$DATE$C$M$C$Y$C[C.DATE] = utcPad + "Date", _C$D$C$DATE$C$M$C$Y$C[C.M] = utcPad + "Month", _C$D$C$DATE$C$M$C$Y$C[C.Y] = utcPad + "FullYear", _C$D$C$DATE$C$M$C$Y$C[C.H] = utcPad + "Hours", _C$D$C$DATE$C$M$C$Y$C[C.MIN] = utcPad + "Minutes", _C$D$C$DATE$C$M$C$Y$C[C.S] = utcPad + "Seconds", _C$D$C$DATE$C$M$C$Y$C[C.MS] = utcPad + "Milliseconds", _C$D$C$DATE$C$M$C$Y$C)[unit];
231 var arg = unit === C.D ? this.$D + (_int - this.$W) : _int;
232
233 if (unit === C.M || unit === C.Y) {
234 // clone is for badMutable plugin
235 var date = this.clone().set(C.DATE, 1);
236 date.$d[name](arg);
237 date.init();
238 this.$d = date.set(C.DATE, Math.min(this.$D, date.daysInMonth())).$d;
239 } else if (name) this.$d[name](arg);
240
241 this.init();
242 return this;
243 };
244
245 _proto.set = function set(string, _int2) {
246 return this.clone().$set(string, _int2);
247 };
248
249 _proto.get = function get(unit) {
250 return this[Utils.p(unit)]();
251 };
252
253 _proto.add = function add(number, units) {
254 var _this2 = this,
255 _C$MIN$C$H$C$S$unit;
256
257 number = Number(number); // eslint-disable-line no-param-reassign
258
259 var unit = Utils.p(units);
260
261 var instanceFactorySet = function instanceFactorySet(n) {
262 var d = dayjs(_this2);
263 return Utils.w(d.date(d.date() + Math.round(n * number)), _this2);
264 };
265
266 if (unit === C.M) {
267 return this.set(C.M, this.$M + number);
268 }
269
270 if (unit === C.Y) {
271 return this.set(C.Y, this.$y + number);
272 }
273
274 if (unit === C.D) {
275 return instanceFactorySet(1);
276 }
277
278 if (unit === C.W) {
279 return instanceFactorySet(7);
280 }
281
282 var step = (_C$MIN$C$H$C$S$unit = {}, _C$MIN$C$H$C$S$unit[C.MIN] = C.MILLISECONDS_A_MINUTE, _C$MIN$C$H$C$S$unit[C.H] = C.MILLISECONDS_A_HOUR, _C$MIN$C$H$C$S$unit[C.S] = C.MILLISECONDS_A_SECOND, _C$MIN$C$H$C$S$unit)[unit] || 1; // ms
283
284 var nextTimeStamp = this.$d.getTime() + number * step;
285 return Utils.w(nextTimeStamp, this);
286 };
287
288 _proto.subtract = function subtract(number, string) {
289 return this.add(number * -1, string);
290 };
291
292 _proto.format = function format(formatStr) {
293 var _this3 = this;
294
295 var locale = this.$locale();
296 if (!this.isValid()) return locale.invalidDate || C.INVALID_DATE_STRING;
297 var str = formatStr || C.FORMAT_DEFAULT;
298 var zoneStr = Utils.z(this);
299 var $H = this.$H,
300 $m = this.$m,
301 $M = this.$M;
302 var weekdays = locale.weekdays,
303 months = locale.months,
304 meridiem = locale.meridiem;
305
306 var getShort = function getShort(arr, index, full, length) {
307 return arr && (arr[index] || arr(_this3, str)) || full[index].slice(0, length);
308 };
309
310 var get$H = function get$H(num) {
311 return Utils.s($H % 12 || 12, num, '0');
312 };
313
314 var meridiemFunc = meridiem || function (hour, minute, isLowercase) {
315 var m = hour < 12 ? 'AM' : 'PM';
316 return isLowercase ? m.toLowerCase() : m;
317 };
318
319 var matches = {
320 YY: String(this.$y).slice(-2),
321 YYYY: this.$y,
322 M: $M + 1,
323 MM: Utils.s($M + 1, 2, '0'),
324 MMM: getShort(locale.monthsShort, $M, months, 3),
325 MMMM: getShort(months, $M),
326 D: this.$D,
327 DD: Utils.s(this.$D, 2, '0'),
328 d: String(this.$W),
329 dd: getShort(locale.weekdaysMin, this.$W, weekdays, 2),
330 ddd: getShort(locale.weekdaysShort, this.$W, weekdays, 3),
331 dddd: weekdays[this.$W],
332 H: String($H),
333 HH: Utils.s($H, 2, '0'),
334 h: get$H(1),
335 hh: get$H(2),
336 a: meridiemFunc($H, $m, true),
337 A: meridiemFunc($H, $m, false),
338 m: String($m),
339 mm: Utils.s($m, 2, '0'),
340 s: String(this.$s),
341 ss: Utils.s(this.$s, 2, '0'),
342 SSS: Utils.s(this.$ms, 3, '0'),
343 Z: zoneStr // 'ZZ' logic below
344
345 };
346 return str.replace(C.REGEX_FORMAT, function (match, $1) {
347 return $1 || matches[match] || zoneStr.replace(':', '');
348 }); // 'ZZ'
349 };
350
351 _proto.utcOffset = function utcOffset() {
352 // Because a bug at FF24, we're rounding the timezone offset around 15 minutes
353 // https://github.com/moment/moment/pull/1871
354 return -Math.round(this.$d.getTimezoneOffset() / 15) * 15;
355 };
356
357 _proto.diff = function diff(input, units, _float) {
358 var _C$Y$C$M$C$Q$C$W$C$D$;
359
360 var unit = Utils.p(units);
361 var that = dayjs(input);
362 var zoneDelta = (that.utcOffset() - this.utcOffset()) * C.MILLISECONDS_A_MINUTE;
363 var diff = this - that;
364 var result = Utils.m(this, that);
365 result = (_C$Y$C$M$C$Q$C$W$C$D$ = {}, _C$Y$C$M$C$Q$C$W$C$D$[C.Y] = result / 12, _C$Y$C$M$C$Q$C$W$C$D$[C.M] = result, _C$Y$C$M$C$Q$C$W$C$D$[C.Q] = result / 3, _C$Y$C$M$C$Q$C$W$C$D$[C.W] = (diff - zoneDelta) / C.MILLISECONDS_A_WEEK, _C$Y$C$M$C$Q$C$W$C$D$[C.D] = (diff - zoneDelta) / C.MILLISECONDS_A_DAY, _C$Y$C$M$C$Q$C$W$C$D$[C.H] = diff / C.MILLISECONDS_A_HOUR, _C$Y$C$M$C$Q$C$W$C$D$[C.MIN] = diff / C.MILLISECONDS_A_MINUTE, _C$Y$C$M$C$Q$C$W$C$D$[C.S] = diff / C.MILLISECONDS_A_SECOND, _C$Y$C$M$C$Q$C$W$C$D$)[unit] || diff; // milliseconds
366
367 return _float ? result : Utils.a(result);
368 };
369
370 _proto.daysInMonth = function daysInMonth() {
371 return this.endOf(C.M).$D;
372 };
373
374 _proto.$locale = function $locale() {
375 // get locale object
376 return Ls[this.$L];
377 };
378
379 _proto.locale = function locale(preset, object) {
380 if (!preset) return this.$L;
381 var that = this.clone();
382 var nextLocaleName = parseLocale(preset, object, true);
383 if (nextLocaleName) that.$L = nextLocaleName;
384 return that;
385 };
386
387 _proto.clone = function clone() {
388 return Utils.w(this.$d, this);
389 };
390
391 _proto.toDate = function toDate() {
392 return new Date(this.valueOf());
393 };
394
395 _proto.toJSON = function toJSON() {
396 return this.isValid() ? this.toISOString() : null;
397 };
398
399 _proto.toISOString = function toISOString() {
400 // ie 8 return
401 // new Dayjs(this.valueOf() + this.$d.getTimezoneOffset() * 60000)
402 // .format('YYYY-MM-DDTHH:mm:ss.SSS[Z]')
403 return this.$d.toISOString();
404 };
405
406 _proto.toString = function toString() {
407 return this.$d.toUTCString();
408 };
409
410 return Dayjs;
411}();
412
413var proto = Dayjs.prototype;
414dayjs.prototype = proto;
415[['$ms', C.MS], ['$s', C.S], ['$m', C.MIN], ['$H', C.H], ['$W', C.D], ['$M', C.M], ['$y', C.Y], ['$D', C.DATE]].forEach(function (g) {
416 proto[g[1]] = function (input) {
417 return this.$g(input, g[0], g[1]);
418 };
419});
420
421dayjs.extend = function (plugin, option) {
422 if (!plugin.$i) {
423 // install plugin only once
424 plugin(option, Dayjs, dayjs);
425 plugin.$i = true;
426 }
427
428 return dayjs;
429};
430
431dayjs.locale = parseLocale;
432dayjs.isDayjs = isDayjs;
433
434dayjs.unix = function (timestamp) {
435 return dayjs(timestamp * 1e3);
436};
437
438dayjs.en = Ls[L];
439dayjs.Ls = Ls;
440dayjs.p = {};
441export default dayjs;
\No newline at end of file