robrich / ternary-stream

A ternary stream: conditionally control the flow of stream data
MIT License
36 stars 5 forks source link

Allow to use a plain condition #5

Closed reklatsmasters closed 9 years ago

reklatsmasters commented 9 years ago

Example:

const ifstream  = require('ternary-stream');
const zlib      = require('zlib');

// ...http.get
var enc = response.headers['content-encoding'];
response
    .pipe(ifstream(enc == 'gzip', zlib.createGunzip()))      // condition is not a function
    .pipe(ifstream(enc == 'deflate', zlib.createInflate()))
    // .pipe
robrich commented 9 years ago

Thanks for the PR. Added to https://github.com/robrich/gulp-match/blob/master/index.js#L10

dy commented 6 years ago

@robrich just wonder why that was not merged? Seems to be reasonable and useful for many cases.

robrich commented 6 years ago

@dfcreative: It was merged, just into a higher level module instead of here. Do you have a use-case where it's more helpful here instead?

dy commented 6 years ago

Yes, I was going to use it at plotly here. Since codestyle policy is ES5, it is just uncomfy and redundant to create a whole function when we have condition outputMinified already.

robrich commented 6 years ago

That's a good reason. I'm thinking this will be even better:

if (typeof _condition === 'boolean') {
  return _condition ? trueStream : falseStream;
}

See any issues with this short-cut?

dy commented 6 years ago

@robirch makes perfect sense to me, unless condition is false and falseStream is undefined - probably we should return original stream then?