Skip to content

Commit e942776

Browse files
committed
fix(SymbolShim): ensure for function even if Symbol already exists
In some engines, Symbol exists but does not impelment the for method. fixes #999
1 parent 1298b57 commit e942776

File tree

2 files changed

+27
-12
lines changed

2 files changed

+27
-12
lines changed

spec/util/SymbolShim-spec.js

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,18 @@ describe('SymbolShim', function () {
2525
expect(test).toBe('@@test');
2626
});
2727

28+
it('should add a for method even if Symbol already exists but does not have for', function () {
29+
var root = {
30+
Symbol: {}
31+
};
32+
var result = polyfillSymbol(root);
33+
34+
expect(typeof result.for).toBe('function');
35+
36+
var test = result.for('test');
37+
expect(test).toBe('@@test');
38+
});
39+
2840
describe('when symbols exists on root', function () {
2941
it('should use symbols from root', function () {
3042
var root = {
@@ -59,17 +71,15 @@ describe('SymbolShim', function () {
5971
expect(result.observable).toBe('@@observable');
6072
});
6173
});
62-
});
6374

64-
describe('ensureIterator', function () {
65-
it('should patch root using for symbol if exist', function () {
75+
it('should patch root using Symbol.for if exist', function () {
6676
var root = {
6777
Symbol: {
6878
for: function (x) { return x; }
6979
}
7080
};
71-
ensureIterator(root.Symbol, root);
72-
expect(root.Symbol.iterator).toBe(root.Symbol.for('iterator'));
81+
var result = polyfillSymbol(root);
82+
expect(result.iterator).toBe(root.Symbol.for('iterator'));
7383
});
7484

7585
it('should patch using Set for mozilla bug', function () {
@@ -82,8 +92,8 @@ describe('ensureIterator', function () {
8292
Symbol: {}
8393
};
8494

85-
ensureIterator(root.Symbol, root);
86-
expect(root.Symbol.iterator).toBe('@@iterator');
95+
var result = polyfillSymbol(root);
96+
expect(result.iterator).toBe('@@iterator');
8797
});
8898

8999
it('should patch using map for es6-shim', function () {
@@ -95,7 +105,7 @@ describe('ensureIterator', function () {
95105
root.Map.prototype.key = 'iteratorValue';
96106
root.Map.prototype.entries = 'iteratorValue';
97107

98-
ensureIterator(root.Symbol, root);
99-
expect(root.Symbol.iterator).toBe('key');
108+
var result = polyfillSymbol(root);
109+
expect(result.iterator).toBe('key');
100110
});
101111
});

src/util/SymbolShim.ts

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,19 @@ export function polyfillSymbol(root) {
44
const Symbol = ensureSymbol(root);
55
ensureIterator(Symbol, root);
66
ensureObservable(Symbol);
7+
ensureFor(Symbol);
78
return Symbol;
89
}
910

11+
export function ensureFor(Symbol) {
12+
if (!Symbol.for) {
13+
Symbol.for = symbolForPolyfill;
14+
}
15+
}
16+
1017
export function ensureSymbol(root) {
1118
if (!root.Symbol) {
12-
root.Symbol = {
13-
for: symbolForPolyfill
14-
};
19+
root.Symbol = {};
1520
}
1621
return root.Symbol;
1722
}

0 commit comments

Comments
 (0)