You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

42 lines
1.3 KiB
JavaScript

"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.RegexParser = void 0;
const stream_1 = require("stream");
/**
* A transform stream that uses a regular expression to split the incoming text upon.
*
* To use the `Regex` parser provide a regular expression to split the incoming text upon. Data is emitted as string controllable by the `encoding` option (defaults to `utf8`).
*/
class RegexParser extends stream_1.Transform {
constructor({ regex, ...options }) {
const opts = {
encoding: 'utf8',
...options,
};
if (regex === undefined) {
throw new TypeError('"options.regex" must be a regular expression pattern or object');
}
if (!(regex instanceof RegExp)) {
regex = new RegExp(regex.toString());
}
super(opts);
this.regex = regex;
this.data = '';
}
_transform(chunk, encoding, cb) {
const data = this.data + chunk;
const parts = data.split(this.regex);
this.data = parts.pop() || '';
parts.forEach(part => {
this.push(part);
});
cb();
}
_flush(cb) {
this.push(this.data);
this.data = '';
cb();
}
}
exports.RegexParser = RegexParser;