Skip to content

Commit 1fb1167

Browse files
alexgleasonisaacs
authored andcommitted
Implement the Map interface
PR-URL: #326 Credit: @alexgleason Close: #326 Reviewed-by: @isaacs
1 parent 58e6aa8 commit 1fb1167

File tree

2 files changed

+13
-3
lines changed

2 files changed

+13
-3
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
# cringe lorg
22

3+
## 10.2.0
4+
5+
- types: implement the `Map<K, V>` interface
6+
37
## 10.1.0
48

59
- add `cache.info(key)` to get value as well as ttl and size

src/index.ts

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -812,7 +812,7 @@ export namespace LRUCache {
812812
* Changing any of these will alter the defaults for subsequent method calls,
813813
* but is otherwise safe.
814814
*/
815-
export class LRUCache<K extends {}, V extends {}, FC = unknown> {
815+
export class LRUCache<K extends {}, V extends {}, FC = unknown> implements Map<K,V> {
816816
// properties coming in from the options of these, only max and maxSize
817817
// really *need* to be protected. The rest can be modified, as they just
818818
// set defaults for various methods.
@@ -1392,7 +1392,7 @@ export class LRUCache<K extends {}, V extends {}, FC = unknown> {
13921392
this.#keyList[i] !== undefined &&
13931393
!this.#isBackgroundFetch(this.#valList[i])
13941394
) {
1395-
yield [this.#keyList[i], this.#valList[i]]
1395+
yield [this.#keyList[i], this.#valList[i]] as [K, V]
13961396
}
13971397
}
13981398
}
@@ -1460,7 +1460,7 @@ export class LRUCache<K extends {}, V extends {}, FC = unknown> {
14601460
v !== undefined &&
14611461
!this.#isBackgroundFetch(this.#valList[i])
14621462
) {
1463-
yield this.#valList[i]
1463+
yield this.#valList[i] as V
14641464
}
14651465
}
14661466
}
@@ -1491,6 +1491,12 @@ export class LRUCache<K extends {}, V extends {}, FC = unknown> {
14911491
return this.entries()
14921492
}
14931493

1494+
/**
1495+
* A String value that is used in the creation of the default string description of an object.
1496+
* Called by the built-in method Object.prototype.toString.
1497+
*/
1498+
[Symbol.toStringTag] = 'LRUCache'
1499+
14941500
/**
14951501
* Find a value for which the supplied fn method returns a truthy value,
14961502
* similar to Array.find(). fn is called as fn(value, key, cache).

0 commit comments

Comments
 (0)