stromjs
Version:
Dependency-free streams utils for Node.js
34 lines (33 loc) • 1.17 kB
JavaScript
exports.__esModule = true;
exports.split = void 0;
var stream_1 = require("stream");
var string_decoder_1 = require("string_decoder");
function split(separator, options) {
if (separator === void 0) { separator = "\n"; }
if (options === void 0) { options = {
encoding: "utf8",
objectMode: true
}; }
var buffered = "";
var decoder = new string_decoder_1.StringDecoder(options.encoding);
return new stream_1.Transform({
readableObjectMode: true,
transform: function (chunk, encoding, callback) {
var _this = this;
var asString = decoder.write(chunk);
var splitData = asString.split(separator);
if (splitData.length > 1) {
splitData[0] = buffered.concat(splitData[0]);
buffered = "";
}
buffered += splitData[splitData.length - 1];
splitData.slice(0, -1).forEach(function (part) { return _this.push(part); });
callback();
},
flush: function (callback) {
callback(undefined, buffered + decoder.end());
}
});
}
exports.split = split;
;