esnext.map.emplace.js 757 B

123456789101112131415161718192021222324252627
  1. 'use strict';
  2. var $ = require('../internals/export');
  3. var aMap = require('../internals/a-map');
  4. var MapHelpers = require('../internals/map-helpers');
  5. var get = MapHelpers.get;
  6. var has = MapHelpers.has;
  7. var set = MapHelpers.set;
  8. // `Map.prototype.emplace` method
  9. // https://github.com/tc39/proposal-upsert
  10. $({ target: 'Map', proto: true, real: true, forced: true }, {
  11. emplace: function emplace(key, handler) {
  12. var map = aMap(this);
  13. var value, inserted;
  14. if (has(map, key)) {
  15. value = get(map, key);
  16. if ('update' in handler) {
  17. value = handler.update(value, key, map);
  18. set(map, key, value);
  19. } return value;
  20. }
  21. inserted = handler.insert(key, map);
  22. set(map, key, inserted);
  23. return inserted;
  24. }
  25. });