diff --git a/package.json b/package.json index 66b174f..d8f4c95 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "mhysa", - "version": "0.6.0-beta.0", + "version": "0.6.0-beta.1", "description": "Streams and event emitter utils for Node.js", "keywords": [ "promise", diff --git a/samples/async.js b/samples/async.js new file mode 100644 index 0000000..0f833b0 --- /dev/null +++ b/samples/async.js @@ -0,0 +1,8 @@ +const Mhysa = require("mhysa"); + +Mhysa.fromArray(["a", "b", "c"]) + .pipe(Mhysa.map(s => Promise.resolve(s + s))) + .pipe(Mhysa.flatMap(s => Promise.resolve([s, s.toUpperCase()]))) + .pipe(Mhysa.filter(s => Promise.resolve(s !== "bb"))) + .pipe(Mhysa.join(",")) + .pipe(process.stdout); diff --git a/samples/concat_files.js b/samples/concat.js similarity index 93% rename from samples/concat_files.js rename to samples/concat.js index 871ecba..a3acd30 100644 --- a/samples/concat_files.js +++ b/samples/concat.js @@ -16,4 +16,4 @@ Mhysa.concat( fs.createReadStream(sourceFile1), Mhysa.fromArray(["\n"]), fs.createReadStream(sourceFile2), -).pipe(fs.createWriteStream(outputFile)); +).pipe(process.stdout); diff --git a/samples/demo.js b/samples/demo.js deleted file mode 100644 index c874fe0..0000000 --- a/samples/demo.js +++ /dev/null @@ -1,23 +0,0 @@ -const { - utils: { sleep, delay, once }, - ...Mhysa -} = require("mhysa"); - -async function main() { - const collector = Mhysa.concat( - Mhysa.fromArray(["a\n", "b\n", "c\n"]), - Mhysa.fromArray(["d", "e"]).pipe(Mhysa.join("-")), - ) - .pipe(Mhysa.split("\n")) - .pipe( - Mhysa.flatMap(async s => { - await sleep(100); - return delay([s, s.toUpperCase()], 100); - }), - ) - .pipe(Mhysa.collect({ objectMode: true })); - - const collected = await once(collector, "data"); - console.log(collected); // [ 'a', 'A', 'b', 'B', 'c', 'C', 'd-e', 'D-E' ] (after 6 * 100 ms) -} -main(); diff --git a/samples/filter.js b/samples/filter.js new file mode 100644 index 0000000..f9aeda1 --- /dev/null +++ b/samples/filter.js @@ -0,0 +1,6 @@ +const Mhysa = require("mhysa"); + +Mhysa.fromArray(["a", "b", "c"]) + .pipe(Mhysa.filter(s => s !== "b")) + .pipe(Mhysa.join(",")) + .pipe(process.stdout); diff --git a/samples/flatMap.js b/samples/flatMap.js new file mode 100644 index 0000000..695dd5c --- /dev/null +++ b/samples/flatMap.js @@ -0,0 +1,6 @@ +const Mhysa = require("mhysa"); + +Mhysa.fromArray(["a", "AA"]) + .pipe(Mhysa.flatMap(s => new Array(s.length).fill(s))) + .pipe(Mhysa.join(",")) + .pipe(process.stdout); diff --git a/samples/map.js b/samples/map.js new file mode 100644 index 0000000..7dc8142 --- /dev/null +++ b/samples/map.js @@ -0,0 +1,6 @@ +const Mhysa = require("mhysa"); + +Mhysa.fromArray(["a", "b"]) + .pipe(Mhysa.map(s => s.toUpperCase())) + .pipe(Mhysa.join(",")) + .pipe(process.stdout); diff --git a/samples/reduce.js b/samples/reduce.js new file mode 100644 index 0000000..7350341 --- /dev/null +++ b/samples/reduce.js @@ -0,0 +1,5 @@ +const Mhysa = require("mhysa"); + +Mhysa.fromArray(["a", "b", "cc"]) + .pipe(Mhysa.reduce((acc, s) => ({ ...acc, [s]: s.length }), {})) + .pipe(process.stdout); diff --git a/src/index.ts b/src/index.ts index 64cadaa..3564966 100644 --- a/src/index.ts +++ b/src/index.ts @@ -182,7 +182,17 @@ export function reduce( } }, flush(callback) { - callback(undefined, value); + // Best effort attempt at yielding the final value (will throw if e.g. yielding an object and + // downstream doesn't expect objects) + try { + callback(undefined, value); + } catch (err) { + try { + this.emit("error", err); + } catch { + // Best effort was made + } + } }, }); }