Skip to content

Commit

Permalink
[all] Change parsers to be es6 classes
Browse files Browse the repository at this point in the history
- Reduces code a bit
- Provides a better code example
- clean up options passing into transform streams
  • Loading branch information
reconbot committed Jul 29, 2017
1 parent ae89830 commit 8b07a48
Show file tree
Hide file tree
Showing 12 changed files with 218 additions and 285 deletions.
56 changes: 33 additions & 23 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -723,54 +723,64 @@ The default `Parsers` are [Transform streams](https://nodejs.org/api/stream.html

**Example**
```js
var SerialPort = require('serialport');
var Readline = SerialPort.parsers.Readline;
var port = new SerialPort('/dev/tty-usbserial1');
var parser = new Readline();
const SerialPort = require('serialport');
const Readline = SerialPort.parsers.Readline;
const port = new SerialPort('/dev/tty-usbserial1');
const parser = new Readline();
port.pipe(parser);
parser.on('data', console.log);
port.write('ROBOT PLEASE RESPOND\n');

// Creating the parser and piping can be shortened to
var parser = port.pipe(new Readline());
// const parser = port.pipe(new Readline());
```

To use the `ByteLength` parser, provide the length of the number of bytes:
```js
var SerialPort = require('serialport');
var ByteLength = SerialPort.parsers.ByteLength
var port = new SerialPort('/dev/tty-usbserial1');
var parser = port.pipe(new ByteLength({length: 8}));
const SerialPort = require('serialport');
const ByteLength = SerialPort.parsers.ByteLength
const port = new SerialPort('/dev/tty-usbserial1');
const parser = port.pipe(new ByteLength({length: 8}));
parser.on('data', console.log);
```

To use the `Delimiter` parser, provide a delimiter as a string, buffer, or array of bytes:
```js
var SerialPort = require('serialport');
var Delimiter = SerialPort.parsers.Delimiter;
var port = new SerialPort('/dev/tty-usbserial1');
var parser = port.pipe(new Delimiter({delimiter: Buffer.from('EOL')}));
const SerialPort = require('serialport');
const Delimiter = SerialPort.parsers.Delimiter;
const port = new SerialPort('/dev/tty-usbserial1');
const parser = port.pipe(new Delimiter({ delimiter: Buffer.from('EOL') }));
parser.on('data', console.log);
```

To use the `Readline` parser, provide a delimiter (defaults to '\n')
To use the `Readline` parser, provide a delimiter (defaults to '\n'). Data is emitted as string controllable by the `encoding` option (defaults to `utf8`).
```js
var SerialPort = require('serialport');
var Readline = SerialPort.parsers.Readline;
var port = new SerialPort('/dev/tty-usbserial1');
var parser = port.pipe(Readline({delimiter: '\r\n'}));
const SerialPort = require('serialport');
const Readline = SerialPort.parsers.Readline;
const port = new SerialPort('/dev/tty-usbserial1');
const parser = port.pipe(Readline({ delimiter: '\r\n' }));
parser.on('data', console.log);
```

To use the `Ready` parser provide a byte start sequence. After the bytes have been received data events will be passed through.
To use the `Ready` parser provide a byte start sequence. After the bytes have been received data events are passed through.
```js
var SerialPort = require('serialport');
var Ready = SerialPort.parsers.Ready;
var port = new SerialPort('/dev/tty-usbserial1');
var parser = port.pipe(Ready({data: 'READY'}));
const SerialPort = require('serialport');
const Ready = SerialPort.parsers.Ready;
const port = new SerialPort('/dev/tty-usbserial1');
const parser = port.pipe(Ready({ data: 'READY' }));
parser.on('ready', () => console.log('the ready byte sequence has been received'))
parser.on('data', console.log); // all data after READY is received
```

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`).
```js
const SerialPort = require('serialport');
const Regex = SerialPort.parsers.Regex;
const port = new SerialPort('/dev/tty-usbserial1');
const parser = port.pipe(Regex({ regex: /[\r\n]+/ }));
parser.on('data', console.log);
```

* * *

<a name="module_serialport--SerialPort.list"></a>
Expand Down
59 changes: 26 additions & 33 deletions lib/parsers/byte-length.js
Original file line number Diff line number Diff line change
@@ -1,45 +1,38 @@
'use strict';
const Buffer = require('safe-buffer').Buffer;
const inherits = require('util').inherits;
const Transform = require('stream').Transform;

function ByteLengthParser(options) {
if (!(this instanceof ByteLengthParser)) {
return new ByteLengthParser(options);
}
Transform.call(this, options);
module.exports = class ByteLengthParser extends Transform {
constructor(options) {
super(options);
options = options || {};

options = options || {};
if (typeof options.length !== 'number') {
throw new TypeError('"length" is not a number');
}

if (typeof options.length !== 'number') {
throw new TypeError('"length" is not a number');
}
if (options.length < 1) {
throw new TypeError('"length" is not greater than 0');
}

if (options.length < 1) {
throw new TypeError('"length" is not greater than 0');
this.length = options.length;
this.buffer = Buffer.alloc(0);
}

this.length = options.length;
this.buffer = Buffer.alloc(0);
}

inherits(ByteLengthParser, Transform);

ByteLengthParser.prototype._transform = function(chunk, encoding, cb) {
let data = Buffer.concat([this.buffer, chunk]);
while (data.length >= this.length) {
const out = data.slice(0, this.length);
this.push(out);
data = data.slice(this.length);
_transform(chunk, encoding, cb) {
let data = Buffer.concat([this.buffer, chunk]);
while (data.length >= this.length) {
const out = data.slice(0, this.length);
this.push(out);
data = data.slice(this.length);
}
this.buffer = data;
cb();
}
this.buffer = data;
cb();
};

ByteLengthParser.prototype._flush = function(cb) {
this.push(this.buffer);
this.buffer = Buffer.alloc(0);
cb();
_flush(cb) {
this.push(this.buffer);
this.buffer = Buffer.alloc(0);
cb();
}
};

module.exports = ByteLengthParser;
59 changes: 26 additions & 33 deletions lib/parsers/delimiter.js
Original file line number Diff line number Diff line change
@@ -1,45 +1,38 @@
'use strict';
const Buffer = require('safe-buffer').Buffer;
const inherits = require('util').inherits;
const Transform = require('stream').Transform;

function DelimiterParser(options) {
if (!(this instanceof DelimiterParser)) {
return new DelimiterParser(options);
}
Transform.call(this, options);
module.exports = class DelimiterParser extends Transform {
constructor(options) {
options = options || {};
super(options);

options = options || {};
if (options.delimiter === undefined) {
throw new TypeError('"delimiter" is not a bufferable object');
}

if (options.delimiter === undefined) {
throw new TypeError('"delimiter" is not a bufferable object');
}
if (options.delimiter.length === 0) {
throw new TypeError('"delimiter" has a 0 or undefined length');
}

if (options.delimiter.length === 0) {
throw new TypeError('"delimiter" has a 0 or undefined length');
this.delimiter = Buffer.from(options.delimiter);
this.buffer = Buffer.alloc(0);
}

this.delimiter = Buffer.from(options.delimiter);
this.buffer = Buffer.alloc(0);
}

inherits(DelimiterParser, Transform);

DelimiterParser.prototype._transform = function(chunk, encoding, cb) {
let data = Buffer.concat([this.buffer, chunk]);
let position;
while ((position = data.indexOf(this.delimiter)) !== -1) {
this.push(data.slice(0, position));
data = data.slice(position + this.delimiter.length);
_transform(chunk, encoding, cb) {
let data = Buffer.concat([this.buffer, chunk]);
let position;
while ((position = data.indexOf(this.delimiter)) !== -1) {
this.push(data.slice(0, position));
data = data.slice(position + this.delimiter.length);
}
this.buffer = data;
cb();
}
this.buffer = data;
cb();
};

DelimiterParser.prototype._flush = function(cb) {
this.push(this.buffer);
this.buffer = Buffer.alloc(0);
cb();
_flush(cb) {
this.push(this.buffer);
this.buffer = Buffer.alloc(0);
cb();
}
};

module.exports = DelimiterParser;
64 changes: 37 additions & 27 deletions lib/parsers/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,59 +12,69 @@
* @since 5.0.0
* @example
```js
var SerialPort = require('serialport');
var Readline = SerialPort.parsers.Readline;
var port = new SerialPort('/dev/tty-usbserial1');
var parser = new Readline();
const SerialPort = require('serialport');
const Readline = SerialPort.parsers.Readline;
const port = new SerialPort('/dev/tty-usbserial1');
const parser = new Readline();
port.pipe(parser);
parser.on('data', console.log);
port.write('ROBOT PLEASE RESPOND\n');
// Creating the parser and piping can be shortened to
var parser = port.pipe(new Readline());
// const parser = port.pipe(new Readline());
```
To use the `ByteLength` parser, provide the length of the number of bytes:
```js
var SerialPort = require('serialport');
var ByteLength = SerialPort.parsers.ByteLength
var port = new SerialPort('/dev/tty-usbserial1');
var parser = port.pipe(new ByteLength({length: 8}));
const SerialPort = require('serialport');
const ByteLength = SerialPort.parsers.ByteLength
const port = new SerialPort('/dev/tty-usbserial1');
const parser = port.pipe(new ByteLength({length: 8}));
parser.on('data', console.log);
```
To use the `Delimiter` parser, provide a delimiter as a string, buffer, or array of bytes:
```js
var SerialPort = require('serialport');
var Delimiter = SerialPort.parsers.Delimiter;
var port = new SerialPort('/dev/tty-usbserial1');
var parser = port.pipe(new Delimiter({delimiter: Buffer.from('EOL')}));
const SerialPort = require('serialport');
const Delimiter = SerialPort.parsers.Delimiter;
const port = new SerialPort('/dev/tty-usbserial1');
const parser = port.pipe(new Delimiter({ delimiter: Buffer.from('EOL') }));
parser.on('data', console.log);
```
To use the `Readline` parser, provide a delimiter (defaults to '\n')
To use the `Readline` parser, provide a delimiter (defaults to '\n'). Data is emitted as string controllable by the `encoding` option (defaults to `utf8`).
```js
var SerialPort = require('serialport');
var Readline = SerialPort.parsers.Readline;
var port = new SerialPort('/dev/tty-usbserial1');
var parser = port.pipe(Readline({delimiter: '\r\n'}));
const SerialPort = require('serialport');
const Readline = SerialPort.parsers.Readline;
const port = new SerialPort('/dev/tty-usbserial1');
const parser = port.pipe(Readline({ delimiter: '\r\n' }));
parser.on('data', console.log);
```
To use the `Ready` parser provide a byte start sequence. After the bytes have been received data events will be passed through.
To use the `Ready` parser provide a byte start sequence. After the bytes have been received data events are passed through.
```js
var SerialPort = require('serialport');
var Ready = SerialPort.parsers.Ready;
var port = new SerialPort('/dev/tty-usbserial1');
var parser = port.pipe(Ready({data: 'READY'}));
const SerialPort = require('serialport');
const Ready = SerialPort.parsers.Ready;
const port = new SerialPort('/dev/tty-usbserial1');
const parser = port.pipe(Ready({ data: 'READY' }));
parser.on('ready', () => console.log('the ready byte sequence has been received'))
parser.on('data', console.log); // all data after READY is received
```
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`).
```js
const SerialPort = require('serialport');
const Regex = SerialPort.parsers.Regex;
const port = new SerialPort('/dev/tty-usbserial1');
const parser = port.pipe(Regex({ regex: /[\r\n]+/ }));
parser.on('data', console.log);
```
*/

module.exports = {
Readline: require('./readline'),
Delimiter: require('./delimiter'),
ByteLength: require('./byte-length'),
Regex: require('./regex'),
Ready: require('./ready')
Delimiter: require('./delimiter'),
Readline: require('./readline'),
Ready: require('./ready'),
Regex: require('./regex')
};
30 changes: 11 additions & 19 deletions lib/parsers/readline.js
Original file line number Diff line number Diff line change
@@ -1,26 +1,18 @@
'use strict';

const inherits = require('util').inherits;
const Buffer = require('safe-buffer').Buffer;
const DelimiterParser = require('./delimiter');

function ReadlineParser(options) {
if (!(this instanceof ReadlineParser)) {
return new ReadlineParser(options);
}
module.exports = class ReadLineParser extends DelimiterParser {
constructor(options) {
const opts = Object.assign({
delimiter: Buffer.from('\n', 'utf8'),
encoding: 'utf8'
}, options);

options = options || {};
if (typeof opts.delimiter === 'string') {
opts.delimiter = Buffer.from(opts.delimiter, opts.encoding);
}

if (options.delimiter === undefined) {
options.delimiter = Buffer.from('\n', 'utf8');
super(opts);
}

DelimiterParser.call(this, options);

const encoding = options.encoding || 'utf8';
this.delimiter = Buffer.from(options.delimiter, encoding);
this.setEncoding(encoding);
}

inherits(ReadlineParser, DelimiterParser);
module.exports = ReadlineParser;
};
Loading

0 comments on commit 8b07a48

Please sign in to comment.