UNPKG

922 BJavaScriptView Raw
1'use strict';
2var $ = require('../internals/export');
3var aCallable = require('../internals/a-callable');
4var aMap = require('../internals/a-map');
5var MapHelpers = require('../internals/map-helpers');
6
7var $TypeError = TypeError;
8var get = MapHelpers.get;
9var has = MapHelpers.has;
10var set = MapHelpers.set;
11
12// `Map.prototype.update` method
13// https://github.com/tc39/proposal-collection-methods
14$({ target: 'Map', proto: true, real: true, forced: true }, {
15 update: function update(key, callback /* , thunk */) {
16 var map = aMap(this);
17 var length = arguments.length;
18 aCallable(callback);
19 var isPresentInMap = has(map, key);
20 if (!isPresentInMap && length < 3) {
21 throw new $TypeError('Updating absent value');
22 }
23 var value = isPresentInMap ? get(map, key) : aCallable(length > 2 ? arguments[2] : undefined)(key, map);
24 set(map, key, callback(value, key, map));
25 return map;
26 }
27});