Initial commit
[yaffs-website] / node_modules / unique-stream / README.md
1 # unique-stream
2
3 node.js through stream that emits a unique stream of objects based on criteria
4
5 [![build status](https://secure.travis-ci.org/eugeneware/unique-stream.png)](http://travis-ci.org/eugeneware/unique-stream)
6
7 ## Installation
8
9 Install via npm:
10
11 ```
12 $ npm install unique-stream
13 ```
14
15 ## Examples
16
17 ### Dedupe a ReadStream based on JSON.stringify:
18
19 ``` js
20 var unique = require('unique-stream')
21   , Stream = require('stream');
22
23 // return a stream of 3 identical objects
24 function makeStreamOfObjects() {
25   var s = new Stream;
26   s.readable = true;
27   var count = 3;
28   for (var i = 0; i < 3; i++) {
29     setImmediate(function () {
30       s.emit('data', { name: 'Bob', number: 123 });
31       --count && end();
32     });
33   }
34
35   function end() {
36     s.emit('end');
37   }
38
39   return s;
40 }
41
42 // Will only print out one object as the rest are dupes. (Uses JSON.stringify)
43 makeStreamOfObjects()
44   .pipe(unique())
45   .on('data', console.log);
46
47 ```
48
49 ### Dedupe a ReadStream based on an object property:
50
51 ``` js
52 // Use name as the key field to dedupe on. Will only print one object
53 makeStreamOfObjects()
54   .pipe(unique('name'))
55   .on('data', console.log);
56 ```
57
58 ### Dedupe a ReadStream based on a custom function:
59
60 ``` js
61 // Use a custom function to dedupe on. Use the 'number' field. Will only print one object.
62 makeStreamOfObjects()
63   .pipe(function (data) {
64     return data.number;
65   })
66   .on('data', console.log);
67 ```
68
69 ## Dedupe multiple streams
70
71 The reason I wrote this was to dedupe multiple object streams:
72
73 ``` js
74 var aggregator = unique();
75
76 // Stream 1
77 makeStreamOfObjects()
78   .pipe(aggregator);
79
80 // Stream 2
81 makeStreamOfObjects()
82   .pipe(aggregator);
83
84 // Stream 3
85 makeStreamOfObjects()
86   .pipe(aggregator);
87
88 aggregator.on('data', console.log);
89 ```