UNPKG

1.42 kBMarkdownView Raw
1# sorted-union-stream
2
3Get the union of two sorted streams
4
5```
6npm install sorted-union-stream
7```
8
9[![build status](https://secure.travis-ci.org/mafintosh/sorted-union-stream.png)](http://travis-ci.org/mafintosh/sorted-union-stream)
10
11## Usage
12
13``` js
14var union = require('sorted-union-stream')
15var from = require('from2-array')
16
17// es.readArray converts an array into a stream
18var sorted1 = from.obj([1,10,24,42,43,50,55])
19var sorted2 = from.obj([10,42,53,55,60])
20
21// combine the two streams into a single sorted stream
22var u = union(sorted1, sorted2)
23
24u.on('data', function(data) {
25 console.log(data)
26})
27u.on('end', function() {
28 console.log('no more data')
29})
30```
31
32Running the above example will print
33
34```
351
3610
3724
3842
3943
4050
4153
4255
4360
44no more data
45```
46
47## Streaming objects
48
49If you are streaming objects sorting is based on `.key`.
50
51If this property is not present you should add a `toKey` function as the third parameter.
52`toKey` should return an key representation of the data that can be used to compare objects.
53
54_The keys MUST be sorted_
55
56``` js
57var sorted1 = from.obj([{foo:'a'}, {foo:'b'}, {foo:'c'}])
58var sorted2 = from.obj([{foo:'b'}, {foo:'d'}])
59
60var u = union(sorted1, sorted2, function(data) {
61 return data.foo // the foo property is sorted
62})
63
64union.on('data', function(data) {
65 console.log(data)
66});
67```
68
69Running the above will print
70
71```
72{foo:'a'}
73{foo:'b'}
74{foo:'c'}
75{foo:'d'}
76```
77
78## License
79
80MIT