esnext.map.update.js 968 B

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