import lang = require ( './lang' ) ; declare var exports ; export function repeat ( string , times ) { if ( ! string || times <= 0 ) { return '' ; } var buffer = [ ] ; while ( true ) { if ( times & 1 ) { buffer . push ( string ) ; } times >>= 1 ; if ( ! times ) { break ; } string += string ; } return buffer . join ( '' ) ; } enum Padding { Left , Right , Both } ; function _pad ( text , size , character , position = Padding . Right ) { var length = size - text . length , pad = exports . repeat ( character , Math . ceil ( length / character . length ) ) ; if ( position === Padding . Left ) { return pad + text ; } else if ( position === Padding . Right ) { return text + pad ; } else { var left = Math . ceil ( length / 2 ) ; return pad . substr ( 0 , left ) + text + pad . substr ( 0 , length - left ) ; } } export function pad ( text , size , character = ' ' ) { return _pad ( text , size , character , Padding . Both ) ; } export function padr ( text , size , character = ' ' ) { return _pad ( text , size , character , Padding . Right ) ; } export function padl ( text , size , character = ' ' ) { return _pad ( text , size , character , Padding . Left ) ; } export interface ITransform { ( value , key ? ) : any ; } var substitutePattern = /\$\{([^\s\:\}]+)(?:\:([^\s\:\}]+))?\}/g ; function defaultTransform ( value ) { return value ; } ; export function substitute ( template , map , transform ? , context ? ) ; export function substitute ( template , map , transform ? , context ? ) ; export function substitute ( template , map , transform ? , context ? ) { context = context || undefined ; transform = transform ? transform . bind ( context ) : defaultTransform ; return template . replace ( substitutePattern , function ( match , key , format ) { var value = lang . getProperty ( map , key ) ; if ( format ) { value = lang . getProperty ( context , format ) . call ( context , value , key ) ; } return transform ( value , key ) + '' ; } ) ; } export function count ( haystack , needle ) { var hits = 0 , lastIndex = haystack . indexOf ( needle ) ; while ( lastIndex > - 1 ) { ++ hits ; lastIndex = haystack . indexOf ( needle , lastIndex + 1 ) ; } return hits ; } var regExpPattern = /[-\[\]{}()*+?.,\\\^$|#\s]/g ; export function escapeRegExpString ( string ) { return string . replace ( regExpPattern , '\\$&' ) ; }