Added cache find function
All checks were successful
Build / Build NPM Project (push) Successful in 41s
Build / Tag Version (push) Successful in 8s
Build / Publish Documentation (push) Successful in 35s

This commit is contained in:
Zakary Timson 2025-06-19 19:46:59 -04:00
parent 4ed23e1502
commit 11cfc67650
2 changed files with 12 additions and 2 deletions

View File

@ -1,6 +1,6 @@
{
"name": "@ztimson/utils",
"version": "0.25.12",
"version": "0.25.13",
"description": "Utility library",
"author": "Zak Timson",
"license": "MIT",

View File

@ -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<K extends string | number | symbol, T> {
return this;
}
/**
* Find the first cached item to match a filter
* @param {Partial<T>} 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<T>, expired?: boolean): T | undefined {
return <T>Object.values(this.store).find((row: any) => (expired || !row._expired) && includes(row, filter));
}
/**
* Get item from the cache
* @param {K} key Key to lookup