1 | # sorted-union-stream
|
2 |
|
3 | Get the union of two sorted streams
|
4 |
|
5 | ```
|
6 | npm 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
|
14 | var union = require('sorted-union-stream')
|
15 | var from = require('from2-array')
|
16 |
|
17 | // es.readArray converts an array into a stream
|
18 | var sorted1 = from.obj([1,10,24,42,43,50,55])
|
19 | var sorted2 = from.obj([10,42,53,55,60])
|
20 |
|
21 | // combine the two streams into a single sorted stream
|
22 | var u = union(sorted1, sorted2)
|
23 |
|
24 | u.on('data', function(data) {
|
25 | console.log(data)
|
26 | })
|
27 | u.on('end', function() {
|
28 | console.log('no more data')
|
29 | })
|
30 | ```
|
31 |
|
32 | Running the above example will print
|
33 |
|
34 | ```
|
35 | 1
|
36 | 10
|
37 | 24
|
38 | 42
|
39 | 43
|
40 | 50
|
41 | 53
|
42 | 55
|
43 | 60
|
44 | no more data
|
45 | ```
|
46 |
|
47 | ## Streaming objects
|
48 |
|
49 | If you are streaming objects sorting is based on `.key`.
|
50 |
|
51 | If 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
|
57 | var sorted1 = from.obj([{foo:'a'}, {foo:'b'}, {foo:'c'}])
|
58 | var sorted2 = from.obj([{foo:'b'}, {foo:'d'}])
|
59 |
|
60 | var u = union(sorted1, sorted2, function(data) {
|
61 | return data.foo // the foo property is sorted
|
62 | })
|
63 |
|
64 | union.on('data', function(data) {
|
65 | console.log(data)
|
66 | });
|
67 | ```
|
68 |
|
69 | Running the above will print
|
70 |
|
71 | ```
|
72 | {foo:'a'}
|
73 | {foo:'b'}
|
74 | {foo:'c'}
|
75 | {foo:'d'}
|
76 | ```
|
77 |
|
78 | ## License
|
79 |
|
80 | MIT
|