Created
September 23, 2020 16:19
-
-
Save tharzeez/c4da0b24d31bbf8eda6dba309ce3fd18 to your computer and use it in GitHub Desktop.
Simple html interpolation for react native
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Html interpolation for react native. | |
* Since lodash template wasnt supported in some devices. | |
* /{{.*?}}/ regex for Lazy quantifiers (non greedy stop at first occurence) | |
*/ | |
let interpolationValues = {name: 'Tharzeez', workspace: 'my workspace'} | |
let interpolatedString = "Hello {{ name }}, welcome back to your {{ workspace }}".replace(/{{.*?}}/g, (char) => { | |
/** | |
* char contains the matched occurence i.e {{ name }} for the first time and {{ workspace }} for the second time. | |
* splits returns array and join with empty space, removing the braces. | |
*/ | |
let path = char.split('{').join('').split('}').join(''); | |
let value = _.get(interpolationValues, _.trim(path)); | |
return value; | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment