@technobuddha/library
Version:
A large library of useful functions
30 lines (26 loc) • 1.04 kB
text/typescript
import escapeRegExp from 'lodash/escapeRegExp';
import { empty } from '../constants';
type Options = {
/** The opening field delimiter */
open?: string;
/** The closing field delimiter */
close?: string;
};
/**
* Fill a template with supplies values
* @param input The template
* @param values A dictionary of name-values used to fill in values in the template
* @param __namedParameters see {@link Options}
* @default open '{{'
* @default close (default '}}')
* @return template with values replaced
*/
export function fillTemplate(input: string, values: Record<string, string | undefined>, { open = '{{', close = '}}' }: Options = {}): string {
// eslint-disable-next-line @typescript-eslint/prefer-regexp-exec
for(const match of input.match(new RegExp(`${escapeRegExp(open)}(.+?)${escapeRegExp(close)}`, 'ug')) ?? []) {
const key = match.slice(open.length, -close.length).trim();
input = input.replace(match, values[key] ?? empty);
}
return input;
}
export default fillTemplate;