From 55c2bd3ac4f5ce2f3789f2faa0245338288c4364 Mon Sep 17 00:00:00 2001 From: Angelos Chalaris Date: Mon, 1 Oct 2018 20:12:19 +0300 Subject: [PATCH 1/3] Add isStream --- snippets/isStream.md | 14 ++++++++++++++ tag_database | 1 + test/isStream/isStream.js | 2 ++ test/isStream/isStream.test.js | 13 +++++++++++++ 4 files changed, 30 insertions(+) create mode 100644 snippets/isStream.md create mode 100644 test/isStream/isStream.js create mode 100644 test/isStream/isStream.test.js diff --git a/snippets/isStream.md b/snippets/isStream.md new file mode 100644 index 000000000..bce9ea22a --- /dev/null +++ b/snippets/isStream.md @@ -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 a 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 +``` diff --git a/tag_database b/tag_database index 41f8e4564..94f6f2a88 100644 --- a/tag_database +++ b/tag_database @@ -154,6 +154,7 @@ isPrimitive:type,function,array,string,intermediate isPromiseLike:type,function,promise,intermediate isSameDate:date,utility,beginner isSorted:array,intermediate +isStream:type,stream,intermediate isString:type,string,beginner isSymbol:type,beginner isTravisCI:node,intermediate diff --git a/test/isStream/isStream.js b/test/isStream/isStream.js new file mode 100644 index 000000000..cbdd9bd61 --- /dev/null +++ b/test/isStream/isStream.js @@ -0,0 +1,2 @@ +const isStream = val => val !== null && typeof val === 'object' && typeof val.pipe === 'function'; +module.exports = isStream; diff --git a/test/isStream/isStream.test.js b/test/isStream/isStream.test.js new file mode 100644 index 000000000..07dd965b1 --- /dev/null +++ b/test/isStream/isStream.test.js @@ -0,0 +1,13 @@ +const expect = require('expect'); +const isStream = require('./isStream.js'); +const fs = require('fs'); + +test('isStream is a Function', () => { + expect(isStream).toBeInstanceOf(Function); +}); +test('isStream returns true for streams', () => { + expect(isStream(fs.createReadStream('isStream.js'))).toBeTruthy(); +}); +test('isStream returns false for non-streams', () => { + expect(isStream({})).toBeFalsy(); +}); From bff38c6387f153d4bb15cf209cbaae9678960abd Mon Sep 17 00:00:00 2001 From: Angelos Chalaris Date: Tue, 2 Oct 2018 08:35:37 +0300 Subject: [PATCH 2/3] Update tag_database --- tag_database | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tag_database b/tag_database index 94f6f2a88..a93ae3bcc 100644 --- a/tag_database +++ b/tag_database @@ -154,7 +154,7 @@ isPrimitive:type,function,array,string,intermediate isPromiseLike:type,function,promise,intermediate isSameDate:date,utility,beginner isSorted:array,intermediate -isStream:type,stream,intermediate +isStream:node,type,intermediate isString:type,string,beginner isSymbol:type,beginner isTravisCI:node,intermediate From 21622234d6aa66394f1fec851d34dd4a1c686494 Mon Sep 17 00:00:00 2001 From: Angelos Chalaris Date: Wed, 3 Oct 2018 22:16:10 +0300 Subject: [PATCH 3/3] Add readable, writable and duplex --- isReadableStream.js | 0 isStream.js | 0 isWritableStream.js | 0 snippets/isDuplexStream.md | 23 +++++++++++++++++++ snippets/isReadableStream.md | 20 ++++++++++++++++ snippets/isStream.md | 2 +- snippets/isWritableStream.md | 20 ++++++++++++++++ tag_database | 3 +++ test/isDuplexStream/isDuplexStream.js | 10 ++++++++ test/isDuplexStream/isDuplexStream.test.js | 20 ++++++++++++++++ test/isReadableStream/isReadableStream.js | 7 ++++++ .../isReadableStream/isReadableStream.test.js | 20 ++++++++++++++++ test/isStream/isStream.test.js | 9 +++++++- test/isWritableStream/isWritableStream.js | 7 ++++++ .../isWritableStream/isWritableStream.test.js | 20 ++++++++++++++++ test/takeRightWhile/takeRightWhile.js | 3 ++- 16 files changed, 161 insertions(+), 3 deletions(-) create mode 100644 isReadableStream.js create mode 100644 isStream.js create mode 100644 isWritableStream.js create mode 100644 snippets/isDuplexStream.md create mode 100644 snippets/isReadableStream.md create mode 100644 snippets/isWritableStream.md create mode 100644 test/isDuplexStream/isDuplexStream.js create mode 100644 test/isDuplexStream/isDuplexStream.test.js create mode 100644 test/isReadableStream/isReadableStream.js create mode 100644 test/isReadableStream/isReadableStream.test.js create mode 100644 test/isWritableStream/isWritableStream.js create mode 100644 test/isWritableStream/isWritableStream.test.js diff --git a/isReadableStream.js b/isReadableStream.js new file mode 100644 index 000000000..e69de29bb diff --git a/isStream.js b/isStream.js new file mode 100644 index 000000000..e69de29bb diff --git a/isWritableStream.js b/isWritableStream.js new file mode 100644 index 000000000..e69de29bb diff --git a/snippets/isDuplexStream.md b/snippets/isDuplexStream.md new file mode 100644 index 000000000..24e5b05a8 --- /dev/null +++ b/snippets/isDuplexStream.md @@ -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 +``` diff --git a/snippets/isReadableStream.md b/snippets/isReadableStream.md new file mode 100644 index 000000000..3ac9701eb --- /dev/null +++ b/snippets/isReadableStream.md @@ -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 +``` diff --git a/snippets/isStream.md b/snippets/isStream.md index bce9ea22a..6cb83a703 100644 --- a/snippets/isStream.md +++ b/snippets/isStream.md @@ -2,7 +2,7 @@ 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 const isStream = val => val !== null && typeof val === 'object' && typeof val.pipe === 'function'; diff --git a/snippets/isWritableStream.md b/snippets/isWritableStream.md new file mode 100644 index 000000000..8c30b8d2b --- /dev/null +++ b/snippets/isWritableStream.md @@ -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 +``` diff --git a/tag_database b/tag_database index a93ae3bcc..f84b5ee40 100644 --- a/tag_database +++ b/tag_database @@ -139,6 +139,7 @@ isBoolean:type,beginner isBrowser:utility,browser,intermediate isBrowserTabFocused:browser,beginner isDivisible:math,beginner +isDuplexStream:node,type,intermediate isEmpty:type,array,object,string,beginner isEven:math,beginner isFunction:type,function,beginner @@ -152,6 +153,7 @@ isPlainObject:type,object,intermediate isPrime:math,beginner,intermediate isPrimitive:type,function,array,string,intermediate isPromiseLike:type,function,promise,intermediate +isReadableStream:node,type,intermediate isSameDate:date,utility,beginner isSorted:array,intermediate isStream:node,type,intermediate @@ -161,6 +163,7 @@ isTravisCI:node,intermediate isUndefined:type,beginner isUpperCase:string,utility,beginner isValidJSON:type,json,intermediate +isWritableStream:node,type,intermediate join:array,intermediate JSONtoCSV:array,string,object,advanced JSONToFile:node,json,intermediate diff --git a/test/isDuplexStream/isDuplexStream.js b/test/isDuplexStream/isDuplexStream.js new file mode 100644 index 000000000..fe650454b --- /dev/null +++ b/test/isDuplexStream/isDuplexStream.js @@ -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; diff --git a/test/isDuplexStream/isDuplexStream.test.js b/test/isDuplexStream/isDuplexStream.test.js new file mode 100644 index 000000000..6ff332242 --- /dev/null +++ b/test/isDuplexStream/isDuplexStream.test.js @@ -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(); +}); diff --git a/test/isReadableStream/isReadableStream.js b/test/isReadableStream/isReadableStream.js new file mode 100644 index 000000000..a514cc8d2 --- /dev/null +++ b/test/isReadableStream/isReadableStream.js @@ -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; diff --git a/test/isReadableStream/isReadableStream.test.js b/test/isReadableStream/isReadableStream.test.js new file mode 100644 index 000000000..7c5174d99 --- /dev/null +++ b/test/isReadableStream/isReadableStream.test.js @@ -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(); +}); diff --git a/test/isStream/isStream.test.js b/test/isStream/isStream.test.js index 07dd965b1..3d0f18e56 100644 --- a/test/isStream/isStream.test.js +++ b/test/isStream/isStream.test.js @@ -1,13 +1,20 @@ const expect = require('expect'); const isStream = require('./isStream.js'); const fs = require('fs'); +const Stream = require('stream'); test('isStream is a 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(); }); +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', () => { expect(isStream({})).toBeFalsy(); }); diff --git a/test/isWritableStream/isWritableStream.js b/test/isWritableStream/isWritableStream.js new file mode 100644 index 000000000..a9679cc3d --- /dev/null +++ b/test/isWritableStream/isWritableStream.js @@ -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; diff --git a/test/isWritableStream/isWritableStream.test.js b/test/isWritableStream/isWritableStream.test.js new file mode 100644 index 000000000..4ad21eb23 --- /dev/null +++ b/test/isWritableStream/isWritableStream.test.js @@ -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(); +}); diff --git a/test/takeRightWhile/takeRightWhile.js b/test/takeRightWhile/takeRightWhile.js index 29011dc3d..b01512663 100644 --- a/test/takeRightWhile/takeRightWhile.js +++ b/test/takeRightWhile/takeRightWhile.js @@ -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;