forked from serialport/node-serialport
-
Notifications
You must be signed in to change notification settings - Fork 0
/
parsers.js
28 lines (26 loc) · 923 Bytes
/
parsers.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
/*jslint node: true */
"use strict";
// Copyright 2011 Chris Williams <chris@iterativedesigns.com>
var parsers = module.exports = {
raw: function (emitter, buffer) {
emitter.emit("data", buffer);
},
//encoding: ascii utf8 utf16le ucs2 base64 binary hex
//More: http://nodejs.org/api/buffer.html#buffer_buffer
readline: function (delimiter, encoding) {
if (typeof delimiter === "undefined" || delimiter === null) { delimiter = "\r"; }
if (typeof encoding === "undefined" || encoding === null) { encoding = "utf8"; }
// Delimiter buffer saved in closure
var data = "";
return function (emitter, buffer) {
// Collect data
data += buffer.toString(encoding);
// Split collected data by delimiter
var parts = data.split(delimiter);
data = parts.pop();
parts.forEach(function (part, i, array) {
emitter.emit('data', part);
});
};
}
};