mirror of
https://github.com/harttle/liquidjs.git
synced 2026-09-16 12:50:38 -07:00
feat: date filters from Jekyll
This commit is contained in:
+39
-6
@@ -1,13 +1,46 @@
|
||||
import { toValue, stringify, isString, isNumber, TimezoneDate, LiquidDate, strftime, isNil } from '../util'
|
||||
import { FilterImpl } from '../template'
|
||||
import { LiquidOptions } from '../liquid-options'
|
||||
|
||||
export function date (this: FilterImpl, v: string | Date, format?: string, timezoneOffset?: number | string) {
|
||||
const opts = this.context.opts
|
||||
const date = parseDate(v, this.context.opts, timezoneOffset)
|
||||
if (!date) return v
|
||||
format = toValue(format)
|
||||
format = isNil(format) ? this.context.opts.dateFormat : stringify(format)
|
||||
return strftime(date, format)
|
||||
}
|
||||
|
||||
export function date_to_xmlschema (this: FilterImpl, v: string | Date) {
|
||||
return date.call(this, v, '%Y-%m-%dT%H:%M:%S%:z')
|
||||
}
|
||||
|
||||
export function date_to_rfc822 (this: FilterImpl, v: string | Date) {
|
||||
return date.call(this, v, '%a, %d %b %Y %H:%M:%S %z')
|
||||
}
|
||||
|
||||
export function date_to_string (this: FilterImpl, v: string | Date, type?: string, style?: string) {
|
||||
return stringify_date.call(this, v, '%b', type, style)
|
||||
}
|
||||
|
||||
export function date_to_long_string (this: FilterImpl, v: string | Date, type?: string, style?: string) {
|
||||
return stringify_date.call(this, v, '%B', type, style)
|
||||
}
|
||||
|
||||
function stringify_date (this: FilterImpl, v: string | Date, month_type: string, type?: string, style?: string) {
|
||||
const date = parseDate(v, this.context.opts)
|
||||
if (!date) return v
|
||||
if (type === 'ordinal') {
|
||||
const d = date.getDate()
|
||||
return style === 'US'
|
||||
? strftime(date, `${month_type} ${d}%q, %Y`)
|
||||
: strftime(date, `${d}%q ${month_type} %Y`)
|
||||
}
|
||||
return strftime(date, `%d ${month_type} %Y`)
|
||||
}
|
||||
|
||||
function parseDate (v: string | Date, opts: LiquidOptions, timezoneOffset?: number | string): LiquidDate | undefined {
|
||||
let date: LiquidDate
|
||||
v = toValue(v)
|
||||
format = toValue(format)
|
||||
if (isNil(format)) format = opts.dateFormat
|
||||
else format = stringify(format)
|
||||
if (v === 'now' || v === 'today') {
|
||||
date = new Date()
|
||||
} else if (isNumber(v)) {
|
||||
@@ -23,13 +56,13 @@ export function date (this: FilterImpl, v: string | Date, format?: string, timez
|
||||
} else {
|
||||
date = v
|
||||
}
|
||||
if (!isValidDate(date)) return v
|
||||
if (!isValidDate(date)) return
|
||||
if (timezoneOffset !== undefined) {
|
||||
date = new TimezoneDate(date, timezoneOffset)
|
||||
} else if (!(date instanceof TimezoneDate) && opts.timezoneOffset !== undefined) {
|
||||
date = new TimezoneDate(date, opts.timezoneOffset)
|
||||
}
|
||||
return strftime(date, format)
|
||||
return date
|
||||
}
|
||||
|
||||
function isValidDate (date: any): date is Date {
|
||||
|
||||
+8
-23
@@ -45,31 +45,16 @@ function isLeapYear (d: LiquidDate) {
|
||||
const year = d.getFullYear()
|
||||
return !!((year & 3) === 0 && (year % 100 || (year % 400 === 0 && year)))
|
||||
}
|
||||
function getSuffix (d: LiquidDate) {
|
||||
function ordinal (d: LiquidDate) {
|
||||
const date = d.getDate()
|
||||
if ([11, 12, 13].includes(date)) return 'th'
|
||||
|
||||
let suffix = 'th'
|
||||
|
||||
switch (date) {
|
||||
case 11:
|
||||
case 12:
|
||||
case 13:
|
||||
break
|
||||
default:
|
||||
switch (date % 10) {
|
||||
case 1:
|
||||
suffix = 'st'
|
||||
break
|
||||
case 2:
|
||||
suffix = 'nd'
|
||||
break
|
||||
case 3:
|
||||
suffix = 'rd'
|
||||
break
|
||||
}
|
||||
switch (date % 10) {
|
||||
case 1: return 'st'
|
||||
case 2: return 'nd'
|
||||
case 3: return 'rd'
|
||||
default: return 'th'
|
||||
}
|
||||
|
||||
return suffix
|
||||
}
|
||||
function century (d: LiquidDate) {
|
||||
return parseInt(d.getFullYear().toString().substring(0, 2), 10)
|
||||
@@ -138,7 +123,7 @@ const formatCodes = {
|
||||
},
|
||||
p: (d: LiquidDate) => (d.getHours() < 12 ? 'AM' : 'PM'),
|
||||
P: (d: LiquidDate) => (d.getHours() < 12 ? 'am' : 'pm'),
|
||||
q: (d: LiquidDate) => getSuffix(d),
|
||||
q: (d: LiquidDate) => ordinal(d),
|
||||
s: (d: LiquidDate) => Math.round(d.getTime() / 1000),
|
||||
S: (d: LiquidDate) => d.getSeconds(),
|
||||
u: (d: LiquidDate) => d.getDay() || 7,
|
||||
|
||||
Reference in New Issue
Block a user