@elara-services/packages 4.0.3 → 4.5.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/index.d.ts +8 -0
- package/index.js +2 -1
- package/package.json +1 -1
- package/packages/duration.js +74 -0
package/index.d.ts
CHANGED
@@ -184,4 +184,12 @@ declare module "@elara-services/packages" {
|
|
184
184
|
}): string | string[]
|
185
185
|
|
186
186
|
export async function fetch(url: string, key?: string, body?: any, postRequest?: boolean, returnRaw?: boolean): Promise<object|string|null>;
|
187
|
+
|
188
|
+
|
189
|
+
export class Duration extends null {
|
190
|
+
static get timeIds(): Set<string>;
|
191
|
+
static validate(value: string): boolean;
|
192
|
+
static parse(value: string): number | null;
|
193
|
+
static determineTimeType(str: string): number;
|
194
|
+
}
|
187
195
|
}
|
package/index.js
CHANGED
@@ -6,4 +6,5 @@ exports.randomWeight = require("./packages/random/weight");
|
|
6
6
|
exports.Languages = require("./packages/languages");
|
7
7
|
exports.SlashBuilder = require("./packages/SlashBuilder");
|
8
8
|
exports.Interactions = require("./packages/Interactions");
|
9
|
-
exports.fetch = require("./packages/fetch");
|
9
|
+
exports.fetch = require("./packages/fetch");
|
10
|
+
exports.Duration = require("./packages/duration");
|
package/package.json
CHANGED
@@ -0,0 +1,74 @@
|
|
1
|
+
module.exports = class Duration extends null {
|
2
|
+
static parse(value) {
|
3
|
+
const MATCHES_ALL = value.match(/\d+\s*[A-Za-z]+/g);
|
4
|
+
if (MATCHES_ALL) {
|
5
|
+
let totalTime = 0;
|
6
|
+
for (const dur of MATCHES_ALL) {
|
7
|
+
const [ num, str ] = [
|
8
|
+
parseInt(dur.match(/\d+/g)[0]),
|
9
|
+
dur.match(/[A-Za-z]+/g)[0]
|
10
|
+
];
|
11
|
+
if (isNaN(num)) totalTime = 0;
|
12
|
+
else totalTime += num * this.determineTimeType(str);
|
13
|
+
}
|
14
|
+
if (totalTime) return totalTime;
|
15
|
+
}
|
16
|
+
|
17
|
+
return null;
|
18
|
+
};
|
19
|
+
|
20
|
+
static determineTimeType(str) {
|
21
|
+
switch (str) {
|
22
|
+
case "ms": case "millisecond": case "milliseconds": return 1;
|
23
|
+
|
24
|
+
case "s": case "second": case "seconds": return 1000;
|
25
|
+
|
26
|
+
case "m": case "min": case "mins": case "minute": case "minutes": return 60 * 1000;
|
27
|
+
|
28
|
+
case "h": case "hr": case "hour": case "hours": return 60 * 60 * 1000;
|
29
|
+
|
30
|
+
case "d": case "day": case "days": return 24 * 60 * 60 * 1000;
|
31
|
+
|
32
|
+
case "w": case "week": case "weeks": return 7 * 24 * 60 * 60 * 1000;
|
33
|
+
|
34
|
+
case "mo": case "month": case "months": return 30 * 24 * 60 * 60 * 1000;
|
35
|
+
|
36
|
+
case "y": case "year": case "years": return 365 * 24 * 60 * 60 * 1000;
|
37
|
+
|
38
|
+
default: return 1;
|
39
|
+
}
|
40
|
+
};
|
41
|
+
|
42
|
+
static validate(value) {
|
43
|
+
const MATCHES_ALL = value.match(/\d+\s*[A-Za-z]+/g);
|
44
|
+
if (MATCHES_ALL) {
|
45
|
+
for (const match of MATCHES_ALL) {
|
46
|
+
let [ num, str ] = [
|
47
|
+
match.match(/\d+/g),
|
48
|
+
match.match(/[A-Za-z]+/g)
|
49
|
+
]
|
50
|
+
if (!num || (num.length !== 1)) return false;
|
51
|
+
if (!str || (str.length !== 1)) return false;
|
52
|
+
if (!Number.isInteger(parseInt(num[0]))) return false;
|
53
|
+
if (!this.timeIds.has(str[0])) return false;
|
54
|
+
}
|
55
|
+
|
56
|
+
return true;
|
57
|
+
}
|
58
|
+
|
59
|
+
return false;
|
60
|
+
}
|
61
|
+
|
62
|
+
static get timeIds() {
|
63
|
+
return new Set([
|
64
|
+
"ms", "millisecond", "milliseconds",
|
65
|
+
"s", "second", "seconds",
|
66
|
+
"m", "min", "mins", "minute", "minutes",
|
67
|
+
"h", "hr", "hrs", "hour", "hours",
|
68
|
+
"d", "day", "days",
|
69
|
+
"w", "week", "weeks",
|
70
|
+
"mo", "month", "months",
|
71
|
+
"y", "year", "years"
|
72
|
+
])
|
73
|
+
}
|
74
|
+
}
|