@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/cache.d.ts CHANGED
@@ -38,6 +38,7 @@ export declare class Cache<K extends string | number | symbol, T> {
38
38
  */
39
39
  constructor(key?: keyof T | undefined, options?: CacheOptions);
40
40
  private getKey;
41
+ /** Save item to storage */
41
42
  private save;
42
43
  private clearTimer;
43
44
  private touchLRU;
@@ -48,72 +49,30 @@ export declare class Cache<K extends string | number | symbol, T> {
48
49
  all(expired?: boolean): CachedValue<T>[];
49
50
  /**
50
51
  * Add a new item to the cache. Like set, but finds key automatically
51
- * @param {T} value Item to add to cache
52
- * @param {number | undefined} ttl Override default expiry
53
- * @return {this}
54
52
  */
55
53
  add(value: T, ttl?: any): this;
56
54
  /**
57
55
  * Add several rows to the cache
58
- * @param {T[]} rows Several items that will be cached using the default key
59
- * @param complete Mark cache as complete & reliable, defaults to true
60
- * @return {this}
61
56
  */
62
57
  addAll(rows: T[], complete?: boolean): this;
63
- /**
64
- * Remove all keys from cache
65
- */
58
+ /** Remove all keys */
66
59
  clear(): this;
67
- /**
68
- * Delete an item from the cache
69
- * @param {K} key Item's primary key
70
- */
60
+ /** Delete a cached item */
71
61
  delete(key: K): this;
72
- /**
73
- * Return cache as an array of key-value pairs
74
- * @return {[K, T][]} Key-value pairs array
75
- */
62
+ /** Return entries as array */
76
63
  entries(expired?: boolean): [K, CachedValue<T>][];
77
- /**
78
- * Manually expire a cached item
79
- * @param {K} key Key to expire
80
- */
64
+ /** Manually expire a cached item */
81
65
  expire(key: K): this;
82
- /**
83
- * Find the first cached item to match a filter
84
- * @param {Partial<T>} filter Partial item to match
85
- * @param {Boolean} expired Include expired items, defaults to false
86
- * @returns {T | undefined} Cached item or undefined if nothing matched
87
- */
66
+ /** Find first matching item */
88
67
  find(filter: Partial<T>, expired?: boolean): T | undefined;
89
- /**
90
- * Get item from the cache
91
- * @param {K} key Key to lookup
92
- * @param expired Include expired items
93
- * @return {T} Cached item
94
- */
68
+ /** Get cached item by key */
95
69
  get(key: K, expired?: boolean): CachedValue<T> | null;
96
- /**
97
- * Get a list of cached keys
98
- * @return {K[]} Array of keys
99
- */
70
+ /** Return list of keys */
100
71
  keys(expired?: boolean): K[];
101
- /**
102
- * Get map of cached items
103
- * @return {Record<K, T>}
104
- */
72
+ /** Return map of key → item */
105
73
  map(expired?: boolean): Record<K, CachedValue<T>>;
106
- /**
107
- * Add an item to the cache manually specifying the key
108
- * @param {K} key Key item will be cached under
109
- * @param {T} value Item to cache
110
- * @param {number | undefined} ttl Override default expiry in seconds
111
- * @return {this}
112
- */
74
+ /** Add item manually specifying the key */
113
75
  set(key: K, value: T, ttl?: number | undefined): this;
114
- /**
115
- * Get all cached items
116
- * @return {T[]} Array of items
117
- */
76
+ /** Get all cached items */
118
77
  values: (expired?: boolean) => CachedValue<T>[];
119
78
  }
package/dist/index.cjs CHANGED
@@ -121,6 +121,27 @@ ${opts.message || this.desc}`;
121
121
  `;
122
122
  }
123
123
  }
124
+ function JSONAttemptParse(json, fallback) {
125
+ try {
126
+ return JSON.parse(json);
127
+ } catch {
128
+ return fallback ?? json;
129
+ }
130
+ }
131
+ function JSONSerialize(obj) {
132
+ if (typeof obj == "object" && obj != null) return JSONSanitize(obj);
133
+ return obj;
134
+ }
135
+ function JSONSanitize(obj, space) {
136
+ const cache = [];
137
+ return JSON.stringify(obj, (key, value) => {
138
+ if (typeof value === "object" && value !== null) {
139
+ if (cache.includes(value)) return "[Circular]";
140
+ cache.push(value);
141
+ }
142
+ return value;
143
+ }, space);
144
+ }
124
145
  function applyDeltas(base, ...deltas) {
125
146
  function applyDelta(base2, delta) {
126
147
  if (delta === null) return null;
@@ -165,6 +186,9 @@ ${opts.message || this.desc}`;
165
186
  return obj;
166
187
  }
167
188
  function deepCopy(value) {
189
+ if (value == null) return value;
190
+ const t = typeof value;
191
+ if (t === "string" || t === "number" || t === "boolean" || t === "function") return value;
168
192
  try {
169
193
  return structuredClone(value);
170
194
  } catch {
@@ -274,27 +298,6 @@ ${opts.message || this.desc}`;
274
298
  function objectMap(obj, fn2) {
275
299
  return Object.entries(obj).map(([key, value]) => [key, fn2(key, value)]).reduce((acc, [key, value]) => ({ ...acc, [key]: value }), {});
276
300
  }
277
- function JSONAttemptParse(json) {
278
- try {
279
- return JSON.parse(json);
280
- } catch {
281
- return json;
282
- }
283
- }
284
- function JSONSerialize(obj) {
285
- if (typeof obj == "object" && obj != null) return JSONSanitize(obj);
286
- return obj;
287
- }
288
- function JSONSanitize(obj, space) {
289
- const cache = [];
290
- return JSON.stringify(obj, (key, value) => {
291
- if (typeof value === "object" && value !== null) {
292
- if (cache.includes(value)) return "[Circular]";
293
- cache.push(value);
294
- }
295
- return value;
296
- }, space);
297
- }
298
301
  class ASet extends Array {
299
302
  /** Number of elements in set */
300
303
  get size() {
@@ -467,10 +470,7 @@ ${opts.message || this.desc}`;
467
470
  __publicField(this, "complete", false);
468
471
  /** Await initial loading */
469
472
  __publicField(this, "loading", new Promise((r) => this._loading = r));
470
- /**
471
- * Get all cached items
472
- * @return {T[]} Array of items
473
- */
473
+ /** Get all cached items */
474
474
  __publicField(this, "values", this.all);
475
475
  var _a, _b, _c, _d;
476
476
  this.key = key;
@@ -518,6 +518,7 @@ ${opts.message || this.desc}`;
518
518
  if (value[this.key] === void 0) throw new Error(`${this.key.toString()} Doesn't exist on ${JSON.stringify(value, null, 2)}`);
519
519
  return value[this.key];
520
520
  }
521
+ /** Save item to storage */
521
522
  save(key) {
522
523
  var _a, _b;
523
524
  const persists = this.options.persistentStorage;
@@ -571,9 +572,6 @@ ${opts.message || this.desc}`;
571
572
  }
572
573
  /**
573
574
  * Add a new item to the cache. Like set, but finds key automatically
574
- * @param {T} value Item to add to cache
575
- * @param {number | undefined} ttl Override default expiry
576
- * @return {this}
577
575
  */
578
576
  add(value, ttl = this.ttl) {
579
577
  const key = this.getKey(value);
@@ -582,9 +580,6 @@ ${opts.message || this.desc}`;
582
580
  }
583
581
  /**
584
582
  * Add several rows to the cache
585
- * @param {T[]} rows Several items that will be cached using the default key
586
- * @param complete Mark cache as complete & reliable, defaults to true
587
- * @return {this}
588
583
  */
589
584
  addAll(rows, complete = true) {
590
585
  this.clear();
@@ -592,9 +587,7 @@ ${opts.message || this.desc}`;
592
587
  this.complete = complete;
593
588
  return this;
594
589
  }
595
- /**
596
- * Remove all keys from cache
597
- */
590
+ /** Remove all keys */
598
591
  clear() {
599
592
  this.complete = false;
600
593
  for (const [k, t] of this.timers) clearTimeout(t);
@@ -604,10 +597,7 @@ ${opts.message || this.desc}`;
604
597
  this.save();
605
598
  return this;
606
599
  }
607
- /**
608
- * Delete an item from the cache
609
- * @param {K} key Item's primary key
610
- */
600
+ /** Delete a cached item */
611
601
  delete(key) {
612
602
  this.clearTimer(key);
613
603
  const idx = this.lruOrder.indexOf(key);
@@ -616,10 +606,7 @@ ${opts.message || this.desc}`;
616
606
  this.save(key);
617
607
  return this;
618
608
  }
619
- /**
620
- * Return cache as an array of key-value pairs
621
- * @return {[K, T][]} Key-value pairs array
622
- */
609
+ /** Return entries as array */
623
610
  entries(expired) {
624
611
  const out = [];
625
612
  for (const [k, v] of this.store.entries()) {
@@ -628,10 +615,7 @@ ${opts.message || this.desc}`;
628
615
  }
629
616
  return out;
630
617
  }
631
- /**
632
- * Manually expire a cached item
633
- * @param {K} key Key to expire
634
- */
618
+ /** Manually expire a cached item */
635
619
  expire(key) {
636
620
  this.complete = false;
637
621
  if (this.options.expiryPolicy == "keep") {
@@ -644,12 +628,7 @@ ${opts.message || this.desc}`;
644
628
  } else this.delete(key);
645
629
  return this;
646
630
  }
647
- /**
648
- * Find the first cached item to match a filter
649
- * @param {Partial<T>} filter Partial item to match
650
- * @param {Boolean} expired Include expired items, defaults to false
651
- * @returns {T | undefined} Cached item or undefined if nothing matched
652
- */
631
+ /** Find first matching item */
653
632
  find(filter, expired) {
654
633
  for (const v of this.store.values()) {
655
634
  const row = v;
@@ -657,24 +636,16 @@ ${opts.message || this.desc}`;
657
636
  }
658
637
  return void 0;
659
638
  }
660
- /**
661
- * Get item from the cache
662
- * @param {K} key Key to lookup
663
- * @param expired Include expired items
664
- * @return {T} Cached item
665
- */
639
+ /** Get cached item by key */
666
640
  get(key, expired) {
667
641
  const raw = this.store.get(key);
668
642
  if (raw == null) return null;
669
- const cached = deepCopy(raw);
670
643
  this.touchLRU(key);
671
- if (expired || !(cached == null ? void 0 : cached._expired)) return cached;
644
+ const isExpired = raw == null ? void 0 : raw._expired;
645
+ if (expired || !isExpired) return deepCopy(raw);
672
646
  return null;
673
647
  }
674
- /**
675
- * Get a list of cached keys
676
- * @return {K[]} Array of keys
677
- */
648
+ /** Return list of keys */
678
649
  keys(expired) {
679
650
  const out = [];
680
651
  for (const [k, v] of this.store.entries()) {
@@ -683,10 +654,7 @@ ${opts.message || this.desc}`;
683
654
  }
684
655
  return out;
685
656
  }
686
- /**
687
- * Get map of cached items
688
- * @return {Record<K, T>}
689
- */
657
+ /** Return map of key → item */
690
658
  map(expired) {
691
659
  const copy = {};
692
660
  for (const [k, v] of this.store.entries()) {
@@ -695,13 +663,7 @@ ${opts.message || this.desc}`;
695
663
  }
696
664
  return copy;
697
665
  }
698
- /**
699
- * Add an item to the cache manually specifying the key
700
- * @param {K} key Key item will be cached under
701
- * @param {T} value Item to cache
702
- * @param {number | undefined} ttl Override default expiry in seconds
703
- * @return {this}
704
- */
666
+ /** Add item manually specifying the key */
705
667
  set(key, value, ttl = this.options.ttl) {
706
668
  if (this.options.expiryPolicy == "keep") delete value._expired;
707
669
  this.clearTimer(key);
@@ -712,7 +674,7 @@ ${opts.message || this.desc}`;
712
674
  const t = setTimeout(() => {
713
675
  this.expire(key);
714
676
  this.save(key);
715
- }, (ttl || 0) * 1e3);
677
+ }, ttl * 1e3);
716
678
  this.timers.set(key, t);
717
679
  }
718
680
  return this;
@@ -2426,7 +2388,7 @@ ${opts.message || this.desc}`;
2426
2388
  }
2427
2389
  }
2428
2390
  memoryStorage.MemoryStorage = MemoryStorage;
2429
- (function(exports3) {
2391
+ (function(exports$1) {
2430
2392
  var __createBinding = commonjsGlobal && commonjsGlobal.__createBinding || (Object.create ? function(o, m, k, k2) {
2431
2393
  if (k2 === void 0) k2 = k;
2432
2394
  var desc = Object.getOwnPropertyDescriptor(m, k);
@@ -2440,12 +2402,12 @@ ${opts.message || this.desc}`;
2440
2402
  if (k2 === void 0) k2 = k;
2441
2403
  o[k2] = m[k];
2442
2404
  });
2443
- var __exportStar = commonjsGlobal && commonjsGlobal.__exportStar || function(m, exports4) {
2444
- for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports4, p)) __createBinding(exports4, m, p);
2405
+ var __exportStar = commonjsGlobal && commonjsGlobal.__exportStar || function(m, exports$12) {
2406
+ for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports$12, p)) __createBinding(exports$12, m, p);
2445
2407
  };
2446
- Object.defineProperty(exports3, "__esModule", { value: true });
2447
- __exportStar(persist$1, exports3);
2448
- __exportStar(memoryStorage, exports3);
2408
+ Object.defineProperty(exports$1, "__esModule", { value: true });
2409
+ __exportStar(persist$1, exports$1);
2410
+ __exportStar(memoryStorage, exports$1);
2449
2411
  })(dist);
2450
2412
  exports2.ASet = ASet;
2451
2413
  exports2.ArgParser = ArgParser;