23
snippets/isDuplexStream.md
Normal file
23
snippets/isDuplexStream.md
Normal file
@ -0,0 +1,23 @@
|
||||
### isDuplexStream
|
||||
|
||||
Checks if the given argument is a duplex (readable and writable) stream.
|
||||
|
||||
Check if the value is different from `null`, use `typeof` to check if a value is of type `object` and the `pipe` property is of type `function`.
|
||||
Additionally check if the `typeof` the `_read`, `_write` and `_readableState`, `_writableState` properties are `function` and `object` respectively.
|
||||
|
||||
```js
|
||||
const isDuplexStream = val =>
|
||||
val !== null &&
|
||||
typeof val === 'object' &&
|
||||
typeof val.pipe === 'function' &&
|
||||
typeof val.pipe === 'function' &&
|
||||
typeof val._read === 'function' &&
|
||||
typeof val._readableState === 'object' &&
|
||||
typeof val._write === 'function' &&
|
||||
typeof val._writableState === 'object';
|
||||
```
|
||||
|
||||
```js
|
||||
const Stream = require('stream');
|
||||
isDuplexStream(new Stream.Duplex()); // true
|
||||
```
|
||||
20
snippets/isReadableStream.md
Normal file
20
snippets/isReadableStream.md
Normal file
@ -0,0 +1,20 @@
|
||||
### isReadableStream
|
||||
|
||||
Checks if the given argument is a readable stream.
|
||||
|
||||
Check if the value is different from `null`, use `typeof` to check if the value is of type `object` and the `pipe` property is of type `function`.
|
||||
Additionally check if the `typeof` the `_read` and `_readableState` properties are `function` and `object` respectively.
|
||||
|
||||
```js
|
||||
const isReadableStream = val =>
|
||||
val !== null &&
|
||||
typeof val === 'object' &&
|
||||
typeof val.pipe === 'function' &&
|
||||
typeof val._read === 'function' &&
|
||||
typeof val._readableState === 'object';
|
||||
```
|
||||
|
||||
```js
|
||||
const fs = require('fs');
|
||||
isReadableStream(fs.createReadStream('test.txt')); // true
|
||||
```
|
||||
14
snippets/isStream.md
Normal file
14
snippets/isStream.md
Normal file
@ -0,0 +1,14 @@
|
||||
### isStream
|
||||
|
||||
Checks if the given argument is a stream.
|
||||
|
||||
Check if the value is different from `null`, use `typeof` to check if the value is of type `object` and the `pipe` property is of type `function`.
|
||||
|
||||
```js
|
||||
const isStream = val => val !== null && typeof val === 'object' && typeof val.pipe === 'function';
|
||||
```
|
||||
|
||||
```js
|
||||
const fs = require('fs');
|
||||
isStream(fs.createReadStream('test.txt')); // true
|
||||
```
|
||||
20
snippets/isWritableStream.md
Normal file
20
snippets/isWritableStream.md
Normal file
@ -0,0 +1,20 @@
|
||||
### isWritableStream
|
||||
|
||||
Checks if the given argument is a writable stream.
|
||||
|
||||
Check if the value is different from `null`, use `typeof` to check if the value is of type `object` and the `pipe` property is of type `function`.
|
||||
Additionally check if the `typeof` the `_write` and `_writableState` properties are `function` and `object` respectively.
|
||||
|
||||
```js
|
||||
const isWritableStream = val =>
|
||||
val !== null &&
|
||||
typeof val === 'object' &&
|
||||
typeof val.pipe === 'function' &&
|
||||
typeof val._write === 'function' &&
|
||||
typeof val._writableState === 'object';
|
||||
```
|
||||
|
||||
```js
|
||||
const fs = require('fs');
|
||||
isWritableStream(fs.createWriteStream('test.txt')); // true
|
||||
```
|
||||
Reference in New Issue
Block a user