Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

nodejs stream pipeline with custom transform

const fs = require('fs');
const { pipeline, Transform } = require('stream');

const readStream = fs.createReadStream("text.txt", {
    flags: 'r',
    encoding: 'utf-8',
    autoClose: true,
});

const writeStream = fs.createWriteStream("text2.txt", {
    flags: 'w',
    encoding: 'utf-8',
    autoClose: true,
});

const transformStream = new Transform({
  //Transform data to filter for even numbers
        transform(chunk, enc, cb) {
            const data = chunk.toString().split(" ").filter(value => !(parseInt(value) & 1));
            cb(null, data.join(" "));
        }
    });

pipeline(readStream, transformStream, writeStream, (err) => {
    if(err) 
        console.error(err);
    else 
        console.error("success");
  //writeStream has written only even numbers from readStream
})
Comment

PREVIOUS NEXT
Code Example
Javascript :: declare 2 d vector js 
Javascript :: javascript search an array of json for matching attribute 
Javascript :: javascript concat two htmlcollection 
Javascript :: javascript to jquery code converter online 
Javascript :: rxact 6 number long in yup validation 
Javascript :: HDEL in redis 
Javascript :: moment js with nodejs 
Javascript :: js replay animation 
Javascript :: Slice and Splice -Javascript 2 
Javascript :: graphql nested schema 
Javascript :: loop in object 
Javascript :: how to make a string in javascript 
Javascript :: are you sure alert js 
Javascript :: regex and 
Javascript :: sequelize update 
Javascript :: grid in js 
Javascript :: get id javascript 
Javascript :: adding int and string in react props 
Javascript :: export default class react 
Javascript :: donwload data from react js in json file 
Javascript :: react portal example 
Javascript :: check if property has value in array javascript 
Javascript :: jetty 
Javascript :: add 7 days in date using jquery 
Javascript :: for in in javascript 
Javascript :: the event object 
Javascript :: how to disable previous date in datepicker using angular 6 
Javascript :: flatmap js 
Javascript :: amazon s3 upload error ssl certificate 
Javascript :: check identical array javascript 
ADD CONTENT
Topic
Content
Source link
Name
6+1 =