forked from jhermsmeier/node-tle
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Update lib, test: Impl streaming parser
- Loading branch information
1 parent
7661a69
commit 29593aa
Showing
5 changed files
with
7,667 additions
and
1 deletion.
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,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 |
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
Oops, something went wrong.