esnext.map.get-or-insert-computed.js 771 B

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