export interface TemplateOptions { /***
     * Argument name to use in template function
     */ dataName : string ; /***
     * Output array name for use in template function
     */ outName : string ; } param { string } template param { { dataName : string , outName : string } } returns { ( data : any ) => string } export function template ( template , { dataName = '$__' , outName = 'out' } = { } ) { const re = /{{(.+?)}}/g ; const reExp = /(^( )?(if|for|else|switch|case|break|{|}))(.*)?/g ; let code = `with(${ dataName }) {var ${ outName }=[];\n` ; let cursor = 0 ; let match ; const add = function ( line , js ? ) { js ? ( code += line . match ( reExp ) ? line + '\n' : `try{${ outName }.push(` + line + ')}catch(_unused){};\n' ) : ( code += line !== '' ? `${ outName }.push("` + line . replace ( /"/g , '\\"' ) + '");\n' : '' ) ; return add ; } ; while ( match = re . exec ( template ) ) { add ( template . slice ( cursor , match . index ) ) ( match [ 1 ] , true ) ; cursor = match . index + match [ 0 ] . length ; } add ( template . substr ( cursor , template . length - cursor ) ) ; code += `return ${ outName }.join("");}` ; return new Function ( dataName , code . replace ( /[\r\t\n]/g , '' ) ) as ( data : any ) => string ; }