1.0.9 • Published 9 years ago

is-generator-es6 v1.0.9

Weekly downloads
11
License
MIT
Repository
github
Last release
9 years ago

IS-GENERATOR-ES6

Checks ES6:Generator or ES6:GeneratorFunction

  • It doesn't use fn.name

  • It handles values from another frame (browsers)

  • It only checks ES6 generators & ES6 generator functions

  • The module doesn't throw a SyntaxError when it is loaded in older browsers/servers thanks to new Function()

{value} is GeneratorFunction

The function tests if your {value} is an instance of function* (){} constructor.

If the code is executed inside a navigator and instanceof failed, the function will check if the argument begins with function\* to deal with value from another frame.

var isGeneratorFunction = require('is-generator-es6').isGeneratorFunction;
isGeneratorFunction(function* (){}) //returns true

{value} is Generator

Use Object.prototype.toString to ensure that {value} is a ES6:Generator after checking the presence of nextand throw.

var isGenerator = require('is-generator-es6'); 
isGenerator((function* (){})()) //returns true

{value} is Generator n°2

Checks if value.__proto__.__proto__ is equal to (function*(){}).prototype.__proto__ after checking the presence of next and throw.

If the code is executed inside a browser, it will call Object.prototype.toString if the first condition failed.

This solution has better performances than the previous one, however it doesn't offer the same accuracy and it doesn't deal with prototype chaining.

var isGenerator = require('is-generator-es6')._isGenerator; 
isGenerator((function* (){})()) //returns true

To deal with prototype chaining, one solution would be to implement a loop that goes deeper at each iteration until it hits a __proto__ that match (function*(){}).prototype.__proto__.

1.0.9

9 years ago

1.0.8

9 years ago

1.0.7

9 years ago

1.0.6

9 years ago

1.0.5

9 years ago

1.0.4

9 years ago

1.0.3

9 years ago

1.0.2

9 years ago

1.0.1

9 years ago

1.0.0

9 years ago