Skip to content

Commit

Permalink
fix(Symbol.iterator): correctly handle case where Symbol constructor …
Browse files Browse the repository at this point in the history
…itself is not defined
  • Loading branch information
jayphelps committed Mar 7, 2018
1 parent 6319f3c commit 7d15389
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 8 deletions.
33 changes: 33 additions & 0 deletions spec/symbol/iterator-spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import { expect } from 'chai';
import * as sinon from 'sinon';
import { root } from '../../src/internal/util/root';
import { getSymbolIterator } from '../../src/internal/symbol/iterator';

describe('iterator symbol', () => {
it('should use the real Symbol.iterator', () => {
const Symbol_iterator = getSymbolIterator(root);
expect(Symbol_iterator).to.equal(Symbol.iterator);
});

it('should use a fake @@iterator string as a symbol when it the real one does not exist', () => {
sinon.spy(console, 'warn');

const Symbol_iterator = getSymbolIterator({
Symbol: (() => {}) as SymbolConstructor // tslint:disable-line:no-empty
});
expect(Symbol_iterator).to.equal('@@iterator');
expect(console.warn).have.been.calledWithExactly('RxJS: Symbol.iterator does not exist, so things like from(iterable) won\'t work');

(console.warn as any).restore();
});

it('should use a fake @@iterator string as a symbol when Symbol is not even defined', () => {
sinon.spy(console, 'warn');

const Symbol_iterator = getSymbolIterator({});
expect(Symbol_iterator).to.equal('@@iterator');
expect(console.warn).have.been.calledWithExactly("RxJS: Symbol.iterator does not exist, so things like from(iterable) won't work");

(console.warn as any).restore();
});
});
21 changes: 13 additions & 8 deletions src/internal/symbol/iterator.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,19 @@
import { root } from '../util/root';

/* NOTE: Warning users that they don't have a Symbol.iterator
polyfill. We don't want to throw on this, because it's not required
by the library. However it will provide clues to users on older
browsers why things like `from(iterable)` doesn't work. */
if (!Symbol || !Symbol.iterator) {
console.warn('RxJS: Symbol.observable does not exist');
export function getSymbolIterator(root: { Symbol?: SymbolConstructor }): symbol {
/* NOTE: Warning users that they don't have a Symbol.iterator
polyfill. We don't want to throw on this, because it's not required
by the library. However it will provide clues to users on older
browsers why things like `from(iterable)` doesn't work. */
if (typeof root.Symbol !== 'function' || !root.Symbol.iterator) {
console.warn('RxJS: Symbol.iterator does not exist, so things like from(iterable) won\'t work');
return '@@iterator' as any;
}

return root.Symbol.iterator;
}

/** The native Symbol.iterator instance or a string */
export const iterator = Symbol && Symbol.iterator || '@@iterator';
export const iterator = getSymbolIterator(root);

/**
* @deprecated use {@link iterator} instead
Expand Down

0 comments on commit 7d15389

Please sign in to comment.