Skip to content

Commit

Permalink
Add .maxSize getter
Browse files Browse the repository at this point in the history
Fixes #47
  • Loading branch information
sindresorhus committed Sep 11, 2023
1 parent f0bbe48 commit 187fa5b
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 0 deletions.
5 changes: 5 additions & 0 deletions index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,11 @@ export default class QuickLRU<KeyType, ValueType> extends Map<KeyType, ValueType
*/
get size(): number;

/**
The set max size.
*/
get maxSize(): number;

/**
Iterable for all the keys.
*/
Expand Down
4 changes: 4 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -273,6 +273,10 @@ export default class QuickLRU extends Map {
return Math.min(this.#size + oldCacheSize, this.#maxSize);
}

get maxSize() {
return this.#maxSize;
}

entries() {
return this.entriesAscending();
}
Expand Down
4 changes: 4 additions & 0 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,10 @@ Loop over entries calling the `callbackFunction` for each entry (ascending in re

The stored item count.

#### .maxSize *(getter)*

The set max size.

## Related

- [yocto-queue](https://github.com/sindresorhus/yocto-queue) - Tiny queue data structure
6 changes: 6 additions & 0 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,12 @@ test('max size', t => {
t.is(lru.size, 3);
});

test('.maxSize', t => {
const maxSize = 100;
const lru = new QuickLRU({maxSize});
t.is(lru.maxSize, maxSize);
});

test('checks total cache size does not exceed `maxSize`', t => {
const lru = new QuickLRU({maxSize: 2});
lru.set('1', 1);
Expand Down

0 comments on commit 187fa5b

Please sign in to comment.