-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mod.ts
463 lines (449 loc) · 12.5 KB
/
mod.ts
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
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
interface KeyList {
d: string;
h: string;
m: string;
s: string;
ms: string;
us: string;
ns: string;
}
const keyList: KeyList = {
d: "days",
h: "hours",
m: "minutes",
s: "seconds",
ms: "milliseconds",
us: "microseconds",
ns: "nanoseconds",
};
export type DurationKeys = "d" | "h" | "m" | "s" | "ms" | "us" | "ns";
/**
* For the array of values
* @typedef {object} KeyValue
* @property {string} type - Type of key. One of d, h, m, s, ms, us, ns
* @property {number} value - Value of the time unit
*/
export interface KeyValue {
type: DurationKeys;
value: number;
}
/**
* Duration Object
* @typedef {Object} DurationObj
* @property {number} raw - Total number of milliseconds in the duration
* @property {number} d - Number of days held by duration
* @property {number} h - Number of hours held by duration
* @property {number} m - Number of minutes held by duration
* @property {number} s - Number of seconds held by duration
* @property {number} ms - Number of milliseconds held by duration
* @property {number} us - Number of microseconds held by duration
* @property {number} ns - Number of nanoseconds held by duration
*/
export interface DurationObj {
raw: number;
d: number;
h: number;
m: number;
s: number;
ms: number;
us: number;
ns: number;
}
const BaseDurationObj: DurationObj = {
raw: 0,
d: 0,
h: 0,
m: 0,
s: 0,
ms: 0,
us: 0,
ns: 0,
};
/**
* A simple JavaScript class used to parse time durations
* @module Duration
* @class
*/
export class Duration {
raw: number;
d: number;
h: number;
m: number;
s: number;
ms: number;
us: number;
ns: number;
/**
* Parse milliseconds into separate units of time.
* @param {number} timestamp - Milliseconds to parse into a duration object.
* @returns {<Duration>}
*/
constructor(timestamp: number = Duration.getCurrentDuration()) {
if (timestamp < 0) timestamp = 0; // Prevent negative time
this.raw = timestamp;
this.d = Math.trunc(timestamp / 86400000);
this.h = Math.trunc(timestamp / 3600000) % 24;
this.m = Math.trunc(timestamp / 60000) % 60;
this.s = Math.trunc(timestamp / 1000) % 60;
this.ms = Math.trunc(timestamp) % 1000;
this.us = Math.trunc(timestamp * 1000) % 1000;
this.ns = Math.trunc(timestamp * 1000000) % 1000;
}
/**
* An array of time units and their values.
* @returns {KeyValue[]}
*/
get array(): KeyValue[] {
return [
{ type: "d", value: this.d },
{ type: "h", value: this.h },
{ type: "m", value: this.m },
{ type: "s", value: this.s },
{ type: "ms", value: this.ms },
{ type: "us", value: this.µs },
{ type: "ns", value: this.ns },
];
}
/**
* Alias for microseconds.
*/
get µs(): number {
return this.us;
}
/**
* Data in the class mapped as a JavaScript Object.
* @returns {DurationObj}
*/
get json(): DurationObj {
return this.array.reduce(
(acc, stuff) => ((acc[stuff.type] = stuff.value), acc),
BaseDurationObj,
);
}
/**
* Add more days to the duration.
* @param {number} n - Number of days to add.
* @returns {Duration} The updated duration.
*/
addDays(n: number): Duration {
this.d += n;
return this.reload();
}
/**
* Add more hours to the duration.
* @param {number} n - Number of hours to add.
* @returns {Duration} The updated duration.
*/
addHours(n: number): Duration {
this.h += n;
return this.reload();
}
/**
* Add more minutes to the duration.
* @param {number} n - Number of minutes to add.
* @returns {Duration} The updated duration.
*/
addMinutes(n: number): Duration {
this.m += n;
return this.reload();
}
/**
* Add more seconds to the duration.
* @param {number} n - Number of seconds to add.
* @returns {Duration} The updated duration.
*/
addSeconds(n: number): Duration {
this.s += n;
return this.reload();
}
/**
* Add more milliseconds to the duration.
* @param {number} n - Number of milliseconds to add.
* @returns {Duration} The updated duration.
*/
addMilliseconds(n: number): Duration {
this.ms += n;
return this.reload();
}
/**
* Add more microseconds to the duration.
* @param {number} n - Number of microseconds to add.
* @returns {Duration} The updated duration.
*/
addMicroseconds(n: number): Duration {
this.us += n;
return this.reload();
}
/**
* Add more nanoseconds to the duration.
* @param {number} n - Number of nanoseconds to add.
* @returns {Duration} The updated duration.
*/
addNanoseconds(n: number): Duration {
this.ns += n;
return this.reload();
}
/**
* Set days of the duration.
* @param {number} n - Number of days to set.
* @returns {Duration} The updated duration.
*/
setDays(n: number): Duration {
this.d = n;
return this.reload();
}
/**
* Set hours of the duration.
* @param {number} n - Number of hours to set.
* @returns {Duration} The updated duration.
*/
setHours(n: number): Duration {
this.h = n;
return this.reload();
}
/**
* Set minutes of the duration.
* @param {number} n - Number of minutes to set.
* @returns {Duration} The updated duration.
*/
setMinutes(n: number): Duration {
this.m = n;
return this.reload();
}
/**
* Set seconds of the duration.
* @param {number} n - Number of seconds to set.
* @returns {Duration} The updated duration.
*/
setSeconds(n: number): Duration {
this.s = n;
return this.reload();
}
/**
* Set milliseconds of the duration.
* @param {number} n - Number of milliseconds to set.
* @returns {Duration} The updated duration.
*/
setMilliseconds(n: number): Duration {
this.ms = n;
return this.reload();
}
/**
* Set microseconds of the duration.
* @param {number} n - Number of microseconds to set.
* @returns {Duration} The updated duration.
*/
setMicroseconds(n: number): Duration {
this.us = n;
return this.reload();
}
/**
* Set nanoseconds of the duration.
* @param {number} n - Number of nanoseconds to set.
* @returns {Duration} The updated duration.
*/
setNanoseconds(n: number): Duration {
this.ns = n;
return this.reload();
}
/**
* Get a formatted, human-readable string of the duration.
* @param {string[]} values - The values required to display.
* @param {boolean} shortandsweet - If response should be a short string.
* @returns {string} formatted string - The formatted string result.
*/
stringify(values: string[] = [], shortandsweet = false): string {
if (!Array.isArray(values) || values.length == 0) {
if (
!shortandsweet ||
typeof shortandsweet !== "boolean"
) {
return `${
this.array
.map((x) => `${x.value} ${keyList[x.type]}`)
.join(", ")
}`;
}
return `${this.array.map((x) => `${x.value}${x.type}`).join(" ")}`;
}
if (values.length > 0) {
if (
!shortandsweet ||
typeof shortandsweet !== "boolean"
) {
return `${
this.array
.filter((x) => values.includes(x.type))
.map((x) => `${x.value} ${keyList[x.type]}`)
.join(", ")
}`;
}
return `${
this.array
.filter((x) => values.includes(x.type))
.map((x) => `${x.value}${x.type}`)
.join(" ")
}`;
}
return `${
this.array
.map((x) => `${x.value} ${keyList[x.type]}`)
.join(", ")
}`;
}
/**
* Get a duration formatted using colons (:).
* @param {string} fromT - Unit to display from.
* @param {string} toT - Unit to display upto.
* @returns {string} Formatted string.
*/
getFormattedDuration(
fromT: DurationKeys = "d",
toT: DurationKeys = "ns",
): string {
if (
typeof fromT !== "string" ||
typeof toT !== "string" ||
!Object.prototype.hasOwnProperty.call(keyList, fromT.toLowerCase()) ||
!Object.prototype.hasOwnProperty.call(keyList, toT.toLowerCase())
) {
return this.getSimpleFormattedDuration();
}
const durations = [];
const nextIndex =
this.array.findIndex((x) => x.type === toT.toLowerCase()) + 1;
const next = this.array[nextIndex];
for (const obj of this.array) {
if (obj.type !== fromT.toLowerCase() && durations.length === 0) continue;
if (obj.type === next?.type) break;
durations.push(["ms", "us", "ns"].includes(obj.type) ? addZero(obj.value, 3) : obj.type === "d" ? obj.value : addZero(obj.value, 2));
}
return durations.join(":");
}
/**
* Get a simple formatted duration in the form dd:hh:mm:ss:ms
* @returns {string} Formatted string
*/
getSimpleFormattedDuration(): string {
return `${this.array.map((x) => x.value).join(":")}`;
}
/**
* Extra filler function that returns the class data in a single short string.
* @returns {string} Dumb string
*/
toString(): string {
return `[Duration ${this.stringify(["d", "h", "m", "s"], true)}]`;
}
/**
* Update data to match any modification to values.
* @returns {<Duration>}
*/
reload(): Duration {
const ts = this.d * 8.64e7 +
this.h * 3600000 +
this.m * 60000 +
this.s * 1000 +
this.ms + (this.us / 1000) + (this.ns / 1000000);
if (ts === this.raw) return this;
const newDuration = new Duration(ts);
this.d = newDuration.d;
this.h = newDuration.h;
this.m = newDuration.m;
this.s = newDuration.s;
this.ms = newDuration.ms;
this.ns = newDuration.ns;
this.us = newDuration.us;
this.raw = newDuration.raw;
return this;
}
/**
* Reads a given string and parses a duration from it.
* @param {string} str - A string which could contain a duration
* @param {string} doNotParse - Directly return the values read
* @returns {<Duration>}
*/
static fromString(str: string, doNotParse = false): Duration {
const { d, h, m, s, ms, ns, us } = Duration.readString(str);
const ts = d * 8.64e7 + h * 3600000 + m * 60000 + s * 1000 + ms +
(us / 1000) +
(ns / 1000000);
const newDuration = new Duration(ts);
if (doNotParse) {
newDuration.d = d;
newDuration.h = h;
newDuration.m = m;
newDuration.s = s;
newDuration.ms = ms;
newDuration.ns = ns;
newDuration.us = us;
}
return newDuration;
}
/**
* Get the duration till next midnight in milliseconds.
* @returns {number} Duration in milliseconds till the next midnight
*/
static getCurrentDuration(): number {
return Date.now() - new Date().setHours(0, 0, 0, 0);
}
/**
* Read duration data from a string.
* @param {string} str - The string to read
* @returns {DurationObj} obj - Object with days, hours, mins, seconds and milliseconds
*/
static readString(str: string) {
str = str.replace(/\s\s/g, "");
const days = matchReg(str, "d") || matchReg(str, "days") ||
matchReg(str, "day");
const hours = matchReg(str, "h") || matchReg(str, "hours") ||
matchReg(str, "hour");
const minutes = matchReg(str, "m") ||
matchReg(str, "min") ||
matchReg(str, "minute") ||
matchReg(str, "mins") ||
matchReg(str, "minutes");
const seconds = matchReg(str, "s") ||
matchReg(str, "sec") ||
matchReg(str, "second") ||
matchReg(str, "secs") ||
matchReg(str, "seconds");
const milliseconds = matchReg(str, "ms") ||
matchReg(str, "millisecond") ||
matchReg(str, "milliseconds");
const nanoseconds = matchReg(str, "ns") ||
matchReg(str, "nanosecond") ||
matchReg(str, "nanoseconds");
const microseconds = matchReg(str, "µs") ||
matchReg(str, "microsecond") ||
matchReg(str, "microseconds");
matchReg(str, "us");
return {
d: days,
h: hours,
m: minutes,
s: seconds,
ms: milliseconds,
ns: nanoseconds,
us: microseconds,
};
}
}
/**
* Match a unit in a string. Like "1kg", "3L", etc.
* @param {string} str - String to match from
* @param {string} t - Unit to look for. Doesn't support aliases.
* @returns {number} value - Value of the unit matched
*/
function matchReg(str: string, t: string): number {
const reg = new RegExp(`(\\d+)\\s?${t}(?:[^a-z]|$)`, "i");
const matched = reg.exec(str);
if (!matched) return 0;
return parseInt(matched[1].replace(t, ""));
}
function addZero(num: number, digits = 3): string {
const arr = new Array(digits).fill(0);
return `${arr.join("").slice(0, 0 - num.toString().length)}${num}`;
};
// module.exports = Duration;
export default Duration;
export { matchReg as MatchUnit };