"Fossies" - the Fresh Open Source Software Archive

Member "Atom/resources/app/apm/node_modules/es5-ext/node_modules/es6-symbol/polyfill.js" (8 Mar 2017, 5133 Bytes) of archive /windows/misc/atom-windows.zip:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) Javascript source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file.

    1 // ES2015 Symbol polyfill for environments that do not support it (or partially support it)
    2 
    3 'use strict';
    4 
    5 var d              = require('d')
    6   , validateSymbol = require('./validate-symbol')
    7 
    8   , create = Object.create, defineProperties = Object.defineProperties
    9   , defineProperty = Object.defineProperty, objPrototype = Object.prototype
   10   , NativeSymbol, SymbolPolyfill, HiddenSymbol, globalSymbols = create(null)
   11   , isNativeSafe;
   12 
   13 if (typeof Symbol === 'function') {
   14     NativeSymbol = Symbol;
   15     try {
   16         String(NativeSymbol());
   17         isNativeSafe = true;
   18     } catch (ignore) {}
   19 }
   20 
   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 || '');
   27         created[desc] = true;
   28         name = '@@' + desc;
   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;
   38         }));
   39         return name;
   40     };
   41 }());
   42 
   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);
   48 };
   49 
   50 // Exposed `Symbol` constructor
   51 // (returns instances of HiddenSymbol)
   52 module.exports = SymbolPolyfill = function Symbol(description) {
   53     var symbol;
   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))
   61     });
   62 };
   63 defineProperties(SymbolPolyfill, {
   64     for: d(function (key) {
   65         if (globalSymbols[key]) return globalSymbols[key];
   66         return (globalSymbols[key] = SymbolPolyfill(String(key)));
   67     }),
   68     keyFor: d(function (s) {
   69         var key;
   70         validateSymbol(s);
   71         for (key in globalSymbols) if (globalSymbols[key] === s) return key;
   72     }),
   73 
   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'))
   88 });
   89 
   90 // Internal tweaks for real symbol producer
   91 defineProperties(HiddenSymbol.prototype, {
   92     constructor: d(SymbolPolyfill),
   93     toString: d('', function () { return this.__name__; })
   94 });
   95 
   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); })
  101 });
  102 defineProperty(SymbolPolyfill.prototype, SymbolPolyfill.toPrimitive, d('', function () {
  103     var symbol = validateSymbol(this);
  104     if (typeof symbol === 'symbol') return symbol;
  105     return symbol.toString();
  106 }));
  107 defineProperty(SymbolPolyfill.prototype, SymbolPolyfill.toStringTag, d('c', 'Symbol'));
  108 
  109 // Proper implementaton of toPrimitive and toStringTag for returned symbol instances
  110 defineProperty(HiddenSymbol.prototype, SymbolPolyfill.toStringTag,
  111     d('c', SymbolPolyfill.prototype[SymbolPolyfill.toStringTag]));
  112 
  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]));