node.js through stream that emits a unique stream of objects based on criteria
Install via npm:
$ npm install unique-stream
var unique = require('unique-stream')
, Stream = require('stream');
// return a stream of 3 identical objects
function makeStreamOfObjects() {
var s = new Stream;
s.readable = true;
var count = 3;
for (var i = 0; i < 3; i++) {
setImmediate(function () {
s.emit('data', { name: 'Bob', number: 123 });
--count && end();
});
}
function end() {
s.emit('end');
}
return s;
}
// Will only print out one object as the rest are dupes. (Uses JSON.stringify)
makeStreamOfObjects()
.pipe(unique())
.on('data', console.log);
// Use name as the key field to dedupe on. Will only print one object
makeStreamOfObjects()
.pipe(unique('name'))
.on('data', console.log);
// Use a custom function to dedupe on. Use the 'number' field. Will only print one object.
makeStreamOfObjects()
.pipe(function (data) {
return data.number;
})
.on('data', console.log);
The reason I wrote this was to dedupe multiple object streams:
var aggregator = unique();
// Stream 1
makeStreamOfObjects()
.pipe(aggregator);
// Stream 2
makeStreamOfObjects()
.pipe(aggregator);
// Stream 3
makeStreamOfObjects()
.pipe(aggregator);
aggregator.on('data', console.log);
# unique-stream node.js through stream that emits a unique stream of objects based on criteria [![build status](https://secure.travis-ci.org/eugeneware/unique-stream.png)](http://travis-ci.org/eugeneware/unique-stream) ## Installation Install via npm: ``` $ npm install unique-stream ``` ## Examples ### Dedupe a ReadStream based on JSON.stringify: ``` js var unique = require('unique-stream') , Stream = require('stream'); // return a stream of 3 identical objects function makeStreamOfObjects() { var s = new Stream; s.readable = true; var count = 3; for (var i = 0; i < 3; i++) { setImmediate(function () { s.emit('data', { name: 'Bob', number: 123 }); --count && end(); }); } function end() { s.emit('end'); } return s; } // Will only print out one object as the rest are dupes. (Uses JSON.stringify) makeStreamOfObjects() .pipe(unique()) .on('data', console.log); ``` ### Dedupe a ReadStream based on an object property: ``` js // Use name as the key field to dedupe on. Will only print one object makeStreamOfObjects() .pipe(unique('name')) .on('data', console.log); ``` ### Dedupe a ReadStream based on a custom function: ``` js // Use a custom function to dedupe on. Use the 'number' field. Will only print one object. makeStreamOfObjects() .pipe(function (data) { return data.number; }) .on('data', console.log); ``` ## Dedupe multiple streams The reason I wrote this was to dedupe multiple object streams: ``` js var aggregator = unique(); // Stream 1 makeStreamOfObjects() .pipe(aggregator); // Stream 2 makeStreamOfObjects() .pipe(aggregator); // Stream 3 makeStreamOfObjects() .pipe(aggregator); aggregator.on('data', console.log); ```
# | Change | User | Description | Committed | |
---|---|---|---|---|---|
#1 | 19553 | swellard | Move and rename clients | ||
//guest/perforce_software/helix-web-services/main/source/clients/2016.1.0/javascript/node_modules/unique-stream/README.md | |||||
#1 | 18810 | tjuricek |
First-pass at JavaScript client SDK. JavaScript requires Node with Gulp to "browserfy" the library. It's the easiest way I found to use the swagger-js project; bundle up a wrapping method. There is no JavaScript reference guide. The swagger-js doesn't really document what they do very well, actually. Overall I'm not particularly impressed by swagger-js, it was hard to even figure out what the right method syntax was. We may want to invest time in doing it better. This required setting CORS response headers, which are currently defaulted to a fairly insecure setting. |