"use strict" ; import { _ } from "streamline-runtime" ; import { Reader } from "../reader" ; import { Writer } from "../writer" ; export interface ParserOptions { sep ? : string ; encoding ? : string ; } export function parser ( options ? ) { const opts = options || { } ; function clean ( line ) { return ( ! opts . sep && line [ line . length - 1 ] === '\r' ) ? line . substring ( 0 , line . length - 1 ) : line ; } return ( _ , reader , writer ) => { var remain = "" ; reader . forEach ( _ , ( _ , chunk ) => { var str ; if ( typeof chunk === 'string' ) str = chunk ; else if ( Buffer . isBuffer ( chunk ) ) str = chunk . toString ( opts . encoding || 'utf8' ) ; else if ( chunk === undefined ) return ; else throw new Error ( "bad input: " + typeof chunk ) ; const lines = str . split ( opts . sep || '\n' ) ; if ( lines . length > 1 ) { writer . write ( _ , clean ( remain + lines [ 0 ] ) ) ; for ( var i = 1 ; i < lines . length - 1 ; i ++ ) writer . write ( _ , clean ( lines [ i ] ) ) ; remain = lines [ i ] ; } else { remain += lines [ 0 ] ; } } ) ; if ( remain ) writer . write ( _ , remain ) ; } ; } export interface FormatterOptions { eol ? : string ; extra ? : boolean ; } export function formatter ( options ? ) { const opts = options || { } ; const eol = opts . eol || '\n' ; return ( _ , reader , writer ) => { if ( opts . extra ) { reader . forEach ( _ , ( _ , line ) => { writer . write ( _ , line + eol ) ; } ) ; } else { reader . forEach ( _ , ( _ , line , i ) => { writer . write ( _ , i > 0 ? eol + line : line ) ; } ) ; } } ; }