from-node-stream/example.js

44 lines
1.2 KiB
JavaScript
Raw Normal View History

2021-02-10 23:47:40 +01:00
"use strict";
const Promise = require("bluebird");
const fs = require("fs");
const through2 = require("through2");
const fromNodeStream = require("./");
2021-03-27 23:36:42 +01:00
const pipe = require("@promistream/pipe");
const collect = require("@promistream/collect");
const rangeNumbers = require("@promistream/range-numbers");
const bufferedMap = require("@promistream/buffered-map");
const map = require("@promistream/map");
2021-02-10 23:47:40 +01:00
let transform = through2.obj((chunk, encoding, callback) => {
callback(null, chunk + 5);
});
return Promise.try(() => {
return pipe([
rangeNumbers(0, 10),
fromNodeStream(transform),
collect()
]).read();
}).then((result) => {
console.log("transform result:", result);
return pipe([
fromNodeStream(fs.createReadStream("example.js", { encoding: "utf8" })),
bufferedMap((line) => line.split("\n")),
collect()
]).read();
}).then((result) => {
console.log("read result:", result);
return pipe([
rangeNumbers(0, 10),
map((number) => String(number) + "\n"),
2021-02-10 23:47:40 +01:00
fromNodeStream(fs.createWriteStream("numbers.txt", { encoding: "utf8" }))
]).read();
}).then((result) => {
console.log("write result:", result);
console.log("numbers.txt contents:");
console.log(fs.readFileSync("numbers.txt", { encoding: "utf8" }));
});