mirror of
https://github.com/nodejs/node.git
synced 2025-05-16 00:11:20 +00:00

PR-URL: https://github.com/nodejs/node/pull/10330 Reviewed-By: Myles Borins <myles.borins@gmail.com>
60 lines
1.1 KiB
JavaScript
60 lines
1.1 KiB
JavaScript
var iterate = require('stream-iterate')
|
|
var from = require('from2')
|
|
|
|
var defaultKey = function (val) {
|
|
return val.key || val
|
|
}
|
|
|
|
var union = function (streamA, streamB, toKey) {
|
|
var readA = iterate(streamA)
|
|
var readB = iterate(streamB)
|
|
|
|
if (!toKey) toKey = defaultKey
|
|
|
|
var stream = from.obj(function loop (size, cb) {
|
|
readA(function (err, dataA, nextA) {
|
|
if (err) return cb(err)
|
|
readB(function (err, dataB, nextB) {
|
|
if (err) return cb(err)
|
|
|
|
if (!dataA && !dataB) return cb(null, null)
|
|
|
|
if (!dataA) {
|
|
nextB()
|
|
return cb(null, dataB)
|
|
}
|
|
|
|
if (!dataB) {
|
|
nextA()
|
|
return cb(null, dataA)
|
|
}
|
|
|
|
var keyA = toKey(dataA)
|
|
var keyB = toKey(dataB)
|
|
|
|
if (keyA === keyB) {
|
|
nextB()
|
|
return loop(size, cb)
|
|
}
|
|
|
|
if (keyA < keyB) {
|
|
nextA()
|
|
return cb(null, dataA)
|
|
}
|
|
|
|
nextB()
|
|
cb(null, dataB)
|
|
})
|
|
})
|
|
})
|
|
|
|
stream.on('close', function () {
|
|
if (streamA.destroy) streamA.destroy()
|
|
if (streamB.destroy) streamB.destroy()
|
|
})
|
|
|
|
return stream
|
|
}
|
|
|
|
module.exports = union
|