FlexMap getOrSet and getAndDelete

This commit is contained in:
Tony Garnock-Jones 2021-03-03 19:14:35 +01:00
parent eaff7b86d8
commit 550224e0b1
2 changed files with 19 additions and 1 deletions

View File

@ -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",

View File

@ -76,6 +76,16 @@ export class FlexMap<K, V> implements Map<K, V> {
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<K, V> implements Map<K, V> {
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();
}