mirror of
https://github.com/harttle/liquidjs.git
synced 2026-09-15 04:10:40 -07:00
feat: support calling date without format string, #573
This commit is contained in:
+7
-3
@@ -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'
|
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
|
const opts = this.context.opts
|
||||||
let date: LiquidDate
|
let date: LiquidDate
|
||||||
v = toValue(v)
|
v = toValue(v)
|
||||||
format = stringify(format)
|
format = toValue(format)
|
||||||
|
if (isNil(format)) format = DEFAULT_FMT
|
||||||
|
else format = stringify(format)
|
||||||
if (v === 'now' || v === 'today') {
|
if (v === 'now' || v === 'today') {
|
||||||
date = new Date()
|
date = new Date()
|
||||||
} else if (isNumber(v)) {
|
} else if (isNumber(v)) {
|
||||||
|
|||||||
@@ -362,4 +362,19 @@ describe('Issues', function () {
|
|||||||
const html = await liquid.parseAndRender(tpl, ctx)
|
const html = await liquid.parseAndRender(tpl, ctx)
|
||||||
expect(html).to.equal('FOO')
|
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,6 +6,9 @@ describe('filters/date', function () {
|
|||||||
const date = new Date()
|
const date = new Date()
|
||||||
return test('{{ date | date:"%a %b %d %Y"}}', { date }, date.toDateString())
|
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 () {
|
it('should create a new Date when given "now"', function () {
|
||||||
return test('{{ "now" | date: "%Y"}}', (new Date()).getFullYear().toString())
|
return test('{{ "now" | date: "%Y"}}', (new Date()).getFullYear().toString())
|
||||||
})
|
})
|
||||||
@@ -64,6 +67,9 @@ describe('filters/date', function () {
|
|||||||
it('should support timezone offset argument', 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')
|
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 () {
|
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')
|
return test('{{ "1990-12-31T23:00:00Z" | date: "%Y-%m-%dT%H:%M:%S", "Asia/Colombo" }}', '1991-01-01T04:30:00')
|
||||||
})
|
})
|
||||||
|
|||||||
+4
-2
@@ -8,11 +8,13 @@ export function render (src: string, ctx?: object) {
|
|||||||
return liquid.parseAndRender(src, ctx)
|
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) {
|
if (expected === undefined) {
|
||||||
expected = ctx as string
|
expected = ctx as string
|
||||||
ctx = {}
|
ctx = {}
|
||||||
}
|
}
|
||||||
const engine = opts ? new Liquid(opts) : liquid
|
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)
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user