Skip to content

Commit

Permalink
Implement the Map interface
Browse files Browse the repository at this point in the history
PR-URL: isaacs#326
Credit: @alexgleason
Close: isaacs#326
Reviewed-by: @isaacs
  • Loading branch information
alexgleason authored and isaacs committed Jan 25, 2024
1 parent 58e6aa8 commit 1fb1167
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 3 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# cringe lorg

## 10.2.0

- types: implement the `Map<K, V>` interface

## 10.1.0

- add `cache.info(key)` to get value as well as ttl and size
Expand Down
12 changes: 9 additions & 3 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -812,7 +812,7 @@ export namespace LRUCache {
* Changing any of these will alter the defaults for subsequent method calls,
* but is otherwise safe.
*/
export class LRUCache<K extends {}, V extends {}, FC = unknown> {
export class LRUCache<K extends {}, V extends {}, FC = unknown> implements Map<K,V> {
// properties coming in from the options of these, only max and maxSize
// really *need* to be protected. The rest can be modified, as they just
// set defaults for various methods.
Expand Down Expand Up @@ -1392,7 +1392,7 @@ export class LRUCache<K extends {}, V extends {}, FC = unknown> {
this.#keyList[i] !== undefined &&
!this.#isBackgroundFetch(this.#valList[i])
) {
yield [this.#keyList[i], this.#valList[i]]
yield [this.#keyList[i], this.#valList[i]] as [K, V]
}
}
}
Expand Down Expand Up @@ -1460,7 +1460,7 @@ export class LRUCache<K extends {}, V extends {}, FC = unknown> {
v !== undefined &&
!this.#isBackgroundFetch(this.#valList[i])
) {
yield this.#valList[i]
yield this.#valList[i] as V
}
}
}
Expand Down Expand Up @@ -1491,6 +1491,12 @@ export class LRUCache<K extends {}, V extends {}, FC = unknown> {
return this.entries()
}

/**
* A String value that is used in the creation of the default string description of an object.
* Called by the built-in method Object.prototype.toString.
*/
[Symbol.toStringTag] = 'LRUCache'

/**
* Find a value for which the supplied fn method returns a truthy value,
* similar to Array.find(). fn is called as fn(value, key, cache).
Expand Down

0 comments on commit 1fb1167

Please sign in to comment.