esnext.map.update.js 815 B

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