From 83e48df3292968c76a80e07348a23779c156224d Mon Sep 17 00:00:00 2001 From: Kunall Banerjee Date: Tue, 26 Sep 2017 18:18:28 -0400 Subject: [PATCH] :hammer: changes --- index.js | 4 ++-- readme.md | 3 +++ test.js | 8 ++++---- 3 files changed, 9 insertions(+), 6 deletions(-) diff --git a/index.js b/index.js index 2019ec7..005efb9 100644 --- a/index.js +++ b/index.js @@ -81,9 +81,9 @@ is.promise = x => { ); }; -is.generator = x => x && typeof x.next === 'function' && typeof x.throw === 'function'; +is.generator = x => is.iterable(x) && is.function(x.next) && is.function(x.throw); -is.generatorFunction = x => x.constructor.name === 'GeneratorFunction'; +is.generatorFunction = x => getObjectType(x) === 'GeneratorFunction'; is.regExp = x => getObjectType(x) === 'RegExp'; is.date = x => getObjectType(x) === 'Date'; diff --git a/readme.md b/readme.md index e63a2e6..c54e378 100644 --- a/readme.md +++ b/readme.md @@ -79,6 +79,9 @@ Keep in mind that [functions are objects too](https://developer.mozilla.org/en-U Returns `true` for any object with a `.then()` and `.catch()` method. Prefer this one over `.nativePromise()` as you usually want to allow userland promise implementations too. ##### .generator(value) + +Returns `true` for any object that implements its own `.next()` and `.throw()` methods and has a function definition for `Symbol.iterator`. + ##### .generatorFunction(value) ##### .map(value) diff --git a/test.js b/test.js index 0b9e486..4194c0c 100644 --- a/test.js +++ b/test.js @@ -54,6 +54,9 @@ const types = new Map([ PromiseSubclassFixture.resolve() ]], ['promise', {then() {}, catch() {}}], + ['generator', (function * () { + yield 42; + })()], ['map', new Map()], ['set', new Set()], ['weakMap', new WeakMap()], @@ -167,10 +170,7 @@ if (isNode8orHigher) { } test('is.generator', t => { - const genObj = (function * () { - yield 42; - })(); - t.true(m.generator(genObj)); + testType(t, 'generator', ['function']); }); test('is.generatorFunction', t => {