From 11cfc67650d572aa596877be0d3be38874c21380 Mon Sep 17 00:00:00 2001 From: ztimson Date: Thu, 19 Jun 2025 19:46:59 -0400 Subject: [PATCH] Added cache find function --- package.json | 2 +- src/cache.ts | 12 +++++++++++- 2 files changed, 12 insertions(+), 2 deletions(-) diff --git a/package.json b/package.json index c5122c9..160dc7b 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@ztimson/utils", - "version": "0.25.12", + "version": "0.25.13", "description": "Utility library", "author": "Zak Timson", "license": "MIT", diff --git a/src/cache.ts b/src/cache.ts index 56ff35b..00c48ed 100644 --- a/src/cache.ts +++ b/src/cache.ts @@ -1,5 +1,5 @@ import {Table} from './database.ts'; -import {deepCopy, JSONSanitize} from './objects.ts'; +import {deepCopy, includes, JSONSanitize} from './objects.ts'; export type CacheOptions = { /** Delete keys automatically after x amount of seconds */ @@ -143,6 +143,16 @@ export class Cache { return this; } + /** + * Find the first cached item to match a filter + * @param {Partial} filter Partial item to match + * @param {Boolean} expired Include expired items, defaults to false + * @returns {T | undefined} Cached item or undefined if nothing matched + */ + find(filter: Partial, expired?: boolean): T | undefined { + return Object.values(this.store).find((row: any) => (expired || !row._expired) && includes(row, filter)); + } + /** * Get item from the cache * @param {K} key Key to lookup