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