@cloudpss/template
Version:
String and object template engine for Node.js and the browser.
25 lines • 771 B
JavaScript
import { TemplateCompiler } from './compiler.js';
export const defaultEvaluator = {
compile: (expression, type) => {
const key = JSON.stringify(expression.trim());
switch (type) {
case 'formula':
return `context[${key}]`;
case 'interpolation':
return `(context[${key}] ?? '')`;
/* c8 ignore next 2 */
default:
throw new Error(`Unsupported type: ${type}`);
}
},
};
/** 创建模板 */
export function template(template, options = {}) {
const opt = {
objectKeyMode: 'template',
evaluator: defaultEvaluator,
...options,
};
return new TemplateCompiler(template, opt).build();
}
//# sourceMappingURL=index.js.map