fix: null date should return empty (#868) (#872)

This commit is contained in:
Timmy Braun
2026-04-08 00:10:33 +08:00
committed by GitHub
parent f41c1fc02f
commit 4f9a49988a
2 changed files with 9 additions and 3 deletions
+3 -1
View File
@@ -45,7 +45,9 @@ function parseDate (v: string | Date, opts: NormalizedFullOptions, timezoneOffse
const defaultTimezoneOffset = timezoneOffset ?? opts.timezoneOffset
const locale = opts.locale
v = toValue(v)
if (v === 'now' || v === 'today') {
if (isNil(v)) {
return undefined
} else if (v === 'now' || v === 'today') {
date = new LiquidDate(Date.now(), locale, defaultTimezoneOffset)
} else if (isNumber(v)) {
date = new LiquidDate(v * 1000, locale, defaultTimezoneOffset)
+6 -2
View File
@@ -21,8 +21,12 @@ describe('filters/date', function () {
const time = String(new Date('2017-03-07T12:00:00').getTime() / 1000)
return test('{{ time | date: "%Y-%m-%dT%H:%M:%S" }}', { time }, '2017-03-07T12:00:00')
})
it('should treat nil as 0', () => {
expect(liquid.parseAndRenderSync('{{ nil | date: "%Y-%m-%dT%H:%M:%S", "Asia/Shanghai" }}')).toEqual('1970-01-01T08:00:00')
it('should treat null as invalid', () => {
const time = null
return test('{{ time | date: "%Y-%m-%dT%H:%M:%S" }}', { time }, '')
})
it('should treat nil as invalid', () => {
expect(liquid.parseAndRenderSync('{{ nil | date: "%Y-%m-%dT%H:%M:%S", "Asia/Shanghai" }}')).toEqual('')
})
it('should treat undefined as invalid', () => {
expect(liquid.parseAndRenderSync('{{ num | date: "%Y-%m-%dT%H:%M:%S", "Asia/Shanghai" }}', { num: undefined })).toEqual('')