diff-stream2
v1.0.4
Published
Merges two sorted streams into a diffed tuple stream
Downloads
412
Readme
diff-stream2
Merges two sorted streams into a diffed tuple stream
Example
var through = require("through2")
var DiffStream = require("diff-stream2")
// an object map of streams, but could also be an array
var stooges = {
before: through.obj(),
after: through.obj()
}
stooges.before.write({id: 1, name: "Moe"})
stooges.before.write({id: 2, name: "Shemp"})
stooges.before.write({id: 3, name: "Larry"})
stooges.before.end()
stooges.after.write({id: 1, name: "Moe"})
stooges.after.write({id: 3, name: "Larry"})
stooges.after.write({id: 4, name: "Curly"})
stooges.after.end()
function comparator(a, b){ return !a ? 1 : !b ? -1 : a.id - b.id }
var diff = DiffStream(stooges, {comparator: comparator})
tuples.on("data", console.log)
// {before: {id: 2, name: "Shemp"}},
// {after: {id: 4, name: "Curly"}}
API
DiffStream(streams, [options])
Returns a readable stream.
streams
is a required object or array of readable streams, each of which must already be sorted according to the comparator
. To use an unsorted stream, first pipe it through something like sort-stream2. At this time, only two streams are supported.
options
is an optional object that can contain the following key:
comparator
: an optional function used to sort streams. It follows the specification used for Array.prototype.sort, and defaults tofunction(){ return 0 }
.evaluator
: an optional function used to evaluate item equality, which defaults to deep-equal.
The returned stream emits values with the same keys as streams
, but with stream data instead of streams for the values. Identical tuples are omitted from the stream, leaving only those that have changed.