From 3cafa1065548142e4d1e2352a6a4d5478b92b14e Mon Sep 17 00:00:00 2001 From: Gorniaky Date: Thu, 20 Aug 2026 11:18:41 -0300 Subject: [PATCH] chore(prototypes): add Map prototype methods for getOrInsert and getOrInsertComputed --- src/@prototypes/index.ts | 1 + src/@prototypes/map.ts | 16 ++++++++++++++++ src/extension.ts | 1 + 3 files changed, 18 insertions(+) create mode 100644 src/@prototypes/index.ts create mode 100644 src/@prototypes/map.ts diff --git a/src/@prototypes/index.ts b/src/@prototypes/index.ts new file mode 100644 index 00000000..51bda67e --- /dev/null +++ b/src/@prototypes/index.ts @@ -0,0 +1 @@ +import "./map"; diff --git a/src/@prototypes/map.ts b/src/@prototypes/map.ts new file mode 100644 index 00000000..40d4d20d --- /dev/null +++ b/src/@prototypes/map.ts @@ -0,0 +1,16 @@ +Map.prototype.getOrInsert ??= function (key, defaultValue) { + const value = this.get(key); + if (value !== undefined) return value; + + this.set(key, defaultValue); + return defaultValue; +}; + +Map.prototype.getOrInsertComputed ??= function (key, callback) { + const value = this.get(key); + if (value !== undefined) return value; + + const defaultValue = callback(key); + this.set(key, defaultValue); + return defaultValue; +}; diff --git a/src/extension.ts b/src/extension.ts index 271c2783..d7a4c672 100644 --- a/src/extension.ts +++ b/src/extension.ts @@ -1,4 +1,5 @@ import type { ExtensionContext } from "vscode"; +import "./@prototypes"; import ExtensionCore from "./core/extension"; import { localize } from "./localize";