This repository has been archived by the owner on Jun 15, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 31
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Create Node Streams Transformer interface and add command a command l…
…ine tool
- Loading branch information
Showing
5 changed files
with
75 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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);; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters