@ztimson/utils 0.25.12 → 0.25.14

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
@@ -69,6 +69,13 @@ export declare class Cache<K extends string | number | symbol, T> {
69
69
  * @param {K} key Key to expire
70
70
  */
71
71
  expire(key: K): this;
72
+ /**
73
+ * Find the first cached item to match a filter
74
+ * @param {Partial<T>} filter Partial item to match
75
+ * @param {Boolean} expired Include expired items, defaults to false
76
+ * @returns {T | undefined} Cached item or undefined if nothing matched
77
+ */
78
+ find(filter: Partial<T>, expired?: boolean): T | undefined;
72
79
  /**
73
80
  * Get item from the cache
74
81
  * @param {K} key Key to lookup
package/dist/index.cjs CHANGED
@@ -547,7 +547,14 @@ ${opts.message || this.desc}`;
547
547
  if (options.storage instanceof Table) {
548
548
  (async () => {
549
549
  var _a2;
550
- return (await ((_a2 = options.storage) == null ? void 0 : _a2.getAll())).forEach((v) => this.add(v));
550
+ return (await ((_a2 = options.storage) == null ? void 0 : _a2.getAll())).forEach((v) => {
551
+ if (v) {
552
+ try {
553
+ this.add(v);
554
+ } catch {
555
+ }
556
+ }
557
+ });
551
558
  })();
552
559
  } else if (options.storageKey) {
553
560
  const stored = (_a = options.storage) == null ? void 0 : _a.getItem(options.storageKey);
@@ -571,6 +578,7 @@ ${opts.message || this.desc}`;
571
578
  }
572
579
  getKey(value) {
573
580
  if (!this.key) throw new Error("No key defined");
581
+ if (value[this.key] === void 0) throw new Error(`${this.key.toString()} Doesn't exist on ${JSON.stringify(value, null, 2)}`);
574
582
  return value[this.key];
575
583
  }
576
584
  save(key) {
@@ -648,6 +656,15 @@ ${opts.message || this.desc}`;
648
656
  } else this.delete(key);
649
657
  return this;
650
658
  }
659
+ /**
660
+ * Find the first cached item to match a filter
661
+ * @param {Partial<T>} filter Partial item to match
662
+ * @param {Boolean} expired Include expired items, defaults to false
663
+ * @returns {T | undefined} Cached item or undefined if nothing matched
664
+ */
665
+ find(filter, expired) {
666
+ return Object.values(this.store).find((row) => (expired || !row._expired) && includes(row, filter));
667
+ }
651
668
  /**
652
669
  * Get item from the cache
653
670
  * @param {K} key Key to lookup