Skip to content

Commit eadb294

Browse files
committed
Fix has() TTL semantics - use #isExpired() consistently
has() was incorrectly treating expiry === 0 as expired when ttl > 0, while #isExpired() and cleanup() correctly treat expiry === 0 as noTTL. Aligns has() with the rest of the TTL semantics by using #isExpired().
1 parent 01cfb31 commit eadb294

6 files changed

Lines changed: 10 additions & 10 deletions

File tree

coverage.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@
33
ℹ file | line % | branch % | funcs % | uncovered lines
44
ℹ ----------------------------------------------------------
55
ℹ src | | | |
6-
ℹ lru.js | 100.00 | 99.27 | 100.00 |
6+
ℹ lru.js | 100.00 | 99.26 | 100.00 |
77
ℹ ----------------------------------------------------------
8-
ℹ all files | 100.00 | 99.27 | 100.00 |
8+
ℹ all files | 100.00 | 99.26 | 100.00 |
99
ℹ ----------------------------------------------------------
1010
ℹ end of coverage report

dist/tiny-lru.cjs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -213,11 +213,11 @@ class LRU {
213213
* Checks if a key exists in the cache.
214214
*
215215
* @param {string} key - The key to check for.
216-
* @returns {boolean} True if the key exists, false otherwise.
216+
* @returns {boolean} True if the key exists and is not expired, false otherwise.
217217
*/
218218
has(key) {
219219
const item = this.items[key];
220-
return item !== undefined && (this.ttl === 0 || item.expiry > Date.now());
220+
return item !== undefined && !this.#isExpired(item);
221221
}
222222

223223
/**

dist/tiny-lru.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -211,11 +211,11 @@ class LRU {
211211
* Checks if a key exists in the cache.
212212
*
213213
* @param {string} key - The key to check for.
214-
* @returns {boolean} True if the key exists, false otherwise.
214+
* @returns {boolean} True if the key exists and is not expired, false otherwise.
215215
*/
216216
has(key) {
217217
const item = this.items[key];
218-
return item !== undefined && (this.ttl === 0 || item.expiry > Date.now());
218+
return item !== undefined && !this.#isExpired(item);
219219
}
220220

221221
/**

dist/tiny-lru.min.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/tiny-lru.min.js.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/lru.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -204,11 +204,11 @@ export class LRU {
204204
* Checks if a key exists in the cache.
205205
*
206206
* @param {string} key - The key to check for.
207-
* @returns {boolean} True if the key exists, false otherwise.
207+
* @returns {boolean} True if the key exists and is not expired, false otherwise.
208208
*/
209209
has(key) {
210210
const item = this.items[key];
211-
return item !== undefined && (this.ttl === 0 || item.expiry > Date.now());
211+
return item !== undefined && !this.#isExpired(item);
212212
}
213213

214214
/**

0 commit comments

Comments
 (0)