From be1119b4137479e1082937bbd2affe62d357cd50 Mon Sep 17 00:00:00 2001 From: Hongbo Wu Date: Mon, 6 Nov 2023 12:23:11 +0800 Subject: [PATCH] fix: add a lambda function in the template to allow users to format date (#136) * fix: add a lambda function in the template to allow users to format date * return empty string if date variable is undefined in the formatDate function --- src/settings/template.ts | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/src/settings/template.ts b/src/settings/template.ts index 99ed2b7..c49b87b 100644 --- a/src/settings/template.ts +++ b/src/settings/template.ts @@ -118,10 +118,24 @@ function upperCaseFirst() { }; } +function formatDateFunc() { + return function (text: string, render: (text: string) => string) { + // get the date and format from the text + const [dateVariable, format] = text.split(",", 2); + const date = render(dateVariable); + if (!date) { + return ""; + } + // format the date + return formatDate(date, format); + }; +} + const functionMap: FunctionMap = { lowerCase, upperCase, upperCaseFirst, + formatDate: formatDateFunc, }; export const renderFilename = (