esnext.map.get-or-insert.js 529 B

123456789101112131415161718
  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.getOrInsert` method
  9. // https://github.com/tc39/proposal-upsert
  10. $({ target: 'Map', proto: true, real: true, forced: true }, {
  11. getOrInsert: function getOrInsert(key, value) {
  12. if (has(aMap(this), key)) return get(this, key);
  13. set(this, key, value);
  14. return value;
  15. }
  16. });