polyfill.js 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. "use strict";
  2. var isValue = require("es5-ext/object/is-value")
  3. , setPrototypeOf = require("es5-ext/object/set-prototype-of")
  4. , object = require("es5-ext/object/valid-object")
  5. , ensureValue = require("es5-ext/object/valid-value")
  6. , randomUniq = require("es5-ext/string/random-uniq")
  7. , d = require("d")
  8. , getIterator = require("es6-iterator/get")
  9. , forOf = require("es6-iterator/for-of")
  10. , toStringTagSymbol = require("es6-symbol").toStringTag
  11. , isNative = require("./is-native-implemented")
  12. , isArray = Array.isArray, defineProperty = Object.defineProperty
  13. , objHasOwnProperty = Object.prototype.hasOwnProperty, getPrototypeOf = Object.getPrototypeOf
  14. , WeakMapPoly;
  15. module.exports = WeakMapPoly = function (/* Iterable*/) {
  16. var iterable = arguments[0], self;
  17. if (!(this instanceof WeakMapPoly)) throw new TypeError("Constructor requires 'new'");
  18. self = isNative && setPrototypeOf && (WeakMap !== WeakMapPoly)
  19. ? setPrototypeOf(new WeakMap(), getPrototypeOf(this)) : this;
  20. if (isValue(iterable)) {
  21. if (!isArray(iterable)) iterable = getIterator(iterable);
  22. }
  23. defineProperty(self, "__weakMapData__", d("c", "$weakMap$" + randomUniq()));
  24. if (!iterable) return self;
  25. forOf(iterable, function (val) {
  26. ensureValue(val);
  27. self.set(val[0], val[1]);
  28. });
  29. return self;
  30. };
  31. if (isNative) {
  32. if (setPrototypeOf) setPrototypeOf(WeakMapPoly, WeakMap);
  33. WeakMapPoly.prototype = Object.create(WeakMap.prototype, { constructor: d(WeakMapPoly) });
  34. }
  35. Object.defineProperties(WeakMapPoly.prototype, {
  36. delete: d(function (key) {
  37. if (objHasOwnProperty.call(object(key), this.__weakMapData__)) {
  38. delete key[this.__weakMapData__];
  39. return true;
  40. }
  41. return false;
  42. }),
  43. get: d(function (key) {
  44. if (!objHasOwnProperty.call(object(key), this.__weakMapData__)) return undefined;
  45. return key[this.__weakMapData__];
  46. }),
  47. has: d(function (key) {
  48. return objHasOwnProperty.call(object(key), this.__weakMapData__);
  49. }),
  50. set: d(function (key, value) {
  51. defineProperty(object(key), this.__weakMapData__, d("c", value));
  52. return this;
  53. }),
  54. toString: d(function () {
  55. return "[object WeakMap]";
  56. })
  57. });
  58. defineProperty(WeakMapPoly.prototype, toStringTagSymbol, d("c", "WeakMap"));