Skip to content

Commit

Permalink
fix(SymbolShim): Symbol polyfill is a function
Browse files Browse the repository at this point in the history
Symbol is supposed to be a function that takes a description and returns a unique symbol
such that `Symbol('foo') !== Symbol('foo')`. The polyfill therefor returns a string that has
a unique identifier concatenated to it: `@@symbol(description):1` `@@symbol(description):2` etc

fixes #988
  • Loading branch information
benlesh committed Dec 8, 2015
1 parent e942776 commit 1f57157
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 2 deletions.
14 changes: 13 additions & 1 deletion spec/util/SymbolShim-spec.js
Expand Up @@ -5,7 +5,19 @@ var Rx = require('../../dist/cjs/Rx');
var polyfillSymbol = SymbolShim.polyfillSymbol;
var ensureIterator = SymbolShim.ensureIterator;

describe('SymbolShim', function () {
describe('SymbolShim.polyfillSymbol', function () {
it('should polyfill Symbol to be a function that returns a primitive that is unique', function () {
var Symbol = polyfillSymbol({ });

expect(typeof Symbol).toBe('function');
var x = Symbol('test');
var y = Symbol('test');
expect(x !== y).toBe(true); // should be obvious, but this is the important part.

expect(x).toBe('@@Symbol(test):0');
expect(y).toBe('@@Symbol(test):1');
});

it('should setup symbol if root does not have it', function () {
var root = {};

Expand Down
6 changes: 5 additions & 1 deletion src/util/SymbolShim.ts
Expand Up @@ -14,9 +14,13 @@ export function ensureFor(Symbol) {
}
}

let id = 0;

export function ensureSymbol(root) {
if (!root.Symbol) {
root.Symbol = {};
root.Symbol = function symbolFuncPolyfill(description) {
return `@@Symbol(${description}):${id++}`;
};
}
return root.Symbol;
}
Expand Down

0 comments on commit 1f57157

Please sign in to comment.