Product
Introducing SSO
Streamline your login process and enhance security by enabling Single Sign-On (SSO) on the Socket platform, now available for all customers on the Enterprise plan, supporting 20+ identity providers.
fork-stream
Advanced tools
Readme
Fork a stream in multiple directions according to a function.
fork-stream basically gives you conditional branching for streams. You supply the logic, fork-stream supplies the streaming.
Code:
var ForkStream = require("fork-stream");
var fork = new ForkStream({
classifier: function classify(e, done) {
return done(null, e.match(/[aeiou]/));
},
});
fork.a.on("data", console.log.bind(console, "vowels:"));
fork.b.on("data", console.log.bind(console, "no vowels:"));
fork.write("hello");
fork.write("zxcbzz");
fork.write("ooooooo");
fork.end();
Output:
vowels: hello
no vowels: zxcbzz
vowels: ooooooo
Available via npm:
$ npm install fork-stream
Or via git:
$ git clone git://github.com/deoxxa/fork-stream.git node_modules/fork-stream
constructor
Creates a new fork-stream.
new ForkStream(options);
var fork = new ForkStream({
highWaterMark: 5,
classifier: function(e, done) {
return done(null, !!e);
},
});
classifier
property that
fork-stream will use to decide what output stream to send your object down.Also see example.js.
var ForkStream = require("fork-stream");
var fork = new ForkStream({
classifier: function classify(e, done) {
return done(null, e >= 5);
},
});
fork.a.on("data", console.log.bind(null, "a"));
fork.b.on("data", console.log.bind(null, "b"));
for (var i=0;i<20;++i) {
fork.write(Math.round(Math.random() * 10));
}
Output:
b 1
a 6
a 9
a 10
a 7
a 5
b 2
b 4
a 8
b 3
a 5
b 4
a 7
a 8
b 1
a 6
b 2
b 0
a 5
b 1
3-clause BSD. A copy is included with the source.
FAQs
Fork a stream in multiple directions according to a function
The npm package fork-stream receives a total of 165,741 weekly downloads. As such, fork-stream popularity was classified as popular.
We found that fork-stream demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Product
Streamline your login process and enhance security by enabling Single Sign-On (SSO) on the Socket platform, now available for all customers on the Enterprise plan, supporting 20+ identity providers.
Security News
Tea.xyz, a crypto project aimed at rewarding open source contributions, is once again facing backlash due to an influx of spam packages flooding public package registries.
Security News
As cyber threats become more autonomous, AI-powered defenses are crucial for businesses to stay ahead of attackers who can exploit software vulnerabilities at scale.