@dvirus-js/utils 0.0.6 → 0.0.7

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/CHANGELOG.md CHANGED
@@ -1,3 +1,13 @@
1
+ ## 0.0.6 (2026-05-20)
2
+
3
+ ### 🩹 Fixes
4
+
5
+ - update utils index exports ([7919620](https://github.com/Dvirus97/dvirus-js/commit/7919620))
6
+
7
+ ### ❤️ Thank You
8
+
9
+ - Dvir Cohen
10
+
1
11
  ## 0.0.5 (2026-05-20)
2
12
 
3
13
  This was a version bump only for utils to align it with other projects, there were no code changes.
package/index.js CHANGED
@@ -1,62 +1,62 @@
1
- function G(t) {
2
- return t.replace(/([a-z0-9])([A-Z])/g, "$1 $2").replace(/([A-Z]+)([A-Z][a-z])/g, "$1 $2").toLowerCase().replace(/[_-]+/g, " ").replace(/\s+/g, " ").trim();
1
+ function I(e) {
2
+ return e.replace(/([a-z0-9])([A-Z])/g, "$1 $2").replace(/([A-Z]+)([A-Z][a-z])/g, "$1 $2").toLowerCase().replace(/[_-]+/g, " ").replace(/\s+/g, " ").trim();
3
3
  }
4
- function W(t, r) {
5
- const e = G(t);
6
- switch (r) {
4
+ function re(e, n) {
5
+ const t = I(e);
6
+ switch (n) {
7
7
  case "lowercase":
8
- return e.toLowerCase();
8
+ return t.toLowerCase();
9
9
  case "UPPERCASE":
10
- return e.toUpperCase();
10
+ return t.toUpperCase();
11
11
  case "Title Case":
12
- return e.split(" ").map(
13
- (n) => n.charAt(0).toUpperCase() + n.slice(1).toLowerCase()
12
+ return t.split(" ").map(
13
+ (r) => r.charAt(0).toUpperCase() + r.slice(1).toLowerCase()
14
14
  ).join(" ");
15
15
  case "kebab-case":
16
- return e.replace(/\s+/g, "-");
16
+ return t.replace(/\s+/g, "-");
17
17
  case "snake_case":
18
- return e.replace(/\s+/g, "_");
18
+ return t.replace(/\s+/g, "_");
19
19
  case "camelCase": {
20
- const n = e.split(" ");
21
- return n.length === 0 ? "" : n[0]?.toLowerCase() + n.slice(1).map(
20
+ const r = t.split(" ");
21
+ return r.length === 0 ? "" : r[0]?.toLowerCase() + r.slice(1).map(
22
22
  (o) => o.charAt(0).toUpperCase() + o.slice(1).toLowerCase()
23
23
  ).join("");
24
24
  }
25
25
  case "PascalCase":
26
- return e.split(" ").map(
27
- (n) => n.charAt(0).toUpperCase() + n.slice(1).toLowerCase()
26
+ return t.split(" ").map(
27
+ (r) => r.charAt(0).toUpperCase() + r.slice(1).toLowerCase()
28
28
  ).join("");
29
29
  case "dot.case":
30
- return e.replace(/\s+/g, ".");
30
+ return t.replace(/\s+/g, ".");
31
31
  case "path/case":
32
- return e.replace(/\s+/g, "/");
32
+ return t.replace(/\s+/g, "/");
33
33
  case "Sentence case":
34
- return e.replace(/(^\s*\w|[.!?]\s*\w)/g, (n) => n.toUpperCase()).replace(/\bi\b/g, "I");
34
+ return t.replace(/(^\s*\w|[.!?]\s*\w)/g, (r) => r.toUpperCase()).replace(/\bi\b/g, "I");
35
35
  case "Header-Case":
36
- return e.split(" ").map(
37
- (n) => n.charAt(0).toUpperCase() + n.slice(1).toLowerCase()
36
+ return t.split(" ").map(
37
+ (r) => r.charAt(0).toUpperCase() + r.slice(1).toLowerCase()
38
38
  ).join("-");
39
39
  case "reverse":
40
- return e.split("").reverse().join("");
40
+ return t.split("").reverse().join("");
41
41
  default:
42
- throw new Error(`Unsupported case type: ${r}`);
42
+ throw new Error(`Unsupported case type: ${n}`);
43
43
  }
44
44
  }
45
- async function Y(t) {
45
+ async function se(e) {
46
46
  try {
47
- return [await t, null];
48
- } catch (r) {
49
- return [null, r];
47
+ return [await (typeof e == "function" ? e() : e), null];
48
+ } catch (n) {
49
+ return [null, n];
50
50
  }
51
51
  }
52
- function E(t) {
52
+ function E(e) {
53
53
  try {
54
- return [t(), null];
55
- } catch (r) {
56
- return [null, r];
54
+ return [e(), null];
55
+ } catch (n) {
56
+ return [null, n];
57
57
  }
58
58
  }
59
- const ee = {
59
+ const oe = {
60
60
  /**
61
61
  * Makes a GET request to the specified URL.
62
62
  *
@@ -65,9 +65,8 @@ const ee = {
65
65
  * @returns {Promise<any>} The response data.
66
66
  * @throws {Error} If the response is not ok.
67
67
  */
68
- get: async function(t, r) {
69
- const e = await fetch(t, r);
70
- return await C(e);
68
+ get: async function(e, n) {
69
+ return await C(() => fetch(e, n), n);
71
70
  },
72
71
  /**
73
72
  * Makes a POST request to the specified URL with the given data.
@@ -79,17 +78,19 @@ const ee = {
79
78
  * @returns {Promise<R>} The response data.
80
79
  * @throws {Error} If the response is not ok.
81
80
  */
82
- post: async function(t, r, e) {
83
- const n = await fetch(t, {
84
- method: "POST",
85
- headers: {
86
- "Content-Type": "application/json",
87
- ...e?.headers
88
- },
89
- body: JSON.stringify(r),
90
- ...e
91
- });
92
- return await C(n);
81
+ post: async function(e, n, t) {
82
+ return await C(
83
+ () => fetch(e, {
84
+ method: "POST",
85
+ headers: {
86
+ "Content-Type": "application/json",
87
+ ...t?.headers
88
+ },
89
+ body: JSON.stringify(n),
90
+ ...t
91
+ }),
92
+ t
93
+ );
93
94
  },
94
95
  /**
95
96
  * Makes a DELETE request to the specified URL with the given ID.
@@ -101,17 +102,19 @@ const ee = {
101
102
  * @returns {Promise<R>} The response data.
102
103
  * @throws {Error} If the response is not ok.
103
104
  */
104
- delete: async function(t, r, e) {
105
- const n = await fetch(t, {
106
- method: "DELETE",
107
- headers: {
108
- "Content-Type": "application/json",
109
- ...e?.headers
110
- },
111
- body: JSON.stringify({ id: r }),
112
- ...e
113
- });
114
- return await C(n);
105
+ delete: async function(e, n, t) {
106
+ return await C(
107
+ () => fetch(e, {
108
+ method: "DELETE",
109
+ headers: {
110
+ "Content-Type": "application/json",
111
+ ...t?.headers
112
+ },
113
+ body: JSON.stringify({ id: n }),
114
+ ...t
115
+ }),
116
+ t
117
+ );
115
118
  },
116
119
  /**
117
120
  * Makes a PATCH request to the specified URL with the given data.
@@ -123,17 +126,19 @@ const ee = {
123
126
  * @returns {Promise<R>} The response data.
124
127
  * @throws {Error} If the response is not ok.
125
128
  */
126
- patch: async function(t, r, e) {
127
- const n = await fetch(t, {
128
- method: "PATCH",
129
- headers: {
130
- "Content-Type": "application/json",
131
- ...e?.headers
132
- },
133
- body: JSON.stringify(r),
134
- ...e
135
- });
136
- return await C(n);
129
+ patch: async function(e, n, t) {
130
+ return await C(
131
+ () => fetch(e, {
132
+ method: "PATCH",
133
+ headers: {
134
+ "Content-Type": "application/json",
135
+ ...t?.headers
136
+ },
137
+ body: JSON.stringify(n),
138
+ ...t
139
+ }),
140
+ t
141
+ );
137
142
  },
138
143
  /**
139
144
  * Makes a PUT request to the specified URL with the given data.
@@ -145,62 +150,123 @@ const ee = {
145
150
  * @returns {Promise<R>} The response data.
146
151
  * @throws {Error} If the response is not ok.
147
152
  */
148
- put: async function(t, r, e) {
149
- const n = await fetch(t, {
150
- method: "PUT",
151
- headers: {
152
- "Content-Type": "application/json",
153
- ...e?.headers
154
- },
155
- body: JSON.stringify(r),
156
- ...e
157
- });
158
- return await C(n);
153
+ put: async function(e, n, t) {
154
+ return await C(
155
+ () => fetch(e, {
156
+ method: "PUT",
157
+ headers: {
158
+ "Content-Type": "application/json",
159
+ ...t?.headers
160
+ },
161
+ body: JSON.stringify(n),
162
+ ...t
163
+ }),
164
+ t
165
+ );
159
166
  }
160
167
  };
161
- async function C(t) {
162
- if (!t.ok) throw new Error(`Error: ${t.statusText}`);
163
- return await t.json();
164
- }
165
- function te(t, r) {
166
- const e = {};
167
- return t?.forEach((n, o) => {
168
- const s = r(n, o, t)?.toString();
169
- s && (e[s] ??= [], e[s].push(n));
170
- }), e;
171
- }
172
- async function re(t) {
173
- return await new Promise((r) => setTimeout(r, t));
174
- }
175
- function ne(t, r, e) {
176
- return r < t ? t : r > e ? e : r;
177
- }
178
- function se(t, r, e) {
179
- let n;
180
- return e?.isLoadingFn?.(!1), Object.assign((...s) => {
181
- clearTimeout(n), e?.isLoadingFn?.(!0), n = setTimeout(() => {
182
- e?.isLoadingFn?.(!1), t(...s);
183
- }, r);
168
+ async function C(e, n) {
169
+ try {
170
+ const t = await e();
171
+ if (!t.ok) {
172
+ const r = await t.text().catch(() => "");
173
+ throw Object.assign(
174
+ new Error(`HTTP ${t.status} ${t.statusText}`),
175
+ {
176
+ status: t.status,
177
+ statusText: t.statusText,
178
+ body: r
179
+ }
180
+ );
181
+ }
182
+ return n?.parse === "JSON" ? await t.json() : n?.parse === "TEXT" ? await t.text() : n?.parse === "BLOB" ? await t.blob() : n?.parse === "ARRAYBUFFER" ? await t.arrayBuffer() : await t.json();
183
+ } catch (t) {
184
+ throw H(t) ? t : Object.assign(
185
+ new Error(
186
+ t instanceof Error ? t.message : "Network request failed"
187
+ ),
188
+ {
189
+ status: G(t),
190
+ statusText: "NETWORK_ERROR",
191
+ code: V(t),
192
+ isNetworkError: !0,
193
+ cause: t
194
+ }
195
+ );
196
+ }
197
+ }
198
+ function G(e) {
199
+ if (typeof e == "object" && e !== null && "status" in e && typeof e.status == "number")
200
+ return e.status;
201
+ }
202
+ function V(e) {
203
+ if (typeof e == "object" && e !== null && "code" in e && typeof e.code == "string")
204
+ return e.code;
205
+ if (typeof e == "object" && e !== null && "cause" in e && typeof e.cause == "object" && e.cause !== null && "code" in e.cause && typeof e.cause.code == "string")
206
+ return e.cause.code;
207
+ }
208
+ function H(e) {
209
+ return typeof e == "object" && e !== null && "status" in e && typeof e.status == "number";
210
+ }
211
+ function ae(e, n) {
212
+ const t = {};
213
+ return e?.forEach((r, o) => {
214
+ const s = n(r, o, e)?.toString();
215
+ s && (t[s] ??= [], t[s].push(r));
216
+ }), t;
217
+ }
218
+ async function ie(e) {
219
+ return await new Promise((n) => setTimeout(n, e));
220
+ }
221
+ function ce(e, n, t) {
222
+ return n < e ? e : n > t ? t : n;
223
+ }
224
+ function le(e, n, t) {
225
+ let r;
226
+ return t?.isLoadingFn?.(!1), Object.assign((...s) => {
227
+ clearTimeout(r), t?.isLoadingFn?.(!0), r = setTimeout(() => {
228
+ t?.isLoadingFn?.(!1), e(...s);
229
+ }, n);
184
230
  }, {
185
231
  cancel() {
186
- clearTimeout(n), e?.isLoadingFn?.(!1);
232
+ clearTimeout(r), t?.isLoadingFn?.(!1);
187
233
  }
188
234
  });
189
235
  }
190
- function oe(t, r) {
191
- if (!r || typeof r != "string" || typeof t != "object" || t === null)
236
+ function J(e, n) {
237
+ if (!n || typeof n != "string" || typeof e != "object" || e === null)
192
238
  return;
193
- const e = r.split(".");
194
- function n(o, s) {
239
+ const t = n.split(".");
240
+ function r(o, s) {
195
241
  if (!s || typeof s != "object")
196
242
  return;
197
243
  const a = o[0];
198
244
  if (a in s)
199
- return o.length === 1 ? s[a] : n(o.slice(1), s[a]);
245
+ return o.length === 1 ? s[a] : r(o.slice(1), s[a]);
200
246
  }
201
- return n(e, t);
247
+ return r(t, e);
202
248
  }
203
- class m {
249
+ function Z(e, n, t) {
250
+ let r = e;
251
+ for (let o = 0; o < n.length; o++) {
252
+ const s = n[o] ?? "";
253
+ o === n.length - 1 ? r[s] = t : ((!r[s] || typeof r[s] != "object") && (r[s] = {}), r = r[s]);
254
+ }
255
+ }
256
+ function ue(e, n) {
257
+ const t = {};
258
+ if (!e || typeof e != "object")
259
+ return t;
260
+ for (const r of n) {
261
+ const o = J(e, r);
262
+ if (o !== void 0) {
263
+ const s = r.split(".");
264
+ Z(t, s, o);
265
+ }
266
+ }
267
+ return t;
268
+ }
269
+ class y {
204
270
  #e = null;
205
271
  #t = null;
206
272
  /**
@@ -208,24 +274,24 @@ class m {
208
274
  * @param {T} val - The success value.
209
275
  * @returns {Result<T, never>} A Result instance representing a success.
210
276
  */
211
- static ok(r) {
212
- return new m(r, null);
277
+ static ok(n) {
278
+ return new y(n, null);
213
279
  }
214
280
  /**
215
281
  * Creates a failed result.
216
282
  * @param {E | string} err - The error value or message.
217
283
  * @returns {Result<never, E>} A Result instance representing a failure.
218
284
  */
219
- static err(r) {
220
- return typeof r == "string" ? new m(null, new Error(r)) : new m(null, r);
285
+ static err(n) {
286
+ return typeof n == "string" ? new y(null, new Error(n)) : new y(null, n);
221
287
  }
222
288
  /**
223
289
  * Wraps a promise in a Result.
224
290
  * @param {Promise<T>} promise - The promise to wrap.
225
291
  * @returns {Promise<Result<T, E>>} A promise that resolves to a Result.
226
292
  */
227
- static async promise(r) {
228
- return r.then((e) => m.ok(e ?? "__void__")).catch((e) => m.err(e));
293
+ static async promise(n) {
294
+ return n.then((t) => y.ok(t)).catch((t) => y.err(t));
229
295
  }
230
296
  /**
231
297
  * Wraps a function call in a Result.
@@ -233,12 +299,12 @@ class m {
233
299
  * @param {...any[]} args - The arguments to pass to the function.
234
300
  * @returns {Result<T, E>} A Result instance representing the function call result.
235
301
  */
236
- static func(r, ...e) {
302
+ static func(n, ...t) {
237
303
  try {
238
- const n = r(...e);
239
- return m.ok(n);
240
- } catch (n) {
241
- return m.err(n);
304
+ const r = n(...t);
305
+ return y.ok(r);
306
+ } catch (r) {
307
+ return y.err(r);
242
308
  }
243
309
  }
244
310
  /**
@@ -246,16 +312,16 @@ class m {
246
312
  * @param {E | null} err - The error value.
247
313
  * @throws {Error} If both ok and err are provided or neither is provided.
248
314
  */
249
- constructor(r, e) {
250
- if (!r && !e)
315
+ constructor(n, t) {
316
+ if (n == null && t == null)
251
317
  throw new Error(
252
318
  "Result must be initialized with either an ok or an err value"
253
319
  );
254
- if (r && e)
320
+ if (n != null && t != null)
255
321
  throw new Error(
256
322
  "Result can't be initialized with both an ok and an err value"
257
323
  );
258
- r != null ? this.#e = r : this.#t = e;
324
+ n != null ? this.#e = n : this.#t = t;
259
325
  }
260
326
  /**
261
327
  * Gets the success value, throwing an error if the result is a failure.
@@ -280,8 +346,8 @@ class m {
280
346
  * @param {T} defaultValue - The default value to return if the result is a failure.
281
347
  * @returns {T} The success value or the default value.
282
348
  */
283
- unwrapOr(r) {
284
- return this.isOk() ? this.#e : r;
349
+ unwrapOr(n) {
350
+ return this.isOk() ? this.#e : n;
285
351
  }
286
352
  /**
287
353
  * Gets the success value, throwing a custom error message if the result is a failure.
@@ -289,15 +355,15 @@ class m {
289
355
  * @returns {T} The success value.
290
356
  * @throws {Error} If the result is a failure.
291
357
  */
292
- expect(r) {
358
+ expect(n) {
293
359
  if (this.isOk())
294
360
  return this.#e;
295
361
  if (this.isErr()) {
296
- const e = this.#t;
297
- throw new Error(r + `:
298
- ` + e.message);
362
+ const t = this.#t;
363
+ throw new Error(n + `:
364
+ ` + t.message);
299
365
  }
300
- throw new Error(r);
366
+ throw new Error(n);
301
367
  }
302
368
  /**
303
369
  * Checks if the result is a success.
@@ -321,168 +387,168 @@ class m {
321
387
  return this.#t;
322
388
  }
323
389
  }
324
- function ae(t, r = []) {
325
- return t == null ? r : Array.isArray(t) ? t : [t];
390
+ function fe(e, n = []) {
391
+ return e == null ? n : Array.isArray(e) ? e : [e];
326
392
  }
327
- const z = /* @__PURE__ */ Symbol("SIGNAL"), ie = (t) => typeof t == "function" && z in t, p = [], ce = (t) => {
328
- const r = p.splice(0, p.length);
393
+ const O = /* @__PURE__ */ Symbol("SIGNAL"), de = (e) => typeof e == "function" && O in e, p = [], pe = (e) => {
394
+ const n = p.splice(0, p.length);
329
395
  try {
330
- return t();
396
+ return e();
331
397
  } finally {
332
- p.push(...r);
398
+ p.push(...n);
333
399
  }
334
- }, A = (t, r) => {
335
- const e = /* @__PURE__ */ new Set();
336
- let n = 0;
400
+ }, j = (e, n) => {
401
+ const t = /* @__PURE__ */ new Set();
402
+ let r = 0;
337
403
  const o = () => {
338
404
  const l = p[p.length - 1];
339
- l && (e.add(l.setDirty), l.addSource(() => {
340
- e.delete(l.setDirty);
405
+ l && (t.add(l.setDirty), l.addSource(() => {
406
+ t.delete(l.setDirty);
341
407
  }));
342
408
  }, s = (l) => {
343
- for (const y of Array.from(e)) y(l);
344
- }, a = () => n > 0;
345
- return { read: Object.assign(() => (o(), t()), {
346
- [z]: !0,
347
- subscribe: (l) => (e.add(l), n++, r?.(), () => {
348
- e.delete(l), n--;
409
+ for (const m of Array.from(t)) m(l);
410
+ }, a = () => r > 0;
411
+ return { read: Object.assign(() => (o(), e()), {
412
+ [O]: !0,
413
+ subscribe: (l) => (t.add(l), r++, n?.(), () => {
414
+ t.delete(l), r--;
349
415
  })
350
416
  }), notify: s, hasSubscribers: a };
351
- }, S = (t) => {
352
- let r = t;
353
- const { read: e, notify: n } = A(() => r);
354
- return Object.assign(e, {
417
+ }, S = (e) => {
418
+ let n = e;
419
+ const { read: t, notify: r } = j(() => n);
420
+ return Object.assign(t, {
355
421
  set: (o) => {
356
- r !== o && (r = o, n(o));
422
+ n !== o && (n = o, r(o));
357
423
  },
358
424
  update: (o) => {
359
- const s = o(r);
360
- r !== s && (r = s, n(s));
425
+ const s = o(n);
426
+ n !== s && (n = s, r(s));
361
427
  },
362
- asReadonly: () => e
428
+ asReadonly: () => t
363
429
  });
364
- }, k = (t) => {
365
- const r = /* @__PURE__ */ new Set();
366
- let e = !1, n = null, o = !1;
430
+ }, k = (e) => {
431
+ const n = /* @__PURE__ */ new Set();
432
+ let t = !1, r = null, o = !1;
367
433
  const s = () => {
368
- if (o = !1, e) return;
369
- n && (n(), n = null), r.forEach((i) => i()), r.clear();
434
+ if (o = !1, t) return;
435
+ r && (r(), r = null), n.forEach((i) => i()), n.clear();
370
436
  const a = (i) => {
371
- n = i;
437
+ r = i;
372
438
  };
373
439
  p.push({
374
440
  setDirty: () => {
375
- !e && !o && (o = !0, queueMicrotask(s));
441
+ !t && !o && (o = !0, queueMicrotask(s));
376
442
  },
377
- addSource: (i) => r.add(i)
378
- }), t(a), p.pop();
443
+ addSource: (i) => n.add(i)
444
+ }), e(a), p.pop();
379
445
  };
380
446
  return s(), {
381
447
  destroy() {
382
- e = !0, n && (n(), n = null), r.forEach((a) => a()), r.clear();
448
+ t = !0, r && (r(), r = null), n.forEach((a) => a()), n.clear();
383
449
  }
384
450
  };
385
- }, L = (t) => {
386
- const r = /* @__PURE__ */ new Set();
387
- let e, n = !0, o = () => {
451
+ }, L = (e) => {
452
+ const n = /* @__PURE__ */ new Set();
453
+ let t, r = !0, o = () => {
388
454
  };
389
455
  const s = () => {
390
- r.forEach((c) => c()), r.clear(), p.push({
456
+ n.forEach((c) => c()), n.clear(), p.push({
391
457
  setDirty: () => {
392
- if (n) return;
393
- n = !0;
394
- const c = e;
395
- s(), e !== c && o(e);
458
+ if (r) return;
459
+ r = !0;
460
+ const c = t;
461
+ s(), t !== c && o(t);
396
462
  },
397
- addSource: (c) => r.add(c)
398
- }), e = t(), n = !1, p.pop();
399
- }, { read: a, notify: i } = A(
400
- () => (n && s(), e),
463
+ addSource: (c) => n.add(c)
464
+ }), t = e(), r = !1, p.pop();
465
+ }, { read: a, notify: i } = j(
466
+ () => (r && s(), t),
401
467
  () => {
402
- n && s();
468
+ r && s();
403
469
  }
404
470
  );
405
471
  return o = i, a;
406
472
  };
407
- function le(t) {
408
- const r = typeof t == "function", e = r ? void 0 : t.source, n = r ? t : t.computation, o = /* @__PURE__ */ new Set();
473
+ function he(e) {
474
+ const n = typeof e == "function", t = n ? void 0 : e.source, r = n ? e : e.computation, o = /* @__PURE__ */ new Set();
409
475
  let s, a = !0, i = !1, c, l = () => {
410
476
  };
411
- const y = () => {
477
+ const m = () => {
412
478
  if (o.forEach((u) => u()), o.clear(), p.push({
413
479
  setDirty: () => {
414
480
  if (a) return;
415
481
  a = !0, i = !1;
416
482
  const u = s;
417
- y(), s !== u && l(s);
483
+ m(), s !== u && l(s);
418
484
  },
419
485
  addSource: (u) => o.add(u)
420
- }), r)
421
- s = n();
486
+ }), n)
487
+ s = r();
422
488
  else {
423
- const u = e?.();
424
- s = n(u, c), c = { source: u, value: s };
489
+ const u = t?.();
490
+ s = r(u, c), c = { source: u, value: s };
425
491
  }
426
492
  a = !1, p.pop();
427
- }, { read: v, notify: d } = A(
428
- () => (a && !i && y(), s),
493
+ }, { read: T, notify: d } = j(
494
+ () => (a && !i && m(), s),
429
495
  () => {
430
- a && !i && y();
496
+ a && !i && m();
431
497
  }
432
498
  );
433
499
  l = d;
434
- const b = Object.assign(v, {
500
+ const b = Object.assign(T, {
435
501
  set: (u) => {
436
502
  s !== u && (s = u, i = !0, a = !1, d(u));
437
503
  },
438
504
  update: (u) => {
439
- a && !i && y(), b.set(u(s));
505
+ a && !i && m(), b.set(u(s));
440
506
  },
441
- asReadonly: () => v
507
+ asReadonly: () => T
442
508
  });
443
509
  return b;
444
510
  }
445
- class F extends Error {
446
- constructor(r) {
447
- const e = r instanceof Error ? `:
448
- ${r.message}` : "";
449
- super(`Cannot read resource value while in error state${e}`, { cause: r }), this.name = "ResourceErrorState";
511
+ class q extends Error {
512
+ constructor(n) {
513
+ const t = n instanceof Error ? `:
514
+ ${n.message}` : "";
515
+ super(`Cannot read resource value while in error state${t}`, { cause: n }), this.name = "ResourceErrorState";
450
516
  }
451
517
  }
452
- function ue(t) {
453
- const r = S(void 0), e = S("idle"), n = S(void 0), o = L(() => e() === "loading"), s = S(0), a = L(() => {
454
- if (e() === "error")
455
- throw new F(n());
456
- return r();
518
+ function ge(e) {
519
+ const n = S(void 0), t = S("idle"), r = S(void 0), o = L(() => t() === "loading"), s = S(0), a = L(() => {
520
+ if (t() === "error")
521
+ throw new q(r());
522
+ return n();
457
523
  }), i = (d) => {
458
- r.set(d), n.set(void 0), e.set("resolved");
524
+ n.set(d), r.set(void 0), t.set("resolved");
459
525
  }, c = (d) => {
460
- r.update(d), n.set(void 0), e.set("resolved");
461
- }, l = "params" in t;
462
- if ("stream" in t) {
526
+ n.update(d), r.set(void 0), t.set("resolved");
527
+ }, l = "params" in e;
528
+ if ("stream" in e) {
463
529
  const d = S(void 0), b = k((g) => {
464
530
  s();
465
531
  const f = new AbortController();
466
532
  g(() => f.abort());
467
- const D = l ? t.params() : void 0;
468
- e.set("loading"), n.set(void 0);
469
- const $ = t.stream, I = $(l ? {
470
- params: D,
533
+ const _ = l ? e.params() : void 0;
534
+ t.set("loading"), r.set(void 0);
535
+ const $ = e.stream, F = $(l ? {
536
+ params: _,
471
537
  abortSignal: f.signal
472
538
  } : {
473
539
  abortSignal: f.signal
474
540
  });
475
- d.set(I);
541
+ d.set(F);
476
542
  }), u = k(() => {
477
543
  const g = d();
478
544
  if (!g) return;
479
545
  const f = g();
480
- f && ("error" in f && f.error !== void 0 ? (n.set(f.error), e.set("error")) : "value" in f && (r.set(f.value), e.set("resolved")));
546
+ f && ("error" in f && f.error !== void 0 ? (r.set(f.error), t.set("error")) : "value" in f && (n.set(f.value), t.set("resolved")));
481
547
  });
482
548
  return {
483
549
  value: a,
484
- status: e,
485
- error: n,
550
+ status: t,
551
+ error: r,
486
552
  isLoading: o,
487
553
  set: i,
488
554
  update: c,
@@ -494,36 +560,36 @@ function ue(t) {
494
560
  }
495
561
  };
496
562
  }
497
- const v = k((d) => {
563
+ const T = k((d) => {
498
564
  s();
499
- const b = l ? t.params() : void 0;
500
- e.set("loading"), n.set(void 0);
565
+ const b = l ? e.params() : void 0;
566
+ t.set("loading"), r.set(void 0);
501
567
  const u = new AbortController();
502
568
  d(() => u.abort());
503
569
  const g = l ? { params: b, abortSignal: u.signal } : { abortSignal: u.signal };
504
- t.loader(g).then(
570
+ e.loader(g).then(
505
571
  (f) => {
506
- u.signal.aborted || (r.set(f), e.set("resolved"));
572
+ u.signal.aborted || (n.set(f), t.set("resolved"));
507
573
  },
508
574
  (f) => {
509
- u.signal.aborted || (n.set(f), e.set("error"));
575
+ u.signal.aborted || (r.set(f), t.set("error"));
510
576
  }
511
577
  );
512
578
  });
513
579
  return {
514
580
  value: a,
515
- status: e,
516
- error: n,
581
+ status: t,
582
+ error: r,
517
583
  isLoading: o,
518
584
  set: i,
519
585
  update: c,
520
586
  reload: () => {
521
587
  s.update((d) => d + 1);
522
588
  },
523
- destroy: () => v.destroy()
589
+ destroy: () => T.destroy()
524
590
  };
525
591
  }
526
- const x = /* @__PURE__ */ new Map([
592
+ const A = /* @__PURE__ */ new Map([
527
593
  ["b", "b"],
528
594
  ["i", "i"],
529
595
  ["u", "u"],
@@ -547,7 +613,7 @@ const x = /* @__PURE__ */ new Map([
547
613
  ["ul", "ul"],
548
614
  ["ol", "ol"],
549
615
  ["li", "li"]
550
- ]), T = /* @__PURE__ */ new Set([
616
+ ]), v = /* @__PURE__ */ new Set([
551
617
  "div",
552
618
  "p",
553
619
  "ul",
@@ -560,14 +626,14 @@ const x = /* @__PURE__ */ new Map([
560
626
  "h5",
561
627
  "h6"
562
628
  ]);
563
- let O = "rtp-";
564
- function N() {
565
- return O;
629
+ let R = "rtp-";
630
+ function M() {
631
+ return R;
566
632
  }
567
- function V(t) {
568
- O = t;
633
+ function X(e) {
634
+ R = e;
569
635
  }
570
- const j = /* @__PURE__ */ new Map([
636
+ const N = /* @__PURE__ */ new Map([
571
637
  ["br", "br"],
572
638
  ["hr", "hr"]
573
639
  ]), w = /* @__PURE__ */ new Map([
@@ -658,12 +724,12 @@ const j = /* @__PURE__ */ new Map([
658
724
  ["margin", "1em 0"]
659
725
  ])
660
726
  ]
661
- ]), M = (t, r, e, n, o) => {
662
- const s = t.filter((i) => !T.has(i)), a = t.filter((i) => T.has(i));
727
+ ]), x = (e, n, t, r, o) => {
728
+ const s = e.filter((i) => !v.has(i)), a = e.filter((i) => v.has(i));
663
729
  return {
664
- tagNames: t.join(","),
665
- tagNamesList: t,
666
- text: o ?? r.slice(e, n),
730
+ tagNames: e.join(","),
731
+ tagNamesList: e,
732
+ text: o ?? n.slice(t, r),
667
733
  style: s.map(
668
734
  (i) => [...w.get(i)?.entries() ?? []].map(([c, l]) => `${c}: ${l}`).join("; ")
669
735
  ).filter(Boolean).join("; "),
@@ -671,214 +737,216 @@ const j = /* @__PURE__ */ new Map([
671
737
  s.flatMap((i) => Array.from(w.get(i) ?? []))
672
738
  ),
673
739
  containerTagNames: a,
674
- cssClass: s.map((i) => `${N()}${i}`).join(" ")
740
+ cssClass: s.map((i) => `${M()}${i}`).join(" ")
675
741
  };
676
- }, J = () => [...x.keys()].join("|"), H = () => [...j.keys()].join("|"), Z = () => new RegExp(
677
- `<\\/?(${J()})>|<(${H()})\\s*/?>`,
742
+ }, K = () => [...A.keys()].join("|"), W = () => [...N.keys()].join("|"), Y = () => new RegExp(
743
+ `<\\/?(${K()})>|<(${W()})\\s*/?>`,
678
744
  "gi"
679
745
  );
680
- function P(t, r) {
681
- const e = w.get(t) ?? /* @__PURE__ */ new Map();
682
- Object.entries(r).forEach(([n, o]) => e.set(n, o)), w.set(t, e);
683
- }
684
- function R() {
685
- const t = [];
686
- for (const [r, e] of w) {
687
- if (e.size === 0) continue;
688
- const n = [...e.entries()].map(([o, s]) => ` ${o}: ${s};`).join(`
746
+ function z(e, n) {
747
+ const t = w.get(e) ?? /* @__PURE__ */ new Map();
748
+ Object.entries(n).forEach(([r, o]) => t.set(r, o)), w.set(e, t);
749
+ }
750
+ function P() {
751
+ const e = [];
752
+ for (const [n, t] of w) {
753
+ if (t.size === 0) continue;
754
+ const r = [...t.entries()].map(([o, s]) => ` ${o}: ${s};`).join(`
689
755
  `);
690
- t.push(`.${N()}${r} {
691
- ${n}
756
+ e.push(`.${M()}${n} {
757
+ ${r}
692
758
  }`);
693
759
  }
694
- return t.join(`
760
+ return e.join(`
695
761
  `);
696
762
  }
697
- function q() {
763
+ function Q() {
698
764
  if ("document" in globalThis) {
699
- const t = R(), r = globalThis, e = r.document.getElementById(
765
+ const e = P(), n = globalThis, t = n.document.getElementById(
700
766
  "html-text-parser-styles"
701
767
  );
702
- if (e) {
703
- e.textContent = t;
768
+ if (t) {
769
+ t.textContent = e;
704
770
  return;
705
771
  }
706
- const n = r.document.createElement("style");
707
- n.textContent = t, n.setAttribute("id", "html-text-parser-styles"), r.document.head.appendChild(n);
772
+ const r = n.document.createElement("style");
773
+ r.textContent = e, r.setAttribute("id", "html-text-parser-styles"), n.document.head.appendChild(r);
708
774
  return;
709
775
  }
710
776
  console.warn(
711
777
  "[html-text-parser] Cannot inject stylesheet: document is not available."
712
778
  );
713
779
  }
714
- function X(t) {
715
- const r = [];
716
- let e;
717
- for (const n of t) {
718
- const o = n.containerTagNames.join(","), s = e?.containerTagNames.join(",");
719
- o !== s && (e = {
720
- containerTagNames: n.containerTagNames,
721
- cssClass: n.containerTagNames.map((a) => `${N()}${a}`).join(" "),
722
- style: n.containerTagNames.map(
780
+ function ee(e) {
781
+ const n = [];
782
+ let t;
783
+ for (const r of e) {
784
+ const o = r.containerTagNames.join(","), s = t?.containerTagNames.join(",");
785
+ o !== s && (t = {
786
+ containerTagNames: r.containerTagNames,
787
+ cssClass: r.containerTagNames.map((a) => `${M()}${a}`).join(" "),
788
+ style: r.containerTagNames.map(
723
789
  (a) => [...w.get(a)?.entries() ?? []].map(([i, c]) => `${i}: ${c}`).join("; ")
724
790
  ).filter(Boolean).join("; "),
725
791
  styleObject: Object.fromEntries(
726
- n.containerTagNames.flatMap(
792
+ r.containerTagNames.flatMap(
727
793
  (a) => Array.from(w.get(a) ?? [])
728
794
  )
729
795
  ),
730
796
  segments: []
731
- }, r.push(e)), e.segments.push(n);
797
+ }, n.push(t)), t.segments.push(r);
732
798
  }
733
- return r;
799
+ return n;
734
800
  }
735
- function K(t, r, e) {
801
+ function te(e, n, t) {
736
802
  if (!("document" in globalThis)) {
737
803
  console.warn(
738
804
  "[html-text-parser] Cannot append segments: document is not available."
739
805
  );
740
806
  return;
741
807
  }
742
- if (!t || !("appendChild" in t)) {
808
+ if (!e || !("appendChild" in e)) {
743
809
  console.warn(
744
810
  "[html-text-parser] Cannot append segments: provided element does not support appendChild."
745
811
  );
746
812
  return;
747
813
  }
748
- e = { useClasses: !0, useStyles: !1, ...e };
749
- const n = t, o = globalThis.document;
750
- for (const s of r) {
814
+ t = { useClasses: !0, useStyles: !1, ...t };
815
+ const r = e, o = globalThis.document;
816
+ for (const s of n) {
751
817
  const a = o.createElement(
752
818
  s.containerTagNames[0] || "span"
753
819
  );
754
- e.useClasses && (a.className = s.cssClass), e.useStyles && a.setAttribute("style", s.style);
820
+ t.useClasses && (a.className = s.cssClass), t.useStyles && a.setAttribute("style", s.style);
755
821
  for (const i of s.segments) {
756
822
  const c = o.createElement("span");
757
- c.textContent = i.text, e.useClasses && (c.className = i.cssClass), e.useStyles && c.setAttribute("style", i.style), a.appendChild(c);
823
+ c.textContent = i.text, t.useClasses && (c.className = i.cssClass), t.useStyles && c.setAttribute("style", i.style), a.appendChild(c);
758
824
  }
759
- n.appendChild(a);
825
+ r.appendChild(a);
760
826
  }
761
827
  }
762
- function h(t) {
763
- return X(_(t));
828
+ function h(e) {
829
+ return ee(B(e));
764
830
  }
765
- function _(t) {
766
- const r = [], e = /* @__PURE__ */ new Map();
767
- let n = 0;
768
- for (const o of t.matchAll(Z())) {
831
+ function B(e) {
832
+ const n = [], t = /* @__PURE__ */ new Map();
833
+ let r = 0;
834
+ for (const o of e.matchAll(Y())) {
769
835
  const s = o.index;
770
- s > n && r.push(
771
- M([...e.keys()], t, n, s)
836
+ s > r && n.push(
837
+ x([...t.keys()], e, r, s)
772
838
  );
773
839
  const a = o[2]?.toLowerCase();
774
- if (a && j.has(a)) {
775
- r.push(
776
- M(
777
- [...e.keys(), a],
778
- t,
779
- n,
840
+ if (a && N.has(a)) {
841
+ n.push(
842
+ x(
843
+ [...t.keys(), a],
844
+ e,
845
+ r,
780
846
  s,
781
847
  ""
782
848
  )
783
- ), n = s + o[0].length;
849
+ ), r = s + o[0].length;
784
850
  continue;
785
851
  }
786
- const i = o[0][1] === "/", c = x.get(o[1]?.toLowerCase() ?? "") ?? "";
852
+ const i = o[0][1] === "/", c = A.get(o[1]?.toLowerCase() ?? "") ?? "";
787
853
  if (i) {
788
- const l = (e.get(c) ?? 1) - 1;
789
- l <= 0 ? e.delete(c) : e.set(c, l);
854
+ const l = (t.get(c) ?? 1) - 1;
855
+ l <= 0 ? t.delete(c) : t.set(c, l);
790
856
  } else
791
- e.set(c, (e.get(c) ?? 0) + 1);
792
- n = s + o[0].length;
793
- }
794
- return n < t.length && r.push(
795
- M(
796
- [...e.keys()],
797
- t,
798
- n,
799
- t.length
857
+ t.set(c, (t.get(c) ?? 0) + 1);
858
+ r = s + o[0].length;
859
+ }
860
+ return r < e.length && n.push(
861
+ x(
862
+ [...t.keys()],
863
+ e,
864
+ r,
865
+ e.length
800
866
  )
801
- ), r;
867
+ ), n;
802
868
  }
803
- function B(t) {
804
- if (Array.isArray(t)) {
805
- t.forEach((e) => {
806
- B(e);
869
+ function U(e) {
870
+ if (Array.isArray(e)) {
871
+ e.forEach((t) => {
872
+ U(t);
807
873
  });
808
874
  return;
809
875
  }
810
- const r = t.tagName ?? t.tag;
811
- x.set(t.tag, r), t.style != null && Object.keys(t.style).length && P(r, t.style);
876
+ const n = e.tagName ?? e.tag;
877
+ A.set(e.tag, n), e.style != null && Object.keys(e.style).length && z(n, e.style);
812
878
  }
813
- function U(t) {
814
- if (Array.isArray(t)) {
815
- t.forEach((e) => {
816
- U(e);
879
+ function D(e) {
880
+ if (Array.isArray(e)) {
881
+ e.forEach((t) => {
882
+ D(t);
817
883
  });
818
884
  return;
819
885
  }
820
- const r = t.tagName ?? t.tag;
821
- j.set(t.tag, r), t.style != null && Object.keys(t.style).length && P(r, t.style);
822
- }
823
- h.setTag = B;
824
- h.setSelfClosingTag = U;
825
- h.setBlockTag = (t) => T.add(t);
826
- h.removeBlockTag = (t) => T.delete(t);
827
- h.setCssClassPrefix = V;
828
- h.getStylesheet = R;
829
- h.flat = _;
830
- h.generateStylesheet = q;
831
- h.appendToDom = K;
832
- function Q(t, r, e) {
833
- function n(o) {
834
- const [s, a] = E(() => e ? e(o) : o);
835
- if (a) throw new Error(`Error transforming value for ${t}: ${a.message}`);
836
- if (!r(s)) throw new Error(`"${o}" is not a valid ${t}`);
886
+ const n = e.tagName ?? e.tag;
887
+ N.set(e.tag, n), e.style != null && Object.keys(e.style).length && z(n, e.style);
888
+ }
889
+ h.setTag = U;
890
+ h.setSelfClosingTag = D;
891
+ h.setBlockTag = (e) => v.add(e);
892
+ h.removeBlockTag = (e) => v.delete(e);
893
+ h.setCssClassPrefix = X;
894
+ h.getStylesheet = P;
895
+ h.flat = B;
896
+ h.generateStylesheet = Q;
897
+ h.appendToDom = te;
898
+ function ne(e, n, t) {
899
+ function r(o) {
900
+ const [s, a] = E(() => t ? t(o) : o);
901
+ if (a) throw new Error(`Error transforming value for ${e}: ${a.message}`);
902
+ if (!n(s)) throw new Error(`"${o}" is not a valid ${e}`);
837
903
  return s;
838
904
  }
839
- return n.from = (o) => {
840
- const [s, a] = E(() => e ? e(o) : o);
905
+ return r.from = (o) => {
906
+ const [s, a] = E(() => t ? t(o) : o);
841
907
  if (!a)
842
- return r(s) ? s : void 0;
843
- }, n.is = (o) => {
844
- const s = E(() => e ? e(o) : o)[0] ?? o;
845
- return r(s);
846
- }, n;
908
+ return n(s) ? s : void 0;
909
+ }, r.is = (o) => {
910
+ const s = E(() => t ? t(o) : o)[0] ?? o;
911
+ return n(s);
912
+ }, r;
847
913
  }
848
- const fe = Q(
914
+ const ye = ne(
849
915
  "NumericString",
850
- (t) => (
916
+ (e) => (
851
917
  // checks that a given value is:
852
918
  // - a number, and the number is neither positive Infinity, negative Infinity, nor NaN.
853
- typeof t == "string" && t.trim() !== "" && Number.isFinite(Number(t))
919
+ typeof e == "string" && e.trim() !== "" && Number.isFinite(Number(e))
854
920
  ),
855
- (t) => String(t)
921
+ (e) => String(e)
856
922
  );
857
923
  export {
858
- fe as NumericString,
859
- m as Result,
860
- z as SIGNAL,
861
- ne as clamp,
924
+ ye as NumericString,
925
+ y as Result,
926
+ O as SIGNAL,
927
+ ce as clamp,
862
928
  L as computed,
863
- W as convertCase,
864
- Q as createBrand,
865
- se as debounce,
866
- re as delay,
929
+ re as convertCase,
930
+ ne as createBrand,
931
+ le as debounce,
932
+ ie as delay,
867
933
  k as effect,
868
- oe as getProp,
869
- te as groupBy,
870
- ee as http,
871
- ie as isSignal,
872
- le as linkedSignal,
873
- G as normalizeString,
934
+ J as getProp,
935
+ ae as groupBy,
936
+ oe as http,
937
+ de as isSignal,
938
+ he as linkedSignal,
939
+ I as normalizeString,
874
940
  h as parseRichText,
875
- _ as parseRichTextFlat,
876
- ue as resource,
877
- B as setRichTextTag,
878
- U as setSelfClosingTag,
941
+ B as parseRichTextFlat,
942
+ ue as pickPaths,
943
+ ge as resource,
944
+ Z as setDeepValue,
945
+ U as setRichTextTag,
946
+ D as setSelfClosingTag,
879
947
  S as signal,
880
- ae as toArray,
948
+ fe as toArray,
881
949
  E as tryCatch,
882
- Y as tryCatchAsync,
883
- ce as untracked
950
+ se as tryCatchAsync,
951
+ pe as untracked
884
952
  };
package/lib/getProp.d.ts CHANGED
@@ -15,3 +15,13 @@ export type PathValue<T, P extends string> = P extends `${infer K}.${infer Rest}
15
15
  * @returns {PathValue<T, P>} The value at the given path, or undefined if the path is invalid.
16
16
  */
17
17
  export declare function getProp<T, P extends string>(obj: T, path: P): PathValue<T, P>;
18
+ /**
19
+ * Utility to deeply set a value in an object at a given path array.
20
+ * This is a helper function to reconstruct the object structure.
21
+ */
22
+ export declare function setDeepValue<TObj extends object, TValue = any>(obj: TObj, keysPath: string[], value: TValue): void;
23
+ /**
24
+ * Takes an object and an array of paths, returning a new object
25
+ * that preserves the original nested structure for the requested keys.
26
+ */
27
+ export declare function pickPaths<T, P extends string>(obj: T, paths: readonly P[]): Partial<T>;
package/lib/http.d.ts CHANGED
@@ -1,3 +1,14 @@
1
+ export type HttpError = Error & {
2
+ status?: number;
3
+ statusText?: string;
4
+ body?: string;
5
+ code?: string;
6
+ isNetworkError?: boolean;
7
+ cause?: unknown;
8
+ };
9
+ export interface RequestInit extends globalThis.RequestInit {
10
+ parse?: 'JSON' | 'TEXT' | 'BLOB' | 'ARRAYBUFFER';
11
+ }
1
12
  /**
2
13
  * A utility object for making HTTP requests.
3
14
  */
package/lib/tryCatch.d.ts CHANGED
@@ -11,7 +11,7 @@ export type TryResult<T, E = Error> = [T, null] | [null, E];
11
11
  * @param {Promise<T>} promise - The promise to handle
12
12
  * @returns {Promise<TryResult<T, E>>} - A promise that resolves to a tuple with either the result or the error
13
13
  */
14
- export declare function tryCatchAsync<T, E = Error>(promise: Promise<T>): Promise<TryResult<T, E>>;
14
+ export declare function tryCatchAsync<T, E = Error>(promise: Promise<T> | (() => Promise<T>)): Promise<TryResult<T, E>>;
15
15
  /**
16
16
  * Main wrapper function to handle promise with try-catch
17
17
  * @template T - Type of the successful result
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@dvirus-js/utils",
3
- "version": "0.0.6",
3
+ "version": "0.0.7",
4
4
  "type": "module",
5
5
  "main": "./index.js",
6
6
  "types": "./index.d.ts",