Skip to content

Commit 5aa9087

Browse files
committed
fix: throw TypeError if onEvict callback is not a function
1 parent af1d817 commit 5aa9087

2 files changed

Lines changed: 8 additions & 4 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 | 98.54 | 100.00 |
6+
ℹ lru.js | 99.70 | 98.54 | 100.00 | 534-535
77
ℹ ----------------------------------------------------------
8-
ℹ all files | 100.00 | 98.54 | 100.00 |
8+
ℹ all files | 99.70 | 98.54 | 100.00 |
99
ℹ ----------------------------------------------------------
1010
ℹ end of coverage report

src/lru.js

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,7 @@ export class LRU {
126126

127127
item.prev = null;
128128
item.next = null;
129-
if (typeof this.#onEvict === "function") {
129+
if (this.#onEvict !== null) {
130130
this.#onEvict({
131131
key: item.key,
132132
value: item.value,
@@ -530,7 +530,11 @@ export class LRU {
530530
* @returns {LRU} The LRU instance for method chaining.
531531
*/
532532
onEvict(callback) {
533-
this.#onEvict = typeof callback === "function" ? callback : null;
533+
if (typeof callback !== "function") {
534+
throw new TypeError("onEvict callback must be a function");
535+
}
536+
537+
this.#onEvict = callback;
534538

535539
return this;
536540
}

0 commit comments

Comments
 (0)