"use strict" ; import { _ } from "streamline-runtime" ; import { Reader } from "../reader" ; import { Writer } from "../writer" ; export interface Options { size ? : number ; } export function transform < T > ( options ? ) { options = options || { } ; const size = typeof options === 'number' ? options : options . size ; return ( _ , reader , writer ) => { if ( ! size ) return reader . pipe ( _ , writer ) ; var data = reader . read ( _ ) ; while ( data !== undefined ) { if ( data . length < size ) { var d = reader . read ( _ ) ; if ( d === undefined ) { if ( data . length > 0 ) writer . write ( _ , data ) ; data = d ; } else { if ( typeof data === 'string' ) data += d ; else if ( Buffer . isBuffer ( data ) && Buffer . isBuffer ( d ) ) data = Buffer . concat ( [ data , d ] ) ; else if ( Array . isArray ( data ) ) data = data . concat ( d ) ; else throw new Error ( "Cannot cut: bad data type: " + typeof data ) ; } } else { writer . write ( _ , data . slice ( 0 , size ) ) ; data = data . slice ( size ) ; } } } ; }