blob: f19b20a37d42184f2aae5cba15ecc8edfc0ca613 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
|
# 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);
```
|