@prozilla-os/shared 1.1.1 → 1.2.1

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/LICENSE CHANGED
@@ -1,21 +1,21 @@
1
- MIT License
2
-
3
- Copyright (c) 2023 Sieben De Beule
4
-
5
- Permission is hereby granted, free of charge, to any person obtaining a copy
6
- of this software and associated documentation files (the "Software"), to deal
7
- in the Software without restriction, including without limitation the rights
8
- to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
- copies of the Software, and to permit persons to whom the Software is
10
- furnished to do so, subject to the following conditions:
11
-
12
- The above copyright notice and this permission notice shall be included in all
13
- copies or substantial portions of the Software.
14
-
15
- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
- IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
- FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
- AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
- LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
- OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
1
+ MIT License
2
+
3
+ Copyright (c) 2023 Sieben De Beule
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
21
  SOFTWARE.
package/README.md CHANGED
@@ -1,63 +1,40 @@
1
- <div align="center">
2
- <br />
3
- <p>
4
- <a href="https://os.prozilla.dev/"><img src="https://os.prozilla.dev/assets/logo.svg?v=2" height="200" alt="ProzillaOS" /></a>
5
- </p>
6
- <p>
7
- <a href="https://github.com/prozilla-os/ProzillaOS/blob/main/LICENSE.md"><img alt="License" src="https://img.shields.io/github/license/Prozilla/ProzillaOS?style=flat-square&color=FF4D5B&label=License"></a>
8
- <a href="https://github.com/prozilla-os/ProzillaOS"><img alt="Stars" src="https://img.shields.io/github/stars/Prozilla/ProzillaOS?style=flat-square&color=FED24C&label=%E2%AD%90"></a>
9
- <a href="https://github.com/prozilla-os/ProzillaOS"><img alt="Forks" src="https://img.shields.io/github/forks/Prozilla/ProzillaOS?style=flat-square&color=4D9CFF&label=Forks&logo=github"></a>
10
- <a href="https://www.npmjs.com/package/prozilla-os"><img alt="NPM Version" src="https://img.shields.io/npm/v/prozilla-os?logo=npm&style=flat-square&label=prozilla-os&color=FF4D5B"></a>
11
- </p>
12
- </div>
13
-
14
- ## About
15
-
16
- `@prozilla-os/shared` is a library of shared utility functions for ProzillaOS packages.
17
-
18
- ## Installation
19
-
20
- ```sh
21
- $ npm install @prozilla-os/shared
22
- $ yarn add @prozilla-os/shared
23
- $ pnpm add @prozilla-os/shared
24
- ```
25
-
26
- ## Usage
27
-
28
- ### `appViteConfig(basePath, entryPath)`
29
-
30
- Helper function for creating Vite configurations for ProzillaOS apps.
31
-
32
- #### Params
33
-
34
- - `basePath` (string) - Path of base directory
35
- - `entryPath` (string) - Path of library entry
36
-
37
- #### Example
38
- ```tsx
39
- // vite.config.ts
40
-
41
- import { defineConfig } from "vite";
42
- import { appViteConfig } from "@prozilla-os/shared";
43
-
44
- export default defineConfig({
45
- ...appViteConfig(__dirname, "src/main.ts")
46
- });
47
- ```
48
-
49
- ## Links
50
-
51
- - [Demo][demo]
52
- - [Docs][docs]
53
- - [GitHub][github]
54
- - [npm][npm]
55
- - [Discord][discord]
56
- - [Ko-fi][ko-fi]
57
-
58
- [demo]: https://os.prozilla.dev/
59
- [docs]: [docs]: https://os.prozilla.dev/docs/reference/shared
60
- [github]: https://github.com/prozilla-os/ProzillaOS/tree/main/packages/shared
61
- [npm]: https://www.npmjs.com/package/@prozilla-os/shared
62
- [discord]: https://discord.gg/JwbyQP4tdz
63
- [ko-fi]: https://ko-fi.com/prozilla
1
+ <div align="center">
2
+ <br />
3
+ <p>
4
+ <a href="https://os.prozilla.dev/"><img src="https://os.prozilla.dev/assets/logo.svg?v=2" height="200" alt="ProzillaOS" /></a>
5
+ </p>
6
+ <p>
7
+ <a href="https://github.com/prozilla-os/ProzillaOS/blob/main/LICENSE.md"><img alt="License" src="https://img.shields.io/github/license/Prozilla/ProzillaOS?style=flat-square&color=FF4D5B&label=License"></a>
8
+ <a href="https://github.com/prozilla-os/ProzillaOS"><img alt="Stars" src="https://img.shields.io/github/stars/Prozilla/ProzillaOS?style=flat-square&color=FED24C&label=%E2%AD%90"></a>
9
+ <a href="https://github.com/prozilla-os/ProzillaOS"><img alt="Forks" src="https://img.shields.io/github/forks/Prozilla/ProzillaOS?style=flat-square&color=4D9CFF&label=Forks&logo=github"></a>
10
+ <a href="https://www.npmjs.com/package/prozilla-os"><img alt="NPM Version" src="https://img.shields.io/npm/v/prozilla-os?logo=npm&style=flat-square&label=prozilla-os&color=FF4D5B"></a>
11
+ </p>
12
+ </div>
13
+
14
+ ## About
15
+
16
+ `@prozilla-os/shared` is a library of shared utility functions and variables for ProzillaOS packages.
17
+
18
+ ## Installation
19
+
20
+ ```sh
21
+ npm install @prozilla-os/shared
22
+ yarn add @prozilla-os/shared
23
+ pnpm add @prozilla-os/shared
24
+ ```
25
+
26
+ ## Links
27
+
28
+ - [Demo][demo]
29
+ - [Docs][docs]
30
+ - [GitHub][github]
31
+ - [npm][npm]
32
+ - [Discord][discord]
33
+ - [Ko-fi][ko-fi]
34
+
35
+ [demo]: https://os.prozilla.dev/
36
+ [docs]: https://os.prozilla.dev/docs/reference/shared
37
+ [github]: https://github.com/prozilla-os/ProzillaOS/tree/main/packages/shared
38
+ [npm]: https://www.npmjs.com/package/@prozilla-os/shared
39
+ [discord]: https://discord.gg/JwbyQP4tdz
40
+ [ko-fi]: https://ko-fi.com/prozilla
package/dist/main.d.ts CHANGED
@@ -1,12 +1,71 @@
1
- import { UserConfig } from 'vite';
1
+ export declare const ANSI: {
2
+ fg: {
3
+ black: string;
4
+ red: string;
5
+ green: string;
6
+ yellow: string;
7
+ blue: string;
8
+ magenta: string;
9
+ cyan: string;
10
+ white: string;
11
+ };
12
+ bg: {};
13
+ decoration: {
14
+ dim: string;
15
+ bold: string;
16
+ };
17
+ reset: string;
18
+ };
19
+
20
+ export declare function clamp(value: number, min: number, max: number): number;
21
+
22
+ export declare class EventEmitter<EventMap extends EventNamesMap> {
23
+ #private;
24
+ static EVENT_NAMES: EventNamesMap;
25
+ /**
26
+ * Add event listener for an event
27
+ */
28
+ on<Key extends keyof EventMap>(eventName: Key, callback: Listener): Listener;
29
+ /**
30
+ * Remove event listener for an event
31
+ */
32
+ off<Key extends keyof EventMap>(eventName: Key, callback: Listener): void;
33
+ /**
34
+ * Dispatch event
35
+ */
36
+ emit<Key extends keyof EventMap>(eventName: Key, data?: unknown): void;
37
+ }
38
+
39
+ export declare type EventNamesMap = Record<string, string>;
2
40
 
3
41
  /**
4
- * Helper function for creating Vite configurations for ProzillaOS apps
5
- * @param basePath - Path of base directory
6
- * @param entryPath - Path of library entry
7
- * @returns Vite configuration
8
- * @see https://vitejs.dev/config/
42
+ * Format a time relative to now
43
+ * @param date - The date
44
+ * @param maxLength - The maximum amount of units, e.g.: 3 => years, months, days
9
45
  */
10
- export declare const appViteConfig: (basePath: string, entryPath: string) => UserConfig;
46
+ export declare function formatRelativeTime(date: Date, maxLength: number | undefined, allowAffixes: boolean): string;
47
+
48
+ /**
49
+ * Format a time
50
+ * @param time - Time in milliseconds
51
+ * @param maxLength - The maximum amount of units, e.g.: 3 => years, months, days
52
+ */
53
+ export declare function formatTime(time: number, maxLength: number | undefined, allowAffixes: boolean): string;
54
+
55
+ export declare function isValidInteger(number: number | string): number | boolean;
56
+
57
+ export declare type Listener = (data: unknown) => void;
58
+
59
+ export declare function parseBool(input: string): boolean;
60
+
61
+ export declare function randomFromArray<Type>(array: Type[]): Type;
62
+
63
+ export declare function randomRange(min: number, max: number): number;
64
+
65
+ export declare function removeDuplicatesFromArray<Type>(array: Type[]): Type[];
66
+
67
+ export declare function removeFromArray<Type>(item: Type, array: Type[]): void;
68
+
69
+ export declare function round(value: number, precision: number): number;
11
70
 
12
71
  export { }
package/dist/main.js CHANGED
@@ -1,39 +1,139 @@
1
- import r from "@vitejs/plugin-react-swc";
2
- import i from "vite-plugin-dts";
3
- import { libInjectCss as l } from "vite-plugin-lib-inject-css";
4
- import { resolve as n, sep as t, posix as p } from "path";
5
- const f = (s, o) => {
6
- let e = n(s, o);
7
- return t === "\\" && (e = e.split(t).join(p.sep)), console.log("Using Vite config for app"), console.log("Entry: " + e), {
8
- plugins: [
9
- r(),
10
- l(),
11
- i({
12
- include: ["src"],
13
- outDir: "dist",
14
- rollupTypes: !0,
15
- strictOutput: !0,
16
- pathsToAliases: !1,
17
- tsconfigPath: "tsconfig.build.json"
18
- })
19
- ],
20
- build: {
21
- lib: {
22
- entry: e,
23
- formats: ["es"]
24
- },
25
- rollupOptions: {
26
- external: ["react", "react/jsx-runtime", "@prozilla-os/core"],
27
- output: {
28
- assetFileNames: "assets/[name][extname]",
29
- entryFileNames: "[name].js"
30
- }
31
- },
32
- sourcemap: !0
33
- }
34
- };
1
+ var g = Object.defineProperty;
2
+ var m = (t) => {
3
+ throw TypeError(t);
4
+ };
5
+ var M = (t, n, e) => n in t ? g(t, n, { enumerable: !0, configurable: !0, writable: !0, value: e }) : t[n] = e;
6
+ var d = (t, n, e) => M(t, typeof n != "symbol" ? n + "" : n, e), y = (t, n, e) => n.has(t) || m("Cannot " + e);
7
+ var i = (t, n, e) => (y(t, n, "read from private field"), e ? e.call(t) : n.get(t)), x = (t, n, e) => n.has(t) ? m("Cannot add the same private member more than once") : n instanceof WeakSet ? n.add(t) : n.set(t, e);
8
+ const A = {
9
+ fg: {
10
+ black: "\x1B[30m",
11
+ red: "\x1B[31m",
12
+ green: "\x1B[32m",
13
+ yellow: "\x1B[33m",
14
+ blue: "\x1B[34m",
15
+ magenta: "\x1B[35m",
16
+ cyan: "\x1B[36m",
17
+ white: "\x1B[37m"
18
+ },
19
+ bg: {},
20
+ decoration: {
21
+ dim: "\x1B[2m",
22
+ bold: "\x1B[1m"
23
+ },
24
+ reset: "\x1B[0m"
25
+ };
26
+ function E(t, n) {
27
+ const e = n.indexOf(t);
28
+ e !== -1 && n.splice(e, 1);
29
+ }
30
+ function O(t) {
31
+ return t[Math.floor(Math.random() * t.length)];
32
+ }
33
+ function T(t) {
34
+ return t.filter((n, e) => t.indexOf(n) === e);
35
+ }
36
+ const p = {
37
+ s: 1e3,
38
+ m: 1e3 * 60,
39
+ h: 1e3 * 60 * 60,
40
+ d: 1e3 * 60 * 60 * 24,
41
+ w: 1e3 * 60 * 60 * 24 * 7,
42
+ n: 1e3 * 60 * 60 * 24 * 31,
43
+ y: 1e3 * 60 * 60 * 24 * 365,
44
+ c: 1e3 * 60 * 60 * 24 * 365 * 100
35
45
  };
46
+ function b(t, n = 3, e) {
47
+ const s = [], a = (o, f) => {
48
+ if (!e)
49
+ return o.join(", ");
50
+ let u = "", h = "";
51
+ return f ? h = "ago" : u = "in", [u, o.join(", "), h].join(" ").trim();
52
+ };
53
+ let c = !1;
54
+ if (t < 0 && (t = -t, c = !0), Math.abs(t) < p.s)
55
+ return a(["less than a second"], c);
56
+ const l = [], B = {
57
+ s: "seconds",
58
+ m: "minutes",
59
+ h: "hours",
60
+ d: "days",
61
+ n: "months",
62
+ y: "years"
63
+ };
64
+ for (const [o, f] of Object.entries(p).reverse()) {
65
+ if (o === "w" || o === "c")
66
+ continue;
67
+ const u = Math.floor(t / f);
68
+ t -= u * f, u > 0 && l.push({ amount: u, label: B[o] });
69
+ }
70
+ for (let o = 0; o < n; o++) {
71
+ const f = l[o];
72
+ f && s.push(`${f.amount} ${f.label}`);
73
+ }
74
+ return s.length === 0 ? a(["less than a second"], c) : a(s, c);
75
+ }
76
+ function j(t, n = 3, e) {
77
+ const s = t.valueOf() - Date.now();
78
+ return b(s, n, e);
79
+ }
80
+ var r;
81
+ class w {
82
+ constructor() {
83
+ x(this, r, {});
84
+ }
85
+ /**
86
+ * Add event listener for an event
87
+ */
88
+ on(n, e) {
89
+ return i(this, r)[n] || (i(this, r)[n] = []), i(this, r)[n].push(e), e;
90
+ }
91
+ /**
92
+ * Remove event listener for an event
93
+ */
94
+ off(n, e) {
95
+ i(this, r)[n] && (i(this, r)[n] = i(this, r)[n].filter(
96
+ (s) => s !== e
97
+ ));
98
+ }
99
+ /**
100
+ * Dispatch event
101
+ */
102
+ emit(n, e) {
103
+ i(this, r)[n] && i(this, r)[n].forEach((s) => {
104
+ s(e);
105
+ });
106
+ }
107
+ }
108
+ r = new WeakMap(), d(w, "EVENT_NAMES", {});
109
+ function R(t, n, e) {
110
+ return t < n ? n : t > e ? e : t;
111
+ }
112
+ function D(t, n) {
113
+ return Math.random() * (n - t) + t;
114
+ }
115
+ function F(t, n) {
116
+ const e = Math.pow(10, n || 0);
117
+ return Math.round(t * e) / e;
118
+ }
119
+ function S(t) {
120
+ return typeof t == "number" || parseInt(t) || parseInt(t) === 0;
121
+ }
122
+ function C(t) {
123
+ return t.trim().toLowerCase() === "true";
124
+ }
36
125
  export {
37
- f as appViteConfig
126
+ A as ANSI,
127
+ w as EventEmitter,
128
+ R as clamp,
129
+ j as formatRelativeTime,
130
+ b as formatTime,
131
+ S as isValidInteger,
132
+ C as parseBool,
133
+ O as randomFromArray,
134
+ D as randomRange,
135
+ T as removeDuplicatesFromArray,
136
+ E as removeFromArray,
137
+ F as round
38
138
  };
39
139
  //# sourceMappingURL=main.js.map
package/dist/main.js.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"file":"main.js","sources":["../src/configs/app.vite.config.ts"],"sourcesContent":["import { UserConfig } from \"vite\";\r\nimport react from \"@vitejs/plugin-react-swc\";\r\nimport dts from \"vite-plugin-dts\";\r\nimport { libInjectCss } from \"vite-plugin-lib-inject-css\";\r\nimport { posix, resolve, sep } from \"path\";\r\n\r\n/**\r\n * Helper function for creating Vite configurations for ProzillaOS apps\r\n * @param basePath - Path of base directory\r\n * @param entryPath - Path of library entry\r\n * @returns Vite configuration\r\n * @see https://vitejs.dev/config/\r\n */\r\nexport const appViteConfig = (basePath: string, entryPath: string): UserConfig => {\r\n\tlet entryFile = resolve(basePath, entryPath);\r\n\r\n\t// Normalize paths for Windows compatibility\r\n\tif (sep === \"\\\\\")\r\n\t\tentryFile = entryFile.split(sep).join(posix.sep);\r\n\r\n\tconsole.log(\"Using Vite config for app\");\r\n\tconsole.log(\"Entry: \" + entryFile);\r\n\r\n\treturn {\r\n\t\tplugins: [\r\n\t\t\treact(),\r\n\t\t\tlibInjectCss(),\r\n\t\t\tdts({\r\n\t\t\t\tinclude: [\"src\"],\r\n\t\t\t\toutDir: \"dist\",\r\n\t\t\t\trollupTypes: true,\r\n\t\t\t\tstrictOutput: true,\r\n\t\t\t\tpathsToAliases: false,\r\n\t\t\t\ttsconfigPath: \"tsconfig.build.json\"\r\n\t\t\t})\r\n\t\t],\r\n\t\tbuild: {\r\n\t\t\tlib: {\r\n\t\t\t\tentry: entryFile,\r\n\t\t\t\tformats: [\"es\"],\r\n\t\t\t},\r\n\t\t\trollupOptions: {\r\n\t\t\t\texternal: [\"react\", \"react/jsx-runtime\", \"@prozilla-os/core\"],\r\n\t\t\t\toutput: {\r\n\t\t\t\t\tassetFileNames: \"assets/[name][extname]\",\r\n\t\t\t\t\tentryFileNames: \"[name].js\",\r\n\t\t\t\t}\r\n\t\t\t},\r\n\t\t\tsourcemap: true\r\n\t\t}\r\n\t};\r\n};"],"names":["appViteConfig","basePath","entryPath","entryFile","resolve","sep","posix","react","libInjectCss","dts"],"mappings":";;;;AAaa,MAAAA,IAAgB,CAACC,GAAkBC,MAAkC;AAC7E,MAAAC,IAAYC,EAAQH,GAAUC,CAAS;AAG3C,SAAIG,MAAQ,SACXF,IAAYA,EAAU,MAAME,CAAG,EAAE,KAAKC,EAAM,GAAG,IAEhD,QAAQ,IAAI,2BAA2B,GAC/B,QAAA,IAAI,YAAYH,CAAS,GAE1B;AAAA,IACN,SAAS;AAAA,MACRI,EAAM;AAAA,MACNC,EAAa;AAAA,MACbC,EAAI;AAAA,QACH,SAAS,CAAC,KAAK;AAAA,QACf,QAAQ;AAAA,QACR,aAAa;AAAA,QACb,cAAc;AAAA,QACd,gBAAgB;AAAA,QAChB,cAAc;AAAA,MAAA,CACd;AAAA,IACF;AAAA,IACA,OAAO;AAAA,MACN,KAAK;AAAA,QACJ,OAAON;AAAA,QACP,SAAS,CAAC,IAAI;AAAA,MACf;AAAA,MACA,eAAe;AAAA,QACd,UAAU,CAAC,SAAS,qBAAqB,mBAAmB;AAAA,QAC5D,QAAQ;AAAA,UACP,gBAAgB;AAAA,UAChB,gBAAgB;AAAA,QACjB;AAAA,MACD;AAAA,MACA,WAAW;AAAA,IACZ;AAAA,EAAA;AAEF;"}
1
+ {"version":3,"file":"main.js","sources":["../src/constants/ansi.const.ts","../src/features/_utils/array.utils.ts","../src/features/_utils/date.utils.ts","../src/features/_utils/event.utils.ts","../src/features/_utils/math.utils.ts","../src/features/_utils/number.utils.ts","../src/features/_utils/string.utils.ts"],"sourcesContent":["export const ANSI = {\n\tfg: {\n\t\tblack: \"\\u001b[30m\",\n\t\tred: \"\\u001b[31m\",\n\t\tgreen: \"\\u001b[32m\",\n\t\tyellow: \"\\u001b[33m\",\n\t\tblue: \"\\u001b[34m\",\n\t\tmagenta: \"\\u001b[35m\",\n\t\tcyan: \"\\u001b[36m\",\n\t\twhite: \"\\u001b[37m\",\n\t},\n\tbg: {\n\n\t},\n\tdecoration: {\n\t\tdim: \"\\u001b[2m\",\n\t\tbold: \"\\u001b[1m\",\n\t},\n\treset: \"\\u001b[0m\",\n};","export function removeFromArray<Type>(item: Type, array: Type[]) {\n\tconst index = array.indexOf(item);\n\tif (index !== -1) {\n\t\tarray.splice(index, 1);\n\t}\n}\n\nexport function randomFromArray<Type>(array: Type[]): Type {\n\treturn array[Math.floor(Math.random() * array.length)];\n}\n\nexport function removeDuplicatesFromArray<Type>(array: Type[]): Type[] {\n\treturn array.filter((item, index) => array.indexOf(item) === index);\n}","const TIME_INDICATORS = {\n\t\"s\": 1000,\n\t\"m\": 1000 * 60,\n\t\"h\": 1000 * 60 * 60,\n\t\"d\": 1000 * 60 * 60 * 24,\n\t\"w\": 1000 * 60 * 60 * 24 * 7,\n\t\"n\": 1000 * 60 * 60 * 24 * 31,\n\t\"y\": 1000 * 60 * 60 * 24 * 365,\n\t\"c\": 1000 * 60 * 60 * 24 * 365 * 100,\n};\n\n/**\n * Format a time\n * @param time - Time in milliseconds\n * @param maxLength - The maximum amount of units, e.g.: 3 => years, months, days\n */\nexport function formatTime(time: number, maxLength: number = 3, allowAffixes: boolean): string {\n\tconst result: string[] = [];\n\n\tconst formatResult = (result: string[], inPast: boolean): string => {\n\t\tif (!allowAffixes)\n\t\t\treturn result.join(\", \");\n\n\t\tlet prefix = \"\";\n\t\tlet suffix = \"\";\n\n\t\tif (inPast) {\n\t\t\tsuffix = \"ago\";\n\t\t} else {\n\t\t\tprefix = \"in\";\n\t\t}\n\n\t\treturn [prefix, result.join(\", \"), suffix].join(\" \").trim();\n\t};\n\n\tlet inPast = false;\n\n\tif (time < 0) {\n\t\ttime = -time;\n\t\tinPast = true;\n\t}\n\n\tif (Math.abs(time) < TIME_INDICATORS.s) {\n\t\treturn formatResult([\"less than a second\"], inPast);\n\t}\n\n\tconst units: { amount: number, label: string }[] = [];\n\tconst unitLabels: Record<string, string> = {\n\t\t\"s\": \"seconds\",\n\t\t\"m\": \"minutes\",\n\t\t\"h\": \"hours\",\n\t\t\"d\": \"days\",\n\t\t\"n\": \"months\",\n\t\t\"y\": \"years\",\n\t};\n\n\tfor (const [key, value] of Object.entries(TIME_INDICATORS).reverse()) {\n\t\tif (key === \"w\" || key === \"c\")\n\t\t\tcontinue;\n\n\t\tconst amount = Math.floor(time / value);\n\t\ttime -= amount * value;\n\n\t\tif (amount > 0)\n\t\t\tunits.push({ amount, label: unitLabels[key] });\n\t}\n\n\tfor (let i = 0; i < maxLength; i++) {\n\t\tconst unit = units[i];\n\n\t\tif (unit)\n\t\t\tresult.push(`${unit.amount} ${unit.label}`);\n\t}\n\n\tif (result.length === 0) {\n\t\treturn formatResult([\"less than a second\"], inPast);\n\t} else {\n\t\treturn formatResult(result, inPast);\n\t}\n};\n\n/**\n * Format a time relative to now\n * @param date - The date\n * @param maxLength - The maximum amount of units, e.g.: 3 => years, months, days\n */\nexport function formatRelativeTime(date: Date, maxLength: number = 3, allowAffixes: boolean): string {\n\tconst difference = date.valueOf() - Date.now();\n\treturn formatTime(difference, maxLength, allowAffixes);\n};","export type EventNamesMap = Record<string, string>;\nexport type Listener = (data: unknown) => void;\n\nexport class EventEmitter<EventMap extends EventNamesMap> {\n\tstatic EVENT_NAMES: EventNamesMap = {};\n\n\t#events: Record<string, Array<Listener>> = {};\n\n\t/**\n\t * Add event listener for an event\n\t */\n\ton<Key extends keyof EventMap>(eventName: Key, callback: Listener) {\n\t\tif (!this.#events[eventName as string]) {\n\t\t\tthis.#events[eventName as string] = [];\n\t\t}\n\t\tthis.#events[eventName as string].push(callback);\n\t\treturn callback;\n\t}\n\t\n\t/**\n\t * Remove event listener for an event\n\t */\n\toff<Key extends keyof EventMap>(eventName: Key, callback: Listener) {\n\t\tif (this.#events[eventName as string]) {\n\t\t\tthis.#events[eventName as string] = this.#events[eventName as string].filter(\n\t\t\t\t(listener) => listener !== callback\n\t\t\t);\n\t\t}\n\t}\n\t\n\t/**\n\t * Dispatch event\n\t */\n\temit<Key extends keyof EventMap>(eventName: Key, data?: unknown) {\n\t\tif (this.#events[eventName as string]) {\n\t\t\tthis.#events[eventName as string].forEach((listener) => {\n\t\t\t\tlistener(data);\n\t\t\t});\n\t\t}\n\t}\n}","export function clamp(value: number, min: number, max: number): number {\n\tif (value < min) {\n\t\treturn min;\n\t} else if (value > max) {\n\t\treturn max;\n\t} else {\n\t\treturn value;\n\t}\n}\n\nexport function randomRange(min: number, max: number): number {\n\treturn Math.random() * (max - min) + min;\n}\n\nexport function round(value: number, precision: number): number {\n\tconst multiplier = Math.pow(10, precision || 0);\n\treturn Math.round(value * multiplier) / multiplier;\n}","export function isValidInteger(number: number | string): number | boolean {\n\treturn (typeof number === \"number\" || parseInt(number) || parseInt(number) === 0);\n}","export function parseBool(input: string) {\r\n\treturn input.trim().toLowerCase() === \"true\";\r\n}"],"names":["ANSI","removeFromArray","item","array","index","randomFromArray","removeDuplicatesFromArray","TIME_INDICATORS","formatTime","time","maxLength","allowAffixes","result","formatResult","inPast","prefix","suffix","units","unitLabels","key","value","amount","i","unit","formatRelativeTime","date","difference","_events","EventEmitter","__privateAdd","eventName","callback","__privateGet","listener","data","__publicField","clamp","min","max","randomRange","round","precision","multiplier","isValidInteger","number","parseBool","input"],"mappings":";;;;;;;AAAO,MAAMA,IAAO;AAAA,EACnB,IAAI;AAAA,IACH,OAAO;AAAA,IACP,KAAK;AAAA,IACL,OAAO;AAAA,IACP,QAAQ;AAAA,IACR,MAAM;AAAA,IACN,SAAS;AAAA,IACT,MAAM;AAAA,IACN,OAAO;AAAA,EACR;AAAA,EACA,I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,MAAKL,GAAQG,CAAmB,MACnCE,EAAA,MAAKL,GAAQG,CAAmB,IAAIE,EAAA,MAAKL,GAAQG,CAAmB,EAAE;AAAA,MACrE,CAACG,MAAaA,MAAaF;AAAA,IAAA;AAAA,EAG9B;AAAA;AAAA;AAAA;AAAA,EAKA,KAAiCD,GAAgBI,GAAgB;AAC5D,IAAAF,EAAA,MAAKL,GAAQG,CAAmB,KACnCE,EAAA,MAAKL,GAAQG,CAAmB,EAAE,QAAQ,CAACG,MAAa;AACvD,MAAAA,EAASC,CAAI;AAAA,IAAA,CACb;AAAA,EAEH;AACD;AAlCCP,IAAA,eAFAQ,EADYP,GACL,eAA6B,CAAA;ACJrB,SAAAQ,EAAMhB,GAAeiB,GAAaC,GAAqB;AACtE,SAAIlB,IAAQiB,IACJA,IACGjB,IAAQkB,IACXA,IAEAlB;AAET;AAEgB,SAAAmB,EAAYF,GAAaC,GAAqB;AAC7D,SAAO,KAAK,OAAY,KAAAA,IAAMD,KAAOA;AACtC;AAEgB,SAAAG,EAAMpB,GAAeqB,GAA2B;AAC/D,QAAMC,IAAa,KAAK,IAAI,IAAID,KAAa,CAAC;AAC9C,SAAO,KAAK,MAAMrB,IAAQsB,CAAU,IAAIA;AACzC;ACjBO,SAASC,EAAeC,GAA2C;AACjE,SAAA,OAAOA,KAAW,YAAY,SAASA,CAAM,KAAK,SAASA,CAAM,MAAM;AAChF;ACFO,SAASC,EAAUC,GAAe;AACxC,SAAOA,EAAM,KAAA,EAAO,YAAA,MAAkB;AACvC;"}
package/package.json CHANGED
@@ -1,13 +1,14 @@
1
1
  {
2
2
  "name": "@prozilla-os/shared",
3
3
  "description": "A library of shared utility functions for ProzillaOS packages.",
4
- "version": "1.1.1",
4
+ "version": "1.2.1",
5
5
  "homepage": "https://os.prozilla.dev",
6
6
  "author": {
7
7
  "name": "Prozilla",
8
8
  "email": "business@prozilla.dev",
9
9
  "url": "https://prozilla.dev/"
10
10
  },
11
+ "funding": "https://ko-fi.com/prozilla",
11
12
  "type": "module",
12
13
  "main": "dist/main.js",
13
14
  "types": "dist/main.d.ts",
@@ -17,13 +18,12 @@
17
18
  "directory": "packages/shared"
18
19
  },
19
20
  "license": "MIT",
20
- "dependencies": {},
21
21
  "devDependencies": {
22
22
  "@types/node": "^20.14.5",
23
23
  "@types/react": "^18.3.3",
24
24
  "@vitejs/plugin-react-swc": "^3.7.0",
25
- "typescript": "^5.4.5",
26
- "vite": "^5.3.1",
25
+ "typescript": "^5.5.4",
26
+ "vite": "^5.4.8",
27
27
  "vite-plugin-dts": "^3.9.1",
28
28
  "vite-plugin-lib-inject-css": "^2.1.1"
29
29
  },