diff --git a/README.md b/README.md index 325b590..7890485 100644 --- a/README.md +++ b/README.md @@ -45,14 +45,14 @@ deno add npm:lru.min import { createLRU } from 'lru.min'; const max = 2; -const stale = 300000; +const staleAt = 300000; const onEviction = (key, value) => { console.log(`Key "${key}" with value "${value}" has been evicted.`); }; const LRU = createLRU({ max, - stale, + staleAt, onEviction, }); diff --git a/src/index.ts b/src/index.ts index abeb78b..79af7ba 100644 --- a/src/index.ts +++ b/src/index.ts @@ -6,9 +6,9 @@ export type CacheOptions = { * * @default undefined */ - stale?: number; + staleAt?: number; /** - * When `true` and `stale` is set, items remain stale based on their initial expiration. + * When `true` and `staleAt` is set, items remain staleAt based on their initial expiration. * * @default undefined */ @@ -22,16 +22,16 @@ export type CacheOptions = { }; export const createLRU = (options: CacheOptions) => { - let { max, onEviction, stale, keepStale } = options; + let { max, onEviction, staleAt, keepStale } = options; if (!(Number.isInteger(max) && max > 0)) throw new TypeError('`max` must be a positive integer'); if ( - (typeof stale !== 'undefined' && typeof stale !== 'number') || - (typeof stale === 'number' && stale <= 0) + (typeof staleAt !== 'undefined' && typeof staleAt !== 'number') || + (typeof staleAt === 'number' && staleAt <= 0) ) - throw new TypeError('`stale` must be a positive number'); + throw new TypeError('`staleAt` must be a positive number'); const Age = (() => { try { @@ -169,7 +169,7 @@ export const createLRU = (options: CacheOptions) => { return { /** Adds a key-value pair to the cache. Updates the value if the key already exists. */ - set(key: Key, value: Value, options?: { stale?: number }): undefined { + set(key: Key, value: Value, options?: { staleAt?: number }): undefined { if (key === undefined) return; let index = keyMap.get(key); @@ -183,14 +183,15 @@ export const createLRU = (options: CacheOptions) => { valList[index] = value; - const keyMaxAge = options?.stale !== undefined ? options.stale : stale; + const keyMaxAge = + options?.staleAt !== undefined ? options.staleAt : staleAt; if (keyMaxAge !== undefined) { if ( - (typeof stale !== 'undefined' && typeof stale !== 'number') || - (typeof stale === 'number' && stale <= 0) + (typeof staleAt !== 'undefined' && typeof staleAt !== 'number') || + (typeof staleAt === 'number' && staleAt <= 0) ) - throw new TypeError('`stale` must be a positive number'); + throw new TypeError('`staleAt` must be a positive number'); expList[index] = Age.now() + keyMaxAge; ageList[index] = keyMaxAge;