import { DYNAMIC_CONTENT_MODIFIER_CAPITALIZE_FIRST , DYNAMIC_CONTENT_MODIFIER_CAPITALIZE_ALL_FIRST , DYNAMIC_CONTENT_MODIFIER_UPPERCASE , DYNAMIC_CONTENT_MODIFIER_LOWERCASE } from './DynamicContent.constants' ; export class DynamicContent { public content : string ; public values : { [ key ] : any ; } ; public language : string ; constructor ( content , values , language ) { this . content = content ; this . values = values ; this . language = language ; } public getReplacedContent ( ) { const regexp = new RegExp ( '\\{\\{[^\\}\\}]*}}' , 'gm' ) ; return this . content . replace ( regexp , ( substring ) => { const pattern = substring . replace ( '}}' , '' ) . replace ( '{{' , '' ) ; return this . getResultingContent ( pattern ) ; } ) ; } private getResultingContent ( pattern ) { let [ name , modifier , value = '' ] = pattern . split ( '|' ) ; const hasReplacedValueByCurrentLanguage = this . values . localization && this . values . localization [ this . language ] && this . values . localization [ this . language ] [ name ] ; const hasReplacedValueByDefaultLanguage = this . values . localization && this . values . localization [ this . values . default_language ] && this . values . localization [ this . values . default_language ] [ name ] ; if ( hasReplacedValueByCurrentLanguage ) { value = this . values . localization [ this . language ] [ name ] ; } else if ( hasReplacedValueByDefaultLanguage ) { value = this . values . localization [ this . values . default_language ] [ name ] ; } return this . updateStringByModifier ( value , modifier ) ; } private updateStringByModifier ( string , modifier ) { switch ( modifier . toLowerCase ( ) ) { case DYNAMIC_CONTENT_MODIFIER_CAPITALIZE_FIRST : string = string . toLowerCase ( ) ; string = string . charAt ( 0 ) . toUpperCase ( ) + string . substring ( 1 ) ; return string ; case DYNAMIC_CONTENT_MODIFIER_CAPITALIZE_ALL_FIRST : const arrLetters = string . toLowerCase ( ) . split ( ' ' ) ; for ( let i = 0 ; i < arrLetters . length ; i ++ ) { arrLetters [ i ] = arrLetters [ i ] . charAt ( 0 ) . toUpperCase ( ) + arrLetters [ i ] . substring ( 1 ) ; } string = arrLetters . join ( ' ' ) ; return string ; case DYNAMIC_CONTENT_MODIFIER_UPPERCASE : return string . toUpperCase ( ) ; case DYNAMIC_CONTENT_MODIFIER_LOWERCASE : return string . toLowerCase ( ) ; default : return string ; } } }