Skip to content

Instantly share code, notes, and snippets.

@doug
Last active August 29, 2015 14:06
Show Gist options
  • Save doug/5f4bfbc39e08b61f374a to your computer and use it in GitHub Desktop.
Save doug/5f4bfbc39e08b61f374a to your computer and use it in GitHub Desktop.
gulp-eachline.js
var through = require('through2')
var eachline = function(transform, delim) {
delim = delim || '\n'
return through.obj(function(file, _, done) {
var extra = ''
file.contents = file.contents.pipe(
through(function(chunk, _, cb) {
var data = chunk.toString()
if (extra) { data = extra + data }
var lines = data.split(delim)
extra = lines.splice(lines.length - 1, 1)[0]
lines.forEach(function(line) {
if (transform) {
line = transform(line)
}
if (line) { this.push(line) }
}, this)
cb()
}, function(cb) {
if (extra) { this.push(extra) }
extra = null
cb()
})
)
this.push(file)
done()
})
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment