summaryrefslogtreecommitdiff
path: root/deps/npm/node_modules/sorted-union-stream/README.md
blob: bb2959b0de697df8d83f588fa66f99f200a0d555 (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
# sorted-union-stream

Get the union of two sorted streams

```
npm install sorted-union-stream
```

[![build status](https://secure.travis-ci.org/mafintosh/sorted-union-stream.png)](http://travis-ci.org/mafintosh/sorted-union-stream)

## Usage

``` js
var union = require('sorted-union-stream')
var from = require('from2-array')

// es.readArray converts an array into a stream
var sorted1 = from.obj([1,10,24,42,43,50,55])
var sorted2 = from.obj([10,42,53,55,60])

// combine the two streams into a single sorted stream
var u = union(sorted1, sorted2)

u.on('data', function(data) {
  console.log(data)
})
u.on('end', function() {
  console.log('no more data')
})
```

Running the above example will print

```
1
10
24
42
43
50
53
55
60
no more data
```

## Streaming objects

If you are streaming objects sorting is based on `.key`.

If this property is not present you should add a `toKey` function as the third parameter.
`toKey` should return an key representation of the data that can be used to compare objects.

_The keys MUST be sorted_

``` js
var sorted1 = from.obj([{foo:'a'}, {foo:'b'}, {foo:'c'}])
var sorted2 = from.obj([{foo:'b'}, {foo:'d'}])

var u = union(sorted1, sorted2, function(data) {
  return data.foo // the foo property is sorted
})

union.on('data', function(data) {
  console.log(data)
});
```

Running the above will print

```
{foo:'a'}
{foo:'b'}
{foo:'c'}
{foo:'d'}
```

## License

MIT