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.
27 lines
757 B
27 lines
757 B
'use strict'; |
|
var $ = require('../internals/export'); |
|
var aMap = require('../internals/a-map'); |
|
var MapHelpers = require('../internals/map-helpers'); |
|
|
|
var get = MapHelpers.get; |
|
var has = MapHelpers.has; |
|
var set = MapHelpers.set; |
|
|
|
// `Map.prototype.emplace` method |
|
// https://github.com/tc39/proposal-upsert |
|
$({ target: 'Map', proto: true, real: true, forced: true }, { |
|
emplace: function emplace(key, handler) { |
|
var map = aMap(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; |
|
} |
|
});
|
|
|