is-generator
Advanced tools
Comparing version 0.0.1 to 0.0.2
@@ -1,3 +0,22 @@ | ||
module.exports = function (f) { | ||
return typeof f === 'function' && f.constructor.name === 'GeneratorFunction'; | ||
/** | ||
* Check whether an object is a generator. | ||
* | ||
* @param {Object} obj | ||
* @return {Boolean} | ||
*/ | ||
exports = module.exports = function (obj) { | ||
return typeof obj === 'function' && | ||
typeof obj.next === 'function' && | ||
typeof obj.throw === 'function'; | ||
}; | ||
/** | ||
* Check whether a function is generator. | ||
* | ||
* @param {Function} fn | ||
* @return {Boolean} | ||
*/ | ||
exports.fn = function (fn) { | ||
return typeof fn === 'function' && | ||
fn.constructor.name === 'GeneratorFunction'; | ||
}; |
{ | ||
"name": "is-generator", | ||
"version": "0.0.1", | ||
"description": "Check whether a function is a generator", | ||
"version": "0.0.2", | ||
"description": "Check whether a value is a generator or generator function", | ||
"main": "is-generator.js", | ||
@@ -6,0 +6,0 @@ "scripts": { |
# is-generator | ||
Check whether a given value is a generator function. | ||
Check whether a given value is a generator. | ||
@@ -14,7 +14,12 @@ ## Installation | ||
```javascript | ||
var isGenerator = require('is-generator'); | ||
var isGenerator = require('is-generator'); | ||
var isGeneratorFn = require('is-generator').fn; | ||
isGenerator(null); //=> false | ||
isGenerator(function () {}); //=> false | ||
isGenerator(function* () {}); //=> true | ||
isGenerator(null); //=> false | ||
isGenerator(function* () {}); //=> false | ||
isGenerator((function* () {})()); //=> true | ||
isGeneratorFn(null); //=> false | ||
isGeneratorFn(function () {}); //=> false | ||
isGeneratorFn(function* () {}); //=> true | ||
``` | ||
@@ -21,0 +26,0 @@ |
30
test.js
@@ -7,12 +7,28 @@ /* global describe, it */ | ||
describe('is-generator', function () { | ||
it('should return false with non-generators', function () { | ||
assert(!isGenerator(null)); | ||
assert(!isGenerator(25)); | ||
assert(!isGenerator('test')); | ||
assert(!isGenerator(function () {})); | ||
describe('generators', function () { | ||
it('should return false with non-generators', function () { | ||
assert(!isGenerator(null)); | ||
assert(!isGenerator(25)); | ||
assert(!isGenerator('test')); | ||
assert(!isGenerator(function () {})); | ||
assert(!isGenerator(function* () {})); | ||
}); | ||
it('should return true with a generator', function () { | ||
assert(!isGenerator((function* () {})())); | ||
}); | ||
}); | ||
it('should return true with a generator', function () { | ||
assert(isGenerator(function* () { yield 'something'; })); | ||
describe('generator functions', function () { | ||
it('should return false with non-generator function', function () { | ||
assert(!isGenerator.fn(null)); | ||
assert(!isGenerator.fn(25)); | ||
assert(!isGenerator.fn('test')); | ||
assert(!isGenerator.fn(function () {})); | ||
}); | ||
it('should return true with a generator function', function () { | ||
assert(isGenerator.fn(function* () { yield 'something'; })); | ||
}); | ||
}); | ||
}); |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
3881
49
29