UNPKG

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