Skip to content

Instantly share code, notes, and snippets.

@kenjinp
Created November 2, 2020 08:13
Show Gist options
  • Save kenjinp/93c446a19b50cba50eb621aad1056f26 to your computer and use it in GitHub Desktop.
Save kenjinp/93c446a19b50cba50eb621aad1056f26 to your computer and use it in GitHub Desktop.
Simple Templater Function
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