@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 +7 -0
- package/dist/index.cjs +18 -1
- package/dist/index.cjs.map +1 -1
- package/dist/index.mjs +18 -1
- package/dist/index.mjs.map +1 -1
- package/package.json +1 -1
package/dist/index.mjs
CHANGED
|
@@ -543,7 +543,14 @@ class Cache {
|
|
|
543
543
|
if (options.storage instanceof Table) {
|
|
544
544
|
(async () => {
|
|
545
545
|
var _a2;
|
|
546
|
-
return (await ((_a2 = options.storage) == null ? void 0 : _a2.getAll())).forEach((v) =>
|
|
546
|
+
return (await ((_a2 = options.storage) == null ? void 0 : _a2.getAll())).forEach((v) => {
|
|
547
|
+
if (v) {
|
|
548
|
+
try {
|
|
549
|
+
this.add(v);
|
|
550
|
+
} catch {
|
|
551
|
+
}
|
|
552
|
+
}
|
|
553
|
+
});
|
|
547
554
|
})();
|
|
548
555
|
} else if (options.storageKey) {
|
|
549
556
|
const stored = (_a = options.storage) == null ? void 0 : _a.getItem(options.storageKey);
|
|
@@ -567,6 +574,7 @@ class Cache {
|
|
|
567
574
|
}
|
|
568
575
|
getKey(value) {
|
|
569
576
|
if (!this.key) throw new Error("No key defined");
|
|
577
|
+
if (value[this.key] === void 0) throw new Error(`${this.key.toString()} Doesn't exist on ${JSON.stringify(value, null, 2)}`);
|
|
570
578
|
return value[this.key];
|
|
571
579
|
}
|
|
572
580
|
save(key) {
|
|
@@ -644,6 +652,15 @@ class Cache {
|
|
|
644
652
|
} else this.delete(key);
|
|
645
653
|
return this;
|
|
646
654
|
}
|
|
655
|
+
/**
|
|
656
|
+
* Find the first cached item to match a filter
|
|
657
|
+
* @param {Partial<T>} filter Partial item to match
|
|
658
|
+
* @param {Boolean} expired Include expired items, defaults to false
|
|
659
|
+
* @returns {T | undefined} Cached item or undefined if nothing matched
|
|
660
|
+
*/
|
|
661
|
+
find(filter, expired) {
|
|
662
|
+
return Object.values(this.store).find((row) => (expired || !row._expired) && includes(row, filter));
|
|
663
|
+
}
|
|
647
664
|
/**
|
|
648
665
|
* Get item from the cache
|
|
649
666
|
* @param {K} key Key to lookup
|