esnext.map.update.js 1018 B

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