Skip to content
This repository has been archived by the owner on Jun 15, 2022. It is now read-only.

Commit

Permalink
Create Node Streams Transformer interface and add command a command l…
Browse files Browse the repository at this point in the history
…ine tool
  • Loading branch information
tkurki committed Oct 24, 2014
1 parent bfeac72 commit de67d37
Show file tree
Hide file tree
Showing 5 changed files with 75 additions and 2 deletions.
10 changes: 10 additions & 0 deletions bin/parse-nmea
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
#!/usr/bin/env node

var Liner = require('../liner.js');

process.stdin.resume();
process.stdin.setEncoding('utf8');


//process.stdin.pipe(new Liner()).pipe(require('../nmea.js').createDefaultTransformer()).pipe(process.stdout);
process.stdin.pipe(new Liner()).pipe(require('../nmea.js').createDefaultTransformer()).pipe(require('JSONStream').stringify(false)).pipe(process.stdout);;
2 changes: 1 addition & 1 deletion helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -233,7 +233,7 @@ exports.encodeFixed = function(v, f) {
};

// =========================================
// field parsers
// field traditionalDecoders
// =========================================

// separate number and units
Expand Down
49 changes: 49 additions & 0 deletions liner.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
var Transform = require('stream').Transform;

function Liner() {
Transform.call(this, { objectMode: true });
}

Liner.prototype = {
_transform: function (chunk, encoding, done) {
var data = chunk.toString()
if (this._lastLineData) data = this._lastLineData + data

var lines = data.split('\n')
this._lastLineData = lines.splice(lines.length - 1, 1)[0]

lines.forEach(this.push.bind(this))
done()
},
_flush: function (done) {
if (this._lastLineData) this.push(this._lastLineData)
this._lastLineData = null
done()
}
}

extend(Transform, Liner);

function extend(base, sub) {
// Avoid instantiating the base class just to setup inheritance
// See https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/create
// for a polyfill
// Also, do a recursive merge of two prototypes, so we don't overwrite
// the existing prototype, but still maintain the inheritance chain
// Thanks to @ccnokes
var origProto = sub.prototype;
sub.prototype = Object.create(base.prototype);
for (var key in origProto) {
sub.prototype[key] = origProto[key];
}
// Remember the constructor property was set wrong, let's fix it
sub.prototype.constructor = sub;
// In ECMAScript5+ (all modern browsers), you can make the constructor property
// non-enumerable if you define it like this instead
Object.defineProperty(sub.prototype, 'constructor', {
enumerable: false,
value: sub
});
}

module.exports = Liner;
12 changes: 11 additions & 1 deletion nmea.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ var validLine = function(line) {
return checkVal == parseInt(parts[1], 16);
};

exports.parsers = {
exports.traditionalDecoders = {
GGA: GGA.decode,
RMC: RMC.decode,
APB: APB.decode,
Expand Down Expand Up @@ -80,3 +80,13 @@ exports.encode = function(talker, msg) {
throw Error("No encoder for type:" + msg.type);
}
}

exports.createDefaultTransformer = function (options) {
var stream = require('through')(function (data) {
try {
stream.queue(exports.parse(data));
} catch (e) {
}
});
return stream;
};
4 changes: 4 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,9 @@
"should": "~2.0.2",
"mocha": "1.13",
"line-reader": "0.2"
},
"dependencies" : {
"through": ">=2.2.7 <3",
"JSONStream" : "0.7"
}
}

0 comments on commit de67d37

Please sign in to comment.