Skip to content

Commit

Permalink
Update lib, test: Impl streaming parser
Browse files Browse the repository at this point in the history
  • Loading branch information
jhermsmeier committed Oct 25, 2016
1 parent 7661a69 commit 29593aa
Show file tree
Hide file tree
Showing 5 changed files with 7,667 additions and 1 deletion.
68 changes: 68 additions & 0 deletions lib/stream.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
var inherit = require( 'bloodline' )
var Stream = require( 'stream' )
var TLE = require( './tle' )

/**
* Parser
* @constructor
* @param {Object} [options]
* @return {Parser}
*/
function Parser( options ) {

if( !(this instanceof Parser) )
return new Parser( options )

Stream.Transform.call( this, options )

this._readableState.objectMode = true
this._buffer = ''

}

/**
* TLE multiline matching pattern
* @type {RegExp}
*/
Parser.pattern = /(^|\r?\n)([^12][^\r\n]{1,}?)\r?\n(1[^\r\n]{68,})\r?\n(2[^\r\n]{68,})/

/**
* Strip empty lines
* @param {String} str
* @return {String}
*/
Parser.strip = function( str ) {
return str.replace( /^[\s\uFEFF\xA0]*$/gm, '' )
}

/**
* Parser prototype
* @type {Object}
*/
Parser.prototype = {

constructor: Parser,

_transform: function( chunk, _, next ) {

var buffer = Parser.strip( this._buffer + chunk )
var match = null
var tle = null

while( match = Parser.pattern.exec( buffer ) ) {
buffer = buffer.slice( match[0].length )
try { tle = TLE.parse( match[0] ) }
catch( error ) { return this.emit( 'error', error ) }
this.push( tle )
}

this._buffer = buffer

next()

}

}

inherit( Parser, Stream.Transform )
module.exports = Parser
3 changes: 3 additions & 0 deletions lib/tle.js
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,9 @@ TLE.trim = function( str ) {
return str.replace( /^[\s\uFEFF\xA0]+|[\s\uFEFF\xA0]+$/g, '' )
}

TLE.Parser = require( './stream' )
TLE.createParser = TLE.Parser

TLE.prototype = {

constructor: TLE,
Expand Down
4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,9 @@
"space"
],
"main": "lib/tle.js",
"dependencies": {},
"dependencies": {
"bloodline": "~1.0.0"
},
"devDependencies": {
"mocha": "~3.1.2"
},
Expand Down
Loading

0 comments on commit 29593aa

Please sign in to comment.