import util from 'util' ; import debugModule from 'debug' ; import linewise from 'linewise' ; import { Writable , Duplex } from 'stream' ; const debug = debugModule ( 'slate-irc-parser' ) ; export default class Parser extends Writable { nlstream : Duplex ; api constructor ( ) { super ( ) ; this . nlstream = linewise . getPerLineBuffer ( ) ; this . nlstream . on ( 'data' , this . online . bind ( this ) ) ; this . nlstream . resume ( ) ; } param { Buffer } chunk api write ( chunk ) { return this . nlstream . write ( chunk ) ; } param { String } line api online ( line ) { line = line . replace ( /\r$/ , '' ) ; debug ( 'line %s' , util . inspect ( line ) ) ; const orig = line ; let prefix ; if ( ':' == line [ 0 ] ) { const i = line . indexOf ( ' ' ) ; prefix = line . slice ( 1 , i ) ; line = line . slice ( i + 1 ) ; } let i = line . indexOf ( ' ' ) ; if ( - 1 == i ) i = line . length ; const command = line . slice ( 0 , i ) ; line = line . slice ( i ) ; i = line . indexOf ( ' :' ) ; if ( - 1 == i ) i = line . length ; const params = line . slice ( 1 , i ) ; line = line . slice ( i + 2 ) ; const msg = { prefix : prefix || '' , command : command , params : params || '' , trailing : line || '' , string : orig , } ; debug ( 'message %j' , msg ) ; this . emit ( 'message' , msg ) ; } api end ( ) { this . emit ( 'end' ) ; } }