fix: function convertTemplate

This commit is contained in:
Thanaphon Frappet 2024-11-27 12:55:38 +07:00
parent 807c812b1a
commit aef712b409

View file

@ -4,12 +4,31 @@ const templates = {
return context?.join(', ') || ''; return context?.join(', ') || '';
}, },
}, },
'quotation-payment': {
converter: (context?: {
paymentType: 'full' | 'installments';
installments: {
date: string;
amount: number;
}[];
}) => {
return (
context?.installments
.map(
(v) =>
`asdasd
`,
)
.join(', ') || ''
);
},
},
} as const; } as const;
type Template = typeof templates; type Template = typeof templates;
type TemplateName = keyof Template; type TemplateName = keyof Template;
type TemplateContext = { type TemplateContext = {
[key in TemplateName]: Parameters<Template[TemplateName]['converter']>[0]; [key in TemplateName]: Parameters<Template[key]['converter']>[0];
}; };
export function convertTemplate( export function convertTemplate(
@ -25,7 +44,7 @@ export function convertTemplate(
ret = text.replace( ret = text.replace(
new RegExp('\\#\\[' + name.replaceAll('-', '\\-') + '\\]', 'g'), new RegExp('\\#\\[' + name.replaceAll('-', '\\-') + '\\]', 'g'),
typeof template.converter === 'function' typeof template.converter === 'function'
? template.converter(context?.[name as TemplateName]) ? template.converter(context?.[name as TemplateName] as any)
: template.converter, : template.converter,
); );
} }