How to combine multiple transform streams into one in nodejs

I have the following piece of code

function func1(){
  return  makeReadbleStream1()
    .pipe(transformA)
    .pipe(transformB)
    .pipe(transformC)
    .pipe(X);
}

function func2(){
  return  makeReadbleStream2()
    .pipe(transformA)
    .pipe(transformB)
    .pipe(transformC)  
    .pipe(Y);
}

      

Function 1 and 2 share common logic for passing transforms A, B, and C. Based on the DRY principle, I find it better to extract the logic into mixedTransformABC function. However, it doesn't seem to me an obvious way to implement this function based on transformA, B, and C so that I can refactor the code like below.

function func1(){
  return  makeReadbleStream1()
    .pipe(combinedTranformABC)
    .pipe(X);
}

function func2(){
  return  makeReadbleStream2()
    .pipe(combinedTranformABC)
    .pipe(Y);
}    

      

Any idea?

+3


source to share


1 answer


Why not just do this:



function applyTransforms(stream){
    return stream.pipe(transformA)
        .pipe(transformB)
        .pipe(transformC);
}
function func1(){
    return applyTransforms(makeReadbleStream1()).pipe(X);
}
function func2(){
    return applyTransforms(makeReadbleStream2()).pipe(Y);
}

      

0


source







All Articles