Add readable, writable and duplex

This commit is contained in:
Angelos Chalaris
2018-10-03 22:16:10 +03:00
parent fd23b0ca81
commit 551a221df8
16 changed files with 161 additions and 3 deletions

0
isReadableStream.js Normal file
View File

0
isStream.js Normal file
View File

0
isWritableStream.js Normal file
View File

View 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
```

View 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
```

View File

@ -2,7 +2,7 @@
Checks if the given argument is a stream. Checks if the given argument is a 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`. 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 ```js
const isStream = val => val !== null && typeof val === 'object' && typeof val.pipe === 'function'; const isStream = val => val !== null && typeof val === 'object' && typeof val.pipe === 'function';

View 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
```

View File

@ -139,6 +139,7 @@ isBoolean:type,beginner
isBrowser:utility,browser,intermediate isBrowser:utility,browser,intermediate
isBrowserTabFocused:browser,beginner isBrowserTabFocused:browser,beginner
isDivisible:math,beginner isDivisible:math,beginner
isDuplexStream:node,type,intermediate
isEmpty:type,array,object,string,beginner isEmpty:type,array,object,string,beginner
isEven:math,beginner isEven:math,beginner
isFunction:type,function,beginner isFunction:type,function,beginner
@ -152,6 +153,7 @@ isPlainObject:type,object,intermediate
isPrime:math,beginner,intermediate isPrime:math,beginner,intermediate
isPrimitive:type,function,array,string,intermediate isPrimitive:type,function,array,string,intermediate
isPromiseLike:type,function,promise,intermediate isPromiseLike:type,function,promise,intermediate
isReadableStream:node,type,intermediate
isSameDate:date,utility,beginner isSameDate:date,utility,beginner
isSorted:array,intermediate isSorted:array,intermediate
isStream:node,type,intermediate isStream:node,type,intermediate
@ -161,6 +163,7 @@ isTravisCI:node,intermediate
isUndefined:type,beginner isUndefined:type,beginner
isUpperCase:string,utility,beginner isUpperCase:string,utility,beginner
isValidJSON:type,json,intermediate isValidJSON:type,json,intermediate
isWritableStream:node,type,intermediate
join:array,intermediate join:array,intermediate
JSONtoCSV:array,string,object,advanced JSONtoCSV:array,string,object,advanced
JSONToFile:node,json,intermediate JSONToFile:node,json,intermediate

View File

@ -0,0 +1,10 @@
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';
module.exports = isDuplexStream;

View File

@ -0,0 +1,20 @@
const expect = require('expect');
const isDuplexStream = require('./isDuplexStream.js');
const fs = require('fs');
const Stream = require('stream');
test('isDuplexStream is a Function', () => {
expect(isDuplexStream).toBeInstanceOf(Function);
});
test('isDuplexStream returns true for read streams', () => {
expect(isDuplexStream(fs.createReadStream('isDuplexStream.js'))).toBeTruthy();
});
test('isDuplexStream returns true for write streams', () => {
expect(isDuplexStream(fs.createWriteStream('isDuplexStream.js'))).toBeTruthy();
});
test('isDuplexStream returns true for duplex streams', () => {
expect(isDuplexStream(new Stream.Duplex())).toBeTruthy();
});
test('isDuplexStream returns false for non-streams', () => {
expect(isDuplexStream({})).toBeFalsy();
});

View File

@ -0,0 +1,7 @@
const isReadableStream = val =>
val !== null &&
typeof val === 'object' &&
typeof val.pipe === 'function' &&
typeof val._read === 'function' &&
typeof val._readableState === 'object';
module.exports = isReadableStream;

View File

@ -0,0 +1,20 @@
const expect = require('expect');
const isReadableStream = require('./isReadableStream.js');
const fs = require('fs');
const Stream = require('stream');
test('isReadableStream is a Function', () => {
expect(isReadableStream).toBeInstanceOf(Function);
});
test('isReadableStream returns true for read streams', () => {
expect(isReadableStream(fs.createReadStream('isReadableStream.js'))).toBeTruthy();
});
test('isReadableStream returns false for write streams', () => {
expect(isReadableStream(fs.createWriteStream('isReadableStream.js'))).toBeFalsy();
});
test('isReadableStream returns true for duplex streams', () => {
expect(isReadableStream(new Stream.Duplex())).toBeTruthy();
});
test('isReadableStream returns false for non-streams', () => {
expect(isReadableStream({})).toBeFalsy();
});

View File

@ -1,13 +1,20 @@
const expect = require('expect'); const expect = require('expect');
const isStream = require('./isStream.js'); const isStream = require('./isStream.js');
const fs = require('fs'); const fs = require('fs');
const Stream = require('stream');
test('isStream is a Function', () => { test('isStream is a Function', () => {
expect(isStream).toBeInstanceOf(Function); expect(isStream).toBeInstanceOf(Function);
}); });
test('isStream returns true for streams', () => { test('isStream returns true for read streams', () => {
expect(isStream(fs.createReadStream('isStream.js'))).toBeTruthy(); expect(isStream(fs.createReadStream('isStream.js'))).toBeTruthy();
}); });
test('isStream returns true for write streams', () => {
expect(isStream(fs.createWriteStream('isStream.js'))).toBeTruthy();
});
test('isStream returns true for duplex streams', () => {
expect(isStream(new Stream.Duplex())).toBeTruthy();
});
test('isStream returns false for non-streams', () => { test('isStream returns false for non-streams', () => {
expect(isStream({})).toBeFalsy(); expect(isStream({})).toBeFalsy();
}); });

View File

@ -0,0 +1,7 @@
const isWritableStream = val =>
val !== null &&
typeof val === 'object' &&
typeof val.pipe === 'function' &&
typeof val._write === 'function' &&
typeof val._writableState === 'object';
module.exports = isWritableStream;

View File

@ -0,0 +1,20 @@
const expect = require('expect');
const isWritableStream = require('./isWritableStream.js');
const fs = require('fs');
const Stream = require('stream');
test('isWritableStream is a Function', () => {
expect(isWritableStream).toBeInstanceOf(Function);
});
test('isWritableStream returns false for read streams', () => {
expect(isWritableStream(fs.createReadStream('isWritableStream.js'))).toBeFalsy();
});
test('isWritableStream returns true for write streams', () => {
expect(isWritableStream(fs.createWriteStream('isWritableStream.js'))).toBeTruthy();
});
test('isWritableStream returns true for duplex streams', () => {
expect(isWritableStream(new Stream.Duplex())).toBeTruthy();
});
test('isWritableStream returns false for non-streams', () => {
expect(isWritableStream({})).toBeFalsy();
});

View File

@ -1,2 +1,3 @@
const takeRightWhile = (arr, func) => arr.reduceRight((acc, el) => func(el) ? acc : [el, ...acc], []); const takeRightWhile = (arr, func) =>
arr.reduceRight((acc, el) => (func(el) ? acc : [el, ...acc]), []);
module.exports = takeRightWhile; module.exports = takeRightWhile;