esnext.weak-map.get-or-insert.js 571 B

123456789101112131415161718
  1. 'use strict';
  2. var $ = require('../internals/export');
  3. var aWeakMap = require('../internals/a-weak-map');
  4. var WeakMapHelpers = require('../internals/weak-map-helpers');
  5. var get = WeakMapHelpers.get;
  6. var has = WeakMapHelpers.has;
  7. var set = WeakMapHelpers.set;
  8. // `WeakMap.prototype.getOrInsert` method
  9. // https://github.com/tc39/proposal-upsert
  10. $({ target: 'WeakMap', proto: true, real: true, forced: true }, {
  11. getOrInsert: function getOrInsert(key, value) {
  12. if (has(aWeakMap(this), key)) return get(this, key);
  13. set(this, key, value);
  14. return value;
  15. }
  16. });