Skip to content

Commit 4f9a499

Browse files
authored
fix: null date should return empty (#868) (#872)
1 parent f41c1fc commit 4f9a499

2 files changed

Lines changed: 9 additions & 3 deletions

File tree

src/filters/date.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,9 @@ function parseDate (v: string | Date, opts: NormalizedFullOptions, timezoneOffse
4545
const defaultTimezoneOffset = timezoneOffset ?? opts.timezoneOffset
4646
const locale = opts.locale
4747
v = toValue(v)
48-
if (v === 'now' || v === 'today') {
48+
if (isNil(v)) {
49+
return undefined
50+
} else if (v === 'now' || v === 'today') {
4951
date = new LiquidDate(Date.now(), locale, defaultTimezoneOffset)
5052
} else if (isNumber(v)) {
5153
date = new LiquidDate(v * 1000, locale, defaultTimezoneOffset)

test/integration/filters/date.spec.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,12 @@ describe('filters/date', function () {
2121
const time = String(new Date('2017-03-07T12:00:00').getTime() / 1000)
2222
return test('{{ time | date: "%Y-%m-%dT%H:%M:%S" }}', { time }, '2017-03-07T12:00:00')
2323
})
24-
it('should treat nil as 0', () => {
25-
expect(liquid.parseAndRenderSync('{{ nil | date: "%Y-%m-%dT%H:%M:%S", "Asia/Shanghai" }}')).toEqual('1970-01-01T08:00:00')
24+
it('should treat null as invalid', () => {
25+
const time = null
26+
return test('{{ time | date: "%Y-%m-%dT%H:%M:%S" }}', { time }, '')
27+
})
28+
it('should treat nil as invalid', () => {
29+
expect(liquid.parseAndRenderSync('{{ nil | date: "%Y-%m-%dT%H:%M:%S", "Asia/Shanghai" }}')).toEqual('')
2630
})
2731
it('should treat undefined as invalid', () => {
2832
expect(liquid.parseAndRenderSync('{{ num | date: "%Y-%m-%dT%H:%M:%S", "Asia/Shanghai" }}', { num: undefined })).toEqual('')

0 commit comments

Comments
 (0)