@prozilla-os/shared 1.3.0 → 1.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/README.md +3 -3
- package/dist/chunks/index-PU_xBoDn.js +857 -0
- package/dist/chunks/index-PU_xBoDn.js.map +1 -0
- package/dist/chunks/object.utils-xiWGUq8N.js +15 -0
- package/dist/chunks/object.utils-xiWGUq8N.js.map +1 -0
- package/dist/logging.d.ts +711 -0
- package/dist/logging.js +17 -0
- package/dist/logging.js.map +1 -0
- package/dist/main.d.ts +785 -43
- package/dist/main.js +300 -674
- package/dist/main.js.map +1 -1
- package/dist/utils.d.ts +265 -0
- package/dist/utils.js +245 -0
- package/dist/utils.js.map +1 -0
- package/package.json +24 -9
package/dist/utils.js
ADDED
|
@@ -0,0 +1,245 @@
|
|
|
1
|
+
import { i as k, m as q } from "./chunks/object.utils-xiWGUq8N.js";
|
|
2
|
+
function d(e, t) {
|
|
3
|
+
const r = t.indexOf(e);
|
|
4
|
+
r !== -1 && t.splice(r, 1);
|
|
5
|
+
}
|
|
6
|
+
function y(e) {
|
|
7
|
+
return e[b(e)];
|
|
8
|
+
}
|
|
9
|
+
function b(e) {
|
|
10
|
+
return Math.floor(Math.random() * e.length);
|
|
11
|
+
}
|
|
12
|
+
function A(e) {
|
|
13
|
+
return e.filter((t, r) => e.indexOf(t) === r);
|
|
14
|
+
}
|
|
15
|
+
function g(e, t) {
|
|
16
|
+
return t.flatMap((r) => [e, r]).slice(1);
|
|
17
|
+
}
|
|
18
|
+
function v(e, t, r) {
|
|
19
|
+
return e.map((n) => n === t ? r : n);
|
|
20
|
+
}
|
|
21
|
+
const p = {
|
|
22
|
+
s: 1e3,
|
|
23
|
+
m: 1e3 * 60,
|
|
24
|
+
h: 1e3 * 60 * 60,
|
|
25
|
+
d: 1e3 * 60 * 60 * 24,
|
|
26
|
+
w: 1e3 * 60 * 60 * 24 * 7,
|
|
27
|
+
n: 1e3 * 60 * 60 * 24 * 31,
|
|
28
|
+
y: 1e3 * 60 * 60 * 24 * 365,
|
|
29
|
+
c: 1e3 * 60 * 60 * 24 * 365 * 100
|
|
30
|
+
};
|
|
31
|
+
function M(e, t = 3, r = !1) {
|
|
32
|
+
const n = [], o = (s, i) => {
|
|
33
|
+
if (!r)
|
|
34
|
+
return s.join(", ");
|
|
35
|
+
let u = "", c = "";
|
|
36
|
+
return i ? c = "ago" : u = "in", [u, s.join(", "), c].join(" ").trim();
|
|
37
|
+
};
|
|
38
|
+
let f = !1;
|
|
39
|
+
if (e < 0 && (e = -e, f = !0), Math.abs(e) < p.s)
|
|
40
|
+
return o(["less than a second"], f);
|
|
41
|
+
const a = [], l = {
|
|
42
|
+
s: "second",
|
|
43
|
+
m: "minute",
|
|
44
|
+
h: "hour",
|
|
45
|
+
d: "day",
|
|
46
|
+
n: "month",
|
|
47
|
+
y: "year"
|
|
48
|
+
};
|
|
49
|
+
for (const [s, i] of Object.entries(p).reverse()) {
|
|
50
|
+
if (s === "w" || s === "c")
|
|
51
|
+
continue;
|
|
52
|
+
const u = Math.floor(e / i);
|
|
53
|
+
e -= u * i, u > 0 && a.push({ amount: u, label: u != 1 ? l[s] + "s" : l[s] });
|
|
54
|
+
}
|
|
55
|
+
for (let s = 0; s < t; s++)
|
|
56
|
+
if (s < a.length) {
|
|
57
|
+
const i = a[s];
|
|
58
|
+
n.push(`${i.amount} ${i.label}`);
|
|
59
|
+
}
|
|
60
|
+
return n.length === 0 ? o(["less than a second"], f) : o(n, f);
|
|
61
|
+
}
|
|
62
|
+
function x(e, t = 3, r = !1) {
|
|
63
|
+
const n = e.valueOf() - Date.now();
|
|
64
|
+
return M(n, t, r);
|
|
65
|
+
}
|
|
66
|
+
class C {
|
|
67
|
+
#e = {};
|
|
68
|
+
/**
|
|
69
|
+
* Starts listening to an event.
|
|
70
|
+
* @param event - The event to listen to.
|
|
71
|
+
* @param listener - The function to call when the event is emitted.
|
|
72
|
+
* @returns The listener.
|
|
73
|
+
*/
|
|
74
|
+
on(t, r) {
|
|
75
|
+
return this.#e[t] === void 0 ? this.#e[t] = [r] : this.#e[t].push(r), r;
|
|
76
|
+
}
|
|
77
|
+
/**
|
|
78
|
+
* Registers an event listener that is automatically removed when called.
|
|
79
|
+
* @param event - The event to listen to.
|
|
80
|
+
* @param listener - The function to call once the event is emitted.
|
|
81
|
+
* @returns The wrapped listener.
|
|
82
|
+
*/
|
|
83
|
+
once(t, r) {
|
|
84
|
+
const n = (...o) => (this.off(t, n), r(...o));
|
|
85
|
+
return this.on(t, n);
|
|
86
|
+
}
|
|
87
|
+
/**
|
|
88
|
+
* Removes an event listener.
|
|
89
|
+
* @param event - The event to remove the listener from.
|
|
90
|
+
* @param listener - The listener to remove.
|
|
91
|
+
*/
|
|
92
|
+
off(t, r) {
|
|
93
|
+
const n = this.#e[t];
|
|
94
|
+
n !== void 0 && d(r, n);
|
|
95
|
+
}
|
|
96
|
+
/**
|
|
97
|
+
* Emits an event to all its listeners.
|
|
98
|
+
* @param event - The event to emit.
|
|
99
|
+
* @param args - The arguments to pass to the listeners.
|
|
100
|
+
*/
|
|
101
|
+
emit(t, ...r) {
|
|
102
|
+
const n = this.#e[t];
|
|
103
|
+
if (n !== void 0)
|
|
104
|
+
for (const o of n)
|
|
105
|
+
o(...r);
|
|
106
|
+
}
|
|
107
|
+
/**
|
|
108
|
+
* Emits an event and waits for all listeners to resolve.
|
|
109
|
+
* @param event - The event to emit.
|
|
110
|
+
* @param args - The arguments to pass to the listeners.
|
|
111
|
+
*/
|
|
112
|
+
async emitAsync(t, ...r) {
|
|
113
|
+
const n = this.#e[t];
|
|
114
|
+
if (n === void 0)
|
|
115
|
+
return;
|
|
116
|
+
const o = n.map((f) => f(...r));
|
|
117
|
+
await Promise.all(o);
|
|
118
|
+
}
|
|
119
|
+
}
|
|
120
|
+
function I(e, t, r) {
|
|
121
|
+
return e < t ? t : e > r ? r : e;
|
|
122
|
+
}
|
|
123
|
+
function N(e, t) {
|
|
124
|
+
return Math.random() * (t - e) + e;
|
|
125
|
+
}
|
|
126
|
+
function U(e, t) {
|
|
127
|
+
return e = Math.ceil(e), t = Math.floor(t), Math.floor(Math.random() * (t - e)) + e;
|
|
128
|
+
}
|
|
129
|
+
function $(e, t = 0) {
|
|
130
|
+
const r = Math.pow(10, t);
|
|
131
|
+
return Math.round(e * r) / r;
|
|
132
|
+
}
|
|
133
|
+
function O(e) {
|
|
134
|
+
return typeof e == "number" ? !0 : e.trim() === "" ? !1 : Number.isInteger(Number(e));
|
|
135
|
+
}
|
|
136
|
+
function j(e) {
|
|
137
|
+
return typeof e == "number" ? !0 : e.trim() === "" ? !1 : !isNaN(Number(e));
|
|
138
|
+
}
|
|
139
|
+
function T(e, t = 0) {
|
|
140
|
+
return e !== void 0 && O(e) ? Number(e) : t;
|
|
141
|
+
}
|
|
142
|
+
function z(e, t = 0) {
|
|
143
|
+
return e !== void 0 && j(e) ? parseFloat(e) : t;
|
|
144
|
+
}
|
|
145
|
+
function F(e) {
|
|
146
|
+
return e.trim().toLowerCase() === "true";
|
|
147
|
+
}
|
|
148
|
+
function R(e) {
|
|
149
|
+
return e.replace(/-([a-z])/g, (t, r) => r.toUpperCase());
|
|
150
|
+
}
|
|
151
|
+
function D(e) {
|
|
152
|
+
if (!e.length) return "";
|
|
153
|
+
let t = e[0];
|
|
154
|
+
for (let r = 1; r < e.length; r++)
|
|
155
|
+
for (; e[r].indexOf(t) !== 0; )
|
|
156
|
+
if (t = t.substring(0, t.length - 1), t === "") return "";
|
|
157
|
+
return t;
|
|
158
|
+
}
|
|
159
|
+
function m(e, t) {
|
|
160
|
+
return t < 0 || t >= e.length ? [e, ""] : [e.slice(0, t), e.slice(t + 1)];
|
|
161
|
+
}
|
|
162
|
+
function L(e, t, r = {}) {
|
|
163
|
+
const { join: n = !0 } = r, o = {}, f = Object.keys(t);
|
|
164
|
+
function a(i) {
|
|
165
|
+
return i in o || (o[i] = `{${i}}`, o[i] = l([t[i]]).join("")), o[i];
|
|
166
|
+
}
|
|
167
|
+
function l(i) {
|
|
168
|
+
for (const u of f) {
|
|
169
|
+
const c = `{${u}}`;
|
|
170
|
+
i.some((h) => h.includes(c)) && (i = v(
|
|
171
|
+
i.flatMap((h) => w(h, c)),
|
|
172
|
+
c,
|
|
173
|
+
a(u)
|
|
174
|
+
));
|
|
175
|
+
}
|
|
176
|
+
return i;
|
|
177
|
+
}
|
|
178
|
+
const s = l([e]);
|
|
179
|
+
return typeof n == "string" ? s.join(n) : n ? s.join("") : s;
|
|
180
|
+
}
|
|
181
|
+
function w(e, t) {
|
|
182
|
+
return e === "" ? [""] : g(t, e.split(t)).filter((r) => r.length);
|
|
183
|
+
}
|
|
184
|
+
function P(e) {
|
|
185
|
+
return e.length <= 1 ? e.toUpperCase() : e.charAt(0).toUpperCase() + e.slice(1);
|
|
186
|
+
}
|
|
187
|
+
function S(e) {
|
|
188
|
+
return e.replace(/^https?:\/\/|\/$/g, "");
|
|
189
|
+
}
|
|
190
|
+
function E(e) {
|
|
191
|
+
return e.replace(/^https?:\/\/[a-z]+(\.[0-9a-z-]+)+/g, "");
|
|
192
|
+
}
|
|
193
|
+
function _(e) {
|
|
194
|
+
try {
|
|
195
|
+
return new URL(e), !0;
|
|
196
|
+
} catch {
|
|
197
|
+
return !1;
|
|
198
|
+
}
|
|
199
|
+
}
|
|
200
|
+
function B(...e) {
|
|
201
|
+
let t = "";
|
|
202
|
+
const r = /* @__PURE__ */ new Map();
|
|
203
|
+
for (const n of e) {
|
|
204
|
+
const [o, f] = m(n, n.indexOf("?"));
|
|
205
|
+
if (o.length && (t.length ? t.endsWith("/") ? t += o.replace(/^\//, "") : (o.startsWith("/") || (t += "/"), t += o) : t += o), f.length)
|
|
206
|
+
for (const a of f.split(/[?&]/)) {
|
|
207
|
+
const [l, s] = m(a, a.indexOf("="));
|
|
208
|
+
r.set(l, s);
|
|
209
|
+
}
|
|
210
|
+
}
|
|
211
|
+
return r.size && (t += "?" + r.entries().map(([n, o]) => `${n}=${o}`).toArray().join("&")), t;
|
|
212
|
+
}
|
|
213
|
+
export {
|
|
214
|
+
C as EventEmitter,
|
|
215
|
+
P as capitalize,
|
|
216
|
+
I as clamp,
|
|
217
|
+
L as fillTemplate,
|
|
218
|
+
x as formatRelativeTime,
|
|
219
|
+
M as formatTime,
|
|
220
|
+
D as getLongestCommonPrefix,
|
|
221
|
+
g as interleave,
|
|
222
|
+
k as isObject,
|
|
223
|
+
O as isValidInteger,
|
|
224
|
+
j as isValidNumber,
|
|
225
|
+
_ as isValidUrl,
|
|
226
|
+
w as isolateSubstring,
|
|
227
|
+
R as kebabToCamelCase,
|
|
228
|
+
q as mergeDeep,
|
|
229
|
+
F as parseBool,
|
|
230
|
+
z as parseOptionalFloat,
|
|
231
|
+
T as parseOptionalInteger,
|
|
232
|
+
y as randomFromArray,
|
|
233
|
+
b as randomIndex,
|
|
234
|
+
U as randomInt,
|
|
235
|
+
N as randomRange,
|
|
236
|
+
E as removeBaseUrl,
|
|
237
|
+
A as removeDuplicatesFromArray,
|
|
238
|
+
d as removeFromArray,
|
|
239
|
+
S as removeUrlProtocol,
|
|
240
|
+
v as replaceAll,
|
|
241
|
+
B as resolveUrl,
|
|
242
|
+
$ as round,
|
|
243
|
+
m as splitAt
|
|
244
|
+
};
|
|
245
|
+
//# sourceMappingURL=utils.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"utils.js","sources":["../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","../src/features/_utils/url.utils.ts"],"sourcesContent":["/**\n * Removes an item from an array.\n * @param item - The item to remove.\n */\nexport 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\n/**\n * Returns a random item from an array.\n */\nexport function randomFromArray<Type>(array: Type[]): Type {\n\treturn array[randomIndex(array)];\n}\n\n/**\n * Returns a random index within the bounds of an array.\n * @returns A random index.\n */\nexport function randomIndex(array: unknown[]) {\n\treturn Math.floor(Math.random() * array.length);\n}\n\n/**\n * Removes all duplicate items from an array.\n */\nexport function removeDuplicatesFromArray<Type>(array: Type[]): Type[] {\n\treturn array.filter((item, index) => array.indexOf(item) === index);\n}\n\n/**\n * Inserts an item in between every item in an array.\n * @param item - The item to insert.\n * @param array - The array to insert into.\n */\nexport function interleave<Type>(item: Type, array: Type[]) {\n\treturn array.flatMap((nextItem) => [item, nextItem]).slice(1);\n}\n\nexport function replaceAll<Type>(array: Type[], searchItem: Type, replaceItem: Type) {\n\treturn array.map((item) => item === searchItem ? replaceItem : item);\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 * Formats time in a human-readable format.\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 = false): 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\": \"second\",\n\t\t\"m\": \"minute\",\n\t\t\"h\": \"hour\",\n\t\t\"d\": \"day\",\n\t\t\"n\": \"month\",\n\t\t\"y\": \"year\",\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: amount != 1 ? unitLabels[key] + \"s\" : unitLabels[key] });\n\t}\n\n\tfor (let i = 0; i < maxLength; i++) {\n\t\tif (i < units.length) {\n\t\t\tconst unit = units[i];\n\t\t\tresult.push(`${unit.amount} ${unit.label}`);\n\t\t}\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 * Formats 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 = false): string {\n\tconst difference = date.valueOf() - Date.now();\n\treturn formatTime(difference, maxLength, allowAffixes);\n};","import { removeFromArray } from \"./array.utils\";\n\n/**\n * A function that handles an event.\n * @typeParam T - A record of all events.\n * @typeParam K - The event this function handles.\n */\nexport type Listener<T extends Record<keyof T, unknown[]>, K extends keyof T> = (...args: T[K]) => void | Promise<void>;\n\n/**\n * A simple event emitter.\n * @typeParam T - A record of all events.\n */\nexport class EventEmitter<T extends Record<keyof T, unknown[]>> {\n\t#listeners: {\n\t\t[K in keyof T]?: Listener<T, K>[];\n\t} = {};\n\n\t/**\n\t * Starts listening to an event.\n\t * @param event - The event to listen to.\n\t * @param listener - The function to call when the event is emitted.\n\t * @returns The listener.\n\t */\n\tpublic on<K extends keyof T>(event: K, listener: Listener<T, K>) {\n\t\tif (this.#listeners[event] === undefined) {\n\t\t\tthis.#listeners[event] = [listener];\n\t\t} else {\n\t\t\tthis.#listeners[event].push(listener);\n\t\t}\n\t\treturn listener;\n\t}\n\n\t/**\n\t * Registers an event listener that is automatically removed when called.\n\t * @param event - The event to listen to.\n\t * @param listener - The function to call once the event is emitted.\n\t * @returns The wrapped listener.\n\t */\n\tpublic once<K extends keyof T>(event: K, listener: Listener<T, K>) {\n\t\tconst wrapper: Listener<T, K> = (...args) => {\n\t\t\tthis.off(event, wrapper);\n\t\t\treturn listener(...args);\n\t\t};\n\t\treturn this.on(event, wrapper);\n\t}\n\n\t/**\n\t * Removes an event listener.\n\t * @param event - The event to remove the listener from.\n\t * @param listener - The listener to remove.\n\t */\n\tpublic off<K extends keyof T>(event: K, listener: Listener<T, K>): void {\n\t\tconst listeners = this.#listeners[event];\n\t\tif (listeners === undefined)\n\t\t\treturn;\n\t\t\n\t\tremoveFromArray(listener, listeners);\n\t}\n\n\t/**\n\t * Emits an event to all its listeners.\n\t * @param event - The event to emit.\n\t * @param args - The arguments to pass to the listeners.\n\t */\n\tpublic emit<K extends keyof T>(event: K, ...args: T[K]): void {\n\t\tconst listeners = this.#listeners[event];\n\t\tif (listeners === undefined)\n\t\t\treturn;\n\n\t\tfor (const listener of listeners) {\n\t\t\tvoid listener(...args);\n\t\t}\n\t}\n\n\t/**\n\t * Emits an event and waits for all listeners to resolve.\n\t * @param event - The event to emit.\n\t * @param args - The arguments to pass to the listeners.\n\t */\n\tpublic async emitAsync<K extends keyof T>(event: K, ...args: T[K]): Promise<void> {\n\t\tconst listeners = this.#listeners[event];\n\t\tif (listeners === undefined)\n\t\t\treturn;\n\n\t\tconst promises = listeners.map((listener) => listener(...args));\n\t\tawait Promise.all(promises);\n\t}\n}","/**\n * Clamps a value between an upper and lower bound.\n * @param value - The value to clamp.\n * @param min - The lower bound.\n * @param max - The upper bound.\n * @returns The clamped value.\n */\nexport 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\n/**\n * Returns a random value inside a range.\n * @param min - The lower bound.\n * @param max - The upper bound.\n */\nexport function randomRange(min: number, max: number): number {\n\treturn Math.random() * (max - min) + min;\n}\n\n/**\n * Returns a random integer inside a range.\n * @param min - The lower bound (inclusive).\n * @param max - The upper bound (exclusive).\n */\nexport function randomInt(min: number, max: number): number {\n\tmin = Math.ceil(min);\n\tmax = Math.floor(max);\n\treturn Math.floor(Math.random() * (max - min)) + min;\n}\n\n/**\n * Rounds a value.\n * @param value - The value to round.\n * @param precision - The precision to round with.\n */\nexport function round(value: number, precision: number = 0): number {\n\tconst factor = Math.pow(10, precision);\n\treturn Math.round(value * factor) / factor;\n}","/**\n * Checks if `input` is a valid integer.\n */\nexport function isValidInteger(input: number | string): boolean {\n\tif (typeof input === \"number\") return true;\n\tif (input.trim() === \"\") return false;\n\treturn Number.isInteger(Number(input));\n}\n\n/**\n * Checks if `input` is a valid number.\n */\nexport function isValidNumber(input: number | string): boolean {\n\tif (typeof input === \"number\") return true;\n\tif (input.trim() === \"\") return false;\n\treturn !isNaN(Number(input));\n}\n\nexport function parseOptionalInteger(input?: string, defaultValue = 0) {\n\treturn input !== undefined && isValidInteger(input) ? Number(input) : defaultValue;\n}\n\nexport function parseOptionalFloat(input?: string, defaultValue = 0) {\n\treturn input !== undefined && isValidNumber(input) ? parseFloat(input) : defaultValue;\n}","import { interleave, replaceAll } from \"./array.utils\";\n\n/**\n * Converts a string to a boolean.\n * @param input - The string to parse.\n */\nexport function parseBool(input: string) {\n\treturn input.trim().toLowerCase() === \"true\";\n}\n\n/**\n * Converts a kebab-case string to camelCase.\n */\nexport function kebabToCamelCase(value: string): string {\n\treturn value.replace(/-([a-z])/g, (_substring, letter: string) => letter.toUpperCase());\n}\n\n/**\n * Returns the longest common prefix from a list of strings.\n */\nexport function getLongestCommonPrefix(strings: string[]) {\n\tif (!strings.length) return \"\";\n \n\tlet prefix = strings[0];\n\tfor (let i = 1; i < strings.length; i++) {\n\t\twhile (strings[i].indexOf(prefix) !== 0) {\n\t\t\tprefix = prefix.substring(0, prefix.length - 1);\n\t\t\tif (prefix === \"\") return \"\";\n\t\t}\n\t}\n\treturn prefix;\n}\n\n/**\n * Splits a string into two strings at a given index.\n * @param string - The string to split.\n * @param index - The index to split at.\n * @returns The parts of the string before and after the character at the given index.\n */\nexport function splitAt(string: string, index: number): [string, string] {\n\tif (index < 0 || index >= string.length)\n\t\treturn [string, \"\"];\n\treturn [string.slice(0, index), string.slice(index + 1)];\n}\n\nexport function fillTemplate(template: string, properties: Record<string, string>, options: { join: false }): string[]\nexport function fillTemplate(template: string, properties: Record<string, string>, options?: { join?: true | string }): string\nexport function fillTemplate(template: string, properties: Record<string, string>, options: { join?: boolean | string } = {}): string[] | string {\n\tconst { join = true } = options;\n\tconst cache: Record<string, string> = {};\n\tconst keys = Object.keys(properties);\n\n\tfunction resolve(key: string): string {\n\t\tif (key in cache)\n\t\t\treturn cache[key];\n\t\tcache[key] = `{${key}}`;\n\t\tcache[key] = expand([properties[key]]).join(\"\");\n\t\treturn cache[key];\n\t}\n\n\tfunction expand(segments: string[]): string[] {\n\t\tfor (const key of keys) {\n\t\t\tconst placeholder = `{${key}}`;\n\n\t\t\tif (!segments.some((segment) => segment.includes(placeholder)))\n\t\t\t\tcontinue;\n\n\t\t\tsegments = replaceAll(\n\t\t\t\tsegments.flatMap((segment) => isolateSubstring(segment, placeholder)),\n\t\t\t\tplaceholder,\n\t\t\t\tresolve(key)\n\t\t\t);\n\t\t}\n\t\treturn segments;\n\t}\n\n\tconst segments = expand([template]);\n\tif (typeof join === \"string\") {\n\t\treturn segments.join(join);\n\t} else if (join) {\n\t\treturn segments.join(\"\");\n\t}\n\treturn segments;\n}\n\n/**\n * Splits a string into an array of substrings where all substrings matching {@link searchValue} are separate items.\n * @param string - The string to split.\n * @param searchValue - The value to isolate from other substrings.\n * @returns An array of substrings matching {@link searchValue} and the rest of the string.\n */\nexport function isolateSubstring(string: string, searchValue: string) {\n\tif (string === \"\") return [\"\"];\n\treturn interleave(searchValue, string.split(searchValue)).filter((item) => item.length);\n}\n\n/**\n * Transforms the first character of a string to uppercase.\n * @param string - The string to capitalize.\n * @returns The string with the first character transformed to uppercase.\n */\nexport function capitalize<S extends string = string>(string: S): Capitalize<S> {\n\treturn (string.length <= 1\n\t\t? string.toUpperCase()\n\t\t: string.charAt(0).toUpperCase() + string.slice(1)) as Capitalize<S>;\n}","import { splitAt } from \"./string.utils\";\n\n/**\n * Removes the URL protocol from a URL string.\n * @param url - The URL string.\n * @returns The URL string without its protocol.\n * @example\n * removeUrlProtocol(\"https://example.com/foo/bar\") === \"example.com/foo/bar\"\n */\nexport function removeUrlProtocol(url: string) {\n\treturn url.replace(/^https?:\\/\\/|\\/$/g, \"\");\n}\n\n/**\n * Removes the origin/base from a URL string.\n * @param url - The URL string.\n * @returns The URL string without its origin.\n * @example\n * removeBaseUrl(\"https://example.com/foo/bar\") === \"/foo/bar\"\n */\nexport function removeBaseUrl(url: string) {\n\treturn url.replace(/^https?:\\/\\/[a-z]+(\\.[0-9a-z-]+)+/g, \"\");\n}\n\n/**\n * Checks whether a string is a valid URL.\n * @param string - The string to check.\n * @returns `true` if {@link string} is a URL.\n */\nexport function isValidUrl(string: string) {\n\ttry {\n\t\tnew URL(string);\n\t\treturn true;\n\t} catch (_error) {\n\t\treturn false;\n\t}\n}\n\n/**\n * Combines an array of URL segments into a URL string.\n * @param segments - The segments of the URL.\n * @returns The combined URL.\n */\nexport function resolveUrl(...segments: string[]) {\n\tlet path = \"\";\n\tconst parameters = new Map<string, string>();\n\n\tfor (const segment of segments) {\n\t\tconst [pathSegment, parametersSegment] = splitAt(segment, segment.indexOf(\"?\"));\n\n\t\tif (pathSegment.length) {\n\t\t\tif (!path.length) {\n\t\t\t\tpath += pathSegment;\n\t\t\t} else if (path.endsWith(\"/\")) {\n\t\t\t\tpath += pathSegment.replace(/^\\//, \"\");\n\t\t\t} else {\n\t\t\t\tif (!pathSegment.startsWith(\"/\"))\n\t\t\t\t\tpath += \"/\";\n\t\t\t\tpath += pathSegment;\n\t\t\t}\n\t\t}\n\n\t\tif (parametersSegment.length) {\n\t\t\tfor (const parameter of parametersSegment.split(/[?&]/)) {\n\t\t\t\tconst [key, value] = splitAt(parameter, parameter.indexOf(\"=\"));\n\t\t\t\tparameters.set(key, value);\n\t\t\t}\n\t\t}\n\t}\n\n\tif (parameters.size)\n\t\tpath += \"?\" + parameters.entries().map(([key, value]) => `${key}=${value}`).toArray().join(\"&\");\n\treturn path;\n}"],"names":["removeFromArray","item","array","index","randomFromArray","randomIndex","removeDuplicatesFromArray","interleave","nextItem","replaceAll","searchItem","replaceItem","TIME_INDICATORS","formatTime","time","maxLength","allowAffixes","result","formatResult","inPast","prefix","suffix","units","unitLabels","key","value","amount","i","unit","formatRelativeTime","date","difference","EventEmitter","#listeners","event","listener","wrapper","args","listeners","promises","clamp","min","max","randomRange","randomInt","round","precision","factor","isValidInteger","input","isValidNumber","parseOptionalInteger","defaultValue","parseOptionalFloat","parseBool","kebabToCamelCase","_substring","letter","getLongestCommonPrefix","strings","splitAt","string","fillTemplate","template","properties","options","join","cache","keys","resolve","expand","segments","placeholder","segment","isolateSubstring","searchValue","capitalize","removeUrlProtocol","url","removeBaseUrl","isValidUrl","resolveUrl","path","parameters","pathSegment","parametersSegment","parameter"],"mappings":";AAIO,SAASA,EAAsBC,GAAYC,GAAe;AAChE,QAAMC,IAAQD,EAAM,QAAQD,CAAI;AAChC,EAAIE,MAAU,MACbD,EAAM,OAAOC,GAAO,CAAC;AAEvB;AAKO,SAASC,EAAsBF,GAAqB;AAC1D,SAAOA,EAAMG,EAAYH,CAAK,CAAC;AAChC;AAMO,SAASG,EAAYH,GAAkB;AAC7C,SAAO,KAAK,MAAM,KAAK,OAAA,IAAWA,EAAM,MAAM;AAC/C;AAKO,SAASI,EAAgCJ,GAAuB;AACtE,SAAOA,EA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AiB;AAChB,WAAO;AAAA,EACR;AACD;AAOO,SAASoB,KAAcV,GAAoB;AACjD,MAAIW,IAAO;AACX,QAAMC,wBAAiB,IAAA;AAEvB,aAAWV,KAAWF,GAAU;AAC/B,UAAM,CAACa,GAAaC,CAAiB,IAAIzB,EAAQa,GAASA,EAAQ,QAAQ,GAAG,CAAC;AAc9E,QAZIW,EAAY,WACVF,EAAK,SAECA,EAAK,SAAS,GAAG,IAC3BA,KAAQE,EAAY,QAAQ,OAAO,EAAE,KAEhCA,EAAY,WAAW,GAAG,MAC9BF,KAAQ,MACTA,KAAQE,KANRF,KAAQE,IAUNC,EAAkB;AACrB,iBAAWC,KAAaD,EAAkB,MAAM,MAAM,GAAG;AACxD,cAAM,CAAC7D,GAAKC,CAAK,IAAImC,EAAQ0B,GAAWA,EAAU,QAAQ,GAAG,CAAC;AAC9D,QAAAH,EAAW,IAAI3D,GAAKC,CAAK;AAAA,MAC1B;AAAA,EAEF;AAEA,SAAI0D,EAAW,SACdD,KAAQ,MAAMC,EAAW,QAAA,EAAU,IAAI,CAAC,CAAC3D,GAAKC,CAAK,MAAM,GAAGD,CAAG,IAAIC,CAAK,EAAE,EAAE,QAAA,EAAU,KAAK,GAAG,IACxFyD;AACR;"}
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@prozilla-os/shared",
|
|
3
3
|
"description": "A library of shared utility functions for ProzillaOS packages.",
|
|
4
|
-
"version": "1.
|
|
4
|
+
"version": "1.5.0",
|
|
5
5
|
"homepage": "https://os.prozilla.dev",
|
|
6
6
|
"author": {
|
|
7
7
|
"name": "Prozilla",
|
|
@@ -12,6 +12,21 @@
|
|
|
12
12
|
"type": "module",
|
|
13
13
|
"main": "dist/main.js",
|
|
14
14
|
"types": "dist/main.d.ts",
|
|
15
|
+
"exports": {
|
|
16
|
+
".": {
|
|
17
|
+
"types": "./dist/main.d.ts",
|
|
18
|
+
"default": "./dist/main.js"
|
|
19
|
+
},
|
|
20
|
+
"./logging": {
|
|
21
|
+
"types": "./dist/logging.d.ts",
|
|
22
|
+
"default": "./dist/logging.js"
|
|
23
|
+
},
|
|
24
|
+
"./utils": {
|
|
25
|
+
"types": "./dist/utils.d.ts",
|
|
26
|
+
"default": "./dist/utils.js"
|
|
27
|
+
},
|
|
28
|
+
"./package.json": "./package.json"
|
|
29
|
+
},
|
|
15
30
|
"repository": {
|
|
16
31
|
"type": "git",
|
|
17
32
|
"url": "git+https://github.com/prozilla-os/ProzillaOS.git",
|
|
@@ -19,14 +34,13 @@
|
|
|
19
34
|
},
|
|
20
35
|
"license": "MIT",
|
|
21
36
|
"devDependencies": {
|
|
22
|
-
"@types/node": "^20.
|
|
23
|
-
"
|
|
24
|
-
"
|
|
25
|
-
"typescript": "^5.5.4",
|
|
26
|
-
"vite": "^7.3.1",
|
|
37
|
+
"@types/node": "^20.19.39",
|
|
38
|
+
"typescript": "^5.9.3",
|
|
39
|
+
"vite": "^7.3.2",
|
|
27
40
|
"vite-plugin-dts": "^4.5.4",
|
|
28
|
-
"vite-plugin-
|
|
29
|
-
"
|
|
41
|
+
"vite-plugin-externalize-deps": "^0.10.0",
|
|
42
|
+
"vite-plugin-lib-inject-css": "^2.2.2",
|
|
43
|
+
"vitest": "^4.1.5"
|
|
30
44
|
},
|
|
31
45
|
"files": [
|
|
32
46
|
"dist"
|
|
@@ -37,6 +51,7 @@
|
|
|
37
51
|
},
|
|
38
52
|
"scripts": {
|
|
39
53
|
"build": "tsc && vite build",
|
|
40
|
-
"test": "vitest"
|
|
54
|
+
"test": "vitest",
|
|
55
|
+
"lint": "pnpm eslint ."
|
|
41
56
|
}
|
|
42
57
|
}
|