@elara-services/packages 4.0.3 → 4.6.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 CHANGED
@@ -184,4 +184,22 @@ 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
+ };
195
+
196
+ export class Tasks extends null {
197
+ static create(options: {
198
+ id: string;
199
+ time: string;
200
+ shouldCancel?: boolean
201
+ }, run: Function): void;
202
+
203
+ static delete(id: string): void;
204
+ }
187
205
  }
package/index.js CHANGED
@@ -6,4 +6,6 @@ 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");
11
+ exports.Tasks = require("./packages/Tasks");
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@elara-services/packages",
3
- "version": "4.0.3",
3
+ "version": "4.6.0",
4
4
  "description": "",
5
5
  "main": "index.js",
6
6
  "types": "./index.d.ts",
@@ -0,0 +1,22 @@
1
+ let nodeSchedule;
2
+
3
+ try {
4
+ nodeSchedule = require("node-schedule");
5
+ } catch { };
6
+
7
+ module.exports = class Tasks extends null {
8
+ static create({ id = "", time = "", shouldCancel = true } = {}, run) {
9
+ if (!nodeSchedule) return `[Tasks:Create:ERROR]: I was unable to locate the 'node-schedule' package!`;
10
+ if (!id || !time || !run) return `[Tasks:Create:ERROR]: You didn't provide a valid ID, Time or Run function`;
11
+ if (nodeSchedule.scheduledJobs[id]) return `[Tasks:Create:ERROR]: Found (${id}) already in the scheduledJobs object.`;
12
+ return nodeSchedule.scheduleJob(id, time, () => {
13
+ run();
14
+ if (shouldCancel) return nodeSchedule.cancelJob(id);
15
+ });
16
+ };
17
+
18
+ static delete(id) {
19
+ if (!nodeSchedule) return null;
20
+ return nodeSchedule.cancelJob(id);
21
+ };
22
+ };
@@ -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 * module.exports.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 (!module.exports.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
+ }