Last active
March 24, 2018 12:43
-
-
Save Frando/b33045a4e8b1e73c2f8625c99bc06f87 to your computer and use it in GitHub Desktop.
smoosh-stream
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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