2 var global = require('../internals/global');
3 var call = require('../internals/function-call');
4 var aCallable = require('../internals/a-callable');
5 var isCallable = require('../internals/is-callable');
6 var anObject = require('../internals/an-object');
8 var TypeError = global.TypeError;
10 // `Map.prototype.upsert` method
11 // https://github.com/thumbsupep/proposal-upsert
12 module.exports = function upsert(key, updateFn /* , insertFn */) {
13 var map = anObject(this);
14 var get = aCallable(map.get);
15 var has = aCallable(map.has);
16 var set = aCallable(map.set);
17 var insertFn = arguments.length > 2 ? arguments[2] : undefined;
19 if (!isCallable(updateFn) && !isCallable(insertFn)) {
20 throw TypeError('At least one callback required');
22 if (call(has, map, key)) {
23 value = call(get, map, key);
24 if (isCallable(updateFn)) {
25 value = updateFn(value);
26 call(set, map, key, value);
28 } else if (isCallable(insertFn)) {
30 call(set, map, key, value);