-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathweakMap.js
More file actions
24 lines (21 loc) · 644 Bytes
/
weakMap.js
File metadata and controls
24 lines (21 loc) · 644 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
if (! (WeakMap.prototype.emplace instanceof Function)) {
WeakMap.prototype.emplace = function emplace(key, { insert, update } = {}) {
const has = this.has(key);
if (has && update instanceof Function) {
const existing = this.get(key);
const value = update.call(this, existing, key, this);
if (value !== existing) {
this.set(key, existing);
}
return value;
} else if (has) {
return this.get(key);
} else if (insert instanceof Function) {
const value = insert.call(this, key, this);
this.set(key, value);
return value;
} else {
throw new Error('Key is not found and no `insert()` given');
}
};
}