You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
28 lines
799 B
28 lines
799 B
4 weeks ago
|
'use strict';
|
||
|
var $ = require('../internals/export');
|
||
|
var aWeakMap = require('../internals/a-weak-map');
|
||
|
var WeakMapHelpers = require('../internals/weak-map-helpers');
|
||
|
|
||
|
var get = WeakMapHelpers.get;
|
||
|
var has = WeakMapHelpers.has;
|
||
|
var set = WeakMapHelpers.set;
|
||
|
|
||
|
// `WeakMap.prototype.emplace` method
|
||
|
// https://github.com/tc39/proposal-upsert
|
||
|
$({ target: 'WeakMap', proto: true, real: true, forced: true }, {
|
||
|
emplace: function emplace(key, handler) {
|
||
|
var map = aWeakMap(this);
|
||
|
var value, inserted;
|
||
|
if (has(map, key)) {
|
||
|
value = get(map, key);
|
||
|
if ('update' in handler) {
|
||
|
value = handler.update(value, key, map);
|
||
|
set(map, key, value);
|
||
|
} return value;
|
||
|
}
|
||
|
inserted = handler.insert(key, map);
|
||
|
set(map, key, inserted);
|
||
|
return inserted;
|
||
|
}
|
||
|
});
|