From 01425d897418bc8c0586a6c6e491c301e1f1f147 Mon Sep 17 00:00:00 2001 From: Volker Mische Date: Thu, 28 Feb 2019 21:39:40 +0100 Subject: [PATCH] Add support for detecting async generators Async generators are also iterable. This commit also adds a test for sync iterator. --- index.js | 5 +++-- test.js | 3 +++ 2 files changed, 6 insertions(+), 2 deletions(-) diff --git a/index.js b/index.js index f0170b1..cb61e3c 100644 --- a/index.js +++ b/index.js @@ -184,7 +184,7 @@ function isPromise (input) { } /** - * Returns true if the input is an iterable (`Map`, `Set`, `Array` etc.). + * Returns true if the input is an iterable (`Map`, `Set`, `Array`, Generator etc.). * @param {*} - the input to test * @returns {boolean} * @static @@ -193,6 +193,7 @@ function isIterable (input) { if (input === null || !isDefined(input)) { return false } else { - return typeof input[Symbol.iterator] === 'function' + return typeof input[Symbol.iterator] === 'function' || + typeof input[Symbol.asyncIterator] === 'function' } } diff --git a/test.js b/test.js index 22455a2..38fe106 100644 --- a/test.js +++ b/test.js @@ -142,6 +142,9 @@ runner.test('.isIterable', function () { a.strictEqual(t.isIterable(new Map()), true) a.strictEqual(t.isIterable([]), true) a.strictEqual(t.isIterable({ then: function () {} }), false) + // Enable these tests once the minimum supported version is Node.js 10 + //a.strictEqual(t.isIterable((function * () {})()), true) + //a.strictEqual(t.isIterable((async function * () {})()), true) }) runner.test('.isObject', function () {