@ztimson/utils 0.27.4 → 0.27.6

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.mjs CHANGED
@@ -452,17 +452,19 @@ class Cache {
452
452
  * @param options
453
453
  */
454
454
  constructor(key, options = {}) {
455
- __publicField(this, "store", {});
455
+ __publicField(this, "_loading");
456
+ __publicField(this, "store", /* @__PURE__ */ new Map());
457
+ __publicField(this, "timers", /* @__PURE__ */ new Map());
458
+ __publicField(this, "lruOrder", []);
456
459
  /** Whether cache is complete */
457
460
  __publicField(this, "complete", false);
458
- __publicField(this, "_loading");
459
461
  /** Await initial loading */
460
462
  __publicField(this, "loading", new Promise((r) => this._loading = r));
461
463
  /**
462
464
  * Get all cached items
463
465
  * @return {T[]} Array of items
464
466
  */
465
- __publicField(this, "values", this.all());
467
+ __publicField(this, "values", this.all);
466
468
  var _a, _b, _c, _d;
467
469
  this.key = key;
468
470
  this.options = options;
@@ -474,14 +476,18 @@ class Cache {
474
476
  const persists = this.options.persistentStorage;
475
477
  const table = await persists.storage.createTable({ name: persists.key, key: this.key });
476
478
  const rows = await table.getAll();
477
- Object.assign(this.store, rows.reduce((acc, row) => ({ ...acc, [this.getKey(row)]: row }), {}));
479
+ for (const row of rows) this.store.set(this.getKey(row), row);
478
480
  this._loading();
479
481
  })();
480
482
  } else if (((_d = (_c = this.options.persistentStorage) == null ? void 0 : _c.storage) == null ? void 0 : _d.getItem) != void 0) {
481
- const stored = this.options.persistentStorage.storage.getItem(this.options.persistentStorage.key);
482
- if (stored != null) try {
483
- Object.assign(this.store, JSON.parse(stored));
484
- } catch {
483
+ const { storage, key: key2 } = this.options.persistentStorage;
484
+ const stored = storage.getItem(key2);
485
+ if (stored != null) {
486
+ try {
487
+ const obj = JSON.parse(stored);
488
+ for (const k of Object.keys(obj)) this.store.set(k, obj[k]);
489
+ } catch {
490
+ }
485
491
  }
486
492
  this._loading();
487
493
  }
@@ -511,26 +517,50 @@ class Cache {
511
517
  if (!!(persists == null ? void 0 : persists.storage)) {
512
518
  if (((_a = persists.storage) == null ? void 0 : _a.database) != void 0) {
513
519
  persists.storage.createTable({ name: persists.key, key: this.key }).then((table) => {
514
- if (key) {
515
- const value = this.get(key);
520
+ if (key !== void 0) {
521
+ const value = this.get(key, true);
516
522
  if (value != null) table.set(value, key);
517
523
  else table.delete(key);
518
524
  } else {
519
525
  table.clear();
520
- this.all().forEach((row) => table.add(row));
526
+ this.all(true).forEach((row) => table.add(row));
521
527
  }
522
528
  });
523
529
  } else if (((_b = persists.storage) == null ? void 0 : _b.setItem) != void 0) {
524
- persists.storage.setItem(persists.storage.key, JSONSanitize(this.all(true)));
530
+ const obj = {};
531
+ for (const [k, v] of this.store.entries()) obj[k] = v;
532
+ persists.storage.setItem(persists.key, JSONSanitize(obj));
525
533
  }
526
534
  }
527
535
  }
536
+ clearTimer(key) {
537
+ const t = this.timers.get(key);
538
+ if (t) {
539
+ clearTimeout(t);
540
+ this.timers.delete(key);
541
+ }
542
+ }
543
+ touchLRU(key) {
544
+ if (!this.options.sizeLimit || this.options.sizeLimit <= 0) return;
545
+ const idx = this.lruOrder.indexOf(key);
546
+ if (idx >= 0) this.lruOrder.splice(idx, 1);
547
+ this.lruOrder.push(key);
548
+ while (this.lruOrder.length > (this.options.sizeLimit || 0)) {
549
+ const lru = this.lruOrder.shift();
550
+ if (lru !== void 0) this.delete(lru);
551
+ }
552
+ }
528
553
  /**
529
554
  * Get all cached items
530
555
  * @return {T[]} Array of items
531
556
  */
532
557
  all(expired) {
533
- return deepCopy(Object.values(this.store).filter((v) => expired || !v._expired));
558
+ const out = [];
559
+ for (const v of this.store.values()) {
560
+ const val = v;
561
+ if (expired || !(val == null ? void 0 : val._expired)) out.push(deepCopy(val));
562
+ }
563
+ return out;
534
564
  }
535
565
  /**
536
566
  * Add a new item to the cache. Like set, but finds key automatically
@@ -560,7 +590,10 @@ class Cache {
560
590
  */
561
591
  clear() {
562
592
  this.complete = false;
563
- this.store = {};
593
+ for (const [k, t] of this.timers) clearTimeout(t);
594
+ this.timers.clear();
595
+ this.lruOrder = [];
596
+ this.store.clear();
564
597
  this.save();
565
598
  return this;
566
599
  }
@@ -569,7 +602,10 @@ class Cache {
569
602
  * @param {K} key Item's primary key
570
603
  */
571
604
  delete(key) {
572
- delete this.store[key];
605
+ this.clearTimer(key);
606
+ const idx = this.lruOrder.indexOf(key);
607
+ if (idx >= 0) this.lruOrder.splice(idx, 1);
608
+ this.store.delete(key);
573
609
  this.save(key);
574
610
  return this;
575
611
  }
@@ -578,7 +614,12 @@ class Cache {
578
614
  * @return {[K, T][]} Key-value pairs array
579
615
  */
580
616
  entries(expired) {
581
- return deepCopy(Object.entries(this.store).filter((v) => expired || !(v == null ? void 0 : v._expired)));
617
+ const out = [];
618
+ for (const [k, v] of this.store.entries()) {
619
+ const val = v;
620
+ if (expired || !(val == null ? void 0 : val._expired)) out.push([k, deepCopy(val)]);
621
+ }
622
+ return out;
582
623
  }
583
624
  /**
584
625
  * Manually expire a cached item
@@ -587,8 +628,12 @@ class Cache {
587
628
  expire(key) {
588
629
  this.complete = false;
589
630
  if (this.options.expiryPolicy == "keep") {
590
- this.store[key]._expired = true;
591
- this.save(key);
631
+ const v = this.store.get(key);
632
+ if (v) {
633
+ v._expired = true;
634
+ this.store.set(key, v);
635
+ this.save(key);
636
+ }
592
637
  } else this.delete(key);
593
638
  return this;
594
639
  }
@@ -599,7 +644,11 @@ class Cache {
599
644
  * @returns {T | undefined} Cached item or undefined if nothing matched
600
645
  */
601
646
  find(filter, expired) {
602
- return Object.values(this.store).find((row) => (expired || !row._expired) && includes(row, filter));
647
+ for (const v of this.store.values()) {
648
+ const row = v;
649
+ if ((expired || !row._expired) && includes(row, filter)) return deepCopy(row);
650
+ }
651
+ return void 0;
603
652
  }
604
653
  /**
605
654
  * Get item from the cache
@@ -608,7 +657,10 @@ class Cache {
608
657
  * @return {T} Cached item
609
658
  */
610
659
  get(key, expired) {
611
- const cached = deepCopy(this.store[key] ?? null);
660
+ const raw = this.store.get(key);
661
+ if (raw == null) return null;
662
+ const cached = deepCopy(raw);
663
+ this.touchLRU(key);
612
664
  if (expired || !(cached == null ? void 0 : cached._expired)) return cached;
613
665
  return null;
614
666
  }
@@ -617,17 +669,23 @@ class Cache {
617
669
  * @return {K[]} Array of keys
618
670
  */
619
671
  keys(expired) {
620
- return Object.keys(this.store).filter((k) => expired || !this.store[k]._expired);
672
+ const out = [];
673
+ for (const [k, v] of this.store.entries()) {
674
+ const val = v;
675
+ if (expired || !(val == null ? void 0 : val._expired)) out.push(k);
676
+ }
677
+ return out;
621
678
  }
622
679
  /**
623
680
  * Get map of cached items
624
681
  * @return {Record<K, T>}
625
682
  */
626
683
  map(expired) {
627
- const copy = deepCopy(this.store);
628
- if (!expired) Object.keys(copy).forEach((k) => {
629
- if (copy[k]._expired) delete copy[k];
630
- });
684
+ const copy = {};
685
+ for (const [k, v] of this.store.entries()) {
686
+ const val = v;
687
+ if (expired || !(val == null ? void 0 : val._expired)) copy[k] = deepCopy(val);
688
+ }
631
689
  return copy;
632
690
  }
633
691
  /**
@@ -639,12 +697,17 @@ class Cache {
639
697
  */
640
698
  set(key, value, ttl = this.options.ttl) {
641
699
  if (this.options.expiryPolicy == "keep") delete value._expired;
642
- this.store[key] = value;
700
+ this.clearTimer(key);
701
+ this.store.set(key, value);
702
+ this.touchLRU(key);
643
703
  this.save(key);
644
- if (ttl) setTimeout(() => {
645
- this.expire(key);
646
- this.save(key);
647
- }, (ttl || 0) * 1e3);
704
+ if (ttl) {
705
+ const t = setTimeout(() => {
706
+ this.expire(key);
707
+ this.save(key);
708
+ }, (ttl || 0) * 1e3);
709
+ this.timers.set(key, t);
710
+ }
648
711
  return this;
649
712
  }
650
713
  }
@@ -1817,6 +1880,28 @@ function compareVersions(target, vs) {
1817
1880
  const [vMajor, vMinor, vPatch] = vs.split(".").map((v) => +v.replace(/[^0-9]/g, ""));
1818
1881
  return tMajor > vMajor || tMinor > vMinor || tPatch > vPatch ? 1 : tMajor < vMajor || tMinor < vMinor || tPatch < vPatch ? -1 : 0;
1819
1882
  }
1883
+ function consoleInterceptor(out = console, map) {
1884
+ const stderr = [], stdout = [];
1885
+ const cWrapper = (type) => (...args) => {
1886
+ if (out) out[type](...args);
1887
+ if (type == "error") stderr.push(...args);
1888
+ else stdout.push(...args);
1889
+ };
1890
+ return {
1891
+ debug: (map == null ? void 0 : map.debug) != "none" ? cWrapper((map == null ? void 0 : map.debug) || "debug") : () => {
1892
+ },
1893
+ log: (map == null ? void 0 : map.log) != "none" ? cWrapper((map == null ? void 0 : map.log) || "log") : () => {
1894
+ },
1895
+ info: (map == null ? void 0 : map.info) != "none" ? cWrapper((map == null ? void 0 : map.info) || "info") : () => {
1896
+ },
1897
+ warn: (map == null ? void 0 : map.warn) != "none" ? cWrapper((map == null ? void 0 : map.warn) || "warn") : () => {
1898
+ },
1899
+ error: (map == null ? void 0 : map.error) != "none" ? cWrapper((map == null ? void 0 : map.error) || "error") : () => {
1900
+ },
1901
+ stderr,
1902
+ stdout
1903
+ };
1904
+ }
1820
1905
  function escapeRegex(value) {
1821
1906
  return value.replace(/[.*+?^${}()|\[\]\\]/g, "\\$&");
1822
1907
  }
@@ -2403,6 +2488,7 @@ export {
2403
2488
  caseInsensitiveSort,
2404
2489
  clean,
2405
2490
  compareVersions,
2491
+ consoleInterceptor,
2406
2492
  contrast,
2407
2493
  createJwt,
2408
2494
  dayOfWeek,