typed-utilities
Version:
Strongly typed general purpose utilities
33 lines (26 loc) • 665 B
JavaScript
;
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.interpolate = void 0;
const DEFAULT_INTERPOLATE_VARIABLE_PATTERN = /{([^{}]*)}/g;
const interpolate = ({
template,
variables,
strict = false,
pattern = DEFAULT_INTERPOLATE_VARIABLE_PATTERN
}) => template.split(pattern).map((part, index) => {
if (index % 2 === 0) {
return part;
}
const variable = variables[part];
if (typeof variable === `undefined`) {
if (strict) {
throw new Error(`variable not found: '${part}'`);
}
return part;
}
return variable;
});
exports.interpolate = interpolate;
//# sourceMappingURL=interpolate.js.map