pig-dam-core
Version:
Library that should be included in every Pig DAM project we build
35 lines (34 loc) • 1.07 kB
JavaScript
;
/**
* Date: 6/3/18
* Time: 11:39 PM
* @license MIT (see project's LICENSE file)
*
*/
Object.defineProperty(exports, "__esModule", { value: true });
exports.render = void 0;
const _ = require("lodash");
const error_1 = require("./error");
const cache = {};
/**
* Does substitution of variables in template string. Encoding of variables should be as follows: {{variable}}
* @param template
* @param variables - set of key/values
* @param interpolate - regex pattern for finding substitution variables
*/
function render(template, variables, { interpolate = /{{\s*(\S+?)\s*}}/g } = {}) {
if (!(template in cache)) {
cache[template] = _.template(template, { interpolate });
}
try {
return cache[template](variables);
}
catch (error) {
throw new error_1.PigError({
details: `function=${cache[template].toString()}\nvariables=${JSON.stringify(variables)}`,
error,
message: `attempt to render template='${template}' failed`
});
}
}
exports.render = render;