mirror of
https://github.com/harttle/liquidjs.git
synced 2026-09-15 20:30:39 -07:00
feat: add preserveTimezones option
This commit is contained in:
@@ -1,14 +1,21 @@
|
|||||||
import strftime, { TimezoneDate } from '../../util/strftime'
|
import strftime, { TimezoneDate } from '../../util/strftime'
|
||||||
import { isString, isNumber } from '../../util/underscore'
|
import { isString, isNumber } from '../../util/underscore'
|
||||||
|
import { FilterImpl } from '../../template/filter/filter-impl'
|
||||||
|
|
||||||
export function date (v: string | Date, arg: string) {
|
export function date (this: FilterImpl, v: string | Date, arg: string) {
|
||||||
let date = v
|
let date = v
|
||||||
if (v === 'now' || v === 'today') {
|
if (v === 'now' || v === 'today') {
|
||||||
date = new Date()
|
date = new Date()
|
||||||
} else if (isNumber(v)) {
|
} else if (isNumber(v)) {
|
||||||
date = new Date(v * 1000)
|
date = new Date(v * 1000)
|
||||||
} else if (isString(v)) {
|
} else if (isString(v)) {
|
||||||
date = /^\d+$/.test(v) ? new Date(+v * 1000) : new TimezoneDate(v)
|
if (/^\d+$/.test(v)) {
|
||||||
|
date = new Date(+v * 1000)
|
||||||
|
} else if (this.context.opts.preserveTimezones) {
|
||||||
|
date = new TimezoneDate(v)
|
||||||
|
} else {
|
||||||
|
date = new Date(v)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
return isValidDate(date) ? strftime(date, arg) : v
|
return isValidDate(date) ? strftime(date, arg) : v
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -37,6 +37,8 @@ export interface LiquidOptions {
|
|||||||
outputDelimiterLeft?: string;
|
outputDelimiterLeft?: string;
|
||||||
/** The right delimiter for liquid outputs. **/
|
/** The right delimiter for liquid outputs. **/
|
||||||
outputDelimiterRight?: string;
|
outputDelimiterRight?: string;
|
||||||
|
/** Whether input strings to date filter preserve the given timezone **/
|
||||||
|
preserveTimezones?: boolean;
|
||||||
/** Whether `trim*Left`/`trim*Right` is greedy. When set to `true`, all consecutive blank characters including `\n` will be trimed regardless of line breaks. Defaults to `true`. */
|
/** Whether `trim*Left`/`trim*Right` is greedy. When set to `true`, all consecutive blank characters including `\n` will be trimed regardless of line breaks. Defaults to `true`. */
|
||||||
greedy?: boolean;
|
greedy?: boolean;
|
||||||
/** `fs` is used to override the default file-system module with a custom implementation. */
|
/** `fs` is used to override the default file-system module with a custom implementation. */
|
||||||
@@ -69,6 +71,7 @@ export interface NormalizedFullOptions extends NormalizedOptions {
|
|||||||
tagDelimiterRight: string;
|
tagDelimiterRight: string;
|
||||||
outputDelimiterLeft: string;
|
outputDelimiterLeft: string;
|
||||||
outputDelimiterRight: string;
|
outputDelimiterRight: string;
|
||||||
|
preserveTimezones: boolean;
|
||||||
greedy: boolean;
|
greedy: boolean;
|
||||||
globals: object;
|
globals: object;
|
||||||
keepOutputType: boolean;
|
keepOutputType: boolean;
|
||||||
@@ -89,6 +92,7 @@ export const defaultOptions: NormalizedFullOptions = {
|
|||||||
tagDelimiterRight: '%}',
|
tagDelimiterRight: '%}',
|
||||||
outputDelimiterLeft: '{{',
|
outputDelimiterLeft: '{{',
|
||||||
outputDelimiterRight: '}}',
|
outputDelimiterRight: '}}',
|
||||||
|
preserveTimezones: false,
|
||||||
strictFilters: false,
|
strictFilters: false,
|
||||||
strictVariables: false,
|
strictVariables: false,
|
||||||
lenientIf: false,
|
lenientIf: false,
|
||||||
|
|||||||
@@ -1,3 +1,4 @@
|
|||||||
|
import { LiquidOptions } from '../../../../src/liquid-options'
|
||||||
import { test } from '../../../stub/render'
|
import { test } from '../../../stub/render'
|
||||||
|
|
||||||
describe('filters/date', function () {
|
describe('filters/date', function () {
|
||||||
@@ -14,17 +15,21 @@ describe('filters/date', function () {
|
|||||||
it('should parse as Date when given a timezoneless string', function () {
|
it('should parse as Date when given a timezoneless string', function () {
|
||||||
return test('{{ "1991-02-22T00:00:00" | date: "%Y-%m-%dT%H:%M:%S"}}', '1991-02-22T00:00:00')
|
return test('{{ "1991-02-22T00:00:00" | date: "%Y-%m-%dT%H:%M:%S"}}', '1991-02-22T00:00:00')
|
||||||
})
|
})
|
||||||
it('should not change the timezone between input and output', function () {
|
describe('when preserveTimezones is enabled', function () {
|
||||||
return test('{{ "1990-12-31T23:00:00Z" | date: "%Y-%m-%dT%H:%M:%S"}}', '1990-12-31T23:00:00')
|
const opts: LiquidOptions = { preserveTimezones: true };
|
||||||
})
|
|
||||||
it('should apply numeric timezone offset (0)', function () {
|
it('should not change the timezone between input and output', function () {
|
||||||
return test('{{ "1990-12-31T23:00:00+00:00" | date: "%Y-%m-%dT%H:%M:%S"}}', '1990-12-31T23:00:00')
|
return test('{{ "1990-12-31T23:00:00Z" | date: "%Y-%m-%dT%H:%M:%S"}}', '1990-12-31T23:00:00', undefined, opts)
|
||||||
})
|
})
|
||||||
it('should apply numeric timezone offset (-1)', function () {
|
it('should apply numeric timezone offset (0)', function () {
|
||||||
return test('{{ "1990-12-31T23:00:00-01:00" | date: "%Y-%m-%dT%H:%M:%S"}}', '1990-12-31T23:00:00')
|
return test('{{ "1990-12-31T23:00:00+00:00" | date: "%Y-%m-%dT%H:%M:%S"}}', '1990-12-31T23:00:00', undefined, opts)
|
||||||
})
|
})
|
||||||
it('should apply numeric timezone offset (+2.30)', function () {
|
it('should apply numeric timezone offset (-1)', function () {
|
||||||
return test('{{ "1990-12-31T23:00:00+02:30" | date: "%Y-%m-%dT%H:%M:%S"}}', '1990-12-31T23:00:00')
|
return test('{{ "1990-12-31T23:00:00-01:00" | date: "%Y-%m-%dT%H:%M:%S"}}', '1990-12-31T23:00:00', undefined, opts)
|
||||||
|
})
|
||||||
|
it('should apply numeric timezone offset (+2.30)', function () {
|
||||||
|
return test('{{ "1990-12-31T23:00:00+02:30" | date: "%Y-%m-%dT%H:%M:%S"}}', '1990-12-31T23:00:00', undefined, opts)
|
||||||
|
})
|
||||||
})
|
})
|
||||||
it('should render string as string if not valid', function () {
|
it('should render string as string if not valid', function () {
|
||||||
return test('{{ "foo" | date: "%Y"}}', 'foo')
|
return test('{{ "foo" | date: "%Y"}}', 'foo')
|
||||||
|
|||||||
+5
-4
@@ -1,16 +1,17 @@
|
|||||||
import { Liquid } from '../../src/liquid'
|
import { Liquid } from '../../src/liquid'
|
||||||
|
import { LiquidOptions } from '../../src/liquid-options'
|
||||||
import { expect } from 'chai'
|
import { expect } from 'chai'
|
||||||
|
|
||||||
export const liquid = new Liquid()
|
export const liquid = new Liquid()
|
||||||
|
|
||||||
export function render (src: string, ctx?: object) {
|
export function render (src: string, ctx?: object, opts?: LiquidOptions) {
|
||||||
return liquid.parseAndRender(src, ctx)
|
return liquid.parseAndRender(src, ctx, opts)
|
||||||
}
|
}
|
||||||
|
|
||||||
export async function test (src: string, ctx: object | string, dst?: string) {
|
export async function test (src: string, ctx: object | string, dst?: string, opts?: LiquidOptions) {
|
||||||
if (dst === undefined) {
|
if (dst === undefined) {
|
||||||
dst = ctx as string
|
dst = ctx as string
|
||||||
ctx = {}
|
ctx = {}
|
||||||
}
|
}
|
||||||
return expect(await render(src, ctx as object)).to.equal(dst)
|
return expect(await render(src, ctx as object, opts)).to.equal(dst)
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user