Skip to content

Commit

Permalink
ReduxRoutine: Improve isGenerator accuracy
Browse files Browse the repository at this point in the history
  • Loading branch information
aduth committed Aug 7, 2018
1 parent a675f49 commit c1f1c35
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 1 deletion.
7 changes: 6 additions & 1 deletion packages/redux-routine/src/is-generator.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,15 @@
/**
* Returns true if the given object is a generator, or false otherwise.
*
* @link https://www.ecma-international.org/ecma-262/6.0/#sec-generator-objects
*
* @param {*} object Object to test.
*
* @return {boolean} Whether object is a generator.
*/
export default function isGenerator( object ) {
return !! object && typeof object.next === 'function';
return (
object &&
object[ Symbol.toStringTag ] === 'Generator'
);
}
12 changes: 12 additions & 0 deletions packages/redux-routine/src/test/is-generator.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,18 @@ describe( 'isGenerator', () => {
} );
} );

it( 'should return false if an imposter!', () => {
const value = { next() {} };

expect( isGenerator( value ) ).toBe( false );
} );

it( 'should return false if an async generator', () => {
const value = ( async function* () {}() );

expect( isGenerator( value ) ).toBe( false );
} );

it( 'should return true if a generator', () => {
const value = ( function* () {}() );

Expand Down

0 comments on commit c1f1c35

Please sign in to comment.