From ca92f26eb3ade81b5c427a3795ec3991a169b980 Mon Sep 17 00:00:00 2001 From: Nathan Date: Fri, 17 Mar 2017 20:38:30 -0400 Subject: [PATCH] Make Lexer a transform stream --- moo.js | 18 +++++++----------- test/test.js | 4 ++-- 2 files changed, 9 insertions(+), 13 deletions(-) diff --git a/moo.js b/moo.js index cfc7dc0..456436b 100644 --- a/moo.js +++ b/moo.js @@ -271,22 +271,18 @@ this.stack = [] this.setState(state) this.reset() + if (Transform) Transform.call(this, {readableObjectMode: true}) } if (typeof module !== 'undefined' && module.exports) { var Transform = require('stream').Transform + require('util').inherits(Lexer, Transform) - Lexer.prototype.stream = function(state) { - var self = this.reset('', state) - return new Transform({ - readableObjectMode: true, - transform(chunk, encoding, cb) { - self.feed(chunk.toString()) - var token - while (token = self.next()) this.push(token) - cb() - } - }) + Lexer.prototype._transform = function(chunk, encoding, cb) { + this.feed(chunk.toString()) + var token + while (token = this.next()) this.push(token) + cb() } } diff --git a/test/test.js b/test/test.js index a6f1202..0d280e9 100644 --- a/test/test.js +++ b/test/test.js @@ -491,7 +491,7 @@ describe('streams', () => { let index = 0 expect.assertions(tokens.length) - const s = lexer.stream() + const s = lexer.clone() s.write(inputs[0]) s.end(inputs[1]) @@ -528,7 +528,7 @@ describe('streams', () => { }) rs - .on('error', reject).pipe(lexer.stream()) + .on('error', reject).pipe(lexer.clone()) .on('error', reject).pipe(ws) .on('error', reject) .on('finish', resolve)