1 // ES2015 Symbol polyfill for environments that do not support it (or partially support it)
6 , validateSymbol = require('./validate-symbol')
8 , create = Object.create, defineProperties = Object.defineProperties
9 , defineProperty = Object.defineProperty, objPrototype = Object.prototype
10 , NativeSymbol, SymbolPolyfill, HiddenSymbol, globalSymbols = create(null)
13 if (typeof Symbol === 'function') {
14 NativeSymbol = Symbol;
16 String(NativeSymbol());
21 var generateName = (function () {
22 var created = create(null);
23 return function (desc) {
24 var postfix = 0, name, ie11BugWorkaround;
25 while (created[desc + (postfix || '')]) ++postfix;
26 desc += (postfix || '');
29 defineProperty(objPrototype, name, d.gs(null, function (value) {
30 // For IE11 issue see:
31 // https://connect.microsoft.com/IE/feedbackdetail/view/1928508/
32 // ie11-broken-getters-on-dom-objects
33 // https://github.com/medikoo/es6-symbol/issues/12
34 if (ie11BugWorkaround) return;
35 ie11BugWorkaround = true;
36 defineProperty(this, name, d(value));
37 ie11BugWorkaround = false;
43 // Internal constructor (not one exposed) for creating Symbol instances.
44 // This one is used to ensure that `someSymbol instanceof Symbol` always return false
45 HiddenSymbol = function Symbol(description) {
46 if (this instanceof HiddenSymbol) throw new TypeError('TypeError: Symbol is not a constructor');
47 return SymbolPolyfill(description);
50 // Exposed `Symbol` constructor
51 // (returns instances of HiddenSymbol)
52 module.exports = SymbolPolyfill = function Symbol(description) {
54 if (this instanceof Symbol) throw new TypeError('TypeError: Symbol is not a constructor');
55 if (isNativeSafe) return NativeSymbol(description);
56 symbol = create(HiddenSymbol.prototype);
57 description = (description === undefined ? '' : String(description));
58 return defineProperties(symbol, {
59 __description__: d('', description),
60 __name__: d('', generateName(description))
63 defineProperties(SymbolPolyfill, {
64 for: d(function (key) {
65 if (globalSymbols[key]) return globalSymbols[key];
66 return (globalSymbols[key] = SymbolPolyfill(String(key)));
68 keyFor: d(function (s) {
71 for (key in globalSymbols) if (globalSymbols[key] === s) return key;
74 // If there's native implementation of given symbol, let's fallback to it
75 // to ensure proper interoperability with other native functions e.g. Array.from
76 hasInstance: d('', (NativeSymbol && NativeSymbol.hasInstance) || SymbolPolyfill('hasInstance')),
77 isConcatSpreadable: d('', (NativeSymbol && NativeSymbol.isConcatSpreadable) ||
78 SymbolPolyfill('isConcatSpreadable')),
79 iterator: d('', (NativeSymbol && NativeSymbol.iterator) || SymbolPolyfill('iterator')),
80 match: d('', (NativeSymbol && NativeSymbol.match) || SymbolPolyfill('match')),
81 replace: d('', (NativeSymbol && NativeSymbol.replace) || SymbolPolyfill('replace')),
82 search: d('', (NativeSymbol && NativeSymbol.search) || SymbolPolyfill('search')),
83 species: d('', (NativeSymbol && NativeSymbol.species) || SymbolPolyfill('species')),
84 split: d('', (NativeSymbol && NativeSymbol.split) || SymbolPolyfill('split')),
85 toPrimitive: d('', (NativeSymbol && NativeSymbol.toPrimitive) || SymbolPolyfill('toPrimitive')),
86 toStringTag: d('', (NativeSymbol && NativeSymbol.toStringTag) || SymbolPolyfill('toStringTag')),
87 unscopables: d('', (NativeSymbol && NativeSymbol.unscopables) || SymbolPolyfill('unscopables'))
90 // Internal tweaks for real symbol producer
91 defineProperties(HiddenSymbol.prototype, {
92 constructor: d(SymbolPolyfill),
93 toString: d('', function () { return this.__name__; })
96 // Proper implementation of methods exposed on Symbol.prototype
97 // They won't be accessible on produced symbol instances as they derive from HiddenSymbol.prototype
98 defineProperties(SymbolPolyfill.prototype, {
99 toString: d(function () { return 'Symbol (' + validateSymbol(this).__description__ + ')'; }),
100 valueOf: d(function () { return validateSymbol(this); })
102 defineProperty(SymbolPolyfill.prototype, SymbolPolyfill.toPrimitive, d('', function () {
103 var symbol = validateSymbol(this);
104 if (typeof symbol === 'symbol') return symbol;
105 return symbol.toString();
107 defineProperty(SymbolPolyfill.prototype, SymbolPolyfill.toStringTag, d('c', 'Symbol'));
109 // Proper implementaton of toPrimitive and toStringTag for returned symbol instances
110 defineProperty(HiddenSymbol.prototype, SymbolPolyfill.toStringTag,
111 d('c', SymbolPolyfill.prototype[SymbolPolyfill.toStringTag]));
113 // Note: It's important to define `toPrimitive` as last one, as some implementations
114 // implement `toPrimitive` natively without implementing `toStringTag` (or other specified symbols)
115 // And that may invoke error in definition flow:
116 // See: https://github.com/medikoo/es6-symbol/issues/13#issuecomment-164146149
117 defineProperty(HiddenSymbol.prototype, SymbolPolyfill.toPrimitive,
118 d('c', SymbolPolyfill.prototype[SymbolPolyfill.toPrimitive]));