@kyvrixon/utils 0.0.5 → 0.0.6

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/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@kyvrixon/utils",
3
3
  "main": "./src/index.ts",
4
- "version": "0.0.5",
4
+ "version": "0.0.6",
5
5
  "type": "module",
6
6
  "private": false,
7
7
  "license": "MIT",
@@ -9,7 +9,7 @@
9
9
  "src"
10
10
  ],
11
11
  "scripts": {
12
- "pub": "bun buildIndex.ts && bun publish --access public"
12
+ "pub": "bun publish --access public"
13
13
  },
14
14
  "packageManager": "bun@1.3.6",
15
15
  "types": "./src/index.ts",
@@ -0,0 +1,71 @@
1
+ export class CooldownManager {
2
+ public cds = new Map<string, Map<string, number>>();
3
+
4
+ set(uid: string, cmd: string, ttl: number): void {
5
+ if (ttl <= 0) return;
6
+
7
+ const expiry = Date.now() + ttl;
8
+ let userCmds = this.cds.get(uid);
9
+
10
+ if (!userCmds) {
11
+ userCmds = new Map();
12
+ this.cds.set(uid, userCmds);
13
+ }
14
+
15
+ userCmds.set(cmd, expiry);
16
+ }
17
+
18
+ has(uid: string, cmd: string): false | number {
19
+ const userCmds = this.cds.get(uid);
20
+ if (!userCmds) return false;
21
+
22
+ const expiry = userCmds.get(cmd);
23
+ if (!expiry) return false;
24
+
25
+ const remaining = expiry - Date.now();
26
+ if (remaining > 0) return remaining;
27
+
28
+ userCmds.delete(cmd);
29
+
30
+ if (userCmds.size === 0) this.cds.delete(uid);
31
+ return false;
32
+ }
33
+
34
+ del(uid: string, cmd: string): void {
35
+ const userCmds = this.cds.get(uid);
36
+ if (!userCmds) return;
37
+
38
+ userCmds.delete(cmd);
39
+
40
+ if (userCmds.size === 0) this.cds.delete(uid);
41
+ }
42
+
43
+ list(): Record<string, Record<string, number>> {
44
+ const result: ReturnType<CooldownManager["list"]> = {};
45
+
46
+ for (const [uid, cmds] of this.cds) {
47
+ const cmdsObj: Record<string, number> = {};
48
+ for (const [cmd, expiry] of cmds) {
49
+ cmdsObj[cmd] = expiry;
50
+ }
51
+ result[uid] = cmdsObj;
52
+ }
53
+
54
+ return result;
55
+ }
56
+
57
+ clean(): void {
58
+ const now = Date.now();
59
+ for (const [uid, cmds] of this.cds) {
60
+ for (const [cmd, expiry] of Array.from(cmds)) {
61
+ if (expiry < now) cmds.delete(cmd);
62
+ }
63
+
64
+ if (cmds.size === 0) this.cds.delete(uid);
65
+ }
66
+ }
67
+
68
+ clear(): void {
69
+ void this.cds.clear();
70
+ }
71
+ }
@@ -25,7 +25,14 @@ const ALL_UNITS_ORDER: Array<TimeUnitTypes> = [
25
25
  "ms",
26
26
  ];
27
27
 
28
- export const formatSeconds = (
28
+ /**
29
+ * Format a number to a customisable and readable time string
30
+ *
31
+ * @param seconds Number in seconds to format
32
+ * @param options Options for formatting
33
+ * @returns string
34
+ */
35
+ export function formatSeconds(
29
36
  seconds: number,
30
37
  options: {
31
38
  includeZeroUnits?: boolean;
@@ -37,7 +44,7 @@ export const formatSeconds = (
37
44
  label: string,
38
45
  ) => string;
39
46
  } = {},
40
- ): string => {
47
+ ): string {
41
48
  const includeZeroUnits = options.includeZeroUnits ?? false;
42
49
  const onlyUnits = options.onlyUnits ?? [];
43
50
  const format = options.format ?? "long";
@@ -132,4 +139,4 @@ export const formatSeconds = (
132
139
  }
133
140
 
134
141
  return parts.join(format === "short" ? " " : ",");
135
- };
142
+ }
package/src/lib/logger.ts CHANGED
@@ -12,7 +12,7 @@ const formatter = new Intl.DateTimeFormat("en-AU", {
12
12
  });
13
13
 
14
14
  /**
15
- * To enable debuging make the env variable `__DEBUG_MODE` equal to "1"
15
+ * Console logger
16
16
  */
17
17
  export class Logger {
18
18
  private readonly colors: Record<LogLevel, typeof chalk> = {
@@ -60,6 +60,7 @@ export class Logger {
60
60
  return (
61
61
  line
62
62
  .trim()
63
+ // TODO Need to do something with this
63
64
  // .replace(/\\/g, "/")
64
65
  // .replace(
65
66
  // /(.*):(\d+):(\d+)/,
@@ -114,7 +115,7 @@ export class Logger {
114
115
  * @param m Message to display
115
116
  */
116
117
  public debug(m: unknown) {
117
- if ("__DEBUG_MODE" in env && env.__DEBUG_MODE === "1") this.log("debug", m);
118
+ this.log("debug", m);
118
119
  }
119
120
 
120
121
  public divider(text: string): void {