import { _ } from 'streamline-runtime' ; import { Reader } from '../reader' ; import { Writer } from '../writer' ; import { nextTick } from '../util' ; export interface Options { sync ? : boolean ; chunkSize ? : number | ( ( ) => number ) ; } export class StringWriter extends Writer < string > { buf : string ; constructor ( options ) { super ( ( _ , value ) => { if ( ! options . sync ) nextTick ( _ ) ; if ( value !== undefined ) this . buf += value ; return this ; } ) ; this . buf = '' ; } toString ( ) { return this . buf ; } get result ( ) { return this . buf ; } } export function reader ( text , options ? ) { var opts ; if ( typeof options === "number" ) { opts = { chunkSize : options } ; } else opts = options || { } ; const chunkSize = opts . chunkSize || 1024 ; var pos = 0 ; return new Reader ( function read ( _ ) { if ( ! opts . sync ) nextTick ( _ ) ; if ( pos >= text . length ) return ; const len = typeof chunkSize === "function" ? chunkSize ( ) : chunkSize ; const s = text . substring ( pos , pos + len ) ; pos += len ; return s ; } ) ; } export function writer ( options ? ) { return new StringWriter ( options || { } ) ; } export function factory ( url ) { return { reader : ( _ ) => { return module . exports . reader ( url . substring ( url . indexOf ( ':' ) + 1 ) ) ; } , writer : ( _ ) => { return module . exports . writer ( ) ; } , } ; }