UNPKG

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