Skip to content

Commit

Permalink
Add support for detecting async generators
Browse files Browse the repository at this point in the history
Async generators are also iterable.

This commit also adds a test for sync iterator.
  • Loading branch information
vmx committed Feb 28, 2019
1 parent cee1951 commit 01425d8
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 2 deletions.
5 changes: 3 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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'
}
}
3 changes: 3 additions & 0 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 () {
Expand Down

0 comments on commit 01425d8

Please sign in to comment.