1
1
import { toValue , stringify , isString , isNumber , TimezoneDate , LiquidDate , strftime , isNil } from '../util'
2
2
import { FilterImpl } from '../template'
3
+ import { LiquidOptions } from '../liquid-options'
3
4
4
5
export function date ( this : FilterImpl , v : string | Date , format ?: string , timezoneOffset ?: number | string ) {
5
- const opts = this . context . opts
6
+ const date = parseDate ( v , this . context . opts , timezoneOffset )
7
+ if ( ! date ) return v
8
+ format = toValue ( format )
9
+ format = isNil ( format ) ? this . context . opts . dateFormat : stringify ( format )
10
+ return strftime ( date , format )
11
+ }
12
+
13
+ export function date_to_xmlschema ( this : FilterImpl , v : string | Date ) {
14
+ return date . call ( this , v , '%Y-%m-%dT%H:%M:%S%:z' )
15
+ }
16
+
17
+ export function date_to_rfc822 ( this : FilterImpl , v : string | Date ) {
18
+ return date . call ( this , v , '%a, %d %b %Y %H:%M:%S %z' )
19
+ }
20
+
21
+ export function date_to_string ( this : FilterImpl , v : string | Date , type ?: string , style ?: string ) {
22
+ return stringify_date . call ( this , v , '%b' , type , style )
23
+ }
24
+
25
+ export function date_to_long_string ( this : FilterImpl , v : string | Date , type ?: string , style ?: string ) {
26
+ return stringify_date . call ( this , v , '%B' , type , style )
27
+ }
28
+
29
+ function stringify_date ( this : FilterImpl , v : string | Date , month_type : string , type ?: string , style ?: string ) {
30
+ const date = parseDate ( v , this . context . opts )
31
+ if ( ! date ) return v
32
+ if ( type === 'ordinal' ) {
33
+ const d = date . getDate ( )
34
+ return style === 'US'
35
+ ? strftime ( date , `${ month_type } ${ d } %q, %Y` )
36
+ : strftime ( date , `${ d } %q ${ month_type } %Y` )
37
+ }
38
+ return strftime ( date , `%d ${ month_type } %Y` )
39
+ }
40
+
41
+ function parseDate ( v : string | Date , opts : LiquidOptions , timezoneOffset ?: number | string ) : LiquidDate | undefined {
6
42
let date : LiquidDate
7
43
v = toValue ( v )
8
- format = toValue ( format )
9
- if ( isNil ( format ) ) format = opts . dateFormat
10
- else format = stringify ( format )
11
44
if ( v === 'now' || v === 'today' ) {
12
45
date = new Date ( )
13
46
} else if ( isNumber ( v ) ) {
@@ -23,13 +56,13 @@ export function date (this: FilterImpl, v: string | Date, format?: string, timez
23
56
} else {
24
57
date = v
25
58
}
26
- if ( ! isValidDate ( date ) ) return v
59
+ if ( ! isValidDate ( date ) ) return
27
60
if ( timezoneOffset !== undefined ) {
28
61
date = new TimezoneDate ( date , timezoneOffset )
29
62
} else if ( ! ( date instanceof TimezoneDate ) && opts . timezoneOffset !== undefined ) {
30
63
date = new TimezoneDate ( date , opts . timezoneOffset )
31
64
}
32
- return strftime ( date , format )
65
+ return date
33
66
}
34
67
35
68
function isValidDate ( date : any ) : date is Date {
0 commit comments