Code coverage report for src/util/SymbolShim.ts

Statements: 100% (44 / 44)      Branches: 100% (23 / 23)      Functions: 100% (7 / 7)      Lines: 100% (38 / 38)      Ignored: none     

All files » src/util/ » SymbolShim.ts
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 661   1 11 11 11 11 11     1 11 8       1   1 11 4 2     11     1 2     1 11 9 2 7   1 6   1 1 12 12 1 1       5         1 11 10 3   7         1  
import {root} from './root';
 
export function polyfillSymbol(root: any) {
  const Symbol = ensureSymbol(root);
  ensureIterator(Symbol, root);
  ensureObservable(Symbol);
  ensureFor(Symbol);
  return Symbol;
}
 
export function ensureFor(Symbol: any) {
  if (!Symbol.for) {
    Symbol.for = symbolForPolyfill;
  }
}
 
let id = 0;
 
export function ensureSymbol(root: any) {
  if (!root.Symbol) {
    root.Symbol = function symbolFuncPolyfill(description: any) {
      return `@@Symbol(${description}):${id++}`;
    };
  }
  return root.Symbol;
}
 
export function symbolForPolyfill(key: any) {
  return '@@' + key;
}
 
export function ensureIterator(Symbol: any, root: any) {
  if (!Symbol.iterator) {
    if (typeof Symbol.for === 'function') {
      Symbol.iterator = Symbol.for('iterator');
    } else if (root.Set && typeof new root.Set()['@@iterator'] === 'function') {
      // Bug for mozilla version
      Symbol.iterator = '@@iterator';
    } else if (root.Map) {
      // es6-shim specific logic
      let keys = Object.getOwnPropertyNames(root.Map.prototype);
      for (let i = 0; i < keys.length; ++i) {
        let key = keys[i];
        if (key !== 'entries' && key !== 'size' && root.Map.prototype[key] === root.Map.prototype['entries']) {
          Symbol.iterator = key;
          break;
        }
      }
    } else {
      Symbol.iterator = '@@iterator';
    }
  }
}
 
export function ensureObservable(Symbol: any) {
  if (!Symbol.observable) {
    if (typeof Symbol.for === 'function') {
      Symbol.observable = Symbol.for('observable');
    } else {
      Symbol.observable = '@@observable';
    }
  }
}
 
export const SymbolShim = polyfillSymbol(root);