Add is.nodeStream() (#34)

This commit is contained in:
Brandon Smith 2017-11-19 15:16:06 -05:00 committed by Sindre Sorhus
parent ae3adc9818
commit a04a04c131
4 changed files with 38 additions and 1 deletions

View file

@ -49,9 +49,11 @@
"devDependencies": {
"@types/jsdom": "^2.0.31",
"@types/node": "^8.0.47",
"@types/tempy": "^0.1.0",
"ava": "*",
"del-cli": "^1.1.0",
"jsdom": "^9.12.0",
"tempy": "^0.2.1",
"tslint": "^5.8.0",
"tslint-xo": "^0.3.0",
"typescript": "^2.6.1"

View file

@ -211,6 +211,16 @@ is.inRange(3, 10);
Returns `true` if `value` is a DOM Element.
##### .nodeStream(value)
Returns `true` if `value` is a Node.js [stream](https://nodejs.org/api/stream.html).
```js
const fs = require('fs');
is.nodeStream(fs.createReadStream('unicorn.png'));
//=> true
```
##### .infinite(value)
Check if `value` is `Infinity` or `-Infinity`.

View file

@ -238,6 +238,8 @@ namespace is { // tslint:disable-line:no-namespace
export const domElement = (value: any) => object(value) && value.nodeType === NODE_TYPE_ELEMENT && string(value.nodeName) &&
!plainObject(value) && DOM_PROPERTIES_TO_CHECK.every(property => property in value);
export const nodeStream = (value: any) => !nullOrUndefined(value) && isObject(value) && function_(value.pipe);
export const infinite = (value: any) => value === Infinity || value === -Infinity;
const isAbsoluteMod2 = (value: number) => (rem: number) => integer(rem) && Math.abs(rem % 2) === value;

View file

@ -1,4 +1,8 @@
import * as fs from 'fs';
import * as net from 'net';
import * as Stream from 'stream';
import * as util from 'util';
import * as tempy from 'tempy';
import test, {TestContext, Context} from 'ava';
import {jsdom} from 'jsdom';
import m from '..';
@ -286,7 +290,8 @@ const types = new Map<string, Test>([
'canvas',
'script'
].map(createDomElement) }
], ['non-domElements', {
],
['non-domElements', {
is: value => !m.domElement(value),
fixtures: [
document.createTextNode('data'),
@ -297,6 +302,20 @@ const types = new Map<string, Test>([
document.createDocumentFragment()
]
}],
['nodeStream', {
is: m.nodeStream,
fixtures: [
fs.createReadStream('readme.md'),
fs.createWriteStream(tempy.file()),
new net.Socket(),
new Stream.Duplex(),
new Stream.PassThrough(),
new Stream.Readable(),
new Stream.Transform(),
new Stream.Stream(),
new Stream.Writable()
]
}],
['infinite', {
is: m.infinite,
fixtures: [
@ -641,6 +660,10 @@ test('is.domElement', t => {
t.false(m.domElement({nodeType: 1, nodeName: 'div'}));
});
test('is.nodeStream', t => {
testType(t, 'nodeStream');
});
test('is.infinite', t => {
testType(t, 'infinite', ['number']);
});