Support empty {{ date }}

This commit is contained in:
Adria Navarro 2025-02-27 17:11:47 +01:00
parent af74d3df62
commit 6499f7a270
3 changed files with 7 additions and 5 deletions

View File

@ -33,7 +33,7 @@ const outputJSON: Manifest = {}
const ADDED_HELPERS = { const ADDED_HELPERS = {
date: { date: {
date: { date: {
args: ["datetime", "format", "[options]"], args: ["[datetime]", "[format]", "[options]"],
example: '{{date now "DD-MM-YYYY" "America/New_York" }} -> 21-01-2021', example: '{{date now "DD-MM-YYYY" "America/New_York" }} -> 21-01-2021',
description: description:
"Format a date using moment.js date formatting - the timezone is optional and uses the tz database.", "Format a date using moment.js date formatting - the timezone is optional and uses the tz database.",

View File

@ -71,7 +71,7 @@ function getContext(thisArg: any, locals: any, options: any) {
function initialConfig(str: any, pattern: any, options?: any) { function initialConfig(str: any, pattern: any, options?: any) {
if (isOptions(pattern)) { if (isOptions(pattern)) {
options = pattern options = pattern
pattern = null pattern = DEFAULT_FORMAT
} }
if (isOptions(str)) { if (isOptions(str)) {
@ -93,13 +93,15 @@ function setLocale(this: any, str: any, pattern: any, options?: any) {
dayjs.locale(opts.lang || opts.language) dayjs.locale(opts.lang || opts.language)
} }
const DEFAULT_FORMAT = "MMMM DD, YYYY"
export const date = (str: any, pattern: any, options: any) => { export const date = (str: any, pattern: any, options: any) => {
const config = initialConfig(str, pattern, options) const config = initialConfig(str, pattern, options)
// if no args are passed, return a formatted date // if no args are passed, return a formatted date
if (config.str == null && config.pattern == null) { if (config.str == null && config.pattern == null) {
dayjs.locale("en") dayjs.locale("en")
return dayjs().format("MMMM DD, YYYY") return dayjs().format(DEFAULT_FORMAT)
} }
setLocale(config.str, config.pattern, config.options) setLocale(config.str, config.pattern, config.options)

View File

@ -1208,8 +1208,8 @@
"date": { "date": {
"date": { "date": {
"args": [ "args": [
"datetime", "[datetime]",
"format", "[format]",
"[options]" "[options]"
], ],
"example": "{{date now \"DD-MM-YYYY\" \"America/New_York\" }} -> 21-01-2021", "example": "{{date now \"DD-MM-YYYY\" \"America/New_York\" }} -> 21-01-2021",