index.js
8.6 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
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
import { MILLISECONDS_A_DAY, MILLISECONDS_A_HOUR, MILLISECONDS_A_MINUTE, MILLISECONDS_A_SECOND, MILLISECONDS_A_WEEK, REGEX_FORMAT } from '../../constant';
var MILLISECONDS_A_YEAR = MILLISECONDS_A_DAY * 365;
var MILLISECONDS_A_MONTH = MILLISECONDS_A_DAY * 30;
var durationRegex = /^(-|\+)?P(?:([-+]?[0-9,.]*)Y)?(?:([-+]?[0-9,.]*)M)?(?:([-+]?[0-9,.]*)W)?(?:([-+]?[0-9,.]*)D)?(?:T(?:([-+]?[0-9,.]*)H)?(?:([-+]?[0-9,.]*)M)?(?:([-+]?[0-9,.]*)S)?)?$/;
var unitToMS = {
years: MILLISECONDS_A_YEAR,
months: MILLISECONDS_A_MONTH,
days: MILLISECONDS_A_DAY,
hours: MILLISECONDS_A_HOUR,
minutes: MILLISECONDS_A_MINUTE,
seconds: MILLISECONDS_A_SECOND,
milliseconds: 1,
weeks: MILLISECONDS_A_WEEK
};
var isDuration = function isDuration(d) {
return d instanceof Duration;
}; // eslint-disable-line no-use-before-define
var $d;
var $u;
var wrapper = function wrapper(input, instance, unit) {
return new Duration(input, unit, instance.$l);
}; // eslint-disable-line no-use-before-define
var prettyUnit = function prettyUnit(unit) {
return $u.p(unit) + "s";
};
var isNegative = function isNegative(number) {
return number < 0;
};
var roundNumber = function roundNumber(number) {
return isNegative(number) ? Math.ceil(number) : Math.floor(number);
};
var absolute = function absolute(number) {
return Math.abs(number);
};
var getNumberUnitFormat = function getNumberUnitFormat(number, unit) {
if (!number) {
return {
negative: false,
format: ''
};
}
if (isNegative(number)) {
return {
negative: true,
format: "" + absolute(number) + unit
};
}
return {
negative: false,
format: "" + number + unit
};
};
var Duration = /*#__PURE__*/function () {
function Duration(input, unit, locale) {
var _this = this;
this.$d = {};
this.$l = locale;
if (input === undefined) {
this.$ms = 0;
this.parseFromMilliseconds();
}
if (unit) {
return wrapper(input * unitToMS[prettyUnit(unit)], this);
}
if (typeof input === 'number') {
this.$ms = input;
this.parseFromMilliseconds();
return this;
}
if (typeof input === 'object') {
Object.keys(input).forEach(function (k) {
_this.$d[prettyUnit(k)] = input[k];
});
this.calMilliseconds();
return this;
}
if (typeof input === 'string') {
var d = input.match(durationRegex);
if (d) {
var properties = d.slice(2);
var numberD = properties.map(function (value) {
return value != null ? Number(value) : 0;
});
this.$d.years = numberD[0];
this.$d.months = numberD[1];
this.$d.weeks = numberD[2];
this.$d.days = numberD[3];
this.$d.hours = numberD[4];
this.$d.minutes = numberD[5];
this.$d.seconds = numberD[6];
this.calMilliseconds();
return this;
}
}
return this;
}
var _proto = Duration.prototype;
_proto.calMilliseconds = function calMilliseconds() {
var _this2 = this;
this.$ms = Object.keys(this.$d).reduce(function (total, unit) {
return total + (_this2.$d[unit] || 0) * unitToMS[unit];
}, 0);
};
_proto.parseFromMilliseconds = function parseFromMilliseconds() {
var $ms = this.$ms;
this.$d.years = roundNumber($ms / MILLISECONDS_A_YEAR);
$ms %= MILLISECONDS_A_YEAR;
this.$d.months = roundNumber($ms / MILLISECONDS_A_MONTH);
$ms %= MILLISECONDS_A_MONTH;
this.$d.days = roundNumber($ms / MILLISECONDS_A_DAY);
$ms %= MILLISECONDS_A_DAY;
this.$d.hours = roundNumber($ms / MILLISECONDS_A_HOUR);
$ms %= MILLISECONDS_A_HOUR;
this.$d.minutes = roundNumber($ms / MILLISECONDS_A_MINUTE);
$ms %= MILLISECONDS_A_MINUTE;
this.$d.seconds = roundNumber($ms / MILLISECONDS_A_SECOND);
$ms %= MILLISECONDS_A_SECOND;
this.$d.milliseconds = $ms;
};
_proto.toISOString = function toISOString() {
var Y = getNumberUnitFormat(this.$d.years, 'Y');
var M = getNumberUnitFormat(this.$d.months, 'M');
var days = +this.$d.days || 0;
if (this.$d.weeks) {
days += this.$d.weeks * 7;
}
var D = getNumberUnitFormat(days, 'D');
var H = getNumberUnitFormat(this.$d.hours, 'H');
var m = getNumberUnitFormat(this.$d.minutes, 'M');
var seconds = this.$d.seconds || 0;
if (this.$d.milliseconds) {
seconds += this.$d.milliseconds / 1000;
}
var S = getNumberUnitFormat(seconds, 'S');
var negativeMode = Y.negative || M.negative || D.negative || H.negative || m.negative || S.negative;
var T = H.format || m.format || S.format ? 'T' : '';
var P = negativeMode ? '-' : '';
var result = P + "P" + Y.format + M.format + D.format + T + H.format + m.format + S.format;
return result === 'P' || result === '-P' ? 'P0D' : result;
};
_proto.toJSON = function toJSON() {
return this.toISOString();
};
_proto.format = function format(formatStr) {
var str = formatStr || 'YYYY-MM-DDTHH:mm:ss';
var matches = {
Y: this.$d.years,
YY: $u.s(this.$d.years, 2, '0'),
YYYY: $u.s(this.$d.years, 4, '0'),
M: this.$d.months,
MM: $u.s(this.$d.months, 2, '0'),
D: this.$d.days,
DD: $u.s(this.$d.days, 2, '0'),
H: this.$d.hours,
HH: $u.s(this.$d.hours, 2, '0'),
m: this.$d.minutes,
mm: $u.s(this.$d.minutes, 2, '0'),
s: this.$d.seconds,
ss: $u.s(this.$d.seconds, 2, '0'),
SSS: $u.s(this.$d.milliseconds, 3, '0')
};
return str.replace(REGEX_FORMAT, function (match, $1) {
return $1 || String(matches[match]);
});
};
_proto.as = function as(unit) {
return this.$ms / unitToMS[prettyUnit(unit)];
};
_proto.get = function get(unit) {
var base = this.$ms;
var pUnit = prettyUnit(unit);
if (pUnit === 'milliseconds') {
base %= 1000;
} else if (pUnit === 'weeks') {
base = roundNumber(base / unitToMS[pUnit]);
} else {
base = this.$d[pUnit];
}
return base === 0 ? 0 : base; // a === 0 will be true on both 0 and -0
};
_proto.add = function add(input, unit, isSubtract) {
var another;
if (unit) {
another = input * unitToMS[prettyUnit(unit)];
} else if (isDuration(input)) {
another = input.$ms;
} else {
another = wrapper(input, this).$ms;
}
return wrapper(this.$ms + another * (isSubtract ? -1 : 1), this);
};
_proto.subtract = function subtract(input, unit) {
return this.add(input, unit, true);
};
_proto.locale = function locale(l) {
var that = this.clone();
that.$l = l;
return that;
};
_proto.clone = function clone() {
return wrapper(this.$ms, this);
};
_proto.humanize = function humanize(withSuffix) {
return $d().add(this.$ms, 'ms').locale(this.$l).fromNow(!withSuffix);
};
_proto.valueOf = function valueOf() {
return this.asMilliseconds();
};
_proto.milliseconds = function milliseconds() {
return this.get('milliseconds');
};
_proto.asMilliseconds = function asMilliseconds() {
return this.as('milliseconds');
};
_proto.seconds = function seconds() {
return this.get('seconds');
};
_proto.asSeconds = function asSeconds() {
return this.as('seconds');
};
_proto.minutes = function minutes() {
return this.get('minutes');
};
_proto.asMinutes = function asMinutes() {
return this.as('minutes');
};
_proto.hours = function hours() {
return this.get('hours');
};
_proto.asHours = function asHours() {
return this.as('hours');
};
_proto.days = function days() {
return this.get('days');
};
_proto.asDays = function asDays() {
return this.as('days');
};
_proto.weeks = function weeks() {
return this.get('weeks');
};
_proto.asWeeks = function asWeeks() {
return this.as('weeks');
};
_proto.months = function months() {
return this.get('months');
};
_proto.asMonths = function asMonths() {
return this.as('months');
};
_proto.years = function years() {
return this.get('years');
};
_proto.asYears = function asYears() {
return this.as('years');
};
return Duration;
}();
export default (function (option, Dayjs, dayjs) {
$d = dayjs;
$u = dayjs().$utils();
dayjs.duration = function (input, unit) {
var $l = dayjs.locale();
return wrapper(input, {
$l: $l
}, unit);
};
dayjs.isDuration = isDuration;
var oldAdd = Dayjs.prototype.add;
var oldSubtract = Dayjs.prototype.subtract;
Dayjs.prototype.add = function (value, unit) {
if (isDuration(value)) value = value.asMilliseconds();
return oldAdd.bind(this)(value, unit);
};
Dayjs.prototype.subtract = function (value, unit) {
if (isDuration(value)) value = value.asMilliseconds();
return oldSubtract.bind(this)(value, unit);
};
});