mirror of
https://github.com/harttle/liquidjs.git
synced 2026-09-16 21:00:40 -07:00
feat: add preserveTimezones option
This commit is contained in:
@@ -1,14 +1,21 @@
|
||||
import strftime, { TimezoneDate } from '../../util/strftime'
|
||||
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
|
||||
if (v === 'now' || v === 'today') {
|
||||
date = new Date()
|
||||
} else if (isNumber(v)) {
|
||||
date = new Date(v * 1000)
|
||||
} 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
|
||||
}
|
||||
|
||||
@@ -37,6 +37,8 @@ export interface LiquidOptions {
|
||||
outputDelimiterLeft?: string;
|
||||
/** The right delimiter for liquid outputs. **/
|
||||
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`. */
|
||||
greedy?: boolean;
|
||||
/** `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;
|
||||
outputDelimiterLeft: string;
|
||||
outputDelimiterRight: string;
|
||||
preserveTimezones: boolean;
|
||||
greedy: boolean;
|
||||
globals: object;
|
||||
keepOutputType: boolean;
|
||||
@@ -89,6 +92,7 @@ export const defaultOptions: NormalizedFullOptions = {
|
||||
tagDelimiterRight: '%}',
|
||||
outputDelimiterLeft: '{{',
|
||||
outputDelimiterRight: '}}',
|
||||
preserveTimezones: false,
|
||||
strictFilters: false,
|
||||
strictVariables: false,
|
||||
lenientIf: false,
|
||||
|
||||
Reference in New Issue
Block a user