From 67666e3745b8158647c11b4c037945cce6d0dc20 Mon Sep 17 00:00:00 2001 From: Jason Date: Wed, 25 Nov 2026 15:20:05 -0500 Subject: [PATCH 1/2] refactor: deduplicate copy-paste blocks in src/lru.js Extract shared private helpers #insertItem() and #forEachLive() to consolidate the byte-identical insertion block in set()/setWithEvicted() and the near-duplicated traversal-and-skip-expired pattern in entries()/values()/keys()/toJSON(). No behavior change; 100% line coverage and O(1) guarantees preserved. --- coverage.txt | 4 +- dist/tiny-lru.cjs | 89 ++++++++++++++++++++++------------------ dist/tiny-lru.js | 89 ++++++++++++++++++++++------------------ dist/tiny-lru.min.js | 2 +- dist/tiny-lru.min.js.map | 2 +- src/lru.js | 89 ++++++++++++++++++++++------------------ 6 files changed, 151 insertions(+), 124 deletions(-) diff --git a/coverage.txt b/coverage.txt index 8af241a..fd56314 100644 --- a/coverage.txt +++ b/coverage.txt @@ -3,8 +3,8 @@ ℹ file | line % | branch % | funcs % | uncovered lines ℹ ---------------------------------------------------------- ℹ src | | | | -ℹ lru.js | 100.00 | 99.37 | 100.00 | +ℹ lru.js | 100.00 | 99.36 | 100.00 | ℹ ---------------------------------------------------------- -ℹ all files | 100.00 | 99.37 | 100.00 | +ℹ all files | 100.00 | 99.36 | 100.00 | ℹ ---------------------------------------------------------- ℹ end of coverage report diff --git a/dist/tiny-lru.cjs b/dist/tiny-lru.cjs index 1329c72..af8a8e9 100644 --- a/dist/tiny-lru.cjs +++ b/dist/tiny-lru.cjs @@ -105,11 +105,7 @@ class LRU { entries(keys) { if (keys === undefined) { const result = []; - for (let x = this.first; x !== null; x = x.next) { - if (!this.#isExpired(x)) { - result.push([x.key, x.value]); - } - } + this.#forEachLive((x) => result.push([x.key, x.value])); return result; } @@ -298,6 +294,51 @@ class LRU { return item; } + /** + * Creates and links a new item at the most recently used position. + * Wires the first/last pointers and returns the created item. + * + * @param {string} key - The key to store. + * @param {*} value - The value to store. + * @returns {Object} The created and linked item. + * @private + */ + #insertItem(key, value) { + const item = (this.items[key] = { + expiry: this.ttl > 0 ? Date.now() + this.ttl : this.ttl, + key: key, + prev: this.last, + next: null, + value, + }); + + if (++this.size === 1) { + this.first = item; + } else { + this.last.next = item; + } + + this.last = item; + + return item; + } + + /** + * Invokes a callback for each live (non-expired) item in LRU order. + * Accesses items directly without calling get(), so LRU order is not + * mutated and expired items are not deleted mid-iteration. + * + * @param {function(Object): void} callback - Function to call for each live item. + * @private + */ + #forEachLive(callback) { + for (let x = this.first; x !== null; x = x.next) { + if (!this.#isExpired(x)) { + callback(x); + } + } + } + /** * Efficiently moves an item to the end of the LRU list (most recently used position). * This is an internal optimization method that avoids the overhead of the full set() operation @@ -327,11 +368,7 @@ class LRU { */ keys() { const result = []; - for (let x = this.first; x !== null; x = x.next) { - if (!this.#isExpired(x)) { - result.push(x.key); - } - } + this.#forEachLive((x) => result.push(x.key)); return result; } @@ -368,21 +405,7 @@ class LRU { }; } - item = this.items[key] = { - expiry: this.ttl > 0 ? Date.now() + this.ttl : this.ttl, - key: key, - prev: this.last, - next: null, - value, - }; - - if (++this.size === 1) { - this.first = item; - } else { - this.last.next = item; - } - - this.last = item; + item = this.#insertItem(key, value); } this.#stats.sets++; @@ -416,21 +439,7 @@ class LRU { this.evict(); } - item = this.items[key] = { - expiry: this.ttl > 0 ? Date.now() + this.ttl : this.ttl, - key: key, - prev: this.last, - next: null, - value, - }; - - if (++this.size === 1) { - this.first = item; - } else { - this.last.next = item; - } - - this.last = item; + item = this.#insertItem(key, value); } this.#stats.sets++; diff --git a/dist/tiny-lru.js b/dist/tiny-lru.js index 69c9922..a4005bd 100644 --- a/dist/tiny-lru.js +++ b/dist/tiny-lru.js @@ -103,11 +103,7 @@ class LRU { entries(keys) { if (keys === undefined) { const result = []; - for (let x = this.first; x !== null; x = x.next) { - if (!this.#isExpired(x)) { - result.push([x.key, x.value]); - } - } + this.#forEachLive((x) => result.push([x.key, x.value])); return result; } @@ -296,6 +292,51 @@ class LRU { return item; } + /** + * Creates and links a new item at the most recently used position. + * Wires the first/last pointers and returns the created item. + * + * @param {string} key - The key to store. + * @param {*} value - The value to store. + * @returns {Object} The created and linked item. + * @private + */ + #insertItem(key, value) { + const item = (this.items[key] = { + expiry: this.ttl > 0 ? Date.now() + this.ttl : this.ttl, + key: key, + prev: this.last, + next: null, + value, + }); + + if (++this.size === 1) { + this.first = item; + } else { + this.last.next = item; + } + + this.last = item; + + return item; + } + + /** + * Invokes a callback for each live (non-expired) item in LRU order. + * Accesses items directly without calling get(), so LRU order is not + * mutated and expired items are not deleted mid-iteration. + * + * @param {function(Object): void} callback - Function to call for each live item. + * @private + */ + #forEachLive(callback) { + for (let x = this.first; x !== null; x = x.next) { + if (!this.#isExpired(x)) { + callback(x); + } + } + } + /** * Efficiently moves an item to the end of the LRU list (most recently used position). * This is an internal optimization method that avoids the overhead of the full set() operation @@ -325,11 +366,7 @@ class LRU { */ keys() { const result = []; - for (let x = this.first; x !== null; x = x.next) { - if (!this.#isExpired(x)) { - result.push(x.key); - } - } + this.#forEachLive((x) => result.push(x.key)); return result; } @@ -366,21 +403,7 @@ class LRU { }; } - item = this.items[key] = { - expiry: this.ttl > 0 ? Date.now() + this.ttl : this.ttl, - key: key, - prev: this.last, - next: null, - value, - }; - - if (++this.size === 1) { - this.first = item; - } else { - this.last.next = item; - } - - this.last = item; + item = this.#insertItem(key, value); } this.#stats.sets++; @@ -414,21 +437,7 @@ class LRU { this.evict(); } - item = this.items[key] = { - expiry: this.ttl > 0 ? Date.now() + this.ttl : this.ttl, - key: key, - prev: this.last, - next: null, - value, - }; - - if (++this.size === 1) { - this.first = item; - } else { - this.last.next = item; - } - - this.last = item; + item = this.#insertItem(key, value); } this.#stats.sets++; diff --git a/dist/tiny-lru.min.js b/dist/tiny-lru.min.js index 948e563..cd59d4e 100644 --- a/dist/tiny-lru.min.js +++ b/dist/tiny-lru.min.js @@ -2,4 +2,4 @@ 2026 Jason Mulligan @version 13.1.1 */ -class t{#t;#e;constructor(t=0,e=0,s=!1){if(!Number.isInteger(t)||t<0)throw new TypeError("Invalid max value");if(!Number.isInteger(e)||e<0)throw new TypeError("Invalid ttl value");if("boolean"!=typeof s)throw new TypeError("Invalid resetTTL value");this.first=null,this.items=Object.create(null),this.last=null,this.max=t,this.resetTTL=s,this.size=0,this.ttl=e,this.#t={hits:0,misses:0,sets:0,deletes:0,evictions:0},this.#e=null}clear(){for(let t=this.first;null!==t;){const e=t.next;t.prev=null,t.next=null,t=e}return this.first=null,this.items=Object.create(null),this.last=null,this.size=0,this.#t.hits=0,this.#t.misses=0,this.#t.sets=0,this.#t.deletes=0,this.#t.evictions=0,this}delete(t){const e=this.items[t];return void 0!==e&&(this.#s(e),this.#t.deletes++),this}entries(t){if(void 0===t){const t=[];for(let e=this.first;null!==e;e=e.next)this.#i(e)||t.push([e.key,e.value]);return t}if(!Array.isArray(t))throw new TypeError("keys must be an array");const e=Array.from({length:t.length});for(let s=0;s0&&this.size===this.max){const t=this.#r();s={key:t.key,value:t.value,expiry:t.expiry}}i=this.items[t]={expiry:this.ttl>0?Date.now()+this.ttl:this.ttl,key:t,prev:this.last,next:null,value:e},1==++this.size?this.first=i:this.last.next=i,this.last=i}else i.value=e,this.resetTTL&&(i.expiry=this.ttl>0?Date.now()+this.ttl:this.ttl),this.moveToEnd(i);return this.#t.sets++,s}set(t,e){let s=this.items[t];return void 0===s||this.#i(s)?(void 0!==s&&this.#s(s),this.max>0&&this.size===this.max&&this.evict(),s=this.items[t]={expiry:this.ttl>0?Date.now()+this.ttl:this.ttl,key:t,prev:this.last,next:null,value:e},1==++this.size?this.first=s:this.last.next=s,this.last=s):(s.value=e,this.resetTTL&&(s.expiry=this.ttl>0?Date.now()+this.ttl:this.ttl),this.moveToEnd(s)),this.#t.sets++,this}values(t){if(void 0===t){const t=[];for(let e=this.first;null!==e;e=e.next)this.#i(e)||t.push(e.value);return t}if(!Array.isArray(t))throw new TypeError("keys must be an array");const e=Array.from({length:t.length});for(let s=0;st?e++:s++;return{valid:e,expired:s,noTTL:0}}keysByTTL(){if(0===this.ttl)return{valid:this.keys(),expired:[],noTTL:this.keys()};const t=Date.now(),e=[],s=[];for(let i=this.first;null!==i;i=i.next)i.expiry>t?e.push(i.key):s.push(i.key);return{valid:e,expired:s,noTTL:[]}}valuesByTTL(){if(0===this.ttl)return{valid:this.values(),expired:[],noTTL:this.values()};const t=Date.now(),e=[],s=[];for(let i=this.first;null!==i;i=i.next)i.expiry>t?e.push(i.value):s.push(i.value);return{valid:e,expired:s,noTTL:[]}}}function e(e=1e3,s=0,i=!1){return new t(e,s,i)}export{t as LRU,e as lru};//# sourceMappingURL=tiny-lru.min.js.map +class t{#t;#e;constructor(t=0,e=0,s=!1){if(!Number.isInteger(t)||t<0)throw new TypeError("Invalid max value");if(!Number.isInteger(e)||e<0)throw new TypeError("Invalid ttl value");if("boolean"!=typeof s)throw new TypeError("Invalid resetTTL value");this.first=null,this.items=Object.create(null),this.last=null,this.max=t,this.resetTTL=s,this.size=0,this.ttl=e,this.#t={hits:0,misses:0,sets:0,deletes:0,evictions:0},this.#e=null}clear(){for(let t=this.first;null!==t;){const e=t.next;t.prev=null,t.next=null,t=e}return this.first=null,this.items=Object.create(null),this.last=null,this.size=0,this.#t.hits=0,this.#t.misses=0,this.#t.sets=0,this.#t.deletes=0,this.#t.evictions=0,this}delete(t){const e=this.items[t];return void 0!==e&&(this.#s(e),this.#t.deletes++),this}entries(t){if(void 0===t){const t=[];return this.#i(e=>t.push([e.key,e.value])),t}if(!Array.isArray(t))throw new TypeError("keys must be an array");const e=Array.from({length:t.length});for(let s=0;s0?Date.now()+this.ttl:this.ttl,key:t,prev:this.last,next:null,value:e};return 1===++this.size?this.first=s:this.last.next=s,this.last=s,s}#i(t){for(let e=this.first;null!==e;e=e.next)this.#r(e)||t(e)}moveToEnd(t){this.last!==t&&(this.#h(t),t.prev=this.last,t.next=null,this.last.next=t,this.last=t)}keys(){const t=[];return this.#i(e=>t.push(e.key)),t}setWithEvicted(t,e){let s=null,i=this.items[t];if(void 0===i||this.#r(i)){if(void 0!==i&&this.#s(i),this.max>0&&this.size===this.max){const t=this.#n();s={key:t.key,value:t.value,expiry:t.expiry}}i=this.#l(t,e)}else i.value=e,this.resetTTL&&(i.expiry=this.ttl>0?Date.now()+this.ttl:this.ttl),this.moveToEnd(i);return this.#t.sets++,s}set(t,e){let s=this.items[t];return void 0===s||this.#r(s)?(void 0!==s&&this.#s(s),this.max>0&&this.size===this.max&&this.evict(),s=this.#l(t,e)):(s.value=e,this.resetTTL&&(s.expiry=this.ttl>0?Date.now()+this.ttl:this.ttl),this.moveToEnd(s)),this.#t.sets++,this}values(t){if(void 0===t){const t=[];for(let e=this.first;null!==e;e=e.next)this.#r(e)||t.push(e.value);return t}if(!Array.isArray(t))throw new TypeError("keys must be an array");const e=Array.from({length:t.length});for(let s=0;st?e++:s++;return{valid:e,expired:s,noTTL:0}}keysByTTL(){if(0===this.ttl)return{valid:this.keys(),expired:[],noTTL:this.keys()};const t=Date.now(),e=[],s=[];for(let i=this.first;null!==i;i=i.next)i.expiry>t?e.push(i.key):s.push(i.key);return{valid:e,expired:s,noTTL:[]}}valuesByTTL(){if(0===this.ttl)return{valid:this.values(),expired:[],noTTL:this.values()};const t=Date.now(),e=[],s=[];for(let i=this.first;null!==i;i=i.next)i.expiry>t?e.push(i.value):s.push(i.value);return{valid:e,expired:s,noTTL:[]}}}function e(e=1e3,s=0,i=!1){return new t(e,s,i)}export{t as LRU,e as lru};//# sourceMappingURL=tiny-lru.min.js.map diff --git a/dist/tiny-lru.min.js.map b/dist/tiny-lru.min.js.map index 067cb95..d955cb7 100644 --- a/dist/tiny-lru.min.js.map +++ b/dist/tiny-lru.min.js.map @@ -1 +1 @@ -{"version":3,"file":"tiny-lru.min.js","sources":["../src/lru.js"],"sourcesContent":["/**\n * A high-performance Least Recently Used (LRU) cache implementation with optional TTL support.\n * Items are automatically evicted when the cache reaches its maximum size,\n * removing the least recently used items first. All core operations (get, set, delete) are O(1).\n *\n * @class LRU\n */\nexport class LRU {\n\t#stats;\n\t#onEvict;\n\n\t/**\n\t * Creates a new LRU cache instance.\n\t *\n\t * @constructor\n\t * @param {number} [max=0] - Maximum number of items to store. 0 means unlimited.\n\t * @param {number} [ttl=0] - Time to live in milliseconds. 0 means no expiration.\n\t * @param {boolean} [resetTTL=false] - Whether to reset TTL when updating existing items via set().\n\t * @throws {TypeError} When parameters are invalid (negative numbers or wrong types).\n\t */\n\tconstructor(max = 0, ttl = 0, resetTTL = false) {\n\t\tif (!Number.isInteger(max) || max < 0) {\n\t\t\tthrow new TypeError(\"Invalid max value\");\n\t\t}\n\n\t\tif (!Number.isInteger(ttl) || ttl < 0) {\n\t\t\tthrow new TypeError(\"Invalid ttl value\");\n\t\t}\n\n\t\tif (typeof resetTTL !== \"boolean\") {\n\t\t\tthrow new TypeError(\"Invalid resetTTL value\");\n\t\t}\n\n\t\tthis.first = null;\n\t\tthis.items = Object.create(null);\n\t\tthis.last = null;\n\t\tthis.max = max;\n\t\tthis.resetTTL = resetTTL;\n\t\tthis.size = 0;\n\t\tthis.ttl = ttl;\n\t\tthis.#stats = { hits: 0, misses: 0, sets: 0, deletes: 0, evictions: 0 };\n\t\tthis.#onEvict = null;\n\t}\n\n\t/**\n\t * Removes all items from the cache.\n\t *\n\t * @returns {LRU} The LRU instance for method chaining.\n\t */\n\tclear() {\n\t\tfor (let x = this.first; x !== null;) {\n\t\t\tconst next = x.next;\n\t\t\tx.prev = null;\n\t\t\tx.next = null;\n\t\t\tx = next;\n\t\t}\n\n\t\tthis.first = null;\n\t\tthis.items = Object.create(null);\n\t\tthis.last = null;\n\t\tthis.size = 0;\n\t\tthis.#stats.hits = 0;\n\t\tthis.#stats.misses = 0;\n\t\tthis.#stats.sets = 0;\n\t\tthis.#stats.deletes = 0;\n\t\tthis.#stats.evictions = 0;\n\n\t\treturn this;\n\t}\n\n\t/**\n\t * Removes an item from the cache by key.\n\t *\n\t * @param {string} key - The key of the item to delete.\n\t * @returns {LRU} The LRU instance for method chaining.\n\t */\n\tdelete(key) {\n\t\tconst item = this.items[key];\n\n\t\tif (item !== undefined) {\n\t\t\tthis.#removeItem(item);\n\t\t\tthis.#stats.deletes++;\n\t\t}\n\n\t\treturn this;\n\t}\n\n\t/**\n\t * Returns an array of [key, value] pairs for the specified keys.\n\t * When no keys provided, returns all entries in LRU order.\n\t * When keys provided, order matches the input array.\n\t *\n\t * @param {string[]} [keys=this.keys()] - Array of keys to get entries for. Defaults to all keys.\n\t * @returns {Array>} Array of [key, value] pairs.\n\t */\n\tentries(keys) {\n\t\tif (keys === undefined) {\n\t\t\tconst result = [];\n\t\t\tfor (let x = this.first; x !== null; x = x.next) {\n\t\t\t\tif (!this.#isExpired(x)) {\n\t\t\t\t\tresult.push([x.key, x.value]);\n\t\t\t\t}\n\t\t\t}\n\n\t\t\treturn result;\n\t\t}\n\n\t\tif (!Array.isArray(keys)) {\n\t\t\tthrow new TypeError(\"keys must be an array\");\n\t\t}\n\n\t\tconst result = Array.from({ length: keys.length });\n\t\tfor (let i = 0; i < keys.length; i++) {\n\t\t\tconst key = keys[i];\n\t\t\tconst item = this.items[key];\n\t\t\tresult[i] = [key, item !== undefined && !this.#isExpired(item) ? item.value : undefined];\n\t\t}\n\n\t\treturn result;\n\t}\n\n\t/**\n\t * Removes the least recently used item from the cache.\n\t *\n\t * @returns {LRU} The LRU instance for method chaining.\n\t */\n\tevict() {\n\t\tif (this.size === 0) {\n\t\t\treturn this;\n\t\t}\n\n\t\tconst item = this.#evictItem();\n\n\t\tif (this.#onEvict !== null) {\n\t\t\tthis.#onEvict({\n\t\t\t\tkey: item.key,\n\t\t\t\tvalue: item.value,\n\t\t\t\texpiry: item.expiry,\n\t\t\t});\n\t\t}\n\n\t\treturn this;\n\t}\n\n\t/**\n\t * Returns the expiration timestamp for a given key.\n\t *\n\t * @param {string} key - The key to check expiration for.\n\t * @returns {number|undefined} The expiration timestamp in milliseconds, or undefined if key doesn't exist.\n\t */\n\texpiresAt(key) {\n\t\tconst item = this.items[key];\n\t\treturn item !== undefined ? item.expiry : undefined;\n\t}\n\n\t/**\n\t * Checks if an item has expired.\n\t *\n\t * @param {Object} item - The cache item to check.\n\t * @returns {boolean} True if the item has expired, false otherwise.\n\t * @private\n\t */\n\t#isExpired(item) {\n\t\tif (this.ttl === 0) {\n\t\t\treturn false;\n\t\t}\n\n\t\treturn item.expiry <= Date.now();\n\t}\n\n\t/**\n\t * Retrieves a value from the cache by key without updating LRU order.\n\t * Note: Does not perform TTL checks or remove expired items.\n\t *\n\t * @param {string} key - The key to retrieve.\n\t * @returns {*} The value associated with the key, or undefined if not found.\n\t */\n\tpeek(key) {\n\t\tconst item = this.items[key];\n\t\treturn item !== undefined ? item.value : undefined;\n\t}\n\n\t/**\n\t * Retrieves a value from the cache by key. Updates the item's position to most recently used.\n\t *\n\t * @param {string} key - The key to retrieve.\n\t * @returns {*} The value associated with the key, or undefined if not found or expired.\n\t */\n\tget(key) {\n\t\tconst item = this.items[key];\n\n\t\tif (item !== undefined) {\n\t\t\tif (!this.#isExpired(item)) {\n\t\t\t\tthis.moveToEnd(item);\n\t\t\t\tthis.#stats.hits++;\n\t\t\t\treturn item.value;\n\t\t\t}\n\n\t\t\tthis.#removeItem(item);\n\t\t\tthis.#stats.misses++;\n\t\t\treturn undefined;\n\t\t}\n\n\t\tthis.#stats.misses++;\n\t\treturn undefined;\n\t}\n\n\t/**\n\t * Checks if a key exists in the cache.\n\t * Expired items are removed before returning false.\n\t *\n\t * @param {string} key - The key to check for.\n\t * @returns {boolean} True if the key exists and is not expired, false otherwise.\n\t */\n\thas(key) {\n\t\tconst item = this.items[key];\n\n\t\tif (item !== undefined && this.#isExpired(item)) {\n\t\t\tthis.#removeItem(item);\n\t\t\treturn false;\n\t\t}\n\n\t\treturn item !== undefined;\n\t}\n\n\t/**\n\t * Unlinks an item from the doubly-linked list.\n\t * Updates first/last pointers if needed.\n\t * Does NOT clear the item's prev/next pointers or delete from items map.\n\t *\n\t * @private\n\t */\n\t#unlink(item) {\n\t\tif (item.prev !== null) {\n\t\t\titem.prev.next = item.next;\n\t\t}\n\n\t\tif (item.next !== null) {\n\t\t\titem.next.prev = item.prev;\n\t\t}\n\n\t\tif (this.first === item) {\n\t\t\tthis.first = item.next;\n\t\t}\n\n\t\tif (this.last === item) {\n\t\t\tthis.last = item.prev;\n\t\t}\n\t}\n\n\t/**\n\t * Removes an item from the cache without incrementing the deletes stat.\n\t * Used internally by get()/has() when removing expired items.\n\t *\n\t * @param {Object} item - The cache item to remove.\n\t * @private\n\t */\n\t#removeItem(item) {\n\t\tdelete this.items[item.key];\n\t\tthis.size--;\n\t\tthis.#unlink(item);\n\t\titem.prev = null;\n\t\titem.next = null;\n\t}\n\n\t/**\n\t * Evicts the least recently used item from the cache without firing onEvict.\n\t * Used internally by setWithEvicted() to avoid double-notification.\n\t *\n\t * @returns {Object} The evicted item.\n\t * @private\n\t */\n\t#evictItem() {\n\t\tconst item = this.first;\n\n\t\tdelete this.items[item.key];\n\t\tthis.#stats.evictions++;\n\n\t\tif (--this.size === 0) {\n\t\t\tthis.first = null;\n\t\t\tthis.last = null;\n\t\t} else {\n\t\t\tthis.#unlink(item);\n\t\t}\n\n\t\titem.prev = null;\n\t\titem.next = null;\n\n\t\treturn item;\n\t}\n\n\t/**\n\t * Efficiently moves an item to the end of the LRU list (most recently used position).\n\t * This is an internal optimization method that avoids the overhead of the full set() operation\n\t * when only LRU position needs to be updated.\n\t *\n\t * @param {Object} item - The cache item with prev/next pointers to reposition.\n\t * @private\n\t */\n\tmoveToEnd(item) {\n\t\tif (this.last === item) {\n\t\t\treturn;\n\t\t}\n\n\t\tthis.#unlink(item);\n\n\t\titem.prev = this.last;\n\t\titem.next = null;\n\t\tthis.last.next = item;\n\t\tthis.last = item;\n\t}\n\n\t/**\n\t * Returns an array of all keys in the cache, ordered from least to most recently used.\n\t * Expired items are skipped, consistent with entries(), values(), and toJSON().\n\t *\n\t * @returns {string[]} Array of keys in LRU order.\n\t */\n\tkeys() {\n\t\tconst result = [];\n\t\tfor (let x = this.first; x !== null; x = x.next) {\n\t\t\tif (!this.#isExpired(x)) {\n\t\t\t\tresult.push(x.key);\n\t\t\t}\n\t\t}\n\n\t\treturn result;\n\t}\n\n\t/**\n\t * Sets a value in the cache and returns any evicted item.\n\t * Eviction is silent — onEvict is not fired for the returned item.\n\t *\n\t * @param {string} key - The key to set.\n\t * @param {*} value - The value to store.\n\t * @returns {Object|null} The evicted item (if any) with shape {key, value, expiry}, or null.\n\t */\n\tsetWithEvicted(key, value) {\n\t\tlet evicted = null;\n\t\tlet item = this.items[key];\n\n\t\tif (item !== undefined && !this.#isExpired(item)) {\n\t\t\titem.value = value;\n\t\t\tif (this.resetTTL) {\n\t\t\t\titem.expiry = this.ttl > 0 ? Date.now() + this.ttl : this.ttl;\n\t\t\t}\n\t\t\tthis.moveToEnd(item);\n\t\t} else {\n\t\t\tif (item !== undefined) {\n\t\t\t\tthis.#removeItem(item);\n\t\t\t}\n\n\t\t\tif (this.max > 0 && this.size === this.max) {\n\t\t\t\tconst evictedItem = this.#evictItem();\n\t\t\t\tevicted = {\n\t\t\t\t\tkey: evictedItem.key,\n\t\t\t\t\tvalue: evictedItem.value,\n\t\t\t\t\texpiry: evictedItem.expiry,\n\t\t\t\t};\n\t\t\t}\n\n\t\t\titem = this.items[key] = {\n\t\t\t\texpiry: this.ttl > 0 ? Date.now() + this.ttl : this.ttl,\n\t\t\t\tkey: key,\n\t\t\t\tprev: this.last,\n\t\t\t\tnext: null,\n\t\t\t\tvalue,\n\t\t\t};\n\n\t\t\tif (++this.size === 1) {\n\t\t\t\tthis.first = item;\n\t\t\t} else {\n\t\t\t\tthis.last.next = item;\n\t\t\t}\n\n\t\t\tthis.last = item;\n\t\t}\n\n\t\tthis.#stats.sets++;\n\t\treturn evicted;\n\t}\n\n\t/**\n\t * Sets a value in the cache. Updates the item's position to most recently used.\n\t *\n\t * @param {string} key - The key to set.\n\t * @param {*} value - The value to store.\n\t * @returns {LRU} The LRU instance for method chaining.\n\t */\n\tset(key, value) {\n\t\tlet item = this.items[key];\n\n\t\tif (item !== undefined && !this.#isExpired(item)) {\n\t\t\titem.value = value;\n\n\t\t\tif (this.resetTTL) {\n\t\t\t\titem.expiry = this.ttl > 0 ? Date.now() + this.ttl : this.ttl;\n\t\t\t}\n\n\t\t\tthis.moveToEnd(item);\n\t\t} else {\n\t\t\tif (item !== undefined) {\n\t\t\t\tthis.#removeItem(item);\n\t\t\t}\n\n\t\t\tif (this.max > 0 && this.size === this.max) {\n\t\t\t\tthis.evict();\n\t\t\t}\n\n\t\t\titem = this.items[key] = {\n\t\t\t\texpiry: this.ttl > 0 ? Date.now() + this.ttl : this.ttl,\n\t\t\t\tkey: key,\n\t\t\t\tprev: this.last,\n\t\t\t\tnext: null,\n\t\t\t\tvalue,\n\t\t\t};\n\n\t\t\tif (++this.size === 1) {\n\t\t\t\tthis.first = item;\n\t\t\t} else {\n\t\t\t\tthis.last.next = item;\n\t\t\t}\n\n\t\t\tthis.last = item;\n\t\t}\n\n\t\tthis.#stats.sets++;\n\n\t\treturn this;\n\t}\n\n\t/**\n\t * Returns an array of all values in the cache for the specified keys.\n\t * When no keys provided, returns all values in LRU order.\n\t * When keys provided, order matches the input array.\n\t *\n\t * @param {string[]} [keys] - Array of keys to get values for. Defaults to all keys.\n\t * @returns {Array<*>} Array of values corresponding to the keys.\n\t */\n\tvalues(keys) {\n\t\tif (keys === undefined) {\n\t\t\tconst result = [];\n\t\t\tfor (let x = this.first; x !== null; x = x.next) {\n\t\t\t\tif (!this.#isExpired(x)) {\n\t\t\t\t\tresult.push(x.value);\n\t\t\t\t}\n\t\t\t}\n\n\t\t\treturn result;\n\t\t}\n\n\t\tif (!Array.isArray(keys)) {\n\t\t\tthrow new TypeError(\"keys must be an array\");\n\t\t}\n\n\t\tconst result = Array.from({ length: keys.length });\n\t\tfor (let i = 0; i < keys.length; i++) {\n\t\t\tconst item = this.items[keys[i]];\n\t\t\tresult[i] = item !== undefined && !this.#isExpired(item) ? item.value : undefined;\n\t\t}\n\n\t\treturn result;\n\t}\n\n\t/**\n\t * Iterate over cache items in LRU order (least to most recent).\n\t * Note: This method directly accesses items from the linked list without calling\n\t * get() or peek(), so it does not update LRU order. Expired items are skipped.\n\t *\n\t * @param {function(*, any, LRU): void} callback - Function to call for each item. Signature: callback(value, key, cache)\n\t * @param {Object} [thisArg] - Value to use as `this` when executing callback.\n\t * @returns {LRU} The LRU instance for method chaining.\n\t */\n\tforEach(callback, thisArg) {\n\t\tfor (let x = this.first; x !== null;) {\n\t\t\tconst next = x.next;\n\t\t\tif (!this.#isExpired(x)) {\n\t\t\t\tcallback.call(thisArg, x.value, x.key, this);\n\t\t\t}\n\t\t\tx = next;\n\t\t}\n\n\t\treturn this;\n\t}\n\n\t/**\n\t * Batch retrieve multiple items.\n\t *\n\t * @param {string[]} keys - Array of keys to retrieve.\n\t * @returns {Object} Object mapping keys to values (undefined for missing/expired keys).\n\t */\n\tgetMany(keys) {\n\t\tif (!Array.isArray(keys)) {\n\t\t\tthrow new TypeError(\"keys must be an array\");\n\t\t}\n\n\t\tconst result = Object.create(null);\n\t\tfor (let i = 0; i < keys.length; i++) {\n\t\t\tconst key = keys[i];\n\t\t\tresult[key] = this.get(key);\n\t\t}\n\n\t\treturn result;\n\t}\n\n\t/**\n\t * Batch existence check - returns true if ALL keys exist.\n\t *\n\t * @param {string[]} keys - Array of keys to check.\n\t * @returns {boolean} True if all keys exist and are not expired.\n\t */\n\thasAll(keys) {\n\t\tif (!Array.isArray(keys)) {\n\t\t\tthrow new TypeError(\"keys must be an array\");\n\t\t}\n\n\t\tfor (let i = 0; i < keys.length; i++) {\n\t\t\tif (!this.has(keys[i])) {\n\t\t\t\treturn false;\n\t\t\t}\n\t\t}\n\n\t\treturn true;\n\t}\n\n\t/**\n\t * Batch existence check - returns true if ANY key exists.\n\t *\n\t * @param {string[]} keys - Array of keys to check.\n\t * @returns {boolean} True if any key exists and is not expired.\n\t */\n\thasAny(keys) {\n\t\tif (!Array.isArray(keys)) {\n\t\t\tthrow new TypeError(\"keys must be an array\");\n\t\t}\n\n\t\tfor (let i = 0; i < keys.length; i++) {\n\t\t\tif (this.has(keys[i])) {\n\t\t\t\treturn true;\n\t\t\t}\n\t\t}\n\n\t\treturn false;\n\t}\n\n\t/**\n\t * Remove expired items without affecting LRU order.\n\t * Unlike get(), this does not move items to the end.\n\t *\n\t * @param {boolean} [fireOnEvict=false] - When true, invokes the onEvict callback for each removed item.\n\t * @returns {number} Number of expired items removed.\n\t */\n\tcleanup(fireOnEvict = false) {\n\t\tif (this.ttl === 0 || this.size === 0) {\n\t\t\treturn 0;\n\t\t}\n\n\t\tlet removed = 0;\n\n\t\tfor (let x = this.first; x !== null;) {\n\t\t\tconst next = x.next;\n\t\t\tif (this.#isExpired(x)) {\n\t\t\t\tconst key = x.key;\n\t\t\t\tif (this.items[key] !== undefined) {\n\t\t\t\t\tdelete this.items[key];\n\t\t\t\t\tthis.size--;\n\t\t\t\t\tremoved++;\n\t\t\t\t\tthis.#unlink(x);\n\t\t\t\t\tx.prev = null;\n\t\t\t\t\tx.next = null;\n\t\t\t\t\tif (fireOnEvict && this.#onEvict !== null) {\n\t\t\t\t\t\tthis.#onEvict({\n\t\t\t\t\t\t\tkey: x.key,\n\t\t\t\t\t\t\tvalue: x.value,\n\t\t\t\t\t\t\texpiry: x.expiry,\n\t\t\t\t\t\t});\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t}\n\t\t\tx = next;\n\t\t}\n\n\t\treturn removed;\n\t}\n\n\t/**\n\t * Serialize cache to JSON-compatible format.\n\t *\n\t * @returns {Array<{key: any, value: *, expiry: number}>} Array of cache items.\n\t */\n\ttoJSON() {\n\t\tconst result = [];\n\t\tfor (let x = this.first; x !== null; x = x.next) {\n\t\t\tif (!this.#isExpired(x)) {\n\t\t\t\tresult.push({\n\t\t\t\t\tkey: x.key,\n\t\t\t\t\tvalue: x.value,\n\t\t\t\t\texpiry: x.expiry,\n\t\t\t\t});\n\t\t\t}\n\t\t}\n\n\t\treturn result;\n\t}\n\n\t/**\n\t * Get cache statistics.\n\t *\n\t * @returns {Object} Statistics object with hits, misses, sets, deletes, evictions counts.\n\t */\n\tstats() {\n\t\treturn { ...this.#stats };\n\t}\n\n\t/**\n\t * Register callback for evicted items.\n\t *\n\t * @param {function(Object): void} callback - Function called when item is evicted. Receives {key, value, expiry}.\n\t * @returns {LRU} The LRU instance for method chaining.\n\t */\n\tonEvict(callback) {\n\t\tif (typeof callback !== \"function\") {\n\t\t\tthrow new TypeError(\"onEvict callback must be a function\");\n\t\t}\n\n\t\tthis.#onEvict = callback;\n\n\t\treturn this;\n\t}\n\n\t/**\n\t * Get counts of items by TTL status.\n\t *\n\t * @returns {Object} Object with valid, expired, and noTTL counts.\n\t */\n\tsizeByTTL() {\n\t\tif (this.ttl === 0) {\n\t\t\treturn { valid: this.size, expired: 0, noTTL: this.size };\n\t\t}\n\n\t\tconst now = Date.now();\n\t\tlet valid = 0;\n\t\tlet expired = 0;\n\n\t\tfor (let x = this.first; x !== null; x = x.next) {\n\t\t\tif (x.expiry > now) {\n\t\t\t\tvalid++;\n\t\t\t} else {\n\t\t\t\texpired++;\n\t\t\t}\n\t\t}\n\n\t\treturn { valid, expired, noTTL: 0 };\n\t}\n\n\t/**\n\t * Get keys filtered by TTL status.\n\t *\n\t * @returns {Object} Object with valid, expired, and noTTL arrays of keys.\n\t */\n\tkeysByTTL() {\n\t\tif (this.ttl === 0) {\n\t\t\treturn { valid: this.keys(), expired: [], noTTL: this.keys() };\n\t\t}\n\n\t\tconst now = Date.now();\n\t\tconst valid = [];\n\t\tconst expired = [];\n\n\t\tfor (let x = this.first; x !== null; x = x.next) {\n\t\t\tif (x.expiry > now) {\n\t\t\t\tvalid.push(x.key);\n\t\t\t} else {\n\t\t\t\texpired.push(x.key);\n\t\t\t}\n\t\t}\n\n\t\treturn { valid, expired, noTTL: [] };\n\t}\n\n\t/**\n\t * Get values filtered by TTL status.\n\t *\n\t * @returns {Object} Object with valid, expired, and noTTL arrays of values.\n\t */\n\tvaluesByTTL() {\n\t\tif (this.ttl === 0) {\n\t\t\treturn { valid: this.values(), expired: [], noTTL: this.values() };\n\t\t}\n\n\t\tconst now = Date.now();\n\t\tconst valid = [];\n\t\tconst expired = [];\n\n\t\tfor (let x = this.first; x !== null; x = x.next) {\n\t\t\tif (x.expiry > now) {\n\t\t\t\tvalid.push(x.value);\n\t\t\t} else {\n\t\t\t\texpired.push(x.value);\n\t\t\t}\n\t\t}\n\n\t\treturn { valid, expired, noTTL: [] };\n\t}\n}\n\n/**\n * Factory function to create a new LRU cache instance with parameter validation.\n *\n * @function lru\n * @param {number} [max=1000] - Maximum number of items to store. Must be >= 0. Use 0 for unlimited size.\n * @param {number} [ttl=0] - Time to live in milliseconds. Must be >= 0. Use 0 for no expiration.\n * @param {boolean} [resetTTL=false] - Whether to reset TTL when updating existing items via set().\n * @returns {LRU} A new LRU cache instance.\n * @throws {TypeError} When parameters are invalid (negative numbers or wrong types).\n */\nexport function lru(max = 1000, ttl = 0, resetTTL = false) {\n\treturn new LRU(max, ttl, resetTTL);\n}\n"],"names":["LRU","stats","onEvict","constructor","max","ttl","resetTTL","Number","isInteger","TypeError","this","first","items","Object","create","last","size","hits","misses","sets","deletes","evictions","clear","x","next","prev","key","item","undefined","removeItem","entries","keys","result","isExpired","push","value","Array","isArray","from","length","i","evict","evictItem","expiry","expiresAt","Date","now","peek","get","moveToEnd","has","unlink","setWithEvicted","evicted","evictedItem","set","values","forEach","callback","thisArg","call","getMany","hasAll","hasAny","cleanup","fireOnEvict","removed","toJSON","sizeByTTL","valid","expired","noTTL","keysByTTL","valuesByTTL","lru"],"mappings":";;;;AAOO,MAAMA,EACZC,GACAC,GAWA,WAAAC,CAAYC,EAAM,EAAGC,EAAM,EAAGC,GAAW,GACxC,IAAKC,OAAOC,UAAUJ,IAAQA,EAAM,EACnC,MAAM,IAAIK,UAAU,qBAGrB,IAAKF,OAAOC,UAAUH,IAAQA,EAAM,EACnC,MAAM,IAAII,UAAU,qBAGrB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IAIF,MAAO,CAAED,QAAOC,UAASC,MAAO,EACjC,CAOA,SAAAC,GACC,GAAiB,IAAb9D,KAAKL,IACR,MAAO,CAAEgE,MAAO3D,KAAKqB,OAAQuC,QAAS,GAAIC,MAAO7D,KAAKqB,QAGvD,MAAMe,EAAMD,KAAKC,MACXuB,EAAQ,GACRC,EAAU,GAEhB,IAAK,IAAI/C,EAAIb,KAAKC,MAAa,OAANY,EAAYA,EAAIA,EAAEC,KACtCD,EAAEoB,OAASG,EACduB,EAAMnC,KAAKX,EAAEG,KAEb4C,EAAQpC,KAAKX,EAAEG,KAIjB,MAAO,CAAE2C,QAAOC,UAASC,MAAO,GACjC,CAOA,WAAAE,GACC,GAAiB,IAAb/D,KAAKL,IACR,MAAO,CAAEgE,MAAO3D,KAAK8C,SAAUc,QAAS,GAAIC,MAAO7D,KAAK8C,UAGzD,MAAMV,EAAMD,KAAKC,MACXuB,EAAQ,GACRC,EAAU,GAEhB,IAAK,IAAI/C,EAAIb,KAAKC,MAAa,OAANY,EAAYA,EAAIA,EAAEC,KACtCD,EAAEoB,OAASG,EACduB,EAAMnC,KAAKX,EAAEY,OAEbmC,EAAQpC,KAAKX,EAAEY,OAIjB,MAAO,CAAEkC,QAAOC,UAASC,MAAO,GACjC,EAaM,SAASG,EAAItE,EAAM,IAAMC,EAAM,EAAGC,GAAW,GACnD,OAAO,IAAIN,EAAII,EAAKC,EAAKC,EAC1B,QAAAN,SAAA0E"} \ No newline at end of file +{"version":3,"file":"tiny-lru.min.js","sources":["../src/lru.js"],"sourcesContent":["/**\n * A high-performance Least Recently Used (LRU) cache implementation with optional TTL support.\n * Items are automatically evicted when the cache reaches its maximum size,\n * removing the least recently used items first. All core operations (get, set, delete) are O(1).\n *\n * @class LRU\n */\nexport class LRU {\n\t#stats;\n\t#onEvict;\n\n\t/**\n\t * Creates a new LRU cache instance.\n\t *\n\t * @constructor\n\t * @param {number} [max=0] - Maximum number of items to store. 0 means unlimited.\n\t * @param {number} [ttl=0] - Time to live in milliseconds. 0 means no expiration.\n\t * @param {boolean} [resetTTL=false] - Whether to reset TTL when updating existing items via set().\n\t * @throws {TypeError} When parameters are invalid (negative numbers or wrong types).\n\t */\n\tconstructor(max = 0, ttl = 0, resetTTL = false) {\n\t\tif (!Number.isInteger(max) || max < 0) {\n\t\t\tthrow new TypeError(\"Invalid max value\");\n\t\t}\n\n\t\tif (!Number.isInteger(ttl) || ttl < 0) {\n\t\t\tthrow new TypeError(\"Invalid ttl value\");\n\t\t}\n\n\t\tif (typeof resetTTL !== \"boolean\") {\n\t\t\tthrow new TypeError(\"Invalid resetTTL value\");\n\t\t}\n\n\t\tthis.first = null;\n\t\tthis.items = Object.create(null);\n\t\tthis.last = null;\n\t\tthis.max = max;\n\t\tthis.resetTTL = resetTTL;\n\t\tthis.size = 0;\n\t\tthis.ttl = ttl;\n\t\tthis.#stats = { hits: 0, misses: 0, sets: 0, deletes: 0, evictions: 0 };\n\t\tthis.#onEvict = null;\n\t}\n\n\t/**\n\t * Removes all items from the cache.\n\t *\n\t * @returns {LRU} The LRU instance for method chaining.\n\t */\n\tclear() {\n\t\tfor (let x = this.first; x !== null;) {\n\t\t\tconst next = x.next;\n\t\t\tx.prev = null;\n\t\t\tx.next = null;\n\t\t\tx = next;\n\t\t}\n\n\t\tthis.first = null;\n\t\tthis.items = Object.create(null);\n\t\tthis.last = null;\n\t\tthis.size = 0;\n\t\tthis.#stats.hits = 0;\n\t\tthis.#stats.misses = 0;\n\t\tthis.#stats.sets = 0;\n\t\tthis.#stats.deletes = 0;\n\t\tthis.#stats.evictions = 0;\n\n\t\treturn this;\n\t}\n\n\t/**\n\t * Removes an item from the cache by key.\n\t *\n\t * @param {string} key - The key of the item to delete.\n\t * @returns {LRU} The LRU instance for method chaining.\n\t */\n\tdelete(key) {\n\t\tconst item = this.items[key];\n\n\t\tif (item !== undefined) {\n\t\t\tthis.#removeItem(item);\n\t\t\tthis.#stats.deletes++;\n\t\t}\n\n\t\treturn this;\n\t}\n\n\t/**\n\t * Returns an array of [key, value] pairs for the specified keys.\n\t * When no keys provided, returns all entries in LRU order.\n\t * When keys provided, order matches the input array.\n\t *\n\t * @param {string[]} [keys=this.keys()] - Array of keys to get entries for. Defaults to all keys.\n\t * @returns {Array>} Array of [key, value] pairs.\n\t */\n\tentries(keys) {\n\t\tif (keys === undefined) {\n\t\t\tconst result = [];\n\t\t\tthis.#forEachLive((x) => result.push([x.key, x.value]));\n\n\t\t\treturn result;\n\t\t}\n\n\t\tif (!Array.isArray(keys)) {\n\t\t\tthrow new TypeError(\"keys must be an array\");\n\t\t}\n\n\t\tconst result = Array.from({ length: keys.length });\n\t\tfor (let i = 0; i < keys.length; i++) {\n\t\t\tconst key = keys[i];\n\t\t\tconst item = this.items[key];\n\t\t\tresult[i] = [key, item !== undefined && !this.#isExpired(item) ? item.value : undefined];\n\t\t}\n\n\t\treturn result;\n\t}\n\n\t/**\n\t * Removes the least recently used item from the cache.\n\t *\n\t * @returns {LRU} The LRU instance for method chaining.\n\t */\n\tevict() {\n\t\tif (this.size === 0) {\n\t\t\treturn this;\n\t\t}\n\n\t\tconst item = this.#evictItem();\n\n\t\tif (this.#onEvict !== null) {\n\t\t\tthis.#onEvict({\n\t\t\t\tkey: item.key,\n\t\t\t\tvalue: item.value,\n\t\t\t\texpiry: item.expiry,\n\t\t\t});\n\t\t}\n\n\t\treturn this;\n\t}\n\n\t/**\n\t * Returns the expiration timestamp for a given key.\n\t *\n\t * @param {string} key - The key to check expiration for.\n\t * @returns {number|undefined} The expiration timestamp in milliseconds, or undefined if key doesn't exist.\n\t */\n\texpiresAt(key) {\n\t\tconst item = this.items[key];\n\t\treturn item !== undefined ? item.expiry : undefined;\n\t}\n\n\t/**\n\t * Checks if an item has expired.\n\t *\n\t * @param {Object} item - The cache item to check.\n\t * @returns {boolean} True if the item has expired, false otherwise.\n\t * @private\n\t */\n\t#isExpired(item) {\n\t\tif (this.ttl === 0) {\n\t\t\treturn false;\n\t\t}\n\n\t\treturn item.expiry <= Date.now();\n\t}\n\n\t/**\n\t * Retrieves a value from the cache by key without updating LRU order.\n\t * Note: Does not perform TTL checks or remove expired items.\n\t *\n\t * @param {string} key - The key to retrieve.\n\t * @returns {*} The value associated with the key, or undefined if not found.\n\t */\n\tpeek(key) {\n\t\tconst item = this.items[key];\n\t\treturn item !== undefined ? item.value : undefined;\n\t}\n\n\t/**\n\t * Retrieves a value from the cache by key. Updates the item's position to most recently used.\n\t *\n\t * @param {string} key - The key to retrieve.\n\t * @returns {*} The value associated with the key, or undefined if not found or expired.\n\t */\n\tget(key) {\n\t\tconst item = this.items[key];\n\n\t\tif (item !== undefined) {\n\t\t\tif (!this.#isExpired(item)) {\n\t\t\t\tthis.moveToEnd(item);\n\t\t\t\tthis.#stats.hits++;\n\t\t\t\treturn item.value;\n\t\t\t}\n\n\t\t\tthis.#removeItem(item);\n\t\t\tthis.#stats.misses++;\n\t\t\treturn undefined;\n\t\t}\n\n\t\tthis.#stats.misses++;\n\t\treturn undefined;\n\t}\n\n\t/**\n\t * Checks if a key exists in the cache.\n\t * Expired items are removed before returning false.\n\t *\n\t * @param {string} key - The key to check for.\n\t * @returns {boolean} True if the key exists and is not expired, false otherwise.\n\t */\n\thas(key) {\n\t\tconst item = this.items[key];\n\n\t\tif (item !== undefined && this.#isExpired(item)) {\n\t\t\tthis.#removeItem(item);\n\t\t\treturn false;\n\t\t}\n\n\t\treturn item !== undefined;\n\t}\n\n\t/**\n\t * Unlinks an item from the doubly-linked list.\n\t * Updates first/last pointers if needed.\n\t * Does NOT clear the item's prev/next pointers or delete from items map.\n\t *\n\t * @private\n\t */\n\t#unlink(item) {\n\t\tif (item.prev !== null) {\n\t\t\titem.prev.next = item.next;\n\t\t}\n\n\t\tif (item.next !== null) {\n\t\t\titem.next.prev = item.prev;\n\t\t}\n\n\t\tif (this.first === item) {\n\t\t\tthis.first = item.next;\n\t\t}\n\n\t\tif (this.last === item) {\n\t\t\tthis.last = item.prev;\n\t\t}\n\t}\n\n\t/**\n\t * Removes an item from the cache without incrementing the deletes stat.\n\t * Used internally by get()/has() when removing expired items.\n\t *\n\t * @param {Object} item - The cache item to remove.\n\t * @private\n\t */\n\t#removeItem(item) {\n\t\tdelete this.items[item.key];\n\t\tthis.size--;\n\t\tthis.#unlink(item);\n\t\titem.prev = null;\n\t\titem.next = null;\n\t}\n\n\t/**\n\t * Evicts the least recently used item from the cache without firing onEvict.\n\t * Used internally by setWithEvicted() to avoid double-notification.\n\t *\n\t * @returns {Object} The evicted item.\n\t * @private\n\t */\n\t#evictItem() {\n\t\tconst item = this.first;\n\n\t\tdelete this.items[item.key];\n\t\tthis.#stats.evictions++;\n\n\t\tif (--this.size === 0) {\n\t\t\tthis.first = null;\n\t\t\tthis.last = null;\n\t\t} else {\n\t\t\tthis.#unlink(item);\n\t\t}\n\n\t\titem.prev = null;\n\t\titem.next = null;\n\n\t\treturn item;\n\t}\n\n\t/**\n\t * Creates and links a new item at the most recently used position.\n\t * Wires the first/last pointers and returns the created item.\n\t *\n\t * @param {string} key - The key to store.\n\t * @param {*} value - The value to store.\n\t * @returns {Object} The created and linked item.\n\t * @private\n\t */\n\t#insertItem(key, value) {\n\t\tconst item = (this.items[key] = {\n\t\t\texpiry: this.ttl > 0 ? Date.now() + this.ttl : this.ttl,\n\t\t\tkey: key,\n\t\t\tprev: this.last,\n\t\t\tnext: null,\n\t\t\tvalue,\n\t\t});\n\n\t\tif (++this.size === 1) {\n\t\t\tthis.first = item;\n\t\t} else {\n\t\t\tthis.last.next = item;\n\t\t}\n\n\t\tthis.last = item;\n\n\t\treturn item;\n\t}\n\n\t/**\n\t * Invokes a callback for each live (non-expired) item in LRU order.\n\t * Accesses items directly without calling get(), so LRU order is not\n\t * mutated and expired items are not deleted mid-iteration.\n\t *\n\t * @param {function(Object): void} callback - Function to call for each live item.\n\t * @private\n\t */\n\t#forEachLive(callback) {\n\t\tfor (let x = this.first; x !== null; x = x.next) {\n\t\t\tif (!this.#isExpired(x)) {\n\t\t\t\tcallback(x);\n\t\t\t}\n\t\t}\n\t}\n\n\t/**\n\t * Efficiently moves an item to the end of the LRU list (most recently used position).\n\t * This is an internal optimization method that avoids the overhead of the full set() operation\n\t * when only LRU position needs to be updated.\n\t *\n\t * @param {Object} item - The cache item with prev/next pointers to reposition.\n\t * @private\n\t */\n\tmoveToEnd(item) {\n\t\tif (this.last === item) {\n\t\t\treturn;\n\t\t}\n\n\t\tthis.#unlink(item);\n\n\t\titem.prev = this.last;\n\t\titem.next = null;\n\t\tthis.last.next = item;\n\t\tthis.last = item;\n\t}\n\n\t/**\n\t * Returns an array of all keys in the cache, ordered from least to most recently used.\n\t * Expired items are skipped, consistent with entries(), values(), and toJSON().\n\t *\n\t * @returns {string[]} Array of keys in LRU order.\n\t */\n\tkeys() {\n\t\tconst result = [];\n\t\tthis.#forEachLive((x) => result.push(x.key));\n\n\t\treturn result;\n\t}\n\n\t/**\n\t * Sets a value in the cache and returns any evicted item.\n\t * Eviction is silent — onEvict is not fired for the returned item.\n\t *\n\t * @param {string} key - The key to set.\n\t * @param {*} value - The value to store.\n\t * @returns {Object|null} The evicted item (if any) with shape {key, value, expiry}, or null.\n\t */\n\tsetWithEvicted(key, value) {\n\t\tlet evicted = null;\n\t\tlet item = this.items[key];\n\n\t\tif (item !== undefined && !this.#isExpired(item)) {\n\t\t\titem.value = value;\n\t\t\tif (this.resetTTL) {\n\t\t\t\titem.expiry = this.ttl > 0 ? Date.now() + this.ttl : this.ttl;\n\t\t\t}\n\t\t\tthis.moveToEnd(item);\n\t\t} else {\n\t\t\tif (item !== undefined) {\n\t\t\t\tthis.#removeItem(item);\n\t\t\t}\n\n\t\t\tif (this.max > 0 && this.size === this.max) {\n\t\t\t\tconst evictedItem = this.#evictItem();\n\t\t\t\tevicted = {\n\t\t\t\t\tkey: evictedItem.key,\n\t\t\t\t\tvalue: evictedItem.value,\n\t\t\t\t\texpiry: evictedItem.expiry,\n\t\t\t\t};\n\t\t\t}\n\n\t\t\titem = this.#insertItem(key, value);\n\t\t}\n\n\t\tthis.#stats.sets++;\n\t\treturn evicted;\n\t}\n\n\t/**\n\t * Sets a value in the cache. Updates the item's position to most recently used.\n\t *\n\t * @param {string} key - The key to set.\n\t * @param {*} value - The value to store.\n\t * @returns {LRU} The LRU instance for method chaining.\n\t */\n\tset(key, value) {\n\t\tlet item = this.items[key];\n\n\t\tif (item !== undefined && !this.#isExpired(item)) {\n\t\t\titem.value = value;\n\n\t\t\tif (this.resetTTL) {\n\t\t\t\titem.expiry = this.ttl > 0 ? Date.now() + this.ttl : this.ttl;\n\t\t\t}\n\n\t\t\tthis.moveToEnd(item);\n\t\t} else {\n\t\t\tif (item !== undefined) {\n\t\t\t\tthis.#removeItem(item);\n\t\t\t}\n\n\t\t\tif (this.max > 0 && this.size === this.max) {\n\t\t\t\tthis.evict();\n\t\t\t}\n\n\t\t\titem = this.#insertItem(key, value);\n\t\t}\n\n\t\tthis.#stats.sets++;\n\n\t\treturn this;\n\t}\n\n\t/**\n\t * Returns an array of all values in the cache for the specified keys.\n\t * When no keys provided, returns all values in LRU order.\n\t * When keys provided, order matches the input array.\n\t *\n\t * @param {string[]} [keys] - Array of keys to get values for. Defaults to all keys.\n\t * @returns {Array<*>} Array of values corresponding to the keys.\n\t */\n\tvalues(keys) {\n\t\tif (keys === undefined) {\n\t\t\tconst result = [];\n\t\t\tfor (let x = this.first; x !== null; x = x.next) {\n\t\t\t\tif (!this.#isExpired(x)) {\n\t\t\t\t\tresult.push(x.value);\n\t\t\t\t}\n\t\t\t}\n\n\t\t\treturn result;\n\t\t}\n\n\t\tif (!Array.isArray(keys)) {\n\t\t\tthrow new TypeError(\"keys must be an array\");\n\t\t}\n\n\t\tconst result = Array.from({ length: keys.length });\n\t\tfor (let i = 0; i < keys.length; i++) {\n\t\t\tconst item = this.items[keys[i]];\n\t\t\tresult[i] = item !== undefined && !this.#isExpired(item) ? item.value : undefined;\n\t\t}\n\n\t\treturn result;\n\t}\n\n\t/**\n\t * Iterate over cache items in LRU order (least to most recent).\n\t * Note: This method directly accesses items from the linked list without calling\n\t * get() or peek(), so it does not update LRU order. Expired items are skipped.\n\t *\n\t * @param {function(*, any, LRU): void} callback - Function to call for each item. Signature: callback(value, key, cache)\n\t * @param {Object} [thisArg] - Value to use as `this` when executing callback.\n\t * @returns {LRU} The LRU instance for method chaining.\n\t */\n\tforEach(callback, thisArg) {\n\t\tfor (let x = this.first; x !== null;) {\n\t\t\tconst next = x.next;\n\t\t\tif (!this.#isExpired(x)) {\n\t\t\t\tcallback.call(thisArg, x.value, x.key, this);\n\t\t\t}\n\t\t\tx = next;\n\t\t}\n\n\t\treturn this;\n\t}\n\n\t/**\n\t * Batch retrieve multiple items.\n\t *\n\t * @param {string[]} keys - Array of keys to retrieve.\n\t * @returns {Object} Object mapping keys to values (undefined for missing/expired keys).\n\t */\n\tgetMany(keys) {\n\t\tif (!Array.isArray(keys)) {\n\t\t\tthrow new TypeError(\"keys must be an array\");\n\t\t}\n\n\t\tconst result = Object.create(null);\n\t\tfor (let i = 0; i < keys.length; i++) {\n\t\t\tconst key = keys[i];\n\t\t\tresult[key] = this.get(key);\n\t\t}\n\n\t\treturn result;\n\t}\n\n\t/**\n\t * Batch existence check - returns true if ALL keys exist.\n\t *\n\t * @param {string[]} keys - Array of keys to check.\n\t * @returns {boolean} True if all keys exist and are not expired.\n\t */\n\thasAll(keys) {\n\t\tif (!Array.isArray(keys)) {\n\t\t\tthrow new TypeError(\"keys must be an array\");\n\t\t}\n\n\t\tfor (let i = 0; i < keys.length; i++) {\n\t\t\tif (!this.has(keys[i])) {\n\t\t\t\treturn false;\n\t\t\t}\n\t\t}\n\n\t\treturn true;\n\t}\n\n\t/**\n\t * Batch existence check - returns true if ANY key exists.\n\t *\n\t * @param {string[]} keys - Array of keys to check.\n\t * @returns {boolean} True if any key exists and is not expired.\n\t */\n\thasAny(keys) {\n\t\tif (!Array.isArray(keys)) {\n\t\t\tthrow new TypeError(\"keys must be an array\");\n\t\t}\n\n\t\tfor (let i = 0; i < keys.length; i++) {\n\t\t\tif (this.has(keys[i])) {\n\t\t\t\treturn true;\n\t\t\t}\n\t\t}\n\n\t\treturn false;\n\t}\n\n\t/**\n\t * Remove expired items without affecting LRU order.\n\t * Unlike get(), this does not move items to the end.\n\t *\n\t * @param {boolean} [fireOnEvict=false] - When true, invokes the onEvict callback for each removed item.\n\t * @returns {number} Number of expired items removed.\n\t */\n\tcleanup(fireOnEvict = false) {\n\t\tif (this.ttl === 0 || this.size === 0) {\n\t\t\treturn 0;\n\t\t}\n\n\t\tlet removed = 0;\n\n\t\tfor (let x = this.first; x !== null;) {\n\t\t\tconst next = x.next;\n\t\t\tif (this.#isExpired(x)) {\n\t\t\t\tconst key = x.key;\n\t\t\t\tif (this.items[key] !== undefined) {\n\t\t\t\t\tdelete this.items[key];\n\t\t\t\t\tthis.size--;\n\t\t\t\t\tremoved++;\n\t\t\t\t\tthis.#unlink(x);\n\t\t\t\t\tx.prev = null;\n\t\t\t\t\tx.next = null;\n\t\t\t\t\tif (fireOnEvict && this.#onEvict !== null) {\n\t\t\t\t\t\tthis.#onEvict({\n\t\t\t\t\t\t\tkey: x.key,\n\t\t\t\t\t\t\tvalue: x.value,\n\t\t\t\t\t\t\texpiry: x.expiry,\n\t\t\t\t\t\t});\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t}\n\t\t\tx = next;\n\t\t}\n\n\t\treturn removed;\n\t}\n\n\t/**\n\t * Serialize cache to JSON-compatible format.\n\t *\n\t * @returns {Array<{key: any, value: *, expiry: number}>} Array of cache items.\n\t */\n\ttoJSON() {\n\t\tconst result = [];\n\t\tfor (let x = this.first; x !== null; x = x.next) {\n\t\t\tif (!this.#isExpired(x)) {\n\t\t\t\tresult.push({\n\t\t\t\t\tkey: x.key,\n\t\t\t\t\tvalue: x.value,\n\t\t\t\t\texpiry: x.expiry,\n\t\t\t\t});\n\t\t\t}\n\t\t}\n\n\t\treturn result;\n\t}\n\n\t/**\n\t * Get cache statistics.\n\t *\n\t * @returns {Object} Statistics object with hits, misses, sets, deletes, evictions counts.\n\t */\n\tstats() {\n\t\treturn { ...this.#stats };\n\t}\n\n\t/**\n\t * Register callback for evicted items.\n\t *\n\t * @param {function(Object): void} callback - Function called when item is evicted. Receives {key, value, expiry}.\n\t * @returns {LRU} The LRU instance for method chaining.\n\t */\n\tonEvict(callback) {\n\t\tif (typeof callback !== \"function\") {\n\t\t\tthrow new TypeError(\"onEvict callback must be a function\");\n\t\t}\n\n\t\tthis.#onEvict = callback;\n\n\t\treturn this;\n\t}\n\n\t/**\n\t * Get counts of items by TTL status.\n\t *\n\t * @returns {Object} Object with valid, expired, and noTTL counts.\n\t */\n\tsizeByTTL() {\n\t\tif (this.ttl === 0) {\n\t\t\treturn { valid: this.size, expired: 0, noTTL: this.size };\n\t\t}\n\n\t\tconst now = Date.now();\n\t\tlet valid = 0;\n\t\tlet expired = 0;\n\n\t\tfor (let x = this.first; x !== null; x = x.next) {\n\t\t\tif (x.expiry > now) {\n\t\t\t\tvalid++;\n\t\t\t} else {\n\t\t\t\texpired++;\n\t\t\t}\n\t\t}\n\n\t\treturn { valid, expired, noTTL: 0 };\n\t}\n\n\t/**\n\t * Get keys filtered by TTL status.\n\t *\n\t * @returns {Object} Object with valid, expired, and noTTL arrays of keys.\n\t */\n\tkeysByTTL() {\n\t\tif (this.ttl === 0) {\n\t\t\treturn { valid: this.keys(), expired: [], noTTL: this.keys() };\n\t\t}\n\n\t\tconst now = Date.now();\n\t\tconst valid = [];\n\t\tconst expired = [];\n\n\t\tfor (let x = this.first; x !== null; x = x.next) {\n\t\t\tif (x.expiry > now) {\n\t\t\t\tvalid.push(x.key);\n\t\t\t} else {\n\t\t\t\texpired.push(x.key);\n\t\t\t}\n\t\t}\n\n\t\treturn { valid, expired, noTTL: [] };\n\t}\n\n\t/**\n\t * Get values filtered by TTL status.\n\t *\n\t * @returns {Object} Object with valid, expired, and noTTL arrays of values.\n\t */\n\tvaluesByTTL() {\n\t\tif (this.ttl === 0) {\n\t\t\treturn { valid: this.values(), expired: [], noTTL: this.values() };\n\t\t}\n\n\t\tconst now = Date.now();\n\t\tconst valid = [];\n\t\tconst expired = [];\n\n\t\tfor (let x = this.first; x !== null; x = x.next) {\n\t\t\tif (x.expiry > now) {\n\t\t\t\tvalid.push(x.value);\n\t\t\t} else {\n\t\t\t\texpired.push(x.value);\n\t\t\t}\n\t\t}\n\n\t\treturn { valid, expired, noTTL: [] };\n\t}\n}\n\n/**\n * Factory function to create a new LRU cache instance with parameter validation.\n *\n * @function lru\n * @param {number} [max=1000] - Maximum number of items to store. Must be >= 0. Use 0 for unlimited size.\n * @param {number} [ttl=0] - Time to live in milliseconds. Must be >= 0. Use 0 for no expiration.\n * @param {boolean} [resetTTL=false] - Whether to reset TTL when updating existing items via set().\n * @returns {LRU} A new LRU cache instance.\n * @throws {TypeError} When parameters are invalid (negative numbers or wrong types).\n */\nexport function lru(max = 1000, ttl = 0, resetTTL = false) {\n\treturn new LRU(max, ttl, resetTTL);\n}\n"],"names":["LRU","stats","onEvict","constructor","max","ttl","resetTTL","Number","isInteger","TypeError","this","first","items","Object","create","last","size","hits","misses","sets","deletes","evictions","clear","x","next","prev","key","item","undefined","removeItem","entries","keys","result","forEachLive","push","value","Array","isArray","from","length","i","isExpired","evict","evictItem","expiry","expiresAt","Date","now","peek","get","moveToEnd","has","unlink","insertItem","callback","setWithEvicted","evicted","evictedItem","set","values","forEach","thisArg","call","getMany","hasAll","hasAny","cleanup","fireOnEvict","removed","toJSON","sizeByTTL","valid","expired","noTTL","keysByTTL","valuesByTTL","lru"],"mappings":";;;;AAOO,MAAMA,EACZC,GACAC,GAWA,WAAAC,CAAYC,EAAM,EAAGC,EAAM,EAAGC,GAAW,GACxC,IAAKC,OAAOC,UAAUJ,IAAQA,EAAM,EACnC,MAAM,IAAIK,UAAU,qBAGrB,IAAKF,OAAOC,UAAUH,IAAQA,EAAM,EACnC,MAAM,IAAII,UAAU,qBAGrB,GAAwB,kBAAbH,EACV,MAAM,IAAIG,UAAU,0BAGrBC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D,MAAMgB,EAAMD,KAAKC,MACXwB,EAAQ,GACRC,EAAU,GAEhB,IAAK,IAAIjD,EAAIb,KAAKC,MAAa,OAANY,EAAYA,EAAIA,EAAEC,KACtCD,EAAEqB,OAASG,EACdwB,EAAMrC,KAAKX,EAAEG,KAEb8C,EAAQtC,KAAKX,EAAEG,KAIjB,MAAO,CAAE6C,QAAOC,UAASC,MAAO,GACjC,CAOA,WAAAE,GACC,GAAiB,IAAbjE,KAAKL,IACR,MAAO,CAAEkE,MAAO7D,KAAKiD,SAAUa,QAAS,GAAIC,MAAO/D,KAAKiD,UAGzD,MAAMZ,EAAMD,KAAKC,MACXwB,EAAQ,GACRC,EAAU,GAEhB,IAAK,IAAIjD,EAAIb,KAAKC,MAAa,OAANY,EAAYA,EAAIA,EAAEC,KACtCD,EAAEqB,OAASG,EACdwB,EAAMrC,KAAKX,EAAEY,OAEbqC,EAAQtC,KAAKX,EAAEY,OAIjB,MAAO,CAAEoC,QAAOC,UAASC,MAAO,GACjC,EAaM,SAASG,EAAIxE,EAAM,IAAMC,EAAM,EAAGC,GAAW,GACnD,OAAO,IAAIN,EAAII,EAAKC,EAAKC,EAC1B,QAAAN,SAAA4E"} \ No newline at end of file diff --git a/src/lru.js b/src/lru.js index e82a76b..ae6b413 100644 --- a/src/lru.js +++ b/src/lru.js @@ -96,11 +96,7 @@ export class LRU { entries(keys) { if (keys === undefined) { const result = []; - for (let x = this.first; x !== null; x = x.next) { - if (!this.#isExpired(x)) { - result.push([x.key, x.value]); - } - } + this.#forEachLive((x) => result.push([x.key, x.value])); return result; } @@ -289,6 +285,51 @@ export class LRU { return item; } + /** + * Creates and links a new item at the most recently used position. + * Wires the first/last pointers and returns the created item. + * + * @param {string} key - The key to store. + * @param {*} value - The value to store. + * @returns {Object} The created and linked item. + * @private + */ + #insertItem(key, value) { + const item = (this.items[key] = { + expiry: this.ttl > 0 ? Date.now() + this.ttl : this.ttl, + key: key, + prev: this.last, + next: null, + value, + }); + + if (++this.size === 1) { + this.first = item; + } else { + this.last.next = item; + } + + this.last = item; + + return item; + } + + /** + * Invokes a callback for each live (non-expired) item in LRU order. + * Accesses items directly without calling get(), so LRU order is not + * mutated and expired items are not deleted mid-iteration. + * + * @param {function(Object): void} callback - Function to call for each live item. + * @private + */ + #forEachLive(callback) { + for (let x = this.first; x !== null; x = x.next) { + if (!this.#isExpired(x)) { + callback(x); + } + } + } + /** * Efficiently moves an item to the end of the LRU list (most recently used position). * This is an internal optimization method that avoids the overhead of the full set() operation @@ -318,11 +359,7 @@ export class LRU { */ keys() { const result = []; - for (let x = this.first; x !== null; x = x.next) { - if (!this.#isExpired(x)) { - result.push(x.key); - } - } + this.#forEachLive((x) => result.push(x.key)); return result; } @@ -359,21 +396,7 @@ export class LRU { }; } - item = this.items[key] = { - expiry: this.ttl > 0 ? Date.now() + this.ttl : this.ttl, - key: key, - prev: this.last, - next: null, - value, - }; - - if (++this.size === 1) { - this.first = item; - } else { - this.last.next = item; - } - - this.last = item; + item = this.#insertItem(key, value); } this.#stats.sets++; @@ -407,21 +430,7 @@ export class LRU { this.evict(); } - item = this.items[key] = { - expiry: this.ttl > 0 ? Date.now() + this.ttl : this.ttl, - key: key, - prev: this.last, - next: null, - value, - }; - - if (++this.size === 1) { - this.first = item; - } else { - this.last.next = item; - } - - this.last = item; + item = this.#insertItem(key, value); } this.#stats.sets++; From e2e292a016b9c008e4fdd99b830a67d1d845760a Mon Sep 17 00:00:00 2001 From: Jason Date: Wed, 25 Nov 2026 15:20:32 -0500 Subject: [PATCH 2/2] chore: restore dist build artifacts to master state The pre-commit hook's git add -A swept regenerated dist files into the refactor commit. Restore them to the master baseline, consistent with the convention of not shipping rebuilt dist artifacts in refactor PRs. --- dist/tiny-lru.cjs | 89 ++++++++++++++++++---------------------- dist/tiny-lru.js | 89 ++++++++++++++++++---------------------- dist/tiny-lru.min.js | 2 +- dist/tiny-lru.min.js.map | 2 +- 4 files changed, 82 insertions(+), 100 deletions(-) diff --git a/dist/tiny-lru.cjs b/dist/tiny-lru.cjs index af8a8e9..1329c72 100644 --- a/dist/tiny-lru.cjs +++ b/dist/tiny-lru.cjs @@ -105,7 +105,11 @@ class LRU { entries(keys) { if (keys === undefined) { const result = []; - this.#forEachLive((x) => result.push([x.key, x.value])); + for (let x = this.first; x !== null; x = x.next) { + if (!this.#isExpired(x)) { + result.push([x.key, x.value]); + } + } return result; } @@ -294,51 +298,6 @@ class LRU { return item; } - /** - * Creates and links a new item at the most recently used position. - * Wires the first/last pointers and returns the created item. - * - * @param {string} key - The key to store. - * @param {*} value - The value to store. - * @returns {Object} The created and linked item. - * @private - */ - #insertItem(key, value) { - const item = (this.items[key] = { - expiry: this.ttl > 0 ? Date.now() + this.ttl : this.ttl, - key: key, - prev: this.last, - next: null, - value, - }); - - if (++this.size === 1) { - this.first = item; - } else { - this.last.next = item; - } - - this.last = item; - - return item; - } - - /** - * Invokes a callback for each live (non-expired) item in LRU order. - * Accesses items directly without calling get(), so LRU order is not - * mutated and expired items are not deleted mid-iteration. - * - * @param {function(Object): void} callback - Function to call for each live item. - * @private - */ - #forEachLive(callback) { - for (let x = this.first; x !== null; x = x.next) { - if (!this.#isExpired(x)) { - callback(x); - } - } - } - /** * Efficiently moves an item to the end of the LRU list (most recently used position). * This is an internal optimization method that avoids the overhead of the full set() operation @@ -368,7 +327,11 @@ class LRU { */ keys() { const result = []; - this.#forEachLive((x) => result.push(x.key)); + for (let x = this.first; x !== null; x = x.next) { + if (!this.#isExpired(x)) { + result.push(x.key); + } + } return result; } @@ -405,7 +368,21 @@ class LRU { }; } - item = this.#insertItem(key, value); + item = this.items[key] = { + expiry: this.ttl > 0 ? Date.now() + this.ttl : this.ttl, + key: key, + prev: this.last, + next: null, + value, + }; + + if (++this.size === 1) { + this.first = item; + } else { + this.last.next = item; + } + + this.last = item; } this.#stats.sets++; @@ -439,7 +416,21 @@ class LRU { this.evict(); } - item = this.#insertItem(key, value); + item = this.items[key] = { + expiry: this.ttl > 0 ? Date.now() + this.ttl : this.ttl, + key: key, + prev: this.last, + next: null, + value, + }; + + if (++this.size === 1) { + this.first = item; + } else { + this.last.next = item; + } + + this.last = item; } this.#stats.sets++; diff --git a/dist/tiny-lru.js b/dist/tiny-lru.js index a4005bd..69c9922 100644 --- a/dist/tiny-lru.js +++ b/dist/tiny-lru.js @@ -103,7 +103,11 @@ class LRU { entries(keys) { if (keys === undefined) { const result = []; - this.#forEachLive((x) => result.push([x.key, x.value])); + for (let x = this.first; x !== null; x = x.next) { + if (!this.#isExpired(x)) { + result.push([x.key, x.value]); + } + } return result; } @@ -292,51 +296,6 @@ class LRU { return item; } - /** - * Creates and links a new item at the most recently used position. - * Wires the first/last pointers and returns the created item. - * - * @param {string} key - The key to store. - * @param {*} value - The value to store. - * @returns {Object} The created and linked item. - * @private - */ - #insertItem(key, value) { - const item = (this.items[key] = { - expiry: this.ttl > 0 ? Date.now() + this.ttl : this.ttl, - key: key, - prev: this.last, - next: null, - value, - }); - - if (++this.size === 1) { - this.first = item; - } else { - this.last.next = item; - } - - this.last = item; - - return item; - } - - /** - * Invokes a callback for each live (non-expired) item in LRU order. - * Accesses items directly without calling get(), so LRU order is not - * mutated and expired items are not deleted mid-iteration. - * - * @param {function(Object): void} callback - Function to call for each live item. - * @private - */ - #forEachLive(callback) { - for (let x = this.first; x !== null; x = x.next) { - if (!this.#isExpired(x)) { - callback(x); - } - } - } - /** * Efficiently moves an item to the end of the LRU list (most recently used position). * This is an internal optimization method that avoids the overhead of the full set() operation @@ -366,7 +325,11 @@ class LRU { */ keys() { const result = []; - this.#forEachLive((x) => result.push(x.key)); + for (let x = this.first; x !== null; x = x.next) { + if (!this.#isExpired(x)) { + result.push(x.key); + } + } return result; } @@ -403,7 +366,21 @@ class LRU { }; } - item = this.#insertItem(key, value); + item = this.items[key] = { + expiry: this.ttl > 0 ? Date.now() + this.ttl : this.ttl, + key: key, + prev: this.last, + next: null, + value, + }; + + if (++this.size === 1) { + this.first = item; + } else { + this.last.next = item; + } + + this.last = item; } this.#stats.sets++; @@ -437,7 +414,21 @@ class LRU { this.evict(); } - item = this.#insertItem(key, value); + item = this.items[key] = { + expiry: this.ttl > 0 ? Date.now() + this.ttl : this.ttl, + key: key, + prev: this.last, + next: null, + value, + }; + + if (++this.size === 1) { + this.first = item; + } else { + this.last.next = item; + } + + this.last = item; } this.#stats.sets++; diff --git a/dist/tiny-lru.min.js b/dist/tiny-lru.min.js index cd59d4e..948e563 100644 --- a/dist/tiny-lru.min.js +++ b/dist/tiny-lru.min.js @@ -2,4 +2,4 @@ 2026 Jason Mulligan @version 13.1.1 */ -class t{#t;#e;constructor(t=0,e=0,s=!1){if(!Number.isInteger(t)||t<0)throw new TypeError("Invalid max value");if(!Number.isInteger(e)||e<0)throw new TypeError("Invalid ttl value");if("boolean"!=typeof s)throw new TypeError("Invalid resetTTL value");this.first=null,this.items=Object.create(null),this.last=null,this.max=t,this.resetTTL=s,this.size=0,this.ttl=e,this.#t={hits:0,misses:0,sets:0,deletes:0,evictions:0},this.#e=null}clear(){for(let t=this.first;null!==t;){const e=t.next;t.prev=null,t.next=null,t=e}return this.first=null,this.items=Object.create(null),this.last=null,this.size=0,this.#t.hits=0,this.#t.misses=0,this.#t.sets=0,this.#t.deletes=0,this.#t.evictions=0,this}delete(t){const e=this.items[t];return void 0!==e&&(this.#s(e),this.#t.deletes++),this}entries(t){if(void 0===t){const t=[];return this.#i(e=>t.push([e.key,e.value])),t}if(!Array.isArray(t))throw new TypeError("keys must be an array");const e=Array.from({length:t.length});for(let s=0;s0?Date.now()+this.ttl:this.ttl,key:t,prev:this.last,next:null,value:e};return 1===++this.size?this.first=s:this.last.next=s,this.last=s,s}#i(t){for(let e=this.first;null!==e;e=e.next)this.#r(e)||t(e)}moveToEnd(t){this.last!==t&&(this.#h(t),t.prev=this.last,t.next=null,this.last.next=t,this.last=t)}keys(){const t=[];return this.#i(e=>t.push(e.key)),t}setWithEvicted(t,e){let s=null,i=this.items[t];if(void 0===i||this.#r(i)){if(void 0!==i&&this.#s(i),this.max>0&&this.size===this.max){const t=this.#n();s={key:t.key,value:t.value,expiry:t.expiry}}i=this.#l(t,e)}else i.value=e,this.resetTTL&&(i.expiry=this.ttl>0?Date.now()+this.ttl:this.ttl),this.moveToEnd(i);return this.#t.sets++,s}set(t,e){let s=this.items[t];return void 0===s||this.#r(s)?(void 0!==s&&this.#s(s),this.max>0&&this.size===this.max&&this.evict(),s=this.#l(t,e)):(s.value=e,this.resetTTL&&(s.expiry=this.ttl>0?Date.now()+this.ttl:this.ttl),this.moveToEnd(s)),this.#t.sets++,this}values(t){if(void 0===t){const t=[];for(let e=this.first;null!==e;e=e.next)this.#r(e)||t.push(e.value);return t}if(!Array.isArray(t))throw new TypeError("keys must be an array");const e=Array.from({length:t.length});for(let s=0;st?e++:s++;return{valid:e,expired:s,noTTL:0}}keysByTTL(){if(0===this.ttl)return{valid:this.keys(),expired:[],noTTL:this.keys()};const t=Date.now(),e=[],s=[];for(let i=this.first;null!==i;i=i.next)i.expiry>t?e.push(i.key):s.push(i.key);return{valid:e,expired:s,noTTL:[]}}valuesByTTL(){if(0===this.ttl)return{valid:this.values(),expired:[],noTTL:this.values()};const t=Date.now(),e=[],s=[];for(let i=this.first;null!==i;i=i.next)i.expiry>t?e.push(i.value):s.push(i.value);return{valid:e,expired:s,noTTL:[]}}}function e(e=1e3,s=0,i=!1){return new t(e,s,i)}export{t as LRU,e as lru};//# sourceMappingURL=tiny-lru.min.js.map +class t{#t;#e;constructor(t=0,e=0,s=!1){if(!Number.isInteger(t)||t<0)throw new TypeError("Invalid max value");if(!Number.isInteger(e)||e<0)throw new TypeError("Invalid ttl value");if("boolean"!=typeof s)throw new TypeError("Invalid resetTTL value");this.first=null,this.items=Object.create(null),this.last=null,this.max=t,this.resetTTL=s,this.size=0,this.ttl=e,this.#t={hits:0,misses:0,sets:0,deletes:0,evictions:0},this.#e=null}clear(){for(let t=this.first;null!==t;){const e=t.next;t.prev=null,t.next=null,t=e}return this.first=null,this.items=Object.create(null),this.last=null,this.size=0,this.#t.hits=0,this.#t.misses=0,this.#t.sets=0,this.#t.deletes=0,this.#t.evictions=0,this}delete(t){const e=this.items[t];return void 0!==e&&(this.#s(e),this.#t.deletes++),this}entries(t){if(void 0===t){const t=[];for(let e=this.first;null!==e;e=e.next)this.#i(e)||t.push([e.key,e.value]);return t}if(!Array.isArray(t))throw new TypeError("keys must be an array");const e=Array.from({length:t.length});for(let s=0;s0&&this.size===this.max){const t=this.#r();s={key:t.key,value:t.value,expiry:t.expiry}}i=this.items[t]={expiry:this.ttl>0?Date.now()+this.ttl:this.ttl,key:t,prev:this.last,next:null,value:e},1==++this.size?this.first=i:this.last.next=i,this.last=i}else i.value=e,this.resetTTL&&(i.expiry=this.ttl>0?Date.now()+this.ttl:this.ttl),this.moveToEnd(i);return this.#t.sets++,s}set(t,e){let s=this.items[t];return void 0===s||this.#i(s)?(void 0!==s&&this.#s(s),this.max>0&&this.size===this.max&&this.evict(),s=this.items[t]={expiry:this.ttl>0?Date.now()+this.ttl:this.ttl,key:t,prev:this.last,next:null,value:e},1==++this.size?this.first=s:this.last.next=s,this.last=s):(s.value=e,this.resetTTL&&(s.expiry=this.ttl>0?Date.now()+this.ttl:this.ttl),this.moveToEnd(s)),this.#t.sets++,this}values(t){if(void 0===t){const t=[];for(let e=this.first;null!==e;e=e.next)this.#i(e)||t.push(e.value);return t}if(!Array.isArray(t))throw new TypeError("keys must be an array");const e=Array.from({length:t.length});for(let s=0;st?e++:s++;return{valid:e,expired:s,noTTL:0}}keysByTTL(){if(0===this.ttl)return{valid:this.keys(),expired:[],noTTL:this.keys()};const t=Date.now(),e=[],s=[];for(let i=this.first;null!==i;i=i.next)i.expiry>t?e.push(i.key):s.push(i.key);return{valid:e,expired:s,noTTL:[]}}valuesByTTL(){if(0===this.ttl)return{valid:this.values(),expired:[],noTTL:this.values()};const t=Date.now(),e=[],s=[];for(let i=this.first;null!==i;i=i.next)i.expiry>t?e.push(i.value):s.push(i.value);return{valid:e,expired:s,noTTL:[]}}}function e(e=1e3,s=0,i=!1){return new t(e,s,i)}export{t as LRU,e as lru};//# sourceMappingURL=tiny-lru.min.js.map diff --git a/dist/tiny-lru.min.js.map b/dist/tiny-lru.min.js.map index d955cb7..067cb95 100644 --- a/dist/tiny-lru.min.js.map +++ b/dist/tiny-lru.min.js.map @@ -1 +1 @@ -{"version":3,"file":"tiny-lru.min.js","sources":["../src/lru.js"],"sourcesContent":["/**\n * A high-performance Least Recently Used (LRU) cache implementation with optional TTL support.\n * Items are automatically evicted when the cache reaches its maximum size,\n * removing the least recently used items first. All core operations (get, set, delete) are O(1).\n *\n * @class LRU\n */\nexport class LRU {\n\t#stats;\n\t#onEvict;\n\n\t/**\n\t * Creates a new LRU cache instance.\n\t *\n\t * @constructor\n\t * @param {number} [max=0] - Maximum number of items to store. 0 means unlimited.\n\t * @param {number} [ttl=0] - Time to live in milliseconds. 0 means no expiration.\n\t * @param {boolean} [resetTTL=false] - Whether to reset TTL when updating existing items via set().\n\t * @throws {TypeError} When parameters are invalid (negative numbers or wrong types).\n\t */\n\tconstructor(max = 0, ttl = 0, resetTTL = false) {\n\t\tif (!Number.isInteger(max) || max < 0) {\n\t\t\tthrow new TypeError(\"Invalid max value\");\n\t\t}\n\n\t\tif (!Number.isInteger(ttl) || ttl < 0) {\n\t\t\tthrow new TypeError(\"Invalid ttl value\");\n\t\t}\n\n\t\tif (typeof resetTTL !== \"boolean\") {\n\t\t\tthrow new TypeError(\"Invalid resetTTL value\");\n\t\t}\n\n\t\tthis.first = null;\n\t\tthis.items = Object.create(null);\n\t\tthis.last = null;\n\t\tthis.max = max;\n\t\tthis.resetTTL = resetTTL;\n\t\tthis.size = 0;\n\t\tthis.ttl = ttl;\n\t\tthis.#stats = { hits: 0, misses: 0, sets: 0, deletes: 0, evictions: 0 };\n\t\tthis.#onEvict = null;\n\t}\n\n\t/**\n\t * Removes all items from the cache.\n\t *\n\t * @returns {LRU} The LRU instance for method chaining.\n\t */\n\tclear() {\n\t\tfor (let x = this.first; x !== null;) {\n\t\t\tconst next = x.next;\n\t\t\tx.prev = null;\n\t\t\tx.next = null;\n\t\t\tx = next;\n\t\t}\n\n\t\tthis.first = null;\n\t\tthis.items = Object.create(null);\n\t\tthis.last = null;\n\t\tthis.size = 0;\n\t\tthis.#stats.hits = 0;\n\t\tthis.#stats.misses = 0;\n\t\tthis.#stats.sets = 0;\n\t\tthis.#stats.deletes = 0;\n\t\tthis.#stats.evictions = 0;\n\n\t\treturn this;\n\t}\n\n\t/**\n\t * Removes an item from the cache by key.\n\t *\n\t * @param {string} key - The key of the item to delete.\n\t * @returns {LRU} The LRU instance for method chaining.\n\t */\n\tdelete(key) {\n\t\tconst item = this.items[key];\n\n\t\tif (item !== undefined) {\n\t\t\tthis.#removeItem(item);\n\t\t\tthis.#stats.deletes++;\n\t\t}\n\n\t\treturn this;\n\t}\n\n\t/**\n\t * Returns an array of [key, value] pairs for the specified keys.\n\t * When no keys provided, returns all entries in LRU order.\n\t * When keys provided, order matches the input array.\n\t *\n\t * @param {string[]} [keys=this.keys()] - Array of keys to get entries for. Defaults to all keys.\n\t * @returns {Array>} Array of [key, value] pairs.\n\t */\n\tentries(keys) {\n\t\tif (keys === undefined) {\n\t\t\tconst result = [];\n\t\t\tthis.#forEachLive((x) => result.push([x.key, x.value]));\n\n\t\t\treturn result;\n\t\t}\n\n\t\tif (!Array.isArray(keys)) {\n\t\t\tthrow new TypeError(\"keys must be an array\");\n\t\t}\n\n\t\tconst result = Array.from({ length: keys.length });\n\t\tfor (let i = 0; i < keys.length; i++) {\n\t\t\tconst key = keys[i];\n\t\t\tconst item = this.items[key];\n\t\t\tresult[i] = [key, item !== undefined && !this.#isExpired(item) ? item.value : undefined];\n\t\t}\n\n\t\treturn result;\n\t}\n\n\t/**\n\t * Removes the least recently used item from the cache.\n\t *\n\t * @returns {LRU} The LRU instance for method chaining.\n\t */\n\tevict() {\n\t\tif (this.size === 0) {\n\t\t\treturn this;\n\t\t}\n\n\t\tconst item = this.#evictItem();\n\n\t\tif (this.#onEvict !== null) {\n\t\t\tthis.#onEvict({\n\t\t\t\tkey: item.key,\n\t\t\t\tvalue: item.value,\n\t\t\t\texpiry: item.expiry,\n\t\t\t});\n\t\t}\n\n\t\treturn this;\n\t}\n\n\t/**\n\t * Returns the expiration timestamp for a given key.\n\t *\n\t * @param {string} key - The key to check expiration for.\n\t * @returns {number|undefined} The expiration timestamp in milliseconds, or undefined if key doesn't exist.\n\t */\n\texpiresAt(key) {\n\t\tconst item = this.items[key];\n\t\treturn item !== undefined ? item.expiry : undefined;\n\t}\n\n\t/**\n\t * Checks if an item has expired.\n\t *\n\t * @param {Object} item - The cache item to check.\n\t * @returns {boolean} True if the item has expired, false otherwise.\n\t * @private\n\t */\n\t#isExpired(item) {\n\t\tif (this.ttl === 0) {\n\t\t\treturn false;\n\t\t}\n\n\t\treturn item.expiry <= Date.now();\n\t}\n\n\t/**\n\t * Retrieves a value from the cache by key without updating LRU order.\n\t * Note: Does not perform TTL checks or remove expired items.\n\t *\n\t * @param {string} key - The key to retrieve.\n\t * @returns {*} The value associated with the key, or undefined if not found.\n\t */\n\tpeek(key) {\n\t\tconst item = this.items[key];\n\t\treturn item !== undefined ? item.value : undefined;\n\t}\n\n\t/**\n\t * Retrieves a value from the cache by key. Updates the item's position to most recently used.\n\t *\n\t * @param {string} key - The key to retrieve.\n\t * @returns {*} The value associated with the key, or undefined if not found or expired.\n\t */\n\tget(key) {\n\t\tconst item = this.items[key];\n\n\t\tif (item !== undefined) {\n\t\t\tif (!this.#isExpired(item)) {\n\t\t\t\tthis.moveToEnd(item);\n\t\t\t\tthis.#stats.hits++;\n\t\t\t\treturn item.value;\n\t\t\t}\n\n\t\t\tthis.#removeItem(item);\n\t\t\tthis.#stats.misses++;\n\t\t\treturn undefined;\n\t\t}\n\n\t\tthis.#stats.misses++;\n\t\treturn undefined;\n\t}\n\n\t/**\n\t * Checks if a key exists in the cache.\n\t * Expired items are removed before returning false.\n\t *\n\t * @param {string} key - The key to check for.\n\t * @returns {boolean} True if the key exists and is not expired, false otherwise.\n\t */\n\thas(key) {\n\t\tconst item = this.items[key];\n\n\t\tif (item !== undefined && this.#isExpired(item)) {\n\t\t\tthis.#removeItem(item);\n\t\t\treturn false;\n\t\t}\n\n\t\treturn item !== undefined;\n\t}\n\n\t/**\n\t * Unlinks an item from the doubly-linked list.\n\t * Updates first/last pointers if needed.\n\t * Does NOT clear the item's prev/next pointers or delete from items map.\n\t *\n\t * @private\n\t */\n\t#unlink(item) {\n\t\tif (item.prev !== null) {\n\t\t\titem.prev.next = item.next;\n\t\t}\n\n\t\tif (item.next !== null) {\n\t\t\titem.next.prev = item.prev;\n\t\t}\n\n\t\tif (this.first === item) {\n\t\t\tthis.first = item.next;\n\t\t}\n\n\t\tif (this.last === item) {\n\t\t\tthis.last = item.prev;\n\t\t}\n\t}\n\n\t/**\n\t * Removes an item from the cache without incrementing the deletes stat.\n\t * Used internally by get()/has() when removing expired items.\n\t *\n\t * @param {Object} item - The cache item to remove.\n\t * @private\n\t */\n\t#removeItem(item) {\n\t\tdelete this.items[item.key];\n\t\tthis.size--;\n\t\tthis.#unlink(item);\n\t\titem.prev = null;\n\t\titem.next = null;\n\t}\n\n\t/**\n\t * Evicts the least recently used item from the cache without firing onEvict.\n\t * Used internally by setWithEvicted() to avoid double-notification.\n\t *\n\t * @returns {Object} The evicted item.\n\t * @private\n\t */\n\t#evictItem() {\n\t\tconst item = this.first;\n\n\t\tdelete this.items[item.key];\n\t\tthis.#stats.evictions++;\n\n\t\tif (--this.size === 0) {\n\t\t\tthis.first = null;\n\t\t\tthis.last = null;\n\t\t} else {\n\t\t\tthis.#unlink(item);\n\t\t}\n\n\t\titem.prev = null;\n\t\titem.next = null;\n\n\t\treturn item;\n\t}\n\n\t/**\n\t * Creates and links a new item at the most recently used position.\n\t * Wires the first/last pointers and returns the created item.\n\t *\n\t * @param {string} key - The key to store.\n\t * @param {*} value - The value to store.\n\t * @returns {Object} The created and linked item.\n\t * @private\n\t */\n\t#insertItem(key, value) {\n\t\tconst item = (this.items[key] = {\n\t\t\texpiry: this.ttl > 0 ? Date.now() + this.ttl : this.ttl,\n\t\t\tkey: key,\n\t\t\tprev: this.last,\n\t\t\tnext: null,\n\t\t\tvalue,\n\t\t});\n\n\t\tif (++this.size === 1) {\n\t\t\tthis.first = item;\n\t\t} else {\n\t\t\tthis.last.next = item;\n\t\t}\n\n\t\tthis.last = item;\n\n\t\treturn item;\n\t}\n\n\t/**\n\t * Invokes a callback for each live (non-expired) item in LRU order.\n\t * Accesses items directly without calling get(), so LRU order is not\n\t * mutated and expired items are not deleted mid-iteration.\n\t *\n\t * @param {function(Object): void} callback - Function to call for each live item.\n\t * @private\n\t */\n\t#forEachLive(callback) {\n\t\tfor (let x = this.first; x !== null; x = x.next) {\n\t\t\tif (!this.#isExpired(x)) {\n\t\t\t\tcallback(x);\n\t\t\t}\n\t\t}\n\t}\n\n\t/**\n\t * Efficiently moves an item to the end of the LRU list (most recently used position).\n\t * This is an internal optimization method that avoids the overhead of the full set() operation\n\t * when only LRU position needs to be updated.\n\t *\n\t * @param {Object} item - The cache item with prev/next pointers to reposition.\n\t * @private\n\t */\n\tmoveToEnd(item) {\n\t\tif (this.last === item) {\n\t\t\treturn;\n\t\t}\n\n\t\tthis.#unlink(item);\n\n\t\titem.prev = this.last;\n\t\titem.next = null;\n\t\tthis.last.next = item;\n\t\tthis.last = item;\n\t}\n\n\t/**\n\t * Returns an array of all keys in the cache, ordered from least to most recently used.\n\t * Expired items are skipped, consistent with entries(), values(), and toJSON().\n\t *\n\t * @returns {string[]} Array of keys in LRU order.\n\t */\n\tkeys() {\n\t\tconst result = [];\n\t\tthis.#forEachLive((x) => result.push(x.key));\n\n\t\treturn result;\n\t}\n\n\t/**\n\t * Sets a value in the cache and returns any evicted item.\n\t * Eviction is silent — onEvict is not fired for the returned item.\n\t *\n\t * @param {string} key - The key to set.\n\t * @param {*} value - The value to store.\n\t * @returns {Object|null} The evicted item (if any) with shape {key, value, expiry}, or null.\n\t */\n\tsetWithEvicted(key, value) {\n\t\tlet evicted = null;\n\t\tlet item = this.items[key];\n\n\t\tif (item !== undefined && !this.#isExpired(item)) {\n\t\t\titem.value = value;\n\t\t\tif (this.resetTTL) {\n\t\t\t\titem.expiry = this.ttl > 0 ? Date.now() + this.ttl : this.ttl;\n\t\t\t}\n\t\t\tthis.moveToEnd(item);\n\t\t} else {\n\t\t\tif (item !== undefined) {\n\t\t\t\tthis.#removeItem(item);\n\t\t\t}\n\n\t\t\tif (this.max > 0 && this.size === this.max) {\n\t\t\t\tconst evictedItem = this.#evictItem();\n\t\t\t\tevicted = {\n\t\t\t\t\tkey: evictedItem.key,\n\t\t\t\t\tvalue: evictedItem.value,\n\t\t\t\t\texpiry: evictedItem.expiry,\n\t\t\t\t};\n\t\t\t}\n\n\t\t\titem = this.#insertItem(key, value);\n\t\t}\n\n\t\tthis.#stats.sets++;\n\t\treturn evicted;\n\t}\n\n\t/**\n\t * Sets a value in the cache. Updates the item's position to most recently used.\n\t *\n\t * @param {string} key - The key to set.\n\t * @param {*} value - The value to store.\n\t * @returns {LRU} The LRU instance for method chaining.\n\t */\n\tset(key, value) {\n\t\tlet item = this.items[key];\n\n\t\tif (item !== undefined && !this.#isExpired(item)) {\n\t\t\titem.value = value;\n\n\t\t\tif (this.resetTTL) {\n\t\t\t\titem.expiry = this.ttl > 0 ? Date.now() + this.ttl : this.ttl;\n\t\t\t}\n\n\t\t\tthis.moveToEnd(item);\n\t\t} else {\n\t\t\tif (item !== undefined) {\n\t\t\t\tthis.#removeItem(item);\n\t\t\t}\n\n\t\t\tif (this.max > 0 && this.size === this.max) {\n\t\t\t\tthis.evict();\n\t\t\t}\n\n\t\t\titem = this.#insertItem(key, value);\n\t\t}\n\n\t\tthis.#stats.sets++;\n\n\t\treturn this;\n\t}\n\n\t/**\n\t * Returns an array of all values in the cache for the specified keys.\n\t * When no keys provided, returns all values in LRU order.\n\t * When keys provided, order matches the input array.\n\t *\n\t * @param {string[]} [keys] - Array of keys to get values for. Defaults to all keys.\n\t * @returns {Array<*>} Array of values corresponding to the keys.\n\t */\n\tvalues(keys) {\n\t\tif (keys === undefined) {\n\t\t\tconst result = [];\n\t\t\tfor (let x = this.first; x !== null; x = x.next) {\n\t\t\t\tif (!this.#isExpired(x)) {\n\t\t\t\t\tresult.push(x.value);\n\t\t\t\t}\n\t\t\t}\n\n\t\t\treturn result;\n\t\t}\n\n\t\tif (!Array.isArray(keys)) {\n\t\t\tthrow new TypeError(\"keys must be an array\");\n\t\t}\n\n\t\tconst result = Array.from({ length: keys.length });\n\t\tfor (let i = 0; i < keys.length; i++) {\n\t\t\tconst item = this.items[keys[i]];\n\t\t\tresult[i] = item !== undefined && !this.#isExpired(item) ? item.value : undefined;\n\t\t}\n\n\t\treturn result;\n\t}\n\n\t/**\n\t * Iterate over cache items in LRU order (least to most recent).\n\t * Note: This method directly accesses items from the linked list without calling\n\t * get() or peek(), so it does not update LRU order. Expired items are skipped.\n\t *\n\t * @param {function(*, any, LRU): void} callback - Function to call for each item. Signature: callback(value, key, cache)\n\t * @param {Object} [thisArg] - Value to use as `this` when executing callback.\n\t * @returns {LRU} The LRU instance for method chaining.\n\t */\n\tforEach(callback, thisArg) {\n\t\tfor (let x = this.first; x !== null;) {\n\t\t\tconst next = x.next;\n\t\t\tif (!this.#isExpired(x)) {\n\t\t\t\tcallback.call(thisArg, x.value, x.key, this);\n\t\t\t}\n\t\t\tx = next;\n\t\t}\n\n\t\treturn this;\n\t}\n\n\t/**\n\t * Batch retrieve multiple items.\n\t *\n\t * @param {string[]} keys - Array of keys to retrieve.\n\t * @returns {Object} Object mapping keys to values (undefined for missing/expired keys).\n\t */\n\tgetMany(keys) {\n\t\tif (!Array.isArray(keys)) {\n\t\t\tthrow new TypeError(\"keys must be an array\");\n\t\t}\n\n\t\tconst result = Object.create(null);\n\t\tfor (let i = 0; i < keys.length; i++) {\n\t\t\tconst key = keys[i];\n\t\t\tresult[key] = this.get(key);\n\t\t}\n\n\t\treturn result;\n\t}\n\n\t/**\n\t * Batch existence check - returns true if ALL keys exist.\n\t *\n\t * @param {string[]} keys - Array of keys to check.\n\t * @returns {boolean} True if all keys exist and are not expired.\n\t */\n\thasAll(keys) {\n\t\tif (!Array.isArray(keys)) {\n\t\t\tthrow new TypeError(\"keys must be an array\");\n\t\t}\n\n\t\tfor (let i = 0; i < keys.length; i++) {\n\t\t\tif (!this.has(keys[i])) {\n\t\t\t\treturn false;\n\t\t\t}\n\t\t}\n\n\t\treturn true;\n\t}\n\n\t/**\n\t * Batch existence check - returns true if ANY key exists.\n\t *\n\t * @param {string[]} keys - Array of keys to check.\n\t * @returns {boolean} True if any key exists and is not expired.\n\t */\n\thasAny(keys) {\n\t\tif (!Array.isArray(keys)) {\n\t\t\tthrow new TypeError(\"keys must be an array\");\n\t\t}\n\n\t\tfor (let i = 0; i < keys.length; i++) {\n\t\t\tif (this.has(keys[i])) {\n\t\t\t\treturn true;\n\t\t\t}\n\t\t}\n\n\t\treturn false;\n\t}\n\n\t/**\n\t * Remove expired items without affecting LRU order.\n\t * Unlike get(), this does not move items to the end.\n\t *\n\t * @param {boolean} [fireOnEvict=false] - When true, invokes the onEvict callback for each removed item.\n\t * @returns {number} Number of expired items removed.\n\t */\n\tcleanup(fireOnEvict = false) {\n\t\tif (this.ttl === 0 || this.size === 0) {\n\t\t\treturn 0;\n\t\t}\n\n\t\tlet removed = 0;\n\n\t\tfor (let x = this.first; x !== null;) {\n\t\t\tconst next = x.next;\n\t\t\tif (this.#isExpired(x)) {\n\t\t\t\tconst key = x.key;\n\t\t\t\tif (this.items[key] !== undefined) {\n\t\t\t\t\tdelete this.items[key];\n\t\t\t\t\tthis.size--;\n\t\t\t\t\tremoved++;\n\t\t\t\t\tthis.#unlink(x);\n\t\t\t\t\tx.prev = null;\n\t\t\t\t\tx.next = null;\n\t\t\t\t\tif (fireOnEvict && this.#onEvict !== null) {\n\t\t\t\t\t\tthis.#onEvict({\n\t\t\t\t\t\t\tkey: x.key,\n\t\t\t\t\t\t\tvalue: x.value,\n\t\t\t\t\t\t\texpiry: x.expiry,\n\t\t\t\t\t\t});\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t}\n\t\t\tx = next;\n\t\t}\n\n\t\treturn removed;\n\t}\n\n\t/**\n\t * Serialize cache to JSON-compatible format.\n\t *\n\t * @returns {Array<{key: any, value: *, expiry: number}>} Array of cache items.\n\t */\n\ttoJSON() {\n\t\tconst result = [];\n\t\tfor (let x = this.first; x !== null; x = x.next) {\n\t\t\tif (!this.#isExpired(x)) {\n\t\t\t\tresult.push({\n\t\t\t\t\tkey: x.key,\n\t\t\t\t\tvalue: x.value,\n\t\t\t\t\texpiry: x.expiry,\n\t\t\t\t});\n\t\t\t}\n\t\t}\n\n\t\treturn result;\n\t}\n\n\t/**\n\t * Get cache statistics.\n\t *\n\t * @returns {Object} Statistics object with hits, misses, sets, deletes, evictions counts.\n\t */\n\tstats() {\n\t\treturn { ...this.#stats };\n\t}\n\n\t/**\n\t * Register callback for evicted items.\n\t *\n\t * @param {function(Object): void} callback - Function called when item is evicted. Receives {key, value, expiry}.\n\t * @returns {LRU} The LRU instance for method chaining.\n\t */\n\tonEvict(callback) {\n\t\tif (typeof callback !== \"function\") {\n\t\t\tthrow new TypeError(\"onEvict callback must be a function\");\n\t\t}\n\n\t\tthis.#onEvict = callback;\n\n\t\treturn this;\n\t}\n\n\t/**\n\t * Get counts of items by TTL status.\n\t *\n\t * @returns {Object} Object with valid, expired, and noTTL counts.\n\t */\n\tsizeByTTL() {\n\t\tif (this.ttl === 0) {\n\t\t\treturn { valid: this.size, expired: 0, noTTL: this.size };\n\t\t}\n\n\t\tconst now = Date.now();\n\t\tlet valid = 0;\n\t\tlet expired = 0;\n\n\t\tfor (let x = this.first; x !== null; x = x.next) {\n\t\t\tif (x.expiry > now) {\n\t\t\t\tvalid++;\n\t\t\t} else {\n\t\t\t\texpired++;\n\t\t\t}\n\t\t}\n\n\t\treturn { valid, expired, noTTL: 0 };\n\t}\n\n\t/**\n\t * Get keys filtered by TTL status.\n\t *\n\t * @returns {Object} Object with valid, expired, and noTTL arrays of keys.\n\t */\n\tkeysByTTL() {\n\t\tif (this.ttl === 0) {\n\t\t\treturn { valid: this.keys(), expired: [], noTTL: this.keys() };\n\t\t}\n\n\t\tconst now = Date.now();\n\t\tconst valid = [];\n\t\tconst expired = [];\n\n\t\tfor (let x = this.first; x !== null; x = x.next) {\n\t\t\tif (x.expiry > now) {\n\t\t\t\tvalid.push(x.key);\n\t\t\t} else {\n\t\t\t\texpired.push(x.key);\n\t\t\t}\n\t\t}\n\n\t\treturn { valid, expired, noTTL: [] };\n\t}\n\n\t/**\n\t * Get values filtered by TTL status.\n\t *\n\t * @returns {Object} Object with valid, expired, and noTTL arrays of values.\n\t */\n\tvaluesByTTL() {\n\t\tif (this.ttl === 0) {\n\t\t\treturn { valid: this.values(), expired: [], noTTL: this.values() };\n\t\t}\n\n\t\tconst now = Date.now();\n\t\tconst valid = [];\n\t\tconst expired = [];\n\n\t\tfor (let x = this.first; x !== null; x = x.next) {\n\t\t\tif (x.expiry > now) {\n\t\t\t\tvalid.push(x.value);\n\t\t\t} else {\n\t\t\t\texpired.push(x.value);\n\t\t\t}\n\t\t}\n\n\t\treturn { valid, expired, noTTL: [] };\n\t}\n}\n\n/**\n * Factory function to create a new LRU cache instance with parameter validation.\n *\n * @function lru\n * @param {number} [max=1000] - Maximum number of items to store. Must be >= 0. Use 0 for unlimited size.\n * @param {number} [ttl=0] - Time to live in milliseconds. Must be >= 0. Use 0 for no expiration.\n * @param {boolean} [resetTTL=false] - Whether to reset TTL when updating existing items via set().\n * @returns {LRU} A new LRU cache instance.\n * @throws {TypeError} When parameters are invalid (negative numbers or wrong types).\n */\nexport function lru(max = 1000, ttl = 0, resetTTL = false) {\n\treturn new LRU(max, ttl, resetTTL);\n}\n"],"names":["LRU","stats","onEvict","constructor","max","ttl","resetTTL","Number","isInteger","TypeError","this","first","items","Object","create","last","size","hits","misses","sets","deletes","evictions","clear","x","next","prev","key","item","undefined","removeItem","entries","keys","result","forEachLive","push","value","Array","isArray","from","length","i","isExpired","evict","evictItem","expiry","expiresAt","Date","now","peek","get","moveToEnd","has","unlink","insertItem","callback","setWithEvicted","evicted","evictedItem","set","values","forEach","thisArg","call","getMany","hasAll","hasAny","cleanup","fireOnEvict","removed","toJSON","sizeByTTL","valid","expired","noTTL","keysByTTL","valuesByTTL","lru"],"mappings":";;;;AAOO,MAAMA,EACZC,GACAC,GAWA,WAAAC,CAAYC,EAAM,EAAGC,EAAM,EAAGC,GAAW,GACxC,IAAKC,OAAOC,UAAUJ,IAAQA,EAAM,EACnC,MAAM,IAAIK,UAAU,qBAGrB,IAAKF,OAAOC,UAAUH,IAAQA,EAAM,EACnC,MAAM,IAAII,UAAU,qBAGrB,GAAwB,kBAAbH,EACV,MAAM,IAAIG,UAAU,0BAGrBC,KAAKC,MAAQ,KACbD,KAAKE,MAAQC,OAAOC,OAAO,MAC3BJ,KAAKK,KAAO,KACZL,KAAKN,IAAMA,EACXM,KAAKJ,SAAWA,EAChBI,KAAKM,KAAO,EACZN,KAAKL,IAAMA,EACXK,MAAKT,EAAS,CAAEgB,KAAM,EAAGC,OAAQ,EAAGC,KAAM,EAAGC,QAAS,EAAGC,UAAW,GACpEX,MAAKR,EAAW,IACjB,CAOA,KAAAoB,GACC,IAAK,IAAIC,EAAIb,KAAKC,MAAa,OAANY,GAAa,CACrC,MAAMC,EAAOD,EAAEC,KACfD,EAAEE,KAAO,KACTF,EAAEC,KAAO,KACTD,EAAIC,CACL,CAYA,OAVAd,KAAKC,MAAQ,KACbD,KAAKE,MAAQC,OAAOC,OAAO,MAC3BJ,KAAKK,KAAO,KACZL,KAAKM,KAAO,EACZN,MAAKT,EAAOgB,KAAO,EACnBP,MAAKT,EAAOiB,OAAS,EACrBR,MAAKT,EAAOkB,KAAO,EACnBT,MAAKT,EAAOmB,QAAU,EACtBV,MAAKT,EAAOoB,UAAY,EAEjBX,IACR,CAQA,OAAOgB,GACN,MAAMC,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C,EAAKC,EAC1B,QAAAN,SAAA4E"} \ No newline at end of file +{"version":3,"file":"tiny-lru.min.js","sources":["../src/lru.js"],"sourcesContent":["/**\n * A high-performance Least Recently Used (LRU) cache implementation with optional TTL support.\n * Items are automatically evicted when the cache reaches its maximum size,\n * removing the least recently used items first. All core operations (get, set, delete) are O(1).\n *\n * @class LRU\n */\nexport class LRU {\n\t#stats;\n\t#onEvict;\n\n\t/**\n\t * Creates a new LRU cache instance.\n\t *\n\t * @constructor\n\t * @param {number} [max=0] - Maximum number of items to store. 0 means unlimited.\n\t * @param {number} [ttl=0] - Time to live in milliseconds. 0 means no expiration.\n\t * @param {boolean} [resetTTL=false] - Whether to reset TTL when updating existing items via set().\n\t * @throws {TypeError} When parameters are invalid (negative numbers or wrong types).\n\t */\n\tconstructor(max = 0, ttl = 0, resetTTL = false) {\n\t\tif (!Number.isInteger(max) || max < 0) {\n\t\t\tthrow new TypeError(\"Invalid max value\");\n\t\t}\n\n\t\tif (!Number.isInteger(ttl) || ttl < 0) {\n\t\t\tthrow new TypeError(\"Invalid ttl value\");\n\t\t}\n\n\t\tif (typeof resetTTL !== \"boolean\") {\n\t\t\tthrow new TypeError(\"Invalid resetTTL value\");\n\t\t}\n\n\t\tthis.first = null;\n\t\tthis.items = Object.create(null);\n\t\tthis.last = null;\n\t\tthis.max = max;\n\t\tthis.resetTTL = resetTTL;\n\t\tthis.size = 0;\n\t\tthis.ttl = ttl;\n\t\tthis.#stats = { hits: 0, misses: 0, sets: 0, deletes: 0, evictions: 0 };\n\t\tthis.#onEvict = null;\n\t}\n\n\t/**\n\t * Removes all items from the cache.\n\t *\n\t * @returns {LRU} The LRU instance for method chaining.\n\t */\n\tclear() {\n\t\tfor (let x = this.first; x !== null;) {\n\t\t\tconst next = x.next;\n\t\t\tx.prev = null;\n\t\t\tx.next = null;\n\t\t\tx = next;\n\t\t}\n\n\t\tthis.first = null;\n\t\tthis.items = Object.create(null);\n\t\tthis.last = null;\n\t\tthis.size = 0;\n\t\tthis.#stats.hits = 0;\n\t\tthis.#stats.misses = 0;\n\t\tthis.#stats.sets = 0;\n\t\tthis.#stats.deletes = 0;\n\t\tthis.#stats.evictions = 0;\n\n\t\treturn this;\n\t}\n\n\t/**\n\t * Removes an item from the cache by key.\n\t *\n\t * @param {string} key - The key of the item to delete.\n\t * @returns {LRU} The LRU instance for method chaining.\n\t */\n\tdelete(key) {\n\t\tconst item = this.items[key];\n\n\t\tif (item !== undefined) {\n\t\t\tthis.#removeItem(item);\n\t\t\tthis.#stats.deletes++;\n\t\t}\n\n\t\treturn this;\n\t}\n\n\t/**\n\t * Returns an array of [key, value] pairs for the specified keys.\n\t * When no keys provided, returns all entries in LRU order.\n\t * When keys provided, order matches the input array.\n\t *\n\t * @param {string[]} [keys=this.keys()] - Array of keys to get entries for. Defaults to all keys.\n\t * @returns {Array>} Array of [key, value] pairs.\n\t */\n\tentries(keys) {\n\t\tif (keys === undefined) {\n\t\t\tconst result = [];\n\t\t\tfor (let x = this.first; x !== null; x = x.next) {\n\t\t\t\tif (!this.#isExpired(x)) {\n\t\t\t\t\tresult.push([x.key, x.value]);\n\t\t\t\t}\n\t\t\t}\n\n\t\t\treturn result;\n\t\t}\n\n\t\tif (!Array.isArray(keys)) {\n\t\t\tthrow new TypeError(\"keys must be an array\");\n\t\t}\n\n\t\tconst result = Array.from({ length: keys.length });\n\t\tfor (let i = 0; i < keys.length; i++) {\n\t\t\tconst key = keys[i];\n\t\t\tconst item = this.items[key];\n\t\t\tresult[i] = [key, item !== undefined && !this.#isExpired(item) ? item.value : undefined];\n\t\t}\n\n\t\treturn result;\n\t}\n\n\t/**\n\t * Removes the least recently used item from the cache.\n\t *\n\t * @returns {LRU} The LRU instance for method chaining.\n\t */\n\tevict() {\n\t\tif (this.size === 0) {\n\t\t\treturn this;\n\t\t}\n\n\t\tconst item = this.#evictItem();\n\n\t\tif (this.#onEvict !== null) {\n\t\t\tthis.#onEvict({\n\t\t\t\tkey: item.key,\n\t\t\t\tvalue: item.value,\n\t\t\t\texpiry: item.expiry,\n\t\t\t});\n\t\t}\n\n\t\treturn this;\n\t}\n\n\t/**\n\t * Returns the expiration timestamp for a given key.\n\t *\n\t * @param {string} key - The key to check expiration for.\n\t * @returns {number|undefined} The expiration timestamp in milliseconds, or undefined if key doesn't exist.\n\t */\n\texpiresAt(key) {\n\t\tconst item = this.items[key];\n\t\treturn item !== undefined ? item.expiry : undefined;\n\t}\n\n\t/**\n\t * Checks if an item has expired.\n\t *\n\t * @param {Object} item - The cache item to check.\n\t * @returns {boolean} True if the item has expired, false otherwise.\n\t * @private\n\t */\n\t#isExpired(item) {\n\t\tif (this.ttl === 0) {\n\t\t\treturn false;\n\t\t}\n\n\t\treturn item.expiry <= Date.now();\n\t}\n\n\t/**\n\t * Retrieves a value from the cache by key without updating LRU order.\n\t * Note: Does not perform TTL checks or remove expired items.\n\t *\n\t * @param {string} key - The key to retrieve.\n\t * @returns {*} The value associated with the key, or undefined if not found.\n\t */\n\tpeek(key) {\n\t\tconst item = this.items[key];\n\t\treturn item !== undefined ? item.value : undefined;\n\t}\n\n\t/**\n\t * Retrieves a value from the cache by key. Updates the item's position to most recently used.\n\t *\n\t * @param {string} key - The key to retrieve.\n\t * @returns {*} The value associated with the key, or undefined if not found or expired.\n\t */\n\tget(key) {\n\t\tconst item = this.items[key];\n\n\t\tif (item !== undefined) {\n\t\t\tif (!this.#isExpired(item)) {\n\t\t\t\tthis.moveToEnd(item);\n\t\t\t\tthis.#stats.hits++;\n\t\t\t\treturn item.value;\n\t\t\t}\n\n\t\t\tthis.#removeItem(item);\n\t\t\tthis.#stats.misses++;\n\t\t\treturn undefined;\n\t\t}\n\n\t\tthis.#stats.misses++;\n\t\treturn undefined;\n\t}\n\n\t/**\n\t * Checks if a key exists in the cache.\n\t * Expired items are removed before returning false.\n\t *\n\t * @param {string} key - The key to check for.\n\t * @returns {boolean} True if the key exists and is not expired, false otherwise.\n\t */\n\thas(key) {\n\t\tconst item = this.items[key];\n\n\t\tif (item !== undefined && this.#isExpired(item)) {\n\t\t\tthis.#removeItem(item);\n\t\t\treturn false;\n\t\t}\n\n\t\treturn item !== undefined;\n\t}\n\n\t/**\n\t * Unlinks an item from the doubly-linked list.\n\t * Updates first/last pointers if needed.\n\t * Does NOT clear the item's prev/next pointers or delete from items map.\n\t *\n\t * @private\n\t */\n\t#unlink(item) {\n\t\tif (item.prev !== null) {\n\t\t\titem.prev.next = item.next;\n\t\t}\n\n\t\tif (item.next !== null) {\n\t\t\titem.next.prev = item.prev;\n\t\t}\n\n\t\tif (this.first === item) {\n\t\t\tthis.first = item.next;\n\t\t}\n\n\t\tif (this.last === item) {\n\t\t\tthis.last = item.prev;\n\t\t}\n\t}\n\n\t/**\n\t * Removes an item from the cache without incrementing the deletes stat.\n\t * Used internally by get()/has() when removing expired items.\n\t *\n\t * @param {Object} item - The cache item to remove.\n\t * @private\n\t */\n\t#removeItem(item) {\n\t\tdelete this.items[item.key];\n\t\tthis.size--;\n\t\tthis.#unlink(item);\n\t\titem.prev = null;\n\t\titem.next = null;\n\t}\n\n\t/**\n\t * Evicts the least recently used item from the cache without firing onEvict.\n\t * Used internally by setWithEvicted() to avoid double-notification.\n\t *\n\t * @returns {Object} The evicted item.\n\t * @private\n\t */\n\t#evictItem() {\n\t\tconst item = this.first;\n\n\t\tdelete this.items[item.key];\n\t\tthis.#stats.evictions++;\n\n\t\tif (--this.size === 0) {\n\t\t\tthis.first = null;\n\t\t\tthis.last = null;\n\t\t} else {\n\t\t\tthis.#unlink(item);\n\t\t}\n\n\t\titem.prev = null;\n\t\titem.next = null;\n\n\t\treturn item;\n\t}\n\n\t/**\n\t * Efficiently moves an item to the end of the LRU list (most recently used position).\n\t * This is an internal optimization method that avoids the overhead of the full set() operation\n\t * when only LRU position needs to be updated.\n\t *\n\t * @param {Object} item - The cache item with prev/next pointers to reposition.\n\t * @private\n\t */\n\tmoveToEnd(item) {\n\t\tif (this.last === item) {\n\t\t\treturn;\n\t\t}\n\n\t\tthis.#unlink(item);\n\n\t\titem.prev = this.last;\n\t\titem.next = null;\n\t\tthis.last.next = item;\n\t\tthis.last = item;\n\t}\n\n\t/**\n\t * Returns an array of all keys in the cache, ordered from least to most recently used.\n\t * Expired items are skipped, consistent with entries(), values(), and toJSON().\n\t *\n\t * @returns {string[]} Array of keys in LRU order.\n\t */\n\tkeys() {\n\t\tconst result = [];\n\t\tfor (let x = this.first; x !== null; x = x.next) {\n\t\t\tif (!this.#isExpired(x)) {\n\t\t\t\tresult.push(x.key);\n\t\t\t}\n\t\t}\n\n\t\treturn result;\n\t}\n\n\t/**\n\t * Sets a value in the cache and returns any evicted item.\n\t * Eviction is silent — onEvict is not fired for the returned item.\n\t *\n\t * @param {string} key - The key to set.\n\t * @param {*} value - The value to store.\n\t * @returns {Object|null} The evicted item (if any) with shape {key, value, expiry}, or null.\n\t */\n\tsetWithEvicted(key, value) {\n\t\tlet evicted = null;\n\t\tlet item = this.items[key];\n\n\t\tif (item !== undefined && !this.#isExpired(item)) {\n\t\t\titem.value = value;\n\t\t\tif (this.resetTTL) {\n\t\t\t\titem.expiry = this.ttl > 0 ? Date.now() + this.ttl : this.ttl;\n\t\t\t}\n\t\t\tthis.moveToEnd(item);\n\t\t} else {\n\t\t\tif (item !== undefined) {\n\t\t\t\tthis.#removeItem(item);\n\t\t\t}\n\n\t\t\tif (this.max > 0 && this.size === this.max) {\n\t\t\t\tconst evictedItem = this.#evictItem();\n\t\t\t\tevicted = {\n\t\t\t\t\tkey: evictedItem.key,\n\t\t\t\t\tvalue: evictedItem.value,\n\t\t\t\t\texpiry: evictedItem.expiry,\n\t\t\t\t};\n\t\t\t}\n\n\t\t\titem = this.items[key] = {\n\t\t\t\texpiry: this.ttl > 0 ? Date.now() + this.ttl : this.ttl,\n\t\t\t\tkey: key,\n\t\t\t\tprev: this.last,\n\t\t\t\tnext: null,\n\t\t\t\tvalue,\n\t\t\t};\n\n\t\t\tif (++this.size === 1) {\n\t\t\t\tthis.first = item;\n\t\t\t} else {\n\t\t\t\tthis.last.next = item;\n\t\t\t}\n\n\t\t\tthis.last = item;\n\t\t}\n\n\t\tthis.#stats.sets++;\n\t\treturn evicted;\n\t}\n\n\t/**\n\t * Sets a value in the cache. Updates the item's position to most recently used.\n\t *\n\t * @param {string} key - The key to set.\n\t * @param {*} value - The value to store.\n\t * @returns {LRU} The LRU instance for method chaining.\n\t */\n\tset(key, value) {\n\t\tlet item = this.items[key];\n\n\t\tif (item !== undefined && !this.#isExpired(item)) {\n\t\t\titem.value = value;\n\n\t\t\tif (this.resetTTL) {\n\t\t\t\titem.expiry = this.ttl > 0 ? Date.now() + this.ttl : this.ttl;\n\t\t\t}\n\n\t\t\tthis.moveToEnd(item);\n\t\t} else {\n\t\t\tif (item !== undefined) {\n\t\t\t\tthis.#removeItem(item);\n\t\t\t}\n\n\t\t\tif (this.max > 0 && this.size === this.max) {\n\t\t\t\tthis.evict();\n\t\t\t}\n\n\t\t\titem = this.items[key] = {\n\t\t\t\texpiry: this.ttl > 0 ? Date.now() + this.ttl : this.ttl,\n\t\t\t\tkey: key,\n\t\t\t\tprev: this.last,\n\t\t\t\tnext: null,\n\t\t\t\tvalue,\n\t\t\t};\n\n\t\t\tif (++this.size === 1) {\n\t\t\t\tthis.first = item;\n\t\t\t} else {\n\t\t\t\tthis.last.next = item;\n\t\t\t}\n\n\t\t\tthis.last = item;\n\t\t}\n\n\t\tthis.#stats.sets++;\n\n\t\treturn this;\n\t}\n\n\t/**\n\t * Returns an array of all values in the cache for the specified keys.\n\t * When no keys provided, returns all values in LRU order.\n\t * When keys provided, order matches the input array.\n\t *\n\t * @param {string[]} [keys] - Array of keys to get values for. Defaults to all keys.\n\t * @returns {Array<*>} Array of values corresponding to the keys.\n\t */\n\tvalues(keys) {\n\t\tif (keys === undefined) {\n\t\t\tconst result = [];\n\t\t\tfor (let x = this.first; x !== null; x = x.next) {\n\t\t\t\tif (!this.#isExpired(x)) {\n\t\t\t\t\tresult.push(x.value);\n\t\t\t\t}\n\t\t\t}\n\n\t\t\treturn result;\n\t\t}\n\n\t\tif (!Array.isArray(keys)) {\n\t\t\tthrow new TypeError(\"keys must be an array\");\n\t\t}\n\n\t\tconst result = Array.from({ length: keys.length });\n\t\tfor (let i = 0; i < keys.length; i++) {\n\t\t\tconst item = this.items[keys[i]];\n\t\t\tresult[i] = item !== undefined && !this.#isExpired(item) ? item.value : undefined;\n\t\t}\n\n\t\treturn result;\n\t}\n\n\t/**\n\t * Iterate over cache items in LRU order (least to most recent).\n\t * Note: This method directly accesses items from the linked list without calling\n\t * get() or peek(), so it does not update LRU order. Expired items are skipped.\n\t *\n\t * @param {function(*, any, LRU): void} callback - Function to call for each item. Signature: callback(value, key, cache)\n\t * @param {Object} [thisArg] - Value to use as `this` when executing callback.\n\t * @returns {LRU} The LRU instance for method chaining.\n\t */\n\tforEach(callback, thisArg) {\n\t\tfor (let x = this.first; x !== null;) {\n\t\t\tconst next = x.next;\n\t\t\tif (!this.#isExpired(x)) {\n\t\t\t\tcallback.call(thisArg, x.value, x.key, this);\n\t\t\t}\n\t\t\tx = next;\n\t\t}\n\n\t\treturn this;\n\t}\n\n\t/**\n\t * Batch retrieve multiple items.\n\t *\n\t * @param {string[]} keys - Array of keys to retrieve.\n\t * @returns {Object} Object mapping keys to values (undefined for missing/expired keys).\n\t */\n\tgetMany(keys) {\n\t\tif (!Array.isArray(keys)) {\n\t\t\tthrow new TypeError(\"keys must be an array\");\n\t\t}\n\n\t\tconst result = Object.create(null);\n\t\tfor (let i = 0; i < keys.length; i++) {\n\t\t\tconst key = keys[i];\n\t\t\tresult[key] = this.get(key);\n\t\t}\n\n\t\treturn result;\n\t}\n\n\t/**\n\t * Batch existence check - returns true if ALL keys exist.\n\t *\n\t * @param {string[]} keys - Array of keys to check.\n\t * @returns {boolean} True if all keys exist and are not expired.\n\t */\n\thasAll(keys) {\n\t\tif (!Array.isArray(keys)) {\n\t\t\tthrow new TypeError(\"keys must be an array\");\n\t\t}\n\n\t\tfor (let i = 0; i < keys.length; i++) {\n\t\t\tif (!this.has(keys[i])) {\n\t\t\t\treturn false;\n\t\t\t}\n\t\t}\n\n\t\treturn true;\n\t}\n\n\t/**\n\t * Batch existence check - returns true if ANY key exists.\n\t *\n\t * @param {string[]} keys - Array of keys to check.\n\t * @returns {boolean} True if any key exists and is not expired.\n\t */\n\thasAny(keys) {\n\t\tif (!Array.isArray(keys)) {\n\t\t\tthrow new TypeError(\"keys must be an array\");\n\t\t}\n\n\t\tfor (let i = 0; i < keys.length; i++) {\n\t\t\tif (this.has(keys[i])) {\n\t\t\t\treturn true;\n\t\t\t}\n\t\t}\n\n\t\treturn false;\n\t}\n\n\t/**\n\t * Remove expired items without affecting LRU order.\n\t * Unlike get(), this does not move items to the end.\n\t *\n\t * @param {boolean} [fireOnEvict=false] - When true, invokes the onEvict callback for each removed item.\n\t * @returns {number} Number of expired items removed.\n\t */\n\tcleanup(fireOnEvict = false) {\n\t\tif (this.ttl === 0 || this.size === 0) {\n\t\t\treturn 0;\n\t\t}\n\n\t\tlet removed = 0;\n\n\t\tfor (let x = this.first; x !== null;) {\n\t\t\tconst next = x.next;\n\t\t\tif (this.#isExpired(x)) {\n\t\t\t\tconst key = x.key;\n\t\t\t\tif (this.items[key] !== undefined) {\n\t\t\t\t\tdelete this.items[key];\n\t\t\t\t\tthis.size--;\n\t\t\t\t\tremoved++;\n\t\t\t\t\tthis.#unlink(x);\n\t\t\t\t\tx.prev = null;\n\t\t\t\t\tx.next = null;\n\t\t\t\t\tif (fireOnEvict && this.#onEvict !== null) {\n\t\t\t\t\t\tthis.#onEvict({\n\t\t\t\t\t\t\tkey: x.key,\n\t\t\t\t\t\t\tvalue: x.value,\n\t\t\t\t\t\t\texpiry: x.expiry,\n\t\t\t\t\t\t});\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t}\n\t\t\tx = next;\n\t\t}\n\n\t\treturn removed;\n\t}\n\n\t/**\n\t * Serialize cache to JSON-compatible format.\n\t *\n\t * @returns {Array<{key: any, value: *, expiry: number}>} Array of cache items.\n\t */\n\ttoJSON() {\n\t\tconst result = [];\n\t\tfor (let x = this.first; x !== null; x = x.next) {\n\t\t\tif (!this.#isExpired(x)) {\n\t\t\t\tresult.push({\n\t\t\t\t\tkey: x.key,\n\t\t\t\t\tvalue: x.value,\n\t\t\t\t\texpiry: x.expiry,\n\t\t\t\t});\n\t\t\t}\n\t\t}\n\n\t\treturn result;\n\t}\n\n\t/**\n\t * Get cache statistics.\n\t *\n\t * @returns {Object} Statistics object with hits, misses, sets, deletes, evictions counts.\n\t */\n\tstats() {\n\t\treturn { ...this.#stats };\n\t}\n\n\t/**\n\t * Register callback for evicted items.\n\t *\n\t * @param {function(Object): void} callback - Function called when item is evicted. Receives {key, value, expiry}.\n\t * @returns {LRU} The LRU instance for method chaining.\n\t */\n\tonEvict(callback) {\n\t\tif (typeof callback !== \"function\") {\n\t\t\tthrow new TypeError(\"onEvict callback must be a function\");\n\t\t}\n\n\t\tthis.#onEvict = callback;\n\n\t\treturn this;\n\t}\n\n\t/**\n\t * Get counts of items by TTL status.\n\t *\n\t * @returns {Object} Object with valid, expired, and noTTL counts.\n\t */\n\tsizeByTTL() {\n\t\tif (this.ttl === 0) {\n\t\t\treturn { valid: this.size, expired: 0, noTTL: this.size };\n\t\t}\n\n\t\tconst now = Date.now();\n\t\tlet valid = 0;\n\t\tlet expired = 0;\n\n\t\tfor (let x = this.first; x !== null; x = x.next) {\n\t\t\tif (x.expiry > now) {\n\t\t\t\tvalid++;\n\t\t\t} else {\n\t\t\t\texpired++;\n\t\t\t}\n\t\t}\n\n\t\treturn { valid, expired, noTTL: 0 };\n\t}\n\n\t/**\n\t * Get keys filtered by TTL status.\n\t *\n\t * @returns {Object} Object with valid, expired, and noTTL arrays of keys.\n\t */\n\tkeysByTTL() {\n\t\tif (this.ttl === 0) {\n\t\t\treturn { valid: this.keys(), expired: [], noTTL: this.keys() };\n\t\t}\n\n\t\tconst now = Date.now();\n\t\tconst valid = [];\n\t\tconst expired = [];\n\n\t\tfor (let x = this.first; x !== null; x = x.next) {\n\t\t\tif (x.expiry > now) {\n\t\t\t\tvalid.push(x.key);\n\t\t\t} else {\n\t\t\t\texpired.push(x.key);\n\t\t\t}\n\t\t}\n\n\t\treturn { valid, expired, noTTL: [] };\n\t}\n\n\t/**\n\t * Get values filtered by TTL status.\n\t *\n\t * @returns {Object} Object with valid, expired, and noTTL arrays of values.\n\t */\n\tvaluesByTTL() {\n\t\tif (this.ttl === 0) {\n\t\t\treturn { valid: this.values(), expired: [], noTTL: this.values() };\n\t\t}\n\n\t\tconst now = Date.now();\n\t\tconst valid = [];\n\t\tconst expired = [];\n\n\t\tfor (let x = this.first; x !== null; x = x.next) {\n\t\t\tif (x.expiry > now) {\n\t\t\t\tvalid.push(x.value);\n\t\t\t} else {\n\t\t\t\texpired.push(x.value);\n\t\t\t}\n\t\t}\n\n\t\treturn { valid, expired, noTTL: [] };\n\t}\n}\n\n/**\n * Factory function to create a new LRU cache instance with parameter validation.\n *\n * @function lru\n * @param {number} [max=1000] - Maximum number of items to store. Must be >= 0. Use 0 for unlimited size.\n * @param {number} [ttl=0] - Time to live in milliseconds. Must be >= 0. Use 0 for no expiration.\n * @param {boolean} [resetTTL=false] - Whether to reset TTL when updating existing items via set().\n * @returns {LRU} A new LRU cache instance.\n * @throws {TypeError} When parameters are invalid (negative numbers or wrong types).\n */\nexport function lru(max = 1000, ttl = 0, resetTTL = false) {\n\treturn new LRU(max, ttl, resetTTL);\n}\n"],"names":["LRU","stats","onEvict","constructor","max","ttl","resetTTL","Number","isInteger","TypeError","this","first","items","Object","create","last","size","hits","misses","sets","deletes","evictions","clear","x","next","prev","key","item","undefined","removeItem","entries","keys","result","isExpired","push","value","Array","isArray","from","length","i","evict","evictItem","expiry","expiresAt","Date","now","peek","get","moveToEnd","has","unlink","setWithEvicted","evicted","evictedItem","set","values","forEach","callback","thisArg","call","getMany","hasAll","hasAny","cleanup","fireOnEvict","removed","toJSON","sizeByTTL","valid","expired","noTTL","keysByTTL","valuesBy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