@kyvrixon/utils 0.0.4 → 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 +4 -1
- package/src/index.ts +2 -1
- package/src/lib/cooldownManager.ts +71 -0
- package/src/lib/formatSeconds.ts +10 -3
- package/src/lib/logger.ts +3 -2
- package/src/lib/toOrdinal.ts +16 -0
package/package.json
CHANGED
|
@@ -1,13 +1,16 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@kyvrixon/utils",
|
|
3
3
|
"main": "./src/index.ts",
|
|
4
|
-
"version": "0.0.
|
|
4
|
+
"version": "0.0.6",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"private": false,
|
|
7
7
|
"license": "MIT",
|
|
8
8
|
"files": [
|
|
9
9
|
"src"
|
|
10
10
|
],
|
|
11
|
+
"scripts": {
|
|
12
|
+
"pub": "bun publish --access public"
|
|
13
|
+
},
|
|
11
14
|
"packageManager": "bun@1.3.6",
|
|
12
15
|
"types": "./src/index.ts",
|
|
13
16
|
"devDependencies": {
|
package/src/index.ts
CHANGED
|
@@ -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
|
+
}
|
package/src/lib/formatSeconds.ts
CHANGED
|
@@ -25,7 +25,14 @@ const ALL_UNITS_ORDER: Array<TimeUnitTypes> = [
|
|
|
25
25
|
"ms",
|
|
26
26
|
];
|
|
27
27
|
|
|
28
|
-
|
|
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
|
-
*
|
|
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
|
-
|
|
118
|
+
this.log("debug", m);
|
|
118
119
|
}
|
|
119
120
|
|
|
120
121
|
public divider(text: string): void {
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
const suffixes: Record<string, string> = {
|
|
2
|
+
one: "st",
|
|
3
|
+
two: "nd",
|
|
4
|
+
few: "rd",
|
|
5
|
+
other: "th",
|
|
6
|
+
};
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* Converts a number to its ordinal value
|
|
10
|
+
*
|
|
11
|
+
* @param n Number to convert
|
|
12
|
+
* @returns string
|
|
13
|
+
*/
|
|
14
|
+
export function toOrdinal(n: number): string {
|
|
15
|
+
return `${n}${suffixes[new Intl.PluralRules("en-US", { type: "ordinal" }).select(n)]}`;
|
|
16
|
+
}
|