diff --git a/src/filters/date.ts b/src/filters/date.ts index 33bed77248..62156a1cca 100644 --- a/src/filters/date.ts +++ b/src/filters/date.ts @@ -1,11 +1,15 @@ -import { toValue, stringify, isString, isNumber, TimezoneDate, LiquidDate, strftime } from '../util' +import { toValue, stringify, isString, isNumber, TimezoneDate, LiquidDate, strftime, isNil } from '../util' import { FilterImpl } from '../template' -export function date (this: FilterImpl, v: string | Date, format: string, timezoneOffset?: number | string) { +const DEFAULT_FMT = '%A, %B %-e, %Y at %-l:%M %P %z' + +export function date (this: FilterImpl, v: string | Date, format?: string, timezoneOffset?: number | string) { const opts = this.context.opts let date: LiquidDate v = toValue(v) - format = stringify(format) + format = toValue(format) + if (isNil(format)) format = DEFAULT_FMT + else format = stringify(format) if (v === 'now' || v === 'today') { date = new Date() } else if (isNumber(v)) { diff --git a/test/e2e/issues.ts b/test/e2e/issues.ts index ba6af67a23..0e4f332f92 100644 --- a/test/e2e/issues.ts +++ b/test/e2e/issues.ts @@ -362,4 +362,19 @@ describe('Issues', function () { const html = await liquid.parseAndRender(tpl, ctx) expect(html).to.equal('FOO') }) + it('#573 date filter should return parsed input when no format is provided', async () => { + const liquid = new Liquid() + liquid.registerTag('metadata_file', { + parse (tagToken: TagToken, remainTokens: TopLevelToken[]) { + this.str = tagToken.args + }, + async render (ctx: Context) { + const content = await Promise.resolve(`{{${this.str}}}`) + return this.liquid.parseAndRender(content.toString(), ctx) + } + }) + const tpl = `{{ 'now' | date }}` + const html = await liquid.parseAndRender(tpl) + expect(html).to.match(/\w+, January \d+, 2023 at \d+:\d\d [ap]m [-+]\d\d\d\d/) + }) }) diff --git a/test/integration/filters/date.ts b/test/integration/filters/date.ts index ea6902d3cb..ce8987cce2 100644 --- a/test/integration/filters/date.ts +++ b/test/integration/filters/date.ts @@ -6,6 +6,9 @@ describe('filters/date', function () { const date = new Date() return test('{{ date | date:"%a %b %d %Y"}}', { date }, date.toDateString()) }) + it('should support "now"', function () { + return test('{{ "now" | date }}', /\w+, January \d+, 2023 at \d+:\d\d [ap]m [-+]\d\d\d\d/) + }) it('should create a new Date when given "now"', function () { return test('{{ "now" | date: "%Y"}}', (new Date()).getFullYear().toString()) }) @@ -64,6 +67,9 @@ describe('filters/date', function () { it('should support timezone offset argument', function () { return test('{{ "1990-12-31T23:00:00Z" | date: "%Y-%m-%dT%H:%M:%S", 360}}', '1990-12-31T17:00:00') }) + it('should support timezone without format', function () { + return test('{{ "2022-12-08T03:22:18.000Z" | date: nil, "America/Cayman" }}', 'Wednesday, December 7, 2022 at 10:22 pm -0500') + }) it('should support timezone name argument', function () { return test('{{ "1990-12-31T23:00:00Z" | date: "%Y-%m-%dT%H:%M:%S", "Asia/Colombo" }}', '1991-01-01T04:30:00') }) diff --git a/test/stub/render.ts b/test/stub/render.ts index a212a61971..b11419a044 100644 --- a/test/stub/render.ts +++ b/test/stub/render.ts @@ -8,11 +8,13 @@ export function render (src: string, ctx?: object) { return liquid.parseAndRender(src, ctx) } -export async function test (src: string, ctx: object | string, expected?: string, opts?: LiquidOptions) { +export async function test (src: string, ctx: object | string, expected?: string | RegExp, opts?: LiquidOptions) { if (expected === undefined) { expected = ctx as string ctx = {} } const engine = opts ? new Liquid(opts) : liquid - return expect(await engine.parseAndRender(src, ctx as object)).to.equal(expected) + const result = await engine.parseAndRender(src, ctx as object) + if (expected instanceof RegExp) return expect(result).to.match(expected) + return expect(result).to.equal(expected) }