From 976330134d8f4ddebe1573051d7f6f63e9b17129 Mon Sep 17 00:00:00 2001 From: Sami Turcotte Date: Wed, 28 Nov 2018 01:53:12 -0500 Subject: [PATCH] Add README demo in samples, update README and update typings --- README.md | 70 ++++++++++++++++++++++++++++++++++++++++++------- package.json | 4 +-- samples/demo.js | 25 ++++++++++++++++++ src/stream.ts | 13 ++++----- src/utils.ts | 6 ++--- yarn.lock | 15 +++++++---- 6 files changed, 108 insertions(+), 25 deletions(-) create mode 100644 samples/demo.js diff --git a/README.md b/README.md index 52f1577..a8934ae 100644 --- a/README.md +++ b/README.md @@ -13,26 +13,28 @@ yarn add mhysa The following snippet demonstrates most of mhysa's current features. More will come! ```js -const { sleep, once, delay, every, stream } = require("mhysa"); +const { sleep, once, delay, every, stream } = require("mhysa"); async function main() { const collector = stream .concat( - stream.fromArray(["a", "b", "c"]), - stream.fromArray(["d", "e"]) + stream.fromArray(["a\n", "b\n", "c\n"]), + stream.fromArray(["d", "e"]).pipe(stream.join("-")) ) + .pipe(stream.split("\n")) + .pipe(stream.flatMap(s => [s, s.toUpperCase()])) .pipe(stream.collect({ objectMode: true })); const collected = await once(collector, "data"); await sleep(1000); // undefined (after one second) - await delay(collected, 1000); // [ 'a', 'b', 'c', 'd', 'e' ] (after another second) + await delay(collected, 1000); // [ 'a', 'A', 'b', 'B', 'c', 'C', 'd-e', 'D-E' ] (after another second) await every( [Promise.resolve("ab"), delay("cd", 1000)], - c => c.length === 2 + s => s.length === 2 ); // true (after another second) await every( [Promise.resolve("ab"), delay("cd", 1000)], - c => c.length === 1 + s => s.length === 1 ); // false (instantly) } main(); @@ -49,6 +51,56 @@ main(); */ export declare function fromArray(array: any[]): NodeJS.ReadableStream; +/** + * Return a ReadWrite stream that maps streamed chunks + * @param mapper The mapper function, mapping each (chunk, encoding) to a new chunk (or a promise of such) + * @param options + * @param options.readableObjectMode Whether this stream should behave as a readable stream of objects + * @param options.writableObjectMode Whether this stream should behave as a writable stream of objects + */ +export declare function map( + mapper: (chunk: T, encoding: string) => R, + { + readableObjectMode, + writableObjectMode, + }?: { + readableObjectMode?: boolean | undefined; + writableObjectMode?: boolean | undefined; + }, +): NodeJS.ReadWriteStream; + +/** + * Return a ReadWrite stream that flat maps streamed chunks + * @param mapper The mapper function, mapping each (chunk, encoding) to an array of new chunks (or a promise of such) + * @param options + * @param options.readableObjectMode Whether this stream should behave as a readable stream of objects + * @param options.writableObjectMode Whether this stream should behave as a writable stream of objects + */ +export declare function flatMap( + mapper: + | ((chunk: T, encoding: string) => R[]) + | ((chunk: T, encoding: string) => Promise), + { + readableObjectMode, + writableObjectMode, + }?: { + readableObjectMode?: boolean | undefined; + writableObjectMode?: boolean | undefined; + }, +): NodeJS.ReadWriteStream; + +/** + * Return a ReadWrite stream that splits streamed chunks using the given separator + * @param separator The separator to split by, defaulting to "\n" + */ +export declare function split(separator?: string): NodeJS.ReadWriteStream; + +/** + * Return a ReadWrite stream that joins streamed chunks using the given separator + * @param separator The separator to join with + */ +export declare function join(separator: string): NodeJS.ReadWriteStream; + /** * Return a ReadWrite stream that collects streamed objects or bytes into an array or buffer * @param options @@ -99,9 +151,9 @@ export declare function once( ): Promise; /** - * Resolve to false as soon as any of the promises has resolved to a value for which the predicate is - * falsey, or resolve to true when all of the promises have resolved to a value for which the predicate is - * thruthy, or rejects with the reason of the first promise rejection + * Eagerly resolve to false as soon as any of the promises has resolved to a value for which the + * predicate is falsey, or resolve to true when all of the promises have resolved to a value for which + * the predicate is thruthy, or rejects with the reason of the first promise rejection * * @param promises Promises whose resolved values will be tested by the predicate * @param predicate Predicate to apply diff --git a/package.json b/package.json index b42b36a..5b231ec 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "mhysa", - "version": "0.4.0", + "version": "0.4.1", "description": "Promise, Stream and EventEmitter utils for Node.js", "keywords": [ "promise", @@ -35,7 +35,7 @@ "@types/node": "^10.12.10", "ava": "^1.0.0-rc.2", "chai": "^4.2.0", - "mhysa": "0.3.6", + "mhysa": ".", "prettier": "^1.14.3", "ts-node": "^7.0.1", "tslint": "^5.11.0", diff --git a/samples/demo.js b/samples/demo.js new file mode 100644 index 0000000..c418787 --- /dev/null +++ b/samples/demo.js @@ -0,0 +1,25 @@ +const { sleep, once, delay, every, stream } = require("mhysa"); + +async function main() { + const collector = stream + .concat( + stream.fromArray(["a\n", "b\n", "c\n"]), + stream.fromArray(["d", "e"]).pipe(stream.join("-")), + ) + .pipe(stream.split("\n")) + .pipe(stream.flatMap(s => [s, s.toUpperCase()])) + .pipe(stream.collect({ objectMode: true })); + + const collected = await once(collector, "data"); + await sleep(1000); // undefined (after one second) + console.log(await delay(collected, 1000)); // [ 'a', 'A', 'b', 'B', 'c', 'C', 'd-e', 'D-E' ] (after another second) + await every( + [Promise.resolve("ab"), delay("cd", 1000)], + s => s.length === 2, + ); // true (after another second) + await every( + [Promise.resolve("ab"), delay("cd", 1000)], + s => s.length === 1, + ); // false (instantly) +} +main(); diff --git a/src/stream.ts b/src/stream.ts index bc25580..9b9e2e3 100644 --- a/src/stream.ts +++ b/src/stream.ts @@ -52,9 +52,6 @@ export function map( }); } -type FlatMapper = - | ((chunk: T, encoding: string) => R[]) - | ((chunk: T, encoding: string) => Promise); /** * Return a ReadWrite stream that flat maps streamed chunks * @param mapper The mapper function, mapping each (chunk, encoding) to an array of new chunks (or a promise of such) @@ -63,7 +60,9 @@ type FlatMapper = * @param options.writableObjectMode Whether this stream should behave as a writable stream of objects */ export function flatMap( - mapper: FlatMapper, + mapper: + | ((chunk: T, encoding: string) => R[]) + | ((chunk: T, encoding: string) => Promise), { readableObjectMode = true, writableObjectMode = true } = {}, ): NodeJS.ReadWriteStream { return new Transform({ @@ -93,12 +92,14 @@ export function flatMap( * Return a ReadWrite stream that splits streamed chunks using the given separator * @param separator The separator to split by, defaulting to "\n" */ -export function split(separator: string = "\n"): NodeJS.ReadWriteStream { +export function split( + separator: string | RegExp = "\n", +): NodeJS.ReadWriteStream { let buffered: string = ""; return new Transform({ readableObjectMode: true, writableObjectMode: true, - async transform(chunk, encoding, callback) { + async transform(chunk: string, encoding, callback) { const splitted = chunk.split(separator); if (buffered.length > 0 && splitted.length > 1) { splitted[0] = buffered.concat(splitted[0]); diff --git a/src/utils.ts b/src/utils.ts index 82b3f92..31fc3d8 100644 --- a/src/utils.ts +++ b/src/utils.ts @@ -39,9 +39,9 @@ export function once( } /** - * Resolve to false as soon as any of the promises has resolved to a value for which the predicate is - * falsey, or resolve to true when all of the promises have resolved to a value for which the predicate is - * thruthy, or rejects with the reason of the first promise rejection + * Eagerly resolve to false as soon as any of the promises has resolved to a value for which the + * predicate is falsey, or resolve to true when all of the promises have resolved to a value for which + * the predicate is thruthy, or rejects with the reason of the first promise rejection * * @param promises Promises whose resolved values will be tested by the predicate * @param predicate Predicate to apply diff --git a/yarn.lock b/yarn.lock index 8cedf50..706f17b 100644 --- a/yarn.lock +++ b/yarn.lock @@ -330,7 +330,14 @@ resolved "https://registry.yarnpkg.com/@types/chai/-/chai-4.1.7.tgz#1b8e33b61a8c09cbe1f85133071baa0dbf9fa71a" integrity sha512-2Y8uPt0/jwjhQ6EiluT0XCri1Dbplr0ZxfFXUz+ye13gaqE8u5gL5ppao1JrUYr9cIip5S6MvQzBS7Kke7U9VA== -"@types/node@^10.12.10": +"@types/event-stream@^3.3.34": + version "3.3.34" + resolved "https://registry.yarnpkg.com/@types/event-stream/-/event-stream-3.3.34.tgz#104bcedd5c61f90917b734bde04e61c6e64f03e1" + integrity sha512-LLiivgWKii4JeMzFy3trrxqkRrVSdue8WmbXyHuSJLwNrhIQU5MTrc65jhxEPwMyh5HR1xevSdD+k2nnSRKw9g== + dependencies: + "@types/node" "*" + +"@types/node@*", "@types/node@^10.12.10": version "10.12.10" resolved "https://registry.yarnpkg.com/@types/node/-/node-10.12.10.tgz#4fa76e6598b7de3f0cb6ec3abacc4f59e5b3a2ce" integrity sha512-8xZEYckCbUVgK8Eg7lf5Iy4COKJ5uXlnIOnePN0WUwSQggy9tolM+tDJf7wMOnT/JT/W9xDYIaYggt3mRV2O5w== @@ -2059,10 +2066,8 @@ meow@^5.0.0: trim-newlines "^2.0.0" yargs-parser "^10.0.0" -mhysa@0.3.6: - version "0.3.6" - resolved "https://registry.yarnpkg.com/mhysa/-/mhysa-0.3.6.tgz#c33885ca34c5797486ff9af10e33d17e2cd2b2a9" - integrity sha512-X7AIhISZ/r5xOHaod3SwLCTXr11ttu/fDiKQPpsI0p/RsTTqweBR7hGJeRIF9Rt6XMW/UY21pJEf/ftUkfvkHg== +mhysa@.: + version "0.4.1" micromatch@^3.1.10, micromatch@^3.1.4: version "3.1.10"