44 lines
1.2 KiB
JavaScript
44 lines
1.2 KiB
JavaScript
"use strict";
|
|
|
|
const Promise = require("bluebird");
|
|
const fs = require("fs");
|
|
const through2 = require("through2");
|
|
const fromNodeStream = require("./");
|
|
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");
|
|
|
|
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"),
|
|
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" }));
|
|
});
|