add support for is.generator & is.generatorFunction
This commit is contained in:
parent
103c5afe6f
commit
7fcd993232
3 changed files with 22 additions and 1 deletions
4
index.js
4
index.js
|
|
@ -81,6 +81,10 @@ is.promise = x => {
|
|||
);
|
||||
};
|
||||
|
||||
is.generator = x => x && typeof x.next === 'function' && typeof x.throw === 'function';
|
||||
|
||||
is.generatorFunction = x => x.constructor.name === 'GeneratorFunction';
|
||||
|
||||
is.regExp = x => getObjectType(x) === 'RegExp';
|
||||
is.date = x => getObjectType(x) === 'Date';
|
||||
is.error = x => getObjectType(x) === 'Error';
|
||||
|
|
|
|||
|
|
@ -78,6 +78,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)
|
||||
##### .generatorFunction(value)
|
||||
|
||||
##### .map(value)
|
||||
##### .set(value)
|
||||
##### .weakMap(value)
|
||||
|
|
|
|||
16
test.js
16
test.js
|
|
@ -82,7 +82,7 @@ const types = new Map([
|
|||
['integer', 6]
|
||||
]);
|
||||
|
||||
// This ensure a certain method matches only the types
|
||||
// This ensures a certain method matches only the types
|
||||
// it's supposed to and none of the other methods' types
|
||||
const testType = (t, type, exclude) => {
|
||||
for (const [key, value] of types) {
|
||||
|
|
@ -166,6 +166,20 @@ if (isNode8orHigher) {
|
|||
});
|
||||
}
|
||||
|
||||
test('is.generator', t => {
|
||||
const genObj = (function * () {
|
||||
yield 42;
|
||||
})();
|
||||
t.true(m.generator(genObj));
|
||||
});
|
||||
|
||||
test('is.generatorFunction', t => {
|
||||
const gen = function * () {
|
||||
yield 42;
|
||||
};
|
||||
t.true(m.generatorFunction(gen));
|
||||
});
|
||||
|
||||
test('is.map', t => {
|
||||
testType(t, 'map');
|
||||
});
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue