es.map.get-or-insert-computed.js 811 B

123456789101112131415161718192021222324
  1. 'use strict';
  2. var $ = require('../internals/export');
  3. var aCallable = require('../internals/a-callable');
  4. var MapHelpers = require('../internals/map-helpers');
  5. var IS_PURE = require('../internals/is-pure');
  6. var get = MapHelpers.get;
  7. var has = MapHelpers.has;
  8. var set = MapHelpers.set;
  9. // `Map.prototype.getOrInsertComputed` method
  10. // https://tc39.es/ecma262/#sec-map.prototype.getorinsertcomputed
  11. $({ target: 'Map', proto: true, real: true, forced: IS_PURE }, {
  12. getOrInsertComputed: function getOrInsertComputed(key, callbackfn) {
  13. var hasKey = has(this, key);
  14. aCallable(callbackfn);
  15. if (hasKey) 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. });