forked from char0n/ramda-adjunct
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Showing
3 changed files
with
131 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
import { hasIn, curryN } from 'ramda'; | ||
|
||
import isFunction from './isFunction'; | ||
|
||
/** | ||
* Checks whether the passed value is async iterable. | ||
* | ||
* @func isAsyncIterable | ||
* @memberOf RA | ||
* @since {@link https://char0n.github.io/ramda-adjunct/3.2.0|v3.2.0} | ||
* @category Type | ||
* @sig * -> Boolean | ||
* @param {*} val The value to test | ||
* @see {@link https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Symbol/asyncIterator} | ||
* @return {boolean} | ||
* @example | ||
* | ||
* RA.isAsyncIterable({ | ||
* async* [Symbol.asyncIterator]() { | ||
* yield "Blade"; | ||
* yield "Runner" | ||
* } | ||
* }); //=> true | ||
* | ||
* RA.isAsyncIterable({}); //=> false | ||
* RA.isAsyncIterable(-0); //=> false | ||
* RA.isAsyncIterable(null); //=> false | ||
* RA.isAsyncIterable(undefined); //=> false | ||
*/ | ||
const isAsyncIterable = curryN(1, (val) => { | ||
if (typeof Symbol === 'undefined') { | ||
return false; | ||
} | ||
|
||
return ( | ||
hasIn(Symbol.asyncIterator, Object(val)) && | ||
isFunction(val[Symbol.asyncIterator]) | ||
); | ||
}); | ||
|
||
export default isAsyncIterable; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
import { assert } from 'chai'; | ||
import * as R from 'ramda'; | ||
|
||
import * as RA from '../src'; | ||
|
||
describe('isAsyncIterable', function () { | ||
context('given the item is an array with items', function () { | ||
specify('should return false', function () { | ||
assert.isFalse(RA.isAsyncIterable(['arrays', 'are', 'iterable'])); | ||
}); | ||
}); | ||
|
||
context( | ||
'given the item is an Object implementing an asyncIterator', | ||
function () { | ||
specify('should return true', function () { | ||
const asyncIterable = { | ||
async *[Symbol.asyncIterator]() { | ||
yield 'Blade'; | ||
yield 'Runner'; | ||
}, | ||
}; | ||
assert.isTrue(RA.isAsyncIterable(asyncIterable)); | ||
}); | ||
} | ||
); | ||
|
||
context('given the value undefined', function () { | ||
specify('should return false', function () { | ||
assert.isFalse(RA.isAsyncIterable(undefined)); | ||
}); | ||
}); | ||
|
||
context('given a number', function () { | ||
specify('should return false', function () { | ||
assert.isFalse(RA.isAsyncIterable(42)); | ||
}); | ||
}); | ||
|
||
context('given the value null', function () { | ||
specify('should return false', function () { | ||
assert.isFalse(RA.isAsyncIterable(null)); | ||
}); | ||
}); | ||
|
||
context('given the value NaN', function () { | ||
specify('should return false', function () { | ||
assert.isFalse(RA.isAsyncIterable(NaN)); | ||
}); | ||
}); | ||
|
||
context('given the value Infinity', function () { | ||
specify('should return false', function () { | ||
assert.isFalse(RA.isAsyncIterable(Infinity)); | ||
}); | ||
}); | ||
|
||
context('given a boolean value', function () { | ||
specify('should return false', function () { | ||
assert.isFalse(RA.isAsyncIterable(true)); | ||
}); | ||
}); | ||
|
||
context('given the value -0', function () { | ||
specify('should return false', function () { | ||
assert.isFalse(RA.isAsyncIterable(-0)); | ||
}); | ||
}); | ||
|
||
context('given an empty object', function () { | ||
specify('should return false', function () { | ||
assert.isFalse(RA.isAsyncIterable({})); | ||
}); | ||
}); | ||
|
||
context('should support placeholder to specify "gaps"', function () { | ||
specify('should return false', function () { | ||
const isAsyncIterable = RA.isAsyncIterable(R.__); | ||
const asyncIterable = { | ||
async *[Symbol.asyncIterator]() { | ||
yield 'Blade'; | ||
yield 'Runner'; | ||
}, | ||
}; | ||
|
||
assert.isTrue(isAsyncIterable(asyncIterable)); | ||
}); | ||
}); | ||
}); |