Created
November 2, 2020 08:13
-
-
Save kenjinp/93c446a19b50cba50eb621aad1056f26 to your computer and use it in GitHub Desktop.
Simple Templater Function
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
import { get } from 'lodash'; | |
// will result in template parsing inside js-like template-string literals: ${ } | |
// for example ${myObject.name} | |
const templateStringExpression = /\${([^}]*)}/g; | |
const templater = (sourceObject: object, templateString: string) => { | |
return templateString.replace( | |
templateStringExpression, | |
(match: string, group1: string) => { | |
const value = get(sourceObject, group1); | |
return value; | |
} | |
); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment