katanakit-js 2.2.1 → 2.12.9
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 +72 -437
- package/dist/adapters/astro/astro.service.d.ts +103 -15
- package/dist/adapters/astro/astro.service.d.ts.map +1 -1
- package/dist/adapters/astro/astro.service.js +145 -75
- package/dist/adapters/astro/astro.service.js.map +1 -1
- package/dist/adapters/astro/index.d.ts +1 -1
- package/dist/adapters/astro/index.d.ts.map +1 -1
- package/dist/adapters/astro/index.js +1 -1
- package/dist/adapters/astro/index.js.map +1 -1
- package/dist/adapters/astro/rss.service.d.ts +85 -105
- package/dist/adapters/astro/rss.service.d.ts.map +1 -1
- package/dist/adapters/astro/rss.service.js +172 -168
- package/dist/adapters/astro/rss.service.js.map +1 -1
- package/dist/adapters/astro/seo.service.d.ts +10 -0
- package/dist/adapters/astro/seo.service.d.ts.map +1 -0
- package/dist/adapters/astro/seo.service.js +3 -0
- package/dist/adapters/astro/seo.service.js.map +1 -0
- package/dist/adapters/express/app.d.ts +30 -1
- package/dist/adapters/express/app.d.ts.map +1 -1
- package/dist/adapters/express/app.js +31 -3
- package/dist/adapters/express/app.js.map +1 -1
- package/dist/adapters/express/index.d.ts +2 -2
- package/dist/adapters/express/index.d.ts.map +1 -1
- package/dist/adapters/express/index.js +2 -2
- package/dist/adapters/express/index.js.map +1 -1
- package/dist/adapters/express/main.js +2 -2
- package/dist/adapters/express/main.js.map +1 -1
- package/dist/adapters/express/products.controller.d.ts +67 -8
- package/dist/adapters/express/products.controller.d.ts.map +1 -1
- package/dist/adapters/express/products.controller.js +105 -49
- package/dist/adapters/express/products.controller.js.map +1 -1
- package/dist/adapters/express/server.d.ts +37 -13
- package/dist/adapters/express/server.d.ts.map +1 -1
- package/dist/adapters/express/server.js +118 -53
- package/dist/adapters/express/server.js.map +1 -1
- package/dist/adapters/nuxt/nuxt.service.d.ts +8 -6
- package/dist/adapters/nuxt/nuxt.service.d.ts.map +1 -1
- package/dist/adapters/nuxt/nuxt.service.js +34 -10
- package/dist/adapters/nuxt/nuxt.service.js.map +1 -1
- package/dist/adapters/vue/vue.service.d.ts +5 -2
- package/dist/adapters/vue/vue.service.d.ts.map +1 -1
- package/dist/adapters/vue/vue.service.js +41 -7
- package/dist/adapters/vue/vue.service.js.map +1 -1
- package/dist/config/index.d.ts +2 -1
- package/dist/config/index.d.ts.map +1 -1
- package/dist/config/index.js +1 -1
- package/dist/config/index.js.map +1 -1
- package/dist/config/seo-meta.types.d.ts +177 -0
- package/dist/config/seo-meta.types.d.ts.map +1 -0
- package/dist/config/seo-meta.types.js +11 -0
- package/dist/config/seo-meta.types.js.map +1 -0
- package/dist/config/seo.service.d.ts +41 -58
- package/dist/config/seo.service.d.ts.map +1 -1
- package/dist/config/seo.service.js +468 -136
- package/dist/config/seo.service.js.map +1 -1
- package/dist/config/site.config.d.ts +10 -50
- package/dist/config/site.config.d.ts.map +1 -1
- package/dist/config/site.config.js +8 -36
- package/dist/config/site.config.js.map +1 -1
- package/dist/core/index.d.ts +1 -3
- package/dist/core/index.d.ts.map +1 -1
- package/dist/core/index.js +1 -3
- package/dist/core/index.js.map +1 -1
- package/dist/core/services/dates.service.d.ts +149 -22
- package/dist/core/services/dates.service.d.ts.map +1 -1
- package/dist/core/services/dates.service.js +201 -54
- package/dist/core/services/dates.service.js.map +1 -1
- package/dist/core/services/error.service.d.ts +32 -20
- package/dist/core/services/error.service.d.ts.map +1 -1
- package/dist/core/services/error.service.js +44 -32
- package/dist/core/services/error.service.js.map +1 -1
- package/dist/core/services/formatter.service.d.ts +245 -35
- package/dist/core/services/formatter.service.d.ts.map +1 -1
- package/dist/core/services/formatter.service.js +281 -57
- package/dist/core/services/formatter.service.js.map +1 -1
- package/dist/core/services/generator.service.d.ts +115 -26
- package/dist/core/services/generator.service.d.ts.map +1 -1
- package/dist/core/services/generator.service.js +191 -56
- package/dist/core/services/generator.service.js.map +1 -1
- package/dist/core/services/geometry.service.d.ts +369 -39
- package/dist/core/services/geometry.service.d.ts.map +1 -1
- package/dist/core/services/geometry.service.js +424 -66
- package/dist/core/services/geometry.service.js.map +1 -1
- package/dist/core/services/http.service.d.ts +244 -21
- package/dist/core/services/http.service.d.ts.map +1 -1
- package/dist/core/services/http.service.js +419 -98
- package/dist/core/services/http.service.js.map +1 -1
- package/dist/core/services/logger.service.d.ts +95 -29
- package/dist/core/services/logger.service.d.ts.map +1 -1
- package/dist/core/services/logger.service.js +167 -41
- package/dist/core/services/logger.service.js.map +1 -1
- package/dist/core/services/reactive.service.d.ts +146 -19
- package/dist/core/services/reactive.service.d.ts.map +1 -1
- package/dist/core/services/reactive.service.js +278 -137
- package/dist/core/services/reactive.service.js.map +1 -1
- package/dist/core/services/timing.service.d.ts +142 -20
- package/dist/core/services/timing.service.d.ts.map +1 -1
- package/dist/core/services/timing.service.js +324 -185
- package/dist/core/services/timing.service.js.map +1 -1
- package/dist/core/services/utils.service.d.ts +228 -43
- package/dist/core/services/utils.service.d.ts.map +1 -1
- package/dist/core/services/utils.service.js +305 -125
- package/dist/core/services/utils.service.js.map +1 -1
- package/dist/infrastructure/dom/dom.service.d.ts +307 -44
- package/dist/infrastructure/dom/dom.service.d.ts.map +1 -1
- package/dist/infrastructure/dom/dom.service.js +408 -129
- package/dist/infrastructure/dom/dom.service.js.map +1 -1
- package/dist/infrastructure/index.d.ts +0 -3
- package/dist/infrastructure/index.d.ts.map +1 -1
- package/dist/infrastructure/index.js +0 -3
- package/dist/infrastructure/index.js.map +1 -1
- package/dist/infrastructure/observer/observer.service.d.ts +124 -31
- package/dist/infrastructure/observer/observer.service.d.ts.map +1 -1
- package/dist/infrastructure/observer/observer.service.js +227 -131
- package/dist/infrastructure/observer/observer.service.js.map +1 -1
- package/dist/infrastructure/sensors/sensors.service.d.ts +154 -22
- package/dist/infrastructure/sensors/sensors.service.d.ts.map +1 -1
- package/dist/infrastructure/sensors/sensors.service.js +264 -112
- package/dist/infrastructure/sensors/sensors.service.js.map +1 -1
- package/dist/infrastructure/storage/storage.service.d.ts +123 -45
- package/dist/infrastructure/storage/storage.service.d.ts.map +1 -1
- package/dist/infrastructure/storage/storage.service.js +236 -66
- package/dist/infrastructure/storage/storage.service.js.map +1 -1
- package/dist/infrastructure/theme/theme.service.d.ts +93 -29
- package/dist/infrastructure/theme/theme.service.d.ts.map +1 -1
- package/dist/infrastructure/theme/theme.service.js +198 -96
- package/dist/infrastructure/theme/theme.service.js.map +1 -1
- package/dist/infrastructure/viewport/viewport.service.d.ts +233 -35
- package/dist/infrastructure/viewport/viewport.service.d.ts.map +1 -1
- package/dist/infrastructure/viewport/viewport.service.js +358 -139
- package/dist/infrastructure/viewport/viewport.service.js.map +1 -1
- package/dist/infrastructure/worker/worker.service.d.ts +84 -38
- package/dist/infrastructure/worker/worker.service.d.ts.map +1 -1
- package/dist/infrastructure/worker/worker.service.js +234 -133
- package/dist/infrastructure/worker/worker.service.js.map +1 -1
- package/dist/prisma/index.d.ts +2 -0
- package/dist/prisma/index.d.ts.map +1 -0
- package/dist/prisma/index.js +2 -0
- package/dist/prisma/index.js.map +1 -0
- package/dist/prisma/schema.json +11 -33
- package/dist/prisma/use-prisma.d.ts +68 -0
- package/dist/prisma/use-prisma.d.ts.map +1 -0
- package/dist/prisma/use-prisma.js +82 -0
- package/dist/prisma/use-prisma.js.map +1 -0
- package/dist/types/index.d.ts +20 -12
- package/dist/types/index.d.ts.map +1 -1
- package/package.json +124 -125
- package/CHANGELOG.md +0 -101
|
@@ -1,143 +1,323 @@
|
|
|
1
1
|
/**
|
|
2
|
-
*
|
|
2
|
+
* Returns a new array with duplicate values removed, preserving first-seen order.
|
|
3
|
+
*
|
|
4
|
+
* @typeParam T - Element type.
|
|
5
|
+
* @param array - The input array.
|
|
6
|
+
* @returns A new array with unique values.
|
|
7
|
+
*
|
|
8
|
+
* @example
|
|
9
|
+
* ```ts
|
|
10
|
+
* useUnique([1, 2, 2, 3, 1]); // [1, 2, 3]
|
|
11
|
+
* useUnique(["a", "b", "a"]); // ["a", "b"]
|
|
12
|
+
* ```
|
|
3
13
|
*/
|
|
4
|
-
export
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
14
|
+
export function useUnique(array) {
|
|
15
|
+
return [...new Set(array)];
|
|
16
|
+
}
|
|
17
|
+
/**
|
|
18
|
+
* Splits an array into chunks of the given size.
|
|
19
|
+
*
|
|
20
|
+
* @typeParam T - Element type.
|
|
21
|
+
* @param array - The input array.
|
|
22
|
+
* @param size - Number of elements per chunk (must be > 0).
|
|
23
|
+
* @returns An array of chunks.
|
|
24
|
+
* @throws {Error} If `size` is less than or equal to 0.
|
|
25
|
+
*
|
|
26
|
+
* @example
|
|
27
|
+
* ```ts
|
|
28
|
+
* useChunk([1, 2, 3, 4, 5], 2); // [[1, 2], [3, 4], [5]]
|
|
29
|
+
* useChunk(["a", "b", "c"], 1); // [["a"], ["b"], ["c"]]
|
|
30
|
+
* ```
|
|
31
|
+
*/
|
|
32
|
+
export function useChunk(array, size) {
|
|
33
|
+
if (size <= 0)
|
|
34
|
+
throw new Error("Chunk size must be greater than 0");
|
|
35
|
+
return Array.from({ length: Math.ceil(array.length / size) }, (_, i) => array.slice(i * size, i * size + size));
|
|
36
|
+
}
|
|
37
|
+
/**
|
|
38
|
+
* Groups array items by a key property or key selector function.
|
|
39
|
+
*
|
|
40
|
+
* @typeParam T - Element type.
|
|
41
|
+
* @param array - The input array.
|
|
42
|
+
* @param key - A property name or a function that returns the group key.
|
|
43
|
+
* @returns An object mapping group keys to arrays of items.
|
|
44
|
+
*
|
|
45
|
+
* @example
|
|
46
|
+
* ```ts
|
|
47
|
+
* const items = [
|
|
48
|
+
* { type: "fruit", name: "apple" },
|
|
49
|
+
* { type: "veggie", name: "carrot" },
|
|
50
|
+
* { type: "fruit", name: "banana" },
|
|
51
|
+
* ];
|
|
52
|
+
* useGroupBy(items, "type");
|
|
53
|
+
* // { fruit: [{ type: "fruit", name: "apple" }, { type: "fruit", name: "banana" }], veggie: [...] }
|
|
54
|
+
*
|
|
55
|
+
* useGroupBy(items, (item) => item.type.toUpperCase());
|
|
56
|
+
* // { FRUIT: [...], VEGGIE: [...] }
|
|
57
|
+
* ```
|
|
58
|
+
*/
|
|
59
|
+
export function useGroupBy(array, key) {
|
|
60
|
+
return array.reduce((acc, item) => {
|
|
61
|
+
const groupKey = typeof key === "function" ? key(item) : String(item[key]);
|
|
62
|
+
if (!acc[groupKey]) {
|
|
63
|
+
acc[groupKey] = [];
|
|
10
64
|
}
|
|
11
|
-
|
|
65
|
+
acc[groupKey].push(item);
|
|
66
|
+
return acc;
|
|
67
|
+
}, {});
|
|
68
|
+
}
|
|
69
|
+
/**
|
|
70
|
+
* Type guard that checks whether a value is a plain object (not null, not
|
|
71
|
+
* an array).
|
|
72
|
+
*
|
|
73
|
+
* @param item - The value to test.
|
|
74
|
+
* @returns `true` if the value is a plain `Record<string, unknown>`.
|
|
75
|
+
*
|
|
76
|
+
* @example
|
|
77
|
+
* ```ts
|
|
78
|
+
* useIsObject({ a: 1 }); // true
|
|
79
|
+
* useIsObject([1, 2]); // false
|
|
80
|
+
* useIsObject(null); // false
|
|
81
|
+
* useIsObject("hello"); // false
|
|
82
|
+
* ```
|
|
83
|
+
*/
|
|
84
|
+
export function useIsObject(item) {
|
|
85
|
+
return typeof item === "object" && item !== null && !Array.isArray(item);
|
|
86
|
+
}
|
|
87
|
+
/**
|
|
88
|
+
* Deep clones a value using `structuredClone` with a JSON fallback for
|
|
89
|
+
* environments that do not support it.
|
|
90
|
+
*
|
|
91
|
+
* @typeParam T - Value type.
|
|
92
|
+
* @param value - The value to clone.
|
|
93
|
+
* @returns A deep copy of the value.
|
|
94
|
+
*
|
|
95
|
+
* @example
|
|
96
|
+
* ```ts
|
|
97
|
+
* const original = { a: { b: 1 } };
|
|
98
|
+
* const copy = useDeepClone(original);
|
|
99
|
+
* copy.a.b = 2;
|
|
100
|
+
* original.a.b; // 1 (unchanged)
|
|
101
|
+
* ```
|
|
102
|
+
*/
|
|
103
|
+
export function useDeepClone(value) {
|
|
104
|
+
if (typeof structuredClone === "function") {
|
|
105
|
+
return structuredClone(value);
|
|
12
106
|
}
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
107
|
+
return JSON.parse(JSON.stringify(value));
|
|
108
|
+
}
|
|
109
|
+
/**
|
|
110
|
+
* Deep merges `source` into a shallow copy of `target`. Nested plain
|
|
111
|
+
* objects are merged recursively. Skips prototype-pollution keys
|
|
112
|
+
* (`__proto__`, `constructor`, `prototype`).
|
|
113
|
+
*
|
|
114
|
+
* @typeParam T - Target object type.
|
|
115
|
+
* @param target - The base object (not mutated).
|
|
116
|
+
* @param source - The object to merge in.
|
|
117
|
+
* @returns A new merged object.
|
|
118
|
+
*
|
|
119
|
+
* @example
|
|
120
|
+
* ```ts
|
|
121
|
+
* const base = { a: 1, b: { c: 2, d: 3 } };
|
|
122
|
+
* const override = { b: { c: 99, e: 4 }, f: 5 };
|
|
123
|
+
* useDeepMerge(base, override);
|
|
124
|
+
* // { a: 1, b: { c: 99, d: 3, e: 4 }, f: 5 }
|
|
125
|
+
* ```
|
|
126
|
+
*/
|
|
127
|
+
export function useDeepMerge(target, source) {
|
|
128
|
+
if (!target || !source)
|
|
129
|
+
return { ...target };
|
|
130
|
+
const output = { ...target };
|
|
131
|
+
const dangerousKeys = new Set(["__proto__", "constructor", "prototype"]);
|
|
132
|
+
for (const key of Object.keys(source)) {
|
|
133
|
+
if (dangerousKeys.has(key))
|
|
134
|
+
continue;
|
|
135
|
+
const targetVal = target[key];
|
|
136
|
+
const sourceVal = source[key];
|
|
137
|
+
if (useIsObject(targetVal) && useIsObject(sourceVal)) {
|
|
138
|
+
output[key] = useDeepMerge(targetVal, sourceVal);
|
|
33
139
|
}
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
useDeepMerge = (target, source) => {
|
|
37
|
-
if (!target || !source)
|
|
38
|
-
return { ...target };
|
|
39
|
-
const output = { ...target };
|
|
40
|
-
for (const key of Object.keys(source)) {
|
|
41
|
-
const targetVal = target[key];
|
|
42
|
-
const sourceVal = source[key];
|
|
43
|
-
if (this.useIsObject(targetVal) && this.useIsObject(sourceVal)) {
|
|
44
|
-
output[key] = this.useDeepMerge(targetVal, sourceVal);
|
|
45
|
-
}
|
|
46
|
-
else {
|
|
47
|
-
output[key] = sourceVal;
|
|
48
|
-
}
|
|
140
|
+
else {
|
|
141
|
+
output[key] = sourceVal;
|
|
49
142
|
}
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
usePick = (obj, keys) => {
|
|
53
|
-
return keys.reduce((acc, key) => {
|
|
54
|
-
if (key in obj)
|
|
55
|
-
acc[key] = obj[key];
|
|
56
|
-
return acc;
|
|
57
|
-
}, {});
|
|
58
|
-
};
|
|
59
|
-
useOmit = (obj, keys) => {
|
|
60
|
-
const result = this.useDeepClone(obj);
|
|
61
|
-
for (const key of keys)
|
|
62
|
-
delete result[key];
|
|
63
|
-
return result;
|
|
64
|
-
};
|
|
143
|
+
}
|
|
144
|
+
return output;
|
|
65
145
|
}
|
|
66
146
|
/**
|
|
67
|
-
*
|
|
147
|
+
* Picks the listed keys from an object into a new object.
|
|
148
|
+
*
|
|
149
|
+
* @typeParam T - Source object type.
|
|
150
|
+
* @typeParam K - Keys to pick.
|
|
151
|
+
* @param obj - The source object.
|
|
152
|
+
* @param keys - Array of keys to include.
|
|
153
|
+
* @returns A new object with only the picked keys.
|
|
154
|
+
*
|
|
155
|
+
* @example
|
|
156
|
+
* ```ts
|
|
157
|
+
* const user = { id: 1, name: "Alice", email: "a@b.com" };
|
|
158
|
+
* usePick(user, ["id", "name"]); // { id: 1, name: "Alice" }
|
|
159
|
+
* ```
|
|
68
160
|
*/
|
|
69
|
-
export
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
161
|
+
export function usePick(obj, keys) {
|
|
162
|
+
return keys.reduce((acc, key) => {
|
|
163
|
+
if (key in obj)
|
|
164
|
+
acc[key] = obj[key];
|
|
165
|
+
return acc;
|
|
166
|
+
}, {});
|
|
167
|
+
}
|
|
168
|
+
/**
|
|
169
|
+
* Omits the listed keys from an object, returning a shallow copy without
|
|
170
|
+
* them. Shallow on purpose to avoid `structuredClone` failures on
|
|
171
|
+
* non-cloneable values.
|
|
172
|
+
*
|
|
173
|
+
* @typeParam T - Source object type.
|
|
174
|
+
* @typeParam K - Keys to omit.
|
|
175
|
+
* @param obj - The source object.
|
|
176
|
+
* @param keys - Array of keys to exclude.
|
|
177
|
+
* @returns A new object without the omitted keys.
|
|
178
|
+
*
|
|
179
|
+
* @example
|
|
180
|
+
* ```ts
|
|
181
|
+
* const user = { id: 1, name: "Alice", password: "secret" };
|
|
182
|
+
* useOmit(user, ["password"]); // { id: 1, name: "Alice" }
|
|
183
|
+
* ```
|
|
184
|
+
*/
|
|
185
|
+
export function useOmit(obj, keys) {
|
|
186
|
+
const omit = new Set(keys);
|
|
187
|
+
const result = {};
|
|
188
|
+
for (const key of Object.keys(obj)) {
|
|
189
|
+
if (!omit.has(key)) {
|
|
190
|
+
result[key] = obj[key];
|
|
75
191
|
}
|
|
76
|
-
return SystemUtils.instance;
|
|
77
192
|
}
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
}
|
|
95
|
-
catch {
|
|
96
|
-
return false;
|
|
97
|
-
}
|
|
98
|
-
};
|
|
99
|
-
useGetUrlParams = (urlString) => {
|
|
100
|
-
try {
|
|
101
|
-
const url = new URL(urlString);
|
|
102
|
-
return Object.fromEntries(url.searchParams.entries());
|
|
103
|
-
}
|
|
104
|
-
catch {
|
|
105
|
-
return {};
|
|
106
|
-
}
|
|
107
|
-
};
|
|
108
|
-
useRound = (value, decimals = 2) => {
|
|
109
|
-
const num = typeof value === "string" ? Number.parseFloat(value) : value;
|
|
110
|
-
if (Number.isNaN(num))
|
|
111
|
-
return 0;
|
|
112
|
-
const factor = 10 ** decimals;
|
|
113
|
-
return Math.round(num * factor) / factor;
|
|
114
|
-
};
|
|
115
|
-
useAverage = (numbers) => {
|
|
116
|
-
if (numbers.length === 0)
|
|
117
|
-
return 0;
|
|
118
|
-
const sum = numbers.reduce((acc, n) => acc + n, 0);
|
|
119
|
-
return sum / numbers.length;
|
|
120
|
-
};
|
|
193
|
+
return result;
|
|
194
|
+
}
|
|
195
|
+
/**
|
|
196
|
+
* Returns a promise that resolves after `ms` milliseconds.
|
|
197
|
+
*
|
|
198
|
+
* @param ms - Delay in milliseconds.
|
|
199
|
+
* @returns A promise that resolves after the delay.
|
|
200
|
+
*
|
|
201
|
+
* @example
|
|
202
|
+
* ```ts
|
|
203
|
+
* await useSleep(1000); // waits 1 second
|
|
204
|
+
* console.log("done");
|
|
205
|
+
* ```
|
|
206
|
+
*/
|
|
207
|
+
export function useSleep(ms) {
|
|
208
|
+
return new Promise((resolve) => setTimeout(resolve, ms));
|
|
121
209
|
}
|
|
122
210
|
/**
|
|
123
|
-
*
|
|
211
|
+
* Retries an async function with a fixed delay between attempts.
|
|
212
|
+
*
|
|
213
|
+
* @typeParam T - The resolved value type.
|
|
214
|
+
* @param fn - The async function to retry.
|
|
215
|
+
* @param retries - Maximum number of retries (defaults to `3`).
|
|
216
|
+
* @param delayMs - Delay between retries in milliseconds (defaults to `1000`).
|
|
217
|
+
* @returns The resolved value of `fn`.
|
|
218
|
+
* @throws The last error if all retries are exhausted.
|
|
219
|
+
*
|
|
220
|
+
* @example
|
|
221
|
+
* ```ts
|
|
222
|
+
* const data = await useRetry(() => fetch("/api").then(r => r.json()), 3, 500);
|
|
223
|
+
* ```
|
|
124
224
|
*/
|
|
125
|
-
export
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
system;
|
|
129
|
-
constructor() {
|
|
130
|
-
this.data = DataUtils.getInstance();
|
|
131
|
-
this.system = SystemUtils.getInstance();
|
|
225
|
+
export async function useRetry(fn, retries = 3, delayMs = 1000) {
|
|
226
|
+
try {
|
|
227
|
+
return await fn();
|
|
132
228
|
}
|
|
133
|
-
|
|
134
|
-
if (
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
return
|
|
229
|
+
catch (error) {
|
|
230
|
+
if (retries <= 0)
|
|
231
|
+
throw error;
|
|
232
|
+
await useSleep(delayMs);
|
|
233
|
+
return useRetry(fn, retries - 1, delayMs);
|
|
234
|
+
}
|
|
235
|
+
}
|
|
236
|
+
/**
|
|
237
|
+
* Copies text to the clipboard (browser only). Returns `false` on failure
|
|
238
|
+
* or in non-browser environments (SSR).
|
|
239
|
+
*
|
|
240
|
+
* @param text - The text to copy.
|
|
241
|
+
* @returns `true` if the text was copied, `false` otherwise.
|
|
242
|
+
*
|
|
243
|
+
* @example
|
|
244
|
+
* ```ts
|
|
245
|
+
* const ok = await useCopyToClipboard("Hello, world!");
|
|
246
|
+
* console.log(ok); // true (in a browser with clipboard API)
|
|
247
|
+
* ```
|
|
248
|
+
*/
|
|
249
|
+
export async function useCopyToClipboard(text) {
|
|
250
|
+
try {
|
|
251
|
+
await navigator.clipboard.writeText(text);
|
|
252
|
+
return true;
|
|
253
|
+
}
|
|
254
|
+
catch {
|
|
255
|
+
return false;
|
|
138
256
|
}
|
|
139
257
|
}
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
258
|
+
/**
|
|
259
|
+
* Parses query parameters from a URL string into a plain object.
|
|
260
|
+
* Returns an empty object for invalid URLs.
|
|
261
|
+
*
|
|
262
|
+
* @param urlString - The URL to parse.
|
|
263
|
+
* @returns An object mapping parameter names to values.
|
|
264
|
+
*
|
|
265
|
+
* @example
|
|
266
|
+
* ```ts
|
|
267
|
+
* useGetUrlParams("https://example.com?q=hello&page=1");
|
|
268
|
+
* // { q: "hello", page: "1" }
|
|
269
|
+
*
|
|
270
|
+
* useGetUrlParams("not-a-url"); // {}
|
|
271
|
+
* ```
|
|
272
|
+
*/
|
|
273
|
+
export function useGetUrlParams(urlString) {
|
|
274
|
+
try {
|
|
275
|
+
const url = new URL(urlString);
|
|
276
|
+
return Object.fromEntries(url.searchParams.entries());
|
|
277
|
+
}
|
|
278
|
+
catch {
|
|
279
|
+
return {};
|
|
280
|
+
}
|
|
281
|
+
}
|
|
282
|
+
/**
|
|
283
|
+
* Rounds a number (or numeric string) to the specified number of decimal
|
|
284
|
+
* places. Returns `0` for non-numeric input.
|
|
285
|
+
*
|
|
286
|
+
* @param value - The number or numeric string to round.
|
|
287
|
+
* @param decimals - Number of decimal places (defaults to `2`).
|
|
288
|
+
* @returns The rounded number.
|
|
289
|
+
*
|
|
290
|
+
* @example
|
|
291
|
+
* ```ts
|
|
292
|
+
* useRound(3.14159, 2); // 3.14
|
|
293
|
+
* useRound("5.678", 1); // 5.7
|
|
294
|
+
* useRound("not-a-num"); // 0
|
|
295
|
+
* ```
|
|
296
|
+
*/
|
|
297
|
+
export function useRound(value, decimals = 2) {
|
|
298
|
+
const num = typeof value === "string" ? Number.parseFloat(value) : value;
|
|
299
|
+
if (Number.isNaN(num))
|
|
300
|
+
return 0;
|
|
301
|
+
const factor = 10 ** decimals;
|
|
302
|
+
return Math.round(num * factor) / factor;
|
|
303
|
+
}
|
|
304
|
+
/**
|
|
305
|
+
* Computes the arithmetic mean of an array of numbers. Returns `0` for an
|
|
306
|
+
* empty array.
|
|
307
|
+
*
|
|
308
|
+
* @param numbers - The input numbers.
|
|
309
|
+
* @returns The arithmetic mean.
|
|
310
|
+
*
|
|
311
|
+
* @example
|
|
312
|
+
* ```ts
|
|
313
|
+
* useAverage([1, 2, 3, 4, 5]); // 3
|
|
314
|
+
* useAverage([]); // 0
|
|
315
|
+
* ```
|
|
316
|
+
*/
|
|
317
|
+
export function useAverage(numbers) {
|
|
318
|
+
if (numbers.length === 0)
|
|
319
|
+
return 0;
|
|
320
|
+
const sum = numbers.reduce((acc, n) => acc + n, 0);
|
|
321
|
+
return sum / numbers.length;
|
|
322
|
+
}
|
|
143
323
|
//# sourceMappingURL=utils.service.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"utils.service.js","sourceRoot":"","sources":["../../../src/core/services/utils.service.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"utils.service.js","sourceRoot":"","sources":["../../../src/core/services/utils.service.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;GAYG;AACH,MAAM,UAAU,SAAS,CAAI,KAAU;IACtC,OAAO,CAAC,GAAG,IAAI,GAAG,CAAC,KAAK,CAAC,CAAC,CAAC;AAC5B,CAAC;AAED;;;;;;;;;;;;;;GAcG;AACH,MAAM,UAAU,QAAQ,CAAI,KAAU,EAAE,IAAY;IACnD,IAAI,IAAI,IAAI,CAAC;QAAE,MAAM,IAAI,KAAK,CAAC,mCAAmC,CAAC,CAAC;IACpE,OAAO,KAAK,CAAC,IAAI,CAAC,EAAE,MAAM,EAAE,IAAI,CAAC,IAAI,CAAC,KAAK,CAAC,MAAM,GAAG,IAAI,CAAC,EAAE,EAAE,CAAC,CAAC,EAAE,CAAC,EAAE,EAAE,CACtE,KAAK,CAAC,KAAK,CAAC,CAAC,GAAG,IAAI,EAAE,CAAC,GAAG,IAAI,GAAG,IAAI,CAAC,CACtC,CAAC;AACH,CAAC;AAED;;;;;;;;;;;;;;;;;;;;;GAqBG;AACH,MAAM,UAAU,UAAU,CACzB,KAAU,EACV,GAAoC;IAEpC,OAAO,KAAK,CAAC,MAAM,CAClB,CAAC,GAAG,EAAE,IAAI,EAAE,EAAE;QACb,MAAM,QAAQ,GAAW,OA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