Skip to content

Instantly share code, notes, and snippets.

@Frando
Last active March 24, 2018 12:43
Show Gist options
  • Save Frando/b33045a4e8b1e73c2f8625c99bc06f87 to your computer and use it in GitHub Desktop.
Save Frando/b33045a4e8b1e73c2f8625c99bc06f87 to your computer and use it in GitHub Desktop.
smoosh-stream
var flatten = require('.')
var bulk = require('bulk-write-stream')
var stream = bulk.obj(flatten(write))
function write(batch, cb) {
batch.forEach(function(obj) {
console.log(obj)
})
cb(null)
}
stream.write('first')
stream.write(['second', 'third'])
stream.write(['forth'])
stream.write('last')
// Prints:
// first
// second
// third
// forth
// last
module.exports = flatten
function flatten (write) {
return function (batch, cb) {
var flattened = []
for (var i = 0; i < batch.length; i++) {
var content = batch[i]
if (Array.isArray(content)) {
for (var j = 0; j < content.length; j++) {
flattened.push(content[j])
}
} else {
flattened.push(content)
}
}
write(flattened, cb)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment