Add samples
This commit is contained in:
8
samples/async.js
Normal file
8
samples/async.js
Normal file
@@ -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);
|
||||
@@ -16,4 +16,4 @@ Mhysa.concat(
|
||||
fs.createReadStream(sourceFile1),
|
||||
Mhysa.fromArray(["\n"]),
|
||||
fs.createReadStream(sourceFile2),
|
||||
).pipe(fs.createWriteStream(outputFile));
|
||||
).pipe(process.stdout);
|
||||
@@ -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();
|
||||
6
samples/filter.js
Normal file
6
samples/filter.js
Normal file
@@ -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);
|
||||
6
samples/flatMap.js
Normal file
6
samples/flatMap.js
Normal file
@@ -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);
|
||||
6
samples/map.js
Normal file
6
samples/map.js
Normal file
@@ -0,0 +1,6 @@
|
||||
const Mhysa = require("mhysa");
|
||||
|
||||
Mhysa.fromArray(["a", "b"])
|
||||
.pipe(Mhysa.map(s => s.toUpperCase()))
|
||||
.pipe(Mhysa.join(","))
|
||||
.pipe(process.stdout);
|
||||
5
samples/reduce.js
Normal file
5
samples/reduce.js
Normal file
@@ -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);
|
||||
Reference in New Issue
Block a user