Last active
August 29, 2015 14:06
-
-
Save doug/5f4bfbc39e08b61f374a to your computer and use it in GitHub Desktop.
gulp-eachline.js
This file contains hidden or 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 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