Add is.asyncIterable (#59)
This commit is contained in:
parent
c84c2cbeca
commit
c8736c2972
4 changed files with 20 additions and 1 deletions
|
|
@ -209,6 +209,7 @@ Returns `true` if `value` is a [safe integer](https://developer.mozilla.org/en-U
|
||||||
An object is plain if it's created by either `{}`, `new Object()`, or `Object.create(null)`.
|
An object is plain if it's created by either `{}`, `new Object()`, or `Object.create(null)`.
|
||||||
|
|
||||||
##### .iterable(value)
|
##### .iterable(value)
|
||||||
|
##### .asyncIterable(value)
|
||||||
##### .class(value)
|
##### .class(value)
|
||||||
|
|
||||||
Returns `true` for instances created by a ES2015 class.
|
Returns `true` for instances created by a ES2015 class.
|
||||||
|
|
|
||||||
|
|
@ -136,6 +136,7 @@ namespace is { // tslint:disable-line:no-namespace
|
||||||
export const nullOrUndefined = (value: any): value is null | undefined => null_(value) || undefined(value);
|
export const nullOrUndefined = (value: any): value is null | undefined => null_(value) || undefined(value);
|
||||||
export const object = (value: any): value is object => !nullOrUndefined(value) && (function_(value) || isObject(value));
|
export const object = (value: any): value is object => !nullOrUndefined(value) && (function_(value) || isObject(value));
|
||||||
export const iterable = (value: any): value is IterableIterator<any> => !nullOrUndefined(value) && function_(value[Symbol.iterator]);
|
export const iterable = (value: any): value is IterableIterator<any> => !nullOrUndefined(value) && function_(value[Symbol.iterator]);
|
||||||
|
export const asyncIterable = (value: any): value is AsyncIterableIterator<any> => !nullOrUndefined(value) && function_(value[Symbol.asyncIterator]);
|
||||||
export const generator = (value: any): value is Generator => iterable(value) && function_(value.next) && function_(value.throw);
|
export const generator = (value: any): value is Generator => iterable(value) && function_(value.next) && function_(value.throw);
|
||||||
|
|
||||||
export const nativePromise = (value: any): value is Promise<any> =>
|
export const nativePromise = (value: any): value is Promise<any> =>
|
||||||
|
|
|
||||||
|
|
@ -10,6 +10,7 @@ import ZenObservable from 'zen-observable';
|
||||||
import m from '..';
|
import m from '..';
|
||||||
|
|
||||||
const isNode8orHigher = Number(process.versions.node.split('.')[0]) >= 8;
|
const isNode8orHigher = Number(process.versions.node.split('.')[0]) >= 8;
|
||||||
|
const isNode10orHigher = Number(process.versions.node.split('.')[0]) >= 10;
|
||||||
|
|
||||||
class PromiseSubclassFixture<T> extends Promise<T> {}
|
class PromiseSubclassFixture<T> extends Promise<T> {}
|
||||||
class ErrorSubclassFixture extends Error {}
|
class ErrorSubclassFixture extends Error {}
|
||||||
|
|
@ -603,6 +604,21 @@ test('is.iterable', t => {
|
||||||
t.false(m.iterable({}));
|
t.false(m.iterable({}));
|
||||||
});
|
});
|
||||||
|
|
||||||
|
if (isNode10orHigher) {
|
||||||
|
test('is.asyncIterable', t => {
|
||||||
|
t.true(m.asyncIterable({
|
||||||
|
[Symbol.asyncIterator]: () => {} // tslint:disable-line:no-empty
|
||||||
|
}));
|
||||||
|
|
||||||
|
t.false(m.asyncIterable(null));
|
||||||
|
t.false(m.asyncIterable(undefined));
|
||||||
|
t.false(m.asyncIterable(0));
|
||||||
|
t.false(m.asyncIterable(NaN));
|
||||||
|
t.false(m.asyncIterable(Infinity));
|
||||||
|
t.false(m.asyncIterable({}));
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
test('is.class', t => {
|
test('is.class', t => {
|
||||||
class Foo {} // tslint:disable-line
|
class Foo {} // tslint:disable-line
|
||||||
const classDeclarations = [
|
const classDeclarations = [
|
||||||
|
|
|
||||||
|
|
@ -6,7 +6,8 @@
|
||||||
"dom",
|
"dom",
|
||||||
"es2015",
|
"es2015",
|
||||||
"es2016",
|
"es2016",
|
||||||
"es2017.sharedmemory"
|
"es2017.sharedmemory",
|
||||||
|
"esnext.asynciterable"
|
||||||
],
|
],
|
||||||
"module": "commonjs",
|
"module": "commonjs",
|
||||||
"moduleResolution": "node",
|
"moduleResolution": "node",
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue