@cloudpss/template
Version:
Lightweight string and object templating utilities with interpolation and formula support.
61 lines (56 loc) • 1.4 kB
text/typescript
import type { VmContext } from '@mirascript/mirascript';
import { TemplateCompiler } from './compiler.js';
export {
type VmContext,
VmError,
VmExtern,
VmModule,
VmFunction,
type VmAny,
type VmArray,
type VmConst,
type VmImmutable,
type VmPrimitive,
type VmScript,
type VmRecord,
type VmUninitialized,
type VmValue,
createVmContext,
isVmContext,
isVmExtern,
isVmModule,
isVmFunction,
isVmValue,
isVmAny,
isVmArray,
isVmCallable,
isVmConst,
isVmImmutable,
isVmPrimitive,
isVmRecord,
isVmScript,
isVmWrapper,
wrapToVmValue,
unwrapFromVmValue,
configCheckpoint,
} from '@mirascript/mirascript';
/** 模板选项 */
export interface TemplateOptions {
/**
* 对 object key 的处理方式
* - `template` 使用模板进行插值
* - `ignore` 原样输出
* @default 'template'
*/
objectKeyMode?: 'template' | 'ignore';
}
/** 已编译的模板函数 */
export type TemplateFunction<T = unknown> = (context?: VmContext) => T;
/** 创建模板 */
export function template<T = unknown>(template: T, options: TemplateOptions = {}): TemplateFunction<T> {
const opt: Required<TemplateOptions> = {
objectKeyMode: 'template',
...options,
};
return new TemplateCompiler(template, opt).build() as TemplateFunction<T>;
}