Skip to content

Commit 6e25112

Browse files
committed
fix(collection): preserve maps for non-finite upper bounds
1 parent a4ff3e1 commit 6e25112

2 files changed

Lines changed: 36 additions & 0 deletions

File tree

src/infra/map-size.test.ts

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,39 @@ describe("pruneMapToMaxSize", () => {
3434
maxSize: -4,
3535
expected: [],
3636
},
37+
{
38+
name: "leaves maps untouched for NaN limits",
39+
entries: [
40+
["a", 1],
41+
["b", 2],
42+
] as const,
43+
maxSize: Number.NaN,
44+
expected: [
45+
["a", 1],
46+
["b", 2],
47+
],
48+
},
49+
{
50+
name: "leaves maps untouched for positive infinity limits",
51+
entries: [
52+
["a", 1],
53+
["b", 2],
54+
] as const,
55+
maxSize: Number.POSITIVE_INFINITY,
56+
expected: [
57+
["a", 1],
58+
["b", 2],
59+
],
60+
},
61+
{
62+
name: "clears maps for negative infinity limits",
63+
entries: [
64+
["a", 1],
65+
["b", 2],
66+
] as const,
67+
maxSize: Number.NEGATIVE_INFINITY,
68+
expected: [],
69+
},
3770
{
3871
name: "leaves undersized maps untouched",
3972
entries: [["a", 1]] as const,

src/infra/map-size.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
11
export function pruneMapToMaxSize<K, V>(map: Map<K, V>, maxSize: number): void {
2+
if (Number.isNaN(maxSize) || maxSize === Number.POSITIVE_INFINITY) {
3+
return;
4+
}
25
const limit = Math.max(0, Math.floor(maxSize));
36
if (limit <= 0) {
47
map.clear();

0 commit comments

Comments
 (0)