├── .gitignore ├── SECURITY.md ├── .travis.yml ├── is-generator.js ├── LICENSE ├── package.json ├── test.js └── README.md /.gitignore: -------------------------------------------------------------------------------- 1 | .DS_Store 2 | node_modules 3 | coverage 4 | -------------------------------------------------------------------------------- /SECURITY.md: -------------------------------------------------------------------------------- 1 | # Security Policy 2 | 3 | ## Security contact information 4 | 5 | To report a security vulnerability, please use the [Tidelift security contact](https://tidelift.com/security). Tidelift will coordinate the fix and disclosure. 6 | -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | language: node_js 2 | 3 | notifications: 4 | email: 5 | on_success: never 6 | on_failure: change 7 | 8 | node_js: 9 | - "0.11" 10 | - "0.12" 11 | - "iojs" 12 | 13 | after_script: "npm install coveralls@2 && cat ./coverage/lcov.info | coveralls" 14 | -------------------------------------------------------------------------------- /is-generator.js: -------------------------------------------------------------------------------- 1 | /** 2 | * Export generator function checks. 3 | */ 4 | module.exports = isGenerator 5 | module.exports.fn = isGeneratorFunction 6 | 7 | /** 8 | * Check whether an object is a generator. 9 | * 10 | * @param {Object} obj 11 | * @return {Boolean} 12 | */ 13 | function isGenerator (obj) { 14 | return obj && 15 | typeof obj.next === 'function' && 16 | typeof obj.throw === 'function' 17 | } 18 | 19 | /** 20 | * Check whether a function is generator. 21 | * 22 | * @param {Function} fn 23 | * @return {Boolean} 24 | */ 25 | function isGeneratorFunction (fn) { 26 | return typeof fn === 'function' && 27 | fn.constructor && 28 | fn.constructor.name === 'GeneratorFunction' 29 | } 30 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2014 Blake Embrey (hello@blakeembrey.com) 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in 13 | all copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 21 | THE SOFTWARE. 22 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "is-generator", 3 | "version": "1.0.3", 4 | "description": "Check whether a value is a generator or generator function", 5 | "main": "is-generator.js", 6 | "scripts": { 7 | "lint": "standard", 8 | "test-spec": "mocha -R spec --bail", 9 | "test-cov": "node --harmony node_modules/.bin/istanbul cover node_modules/mocha/bin/_mocha -- -R spec --bail", 10 | "test": "npm run lint && npm run test-cov" 11 | }, 12 | "repository": { 13 | "type": "git", 14 | "url": "git://github.com/blakeembrey/is-generator.git" 15 | }, 16 | "keywords": [ 17 | "generator", 18 | "generator function", 19 | "yield", 20 | "function", 21 | "await", 22 | "es6", 23 | "function*" 24 | ], 25 | "author": { 26 | "name": "Blake Embrey", 27 | "email": "hello@blakeembrey.com", 28 | "url": "http://blakeembrey.me" 29 | }, 30 | "license": "MIT", 31 | "bugs": { 32 | "url": "https://github.com/blakeembrey/is-generator/issues" 33 | }, 34 | "homepage": "https://github.com/blakeembrey/is-generator", 35 | "devDependencies": { 36 | "istanbul": "git://github.com/gotwarlost/istanbul#harmony", 37 | "mocha": "^3.3.0", 38 | "pre-commit": "^1.0.7", 39 | "standard": "^10.0.2" 40 | } 41 | } 42 | -------------------------------------------------------------------------------- /test.js: -------------------------------------------------------------------------------- 1 | /* global describe, it */ 2 | 3 | var assert = require('assert') 4 | var isGenerator = require('./') 5 | 6 | describe('is-generator', function () { 7 | describe('generators', function () { 8 | it('should return false with non-generators', function () { 9 | assert(!isGenerator(null)) 10 | assert(!isGenerator(25)) 11 | assert(!isGenerator('test')) 12 | assert(!isGenerator(/* istanbul ignore next */ function () {})) 13 | assert(!isGenerator(/* istanbul ignore next */ function * () {})) 14 | }) 15 | 16 | it('should return true with a generator', function () { 17 | assert(isGenerator((/* istanbul ignore next */ function * () {})())) 18 | }) 19 | }) 20 | 21 | describe('generator functions', function () { 22 | it('should return false with non-generator function', function () { 23 | assert(!isGenerator.fn(null)) 24 | assert(!isGenerator.fn(25)) 25 | assert(!isGenerator.fn('test')) 26 | assert(!isGenerator.fn(/* istanbul ignore next */ function () {})) 27 | 28 | var noConstructorFn = /* istanbul ignore next */ function () {} 29 | noConstructorFn.constructor = undefined 30 | 31 | assert(!isGenerator.fn(noConstructorFn)) 32 | }) 33 | 34 | it('should return true with a generator function', function () { 35 | assert(isGenerator.fn(/* istanbul ignore next */ function * () { yield 'something' })) 36 | }) 37 | }) 38 | }) 39 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Is Generator 2 | 3 | [![NPM version][npm-image]][npm-url] 4 | [![NPM downloads][downloads-image]][downloads-url] 5 | [![Build status][travis-image]][travis-url] 6 | [![Test coverage][coveralls-image]][coveralls-url] 7 | [![Greenkeeper badge](https://badges.greenkeeper.io/blakeembrey/is-generator.svg)](https://greenkeeper.io/) 8 | 9 | > Check whether a value is a generator or generator function. 10 | 11 | **Generator:** A specific type of iterator object. [Reference][def-gen]. 12 | **Generator Function:** A `function*` declaration, returns a generator object. [Reference][def-gen-fn]. 13 | 14 | ## Installation 15 | 16 | ``` 17 | npm install is-generator --save 18 | ``` 19 | 20 | ## Usage 21 | 22 | ```javascript 23 | var isGenerator = require('is-generator') 24 | var isGeneratorFn = require('is-generator').fn 25 | 26 | isGenerator(null) //=> false 27 | isGenerator(function * () {}) //=> false 28 | isGenerator((function * () {})()) //=> true 29 | 30 | isGeneratorFn(null) //=> false 31 | isGeneratorFn(function () {}) //=> false 32 | isGeneratorFn(function * () {}) //=> true 33 | ``` 34 | 35 | ## License 36 | 37 | MIT 38 | 39 | [npm-image]: https://img.shields.io/npm/v/is-generator.svg?style=flat 40 | [npm-url]: https://npmjs.org/package/is-generator 41 | [downloads-image]: https://img.shields.io/npm/dm/is-generator.svg?style=flat 42 | [downloads-url]: https://npmjs.org/package/is-generator 43 | [travis-image]: https://img.shields.io/travis/blakeembrey/is-generator.svg?style=flat 44 | [travis-url]: https://travis-ci.org/blakeembrey/is-generator 45 | [coveralls-image]: https://img.shields.io/coveralls/blakeembrey/is-generator.svg?style=flat 46 | [coveralls-url]: https://coveralls.io/r/blakeembrey/is-generator?branch=master 47 | [def-gen]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Generator 48 | [def-gen-fn]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/function* 49 | --------------------------------------------------------------------------------