@cloudpss/template
Version:
String and object template engine for Node.js and the browser.
39 lines • 1.46 kB
TypeScript
import type { TemplateType } from '../parser.js';
/** 模板编译求值 */
export type TemplateEvaluator = {
/** 注入为 `evaluator` 变量,可在生成的求值代码中使用 */
inject?: unknown;
/** 求值代码是否为异步 @default false */
async?: boolean;
/** 生成求值 JS 代码,可用变量:`evaluator` `context` */
compile: (expression: string, type: TemplateType) => string;
};
/** 模板选项 */
export interface TemplateOptions {
/**
* 模板求值器
* @default context?.[expression] ?? (type === 'formula' ? undefined : '')
*/
evaluator?: TemplateEvaluator;
/**
* 对 object key 的处理方式
* - `template` 使用模板进行插值
* - `ignore` 原样输出
* @default 'template'
*/
objectKeyMode?: 'template' | 'ignore';
}
export declare const defaultEvaluator: TemplateEvaluator;
/** 已编译的模板函数 */
export type TemplateFunction<T = unknown, C = Record<string, unknown>> = ((context?: C) => T) & {
source: string;
};
/** 创建模板 */
export declare function template<T = unknown, C = Record<string, unknown>>(template: T, options: TemplateOptions & {
evaluator: {
async: true;
};
}): TemplateFunction<Promise<T>, C>;
/** 创建模板 */
export declare function template<T = unknown, C = Record<string, unknown>>(template: T, options?: TemplateOptions): TemplateFunction<T, C>;
//# sourceMappingURL=index.d.ts.map