@ztimson/utils 0.27.8 → 0.27.10

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/dist/index.d.ts CHANGED
@@ -9,6 +9,7 @@ export * from './files';
9
9
  export * from './emitter';
10
10
  export * from './errors';
11
11
  export * from './http';
12
+ export * from './json';
12
13
  export * from './jwt';
13
14
  export * from './logger';
14
15
  export * from './math';
package/dist/index.mjs CHANGED
@@ -117,6 +117,27 @@ ${opts.message || this.desc}`;
117
117
  `;
118
118
  }
119
119
  }
120
+ function JSONAttemptParse(json, fallback) {
121
+ try {
122
+ return JSON.parse(json);
123
+ } catch {
124
+ return fallback ?? json;
125
+ }
126
+ }
127
+ function JSONSerialize(obj) {
128
+ if (typeof obj == "object" && obj != null) return JSONSanitize(obj);
129
+ return obj;
130
+ }
131
+ function JSONSanitize(obj, space) {
132
+ const cache = [];
133
+ return JSON.stringify(obj, (key, value) => {
134
+ if (typeof value === "object" && value !== null) {
135
+ if (cache.includes(value)) return "[Circular]";
136
+ cache.push(value);
137
+ }
138
+ return value;
139
+ }, space);
140
+ }
120
141
  function applyDeltas(base, ...deltas) {
121
142
  function applyDelta(base2, delta) {
122
143
  if (delta === null) return null;
@@ -161,6 +182,9 @@ function clean(obj, undefinedOnly = false) {
161
182
  return obj;
162
183
  }
163
184
  function deepCopy(value) {
185
+ if (value == null) return value;
186
+ const t = typeof value;
187
+ if (t === "string" || t === "number" || t === "boolean" || t === "function") return value;
164
188
  try {
165
189
  return structuredClone(value);
166
190
  } catch {
@@ -270,27 +294,6 @@ function mixin(target, constructors) {
270
294
  function objectMap(obj, fn2) {
271
295
  return Object.entries(obj).map(([key, value]) => [key, fn2(key, value)]).reduce((acc, [key, value]) => ({ ...acc, [key]: value }), {});
272
296
  }
273
- function JSONAttemptParse(json) {
274
- try {
275
- return JSON.parse(json);
276
- } catch {
277
- return json;
278
- }
279
- }
280
- function JSONSerialize(obj) {
281
- if (typeof obj == "object" && obj != null) return JSONSanitize(obj);
282
- return obj;
283
- }
284
- function JSONSanitize(obj, space) {
285
- const cache = [];
286
- return JSON.stringify(obj, (key, value) => {
287
- if (typeof value === "object" && value !== null) {
288
- if (cache.includes(value)) return "[Circular]";
289
- cache.push(value);
290
- }
291
- return value;
292
- }, space);
293
- }
294
297
  class ASet extends Array {
295
298
  /** Number of elements in set */
296
299
  get size() {
@@ -463,10 +466,7 @@ class Cache {
463
466
  __publicField(this, "complete", false);
464
467
  /** Await initial loading */
465
468
  __publicField(this, "loading", new Promise((r) => this._loading = r));
466
- /**
467
- * Get all cached items
468
- * @return {T[]} Array of items
469
- */
469
+ /** Get all cached items */
470
470
  __publicField(this, "values", this.all);
471
471
  var _a, _b, _c, _d;
472
472
  this.key = key;
@@ -514,6 +514,7 @@ class Cache {
514
514
  if (value[this.key] === void 0) throw new Error(`${this.key.toString()} Doesn't exist on ${JSON.stringify(value, null, 2)}`);
515
515
  return value[this.key];
516
516
  }
517
+ /** Save item to storage */
517
518
  save(key) {
518
519
  var _a, _b;
519
520
  const persists = this.options.persistentStorage;
@@ -567,9 +568,6 @@ class Cache {
567
568
  }
568
569
  /**
569
570
  * Add a new item to the cache. Like set, but finds key automatically
570
- * @param {T} value Item to add to cache
571
- * @param {number | undefined} ttl Override default expiry
572
- * @return {this}
573
571
  */
574
572
  add(value, ttl = this.ttl) {
575
573
  const key = this.getKey(value);
@@ -578,9 +576,6 @@ class Cache {
578
576
  }
579
577
  /**
580
578
  * Add several rows to the cache
581
- * @param {T[]} rows Several items that will be cached using the default key
582
- * @param complete Mark cache as complete & reliable, defaults to true
583
- * @return {this}
584
579
  */
585
580
  addAll(rows, complete = true) {
586
581
  this.clear();
@@ -588,9 +583,7 @@ class Cache {
588
583
  this.complete = complete;
589
584
  return this;
590
585
  }
591
- /**
592
- * Remove all keys from cache
593
- */
586
+ /** Remove all keys */
594
587
  clear() {
595
588
  this.complete = false;
596
589
  for (const [k, t] of this.timers) clearTimeout(t);
@@ -600,10 +593,7 @@ class Cache {
600
593
  this.save();
601
594
  return this;
602
595
  }
603
- /**
604
- * Delete an item from the cache
605
- * @param {K} key Item's primary key
606
- */
596
+ /** Delete a cached item */
607
597
  delete(key) {
608
598
  this.clearTimer(key);
609
599
  const idx = this.lruOrder.indexOf(key);
@@ -612,10 +602,7 @@ class Cache {
612
602
  this.save(key);
613
603
  return this;
614
604
  }
615
- /**
616
- * Return cache as an array of key-value pairs
617
- * @return {[K, T][]} Key-value pairs array
618
- */
605
+ /** Return entries as array */
619
606
  entries(expired) {
620
607
  const out = [];
621
608
  for (const [k, v] of this.store.entries()) {
@@ -624,10 +611,7 @@ class Cache {
624
611
  }
625
612
  return out;
626
613
  }
627
- /**
628
- * Manually expire a cached item
629
- * @param {K} key Key to expire
630
- */
614
+ /** Manually expire a cached item */
631
615
  expire(key) {
632
616
  this.complete = false;
633
617
  if (this.options.expiryPolicy == "keep") {
@@ -640,12 +624,7 @@ class Cache {
640
624
  } else this.delete(key);
641
625
  return this;
642
626
  }
643
- /**
644
- * Find the first cached item to match a filter
645
- * @param {Partial<T>} filter Partial item to match
646
- * @param {Boolean} expired Include expired items, defaults to false
647
- * @returns {T | undefined} Cached item or undefined if nothing matched
648
- */
627
+ /** Find first matching item */
649
628
  find(filter, expired) {
650
629
  for (const v of this.store.values()) {
651
630
  const row = v;
@@ -653,24 +632,16 @@ class Cache {
653
632
  }
654
633
  return void 0;
655
634
  }
656
- /**
657
- * Get item from the cache
658
- * @param {K} key Key to lookup
659
- * @param expired Include expired items
660
- * @return {T} Cached item
661
- */
635
+ /** Get cached item by key */
662
636
  get(key, expired) {
663
637
  const raw = this.store.get(key);
664
638
  if (raw == null) return null;
665
- const cached = deepCopy(raw);
666
639
  this.touchLRU(key);
667
- if (expired || !(cached == null ? void 0 : cached._expired)) return cached;
640
+ const isExpired = raw == null ? void 0 : raw._expired;
641
+ if (expired || !isExpired) return deepCopy(raw);
668
642
  return null;
669
643
  }
670
- /**
671
- * Get a list of cached keys
672
- * @return {K[]} Array of keys
673
- */
644
+ /** Return list of keys */
674
645
  keys(expired) {
675
646
  const out = [];
676
647
  for (const [k, v] of this.store.entries()) {
@@ -679,10 +650,7 @@ class Cache {
679
650
  }
680
651
  return out;
681
652
  }
682
- /**
683
- * Get map of cached items
684
- * @return {Record<K, T>}
685
- */
653
+ /** Return map of key → item */
686
654
  map(expired) {
687
655
  const copy = {};
688
656
  for (const [k, v] of this.store.entries()) {
@@ -691,13 +659,7 @@ class Cache {
691
659
  }
692
660
  return copy;
693
661
  }
694
- /**
695
- * Add an item to the cache manually specifying the key
696
- * @param {K} key Key item will be cached under
697
- * @param {T} value Item to cache
698
- * @param {number | undefined} ttl Override default expiry in seconds
699
- * @return {this}
700
- */
662
+ /** Add item manually specifying the key */
701
663
  set(key, value, ttl = this.options.ttl) {
702
664
  if (this.options.expiryPolicy == "keep") delete value._expired;
703
665
  this.clearTimer(key);
@@ -708,7 +670,7 @@ class Cache {
708
670
  const t = setTimeout(() => {
709
671
  this.expire(key);
710
672
  this.save(key);
711
- }, (ttl || 0) * 1e3);
673
+ }, ttl * 1e3);
712
674
  this.timers.set(key, t);
713
675
  }
714
676
  return this;
@@ -2422,7 +2384,7 @@ class MemoryStorage {
2422
2384
  }
2423
2385
  }
2424
2386
  memoryStorage.MemoryStorage = MemoryStorage;
2425
- (function(exports) {
2387
+ (function(exports$1) {
2426
2388
  var __createBinding = commonjsGlobal && commonjsGlobal.__createBinding || (Object.create ? function(o, m, k, k2) {
2427
2389
  if (k2 === void 0) k2 = k;
2428
2390
  var desc = Object.getOwnPropertyDescriptor(m, k);
@@ -2436,12 +2398,12 @@ memoryStorage.MemoryStorage = MemoryStorage;
2436
2398
  if (k2 === void 0) k2 = k;
2437
2399
  o[k2] = m[k];
2438
2400
  });
2439
- var __exportStar = commonjsGlobal && commonjsGlobal.__exportStar || function(m, exports2) {
2440
- for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports2, p)) __createBinding(exports2, m, p);
2401
+ var __exportStar = commonjsGlobal && commonjsGlobal.__exportStar || function(m, exports$12) {
2402
+ for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports$12, p)) __createBinding(exports$12, m, p);
2441
2403
  };
2442
- Object.defineProperty(exports, "__esModule", { value: true });
2443
- __exportStar(persist$1, exports);
2444
- __exportStar(memoryStorage, exports);
2404
+ Object.defineProperty(exports$1, "__esModule", { value: true });
2405
+ __exportStar(persist$1, exports$1);
2406
+ __exportStar(memoryStorage, exports$1);
2445
2407
  })(dist);
2446
2408
  export {
2447
2409
  ASet,