FlexMap getOrSet and getAndDelete
This commit is contained in:
parent
eaff7b86d8
commit
550224e0b1
|
@ -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",
|
||||
|
|
|
@ -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();
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue