Skip to content

Commit

Permalink
feat: support calling date without format string, #573
Browse files Browse the repository at this point in the history
  • Loading branch information
harttle committed Jan 2, 2023
1 parent 964e5e8 commit aafaa0b
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 5 deletions.
10 changes: 7 additions & 3 deletions src/filters/date.ts
Original file line number Diff line number Diff line change
@@ -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)) {
Expand Down
15 changes: 15 additions & 0 deletions test/e2e/issues.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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/)
})
})
6 changes: 6 additions & 0 deletions test/integration/filters/date.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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())
})
Expand Down Expand Up @@ -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')
})
Expand Down
6 changes: 4 additions & 2 deletions test/stub/render.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

0 comments on commit aafaa0b

Please sign in to comment.