Skip to content

Commit 2ccf024

Browse files
committed
Map.getOrInsert and getOrInsertComputed are supported in Node.js 26
1 parent 4d849f2 commit 2ccf024

2 files changed

Lines changed: 0 additions & 51 deletions

File tree

src/polyfills.test.ts

Lines changed: 0 additions & 16 deletions
This file was deleted.

src/polyfills.ts

Lines changed: 0 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -5,44 +5,9 @@ declare global {
55
interface Math {
66
sumPrecise: (values: number[]) => number;
77
}
8-
9-
// https://github.com/tc39/proposal-upsert
10-
interface Map<K, V> {
11-
getOrInsert(key: K, defaultValue: V): V;
12-
getOrInsertComputed<TK extends K>(key: TK, callbackFunction: (key: TK) => V): V;
13-
}
148
}
159

1610
if (typeof Math.sumPrecise !== "function") {
1711
// intentionally very cheap implementation. But does the thing.
1812
Math.sumPrecise = (values: number[]) => values.reduce((a, b) => a + b, 0);
1913
}
20-
21-
// https://github.com/tc39/proposal-upsert
22-
if (typeof Map.prototype.getOrInsert === "undefined") {
23-
Map.prototype.getOrInsert = function <K, V>(this: Map<K, V>, key: K, defaultValue: V): V {
24-
const v = this.get(key);
25-
if (v !== undefined) {
26-
return v;
27-
}
28-
29-
this.set(key, defaultValue);
30-
return defaultValue;
31-
};
32-
}
33-
if (typeof Map.prototype.getOrInsertComputed === "undefined") {
34-
Map.prototype.getOrInsertComputed = function <K, V>(
35-
this: Map<K, V>,
36-
key: K,
37-
callbackFunction: (key: K) => V,
38-
): V {
39-
const v = this.get(key);
40-
if (v !== undefined) {
41-
return v;
42-
}
43-
44-
const defaultValue = callbackFunction(key);
45-
this.set(key, defaultValue);
46-
return defaultValue;
47-
};
48-
}

0 commit comments

Comments
 (0)