From 550224e0b1ddeb59daf453d2b4ed627316f855d9 Mon Sep 17 00:00:00 2001 From: Tony Garnock-Jones Date: Wed, 3 Mar 2021 19:14:35 +0100 Subject: [PATCH] FlexMap getOrSet and getAndDelete --- implementations/javascript/package.json | 2 +- implementations/javascript/src/flex.ts | 18 ++++++++++++++++++ 2 files changed, 19 insertions(+), 1 deletion(-) diff --git a/implementations/javascript/package.json b/implementations/javascript/package.json index ee61172..5e4b6c8 100644 --- a/implementations/javascript/package.json +++ b/implementations/javascript/package.json @@ -1,6 +1,6 @@ { "name": "preserves", - "version": "0.6.2", + "version": "0.6.3", "description": "Experimental data serialization format", "homepage": "https://gitlab.com/preserves/preserves", "license": "Apache-2.0", diff --git a/implementations/javascript/src/flex.ts b/implementations/javascript/src/flex.ts index 687a167..c237185 100644 --- a/implementations/javascript/src/flex.ts +++ b/implementations/javascript/src/flex.ts @@ -76,6 +76,16 @@ export class FlexMap implements Map { return (e === void 0) ? defaultValue : e[1]; } + getOrSet(k: K, initializer: () => V): V { + const ks = this._key(k); + let e = this.items.get(ks); + if (e === void 0) { + e = [k, initializer()]; + this.items.set(ks, e); + } + return e[1]; + } + set(k: K, v: V): this { this.items.set(this._key(k), [k, v]); return this; @@ -101,6 +111,14 @@ export class FlexMap implements Map { return this.items.delete(this._key(k)); } + getAndDelete(k: K, defaultValue?: V): V | undefined { + const ks = this._key(k); + const e = this.items.get(ks); + if (e === void 0) return defaultValue; + this.items.delete(ks); + return e[1]; + } + clear() { this.items.clear(); }