Skip to content

Commit aafaa0b

Browse files
committed
feat: support calling date without format string, #573
1 parent 964e5e8 commit aafaa0b

File tree

4 files changed

+32
-5
lines changed

4 files changed

+32
-5
lines changed

src/filters/date.ts

+7-3
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,15 @@
1-
import { toValue, stringify, isString, isNumber, TimezoneDate, LiquidDate, strftime } from '../util'
1+
import { toValue, stringify, isString, isNumber, TimezoneDate, LiquidDate, strftime, isNil } from '../util'
22
import { FilterImpl } from '../template'
33

4-
export function date (this: FilterImpl, v: string | Date, format: string, timezoneOffset?: number | string) {
4+
const DEFAULT_FMT = '%A, %B %-e, %Y at %-l:%M %P %z'
5+
6+
export function date (this: FilterImpl, v: string | Date, format?: string, timezoneOffset?: number | string) {
57
const opts = this.context.opts
68
let date: LiquidDate
79
v = toValue(v)
8-
format = stringify(format)
10+
format = toValue(format)
11+
if (isNil(format)) format = DEFAULT_FMT
12+
else format = stringify(format)
913
if (v === 'now' || v === 'today') {
1014
date = new Date()
1115
} else if (isNumber(v)) {

test/e2e/issues.ts

+15
Original file line numberDiff line numberDiff line change
@@ -362,4 +362,19 @@ describe('Issues', function () {
362362
const html = await liquid.parseAndRender(tpl, ctx)
363363
expect(html).to.equal('FOO')
364364
})
365+
it('#573 date filter should return parsed input when no format is provided', async () => {
366+
const liquid = new Liquid()
367+
liquid.registerTag('metadata_file', {
368+
parse (tagToken: TagToken, remainTokens: TopLevelToken[]) {
369+
this.str = tagToken.args
370+
},
371+
async render (ctx: Context) {
372+
const content = await Promise.resolve(`{{${this.str}}}`)
373+
return this.liquid.parseAndRender(content.toString(), ctx)
374+
}
375+
})
376+
const tpl = `{{ 'now' | date }}`
377+
const html = await liquid.parseAndRender(tpl)
378+
expect(html).to.match(/\w+, January \d+, 2023 at \d+:\d\d [ap]m [-+]\d\d\d\d/)
379+
})
365380
})

test/integration/filters/date.ts

+6
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@ describe('filters/date', function () {
66
const date = new Date()
77
return test('{{ date | date:"%a %b %d %Y"}}', { date }, date.toDateString())
88
})
9+
it('should support "now"', function () {
10+
return test('{{ "now" | date }}', /\w+, January \d+, 2023 at \d+:\d\d [ap]m [-+]\d\d\d\d/)
11+
})
912
it('should create a new Date when given "now"', function () {
1013
return test('{{ "now" | date: "%Y"}}', (new Date()).getFullYear().toString())
1114
})
@@ -64,6 +67,9 @@ describe('filters/date', function () {
6467
it('should support timezone offset argument', function () {
6568
return test('{{ "1990-12-31T23:00:00Z" | date: "%Y-%m-%dT%H:%M:%S", 360}}', '1990-12-31T17:00:00')
6669
})
70+
it('should support timezone without format', function () {
71+
return test('{{ "2022-12-08T03:22:18.000Z" | date: nil, "America/Cayman" }}', 'Wednesday, December 7, 2022 at 10:22 pm -0500')
72+
})
6773
it('should support timezone name argument', function () {
6874
return test('{{ "1990-12-31T23:00:00Z" | date: "%Y-%m-%dT%H:%M:%S", "Asia/Colombo" }}', '1991-01-01T04:30:00')
6975
})

test/stub/render.ts

+4-2
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,13 @@ export function render (src: string, ctx?: object) {
88
return liquid.parseAndRender(src, ctx)
99
}
1010

11-
export async function test (src: string, ctx: object | string, expected?: string, opts?: LiquidOptions) {
11+
export async function test (src: string, ctx: object | string, expected?: string | RegExp, opts?: LiquidOptions) {
1212
if (expected === undefined) {
1313
expected = ctx as string
1414
ctx = {}
1515
}
1616
const engine = opts ? new Liquid(opts) : liquid
17-
return expect(await engine.parseAndRender(src, ctx as object)).to.equal(expected)
17+
const result = await engine.parseAndRender(src, ctx as object)
18+
if (expected instanceof RegExp) return expect(result).to.match(expected)
19+
return expect(result).to.equal(expected)
1820
}

0 commit comments

Comments
 (0)