"use strict" ; import { _ } from "streamline-runtime" ; import { Reader } from "../reader" ; import { Writer } from "../writer" ; import * as lines from './lines' ; export interface ParserOptions { sep ? : string ; encoding ? : string ; } export function parser ( options ? ) { const opts = options || { } ; const sep = opts . sep || ',' ; return ( _ , reader , writer ) => { const rd = reader . transform ( lines . parser ( ) ) ; const keys = ( rd . read ( _ ) || '' ) . split ( sep ) ; rd . forEach ( _ , ( _ , line ) => { if ( line . length === 0 ) return ; const values = line . split ( sep ) ; const obj = { } ; keys . forEach ( ( key , i ) => { obj [ key ] = values [ i ] ; } ) ; writer . write ( _ , obj ) ; } ) ; } ; } export interface FormatterOptions { sep ? : string ; eol ? : string ; } export function formatter ( options ? ) { const opts = options || { } ; const sep = opts . sep || ',' ; const eol = opts . eol || '\n' ; return ( _ , reader , writer ) => { var obj = reader . read ( _ ) ; if ( ! obj ) return ; const keys = Object . keys ( obj ) ; writer . write ( _ , keys . join ( sep ) + eol ) ; do { var values = keys . map ( ( key ) => obj [ key ] ) ; writer . write ( _ , values . join ( sep ) + eol ) ; } while ( ( obj = reader . read ( _ ) ) !== undefined ) ; } ; }