import R = require ( './regexes' ) ; /** Quote types to escape in strings */ const enum QUOTES { Single = 1 , Double = 2 } /**
 * Helper for call `JSON.stringify` in `stringifyObject`.
 *
 * It outputs a valid number for non-finite numbers and the source string of
 * regexes because stringify converts `Infinity` and `-Infinity` to `null`
 * and regexes to `{}` (an empty object).
 */ const stringifyFn = ( _ , value ) => { if ( value && value . constructor . name === 'Number' ) { if ( + value === Infinity ) { return Number . MAX_VALUE ; } if ( + value === - Infinity ) { return Number . MIN_VALUE ; } } return value instanceof RegExp ? value . source : value ; } ; param obj returns const stringObject = ( obj ) => { let str ; if ( obj instanceof Date ) { str = isNaN ( + obj ) ? 'NaN' : obj . toJSON ( ) ; } else if ( obj instanceof RegExp ) { str = obj . source ; } else if ( obj instanceof String ) { str = obj . valueOf ( ) ; } else if ( obj instanceof Number ) { str = String ( obj ) ; } else { str = JSON . stringify ( obj , stringifyFn ) ; } return str ; } ; param value const stringValue = ( value , escapeQuotes ) => { if ( ! value ) { return String ( value ) ; } if ( typeof value === 'object' ) { return stringObject ( value ) ; } let str = String ( value ) ; if ( escapeQuotes & QUOTES . Single ) { str = str . replace ( /(?=')/g , '\\' ) ; } if ( escapeQuotes & QUOTES . Double ) { str = str . replace ( /(?=")/g , '\\' ) ; } return str ; } ; param value param match throws const getValueInfo = ( value , match ) => { const propPath = match [ 2 ] && match [ 2 ] . slice ( 1 ) ; let length = match [ 1 ] . length ; if ( propPath ) { const props = propPath . split ( '.' ) ; let prop = props . shift ( ) ; while ( prop && typeof value === 'object' ) { value = value [ prop ] ; length += prop . length + 1 ; prop = props . shift ( ) ; } } return { value , length } ; } ; param props param fragment param start const remapVars = function _remapVars ( props , fragment , start ) { const re = new RegExp ( R . VARS_TO_REPL ) ; let changes = false ; let match ; while ( match = re . exec ( fragment ) ) { const vname = match [ 1 ] . slice ( 1 ) ; if ( vname in props . values ) { const index = start + match . index ; const vinfo = getValueInfo ( props . values [ vname ] , match ) ; props . magicStr . overwrite ( index , index + vinfo . length , stringValue ( vinfo . value , props . escapeQuotes ) ) ; changes = true ; } } return changes ; } ; export = remapVars ;