esnext.weak-map.get-or-insert-computed.js 760 B

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