mirror of
https://github.com/harttle/liquidjs.git
synced 2026-09-16 12:50:38 -07:00
feat: full syntax for strftime, close #177
- strftime syntax: <flags><width><modifier><conversion> - new conversions: %n, %t - fixed conversions: %N - flags: _ ^ - 0 # : - modifiers: E, O (ignored)
This commit is contained in:
+137
-151
@@ -1,5 +1,6 @@
|
||||
import { padStart } from './underscore'
|
||||
import { changeCase, padStart, padEnd } from './underscore'
|
||||
|
||||
const rFormat = /%([-_0^#:]+)?(\d+)?([EO])?(.)/
|
||||
const monthNames = [
|
||||
'January', 'February', 'March', 'April', 'May', 'June', 'July', 'August',
|
||||
'September', 'October', 'November', 'December'
|
||||
@@ -15,170 +16,155 @@ const suffixes = {
|
||||
3: 'rd',
|
||||
'default': 'th'
|
||||
}
|
||||
interface FormatOptions {
|
||||
flags: object;
|
||||
width?: string;
|
||||
modifier?: string;
|
||||
}
|
||||
|
||||
function abbr (str: string) {
|
||||
return str.slice(0, 3)
|
||||
}
|
||||
|
||||
// prototype extensions
|
||||
const _date = {
|
||||
daysInMonth: function (d: Date) {
|
||||
const feb = _date.isLeapYear(d) ? 29 : 28
|
||||
return [31, feb, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]
|
||||
},
|
||||
|
||||
getDayOfYear: function (d: Date) {
|
||||
let num = 0
|
||||
for (let i = 0; i < d.getMonth(); ++i) {
|
||||
num += _date.daysInMonth(d)[i]
|
||||
}
|
||||
return num + d.getDate()
|
||||
},
|
||||
|
||||
getWeekOfYear: function (d: Date, startDay: number) {
|
||||
// Skip to startDay of this week
|
||||
const now = this.getDayOfYear(d) + (startDay - d.getDay())
|
||||
// Find the first startDay of the year
|
||||
const jan1 = new Date(d.getFullYear(), 0, 1)
|
||||
const then = (7 - jan1.getDay() + startDay)
|
||||
return padStart(String(Math.floor((now - then) / 7) + 1), 2, '0')
|
||||
},
|
||||
|
||||
isLeapYear: function (d: Date) {
|
||||
const year = d.getFullYear()
|
||||
return !!((year & 3) === 0 && (year % 100 || (year % 400 === 0 && year)))
|
||||
},
|
||||
|
||||
getSuffix: function (d: Date) {
|
||||
const str = d.getDate().toString()
|
||||
const index = parseInt(str.slice(-1))
|
||||
return suffixes[index] || suffixes['default']
|
||||
},
|
||||
|
||||
century: function (d: Date) {
|
||||
return parseInt(d.getFullYear().toString().substring(0, 2), 10)
|
||||
function daysInMonth (d: Date) {
|
||||
const feb = isLeapYear(d) ? 29 : 28
|
||||
return [31, feb, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]
|
||||
}
|
||||
function getDayOfYear (d: Date) {
|
||||
let num = 0
|
||||
for (let i = 0; i < d.getMonth(); ++i) {
|
||||
num += daysInMonth(d)[i]
|
||||
}
|
||||
return num + d.getDate()
|
||||
}
|
||||
function getWeekOfYear (d: Date, startDay: number) {
|
||||
// Skip to startDay of this week
|
||||
const now = getDayOfYear(d) + (startDay - d.getDay())
|
||||
// Find the first startDay of the year
|
||||
const jan1 = new Date(d.getFullYear(), 0, 1)
|
||||
const then = (7 - jan1.getDay() + startDay)
|
||||
return String(Math.floor((now - then) / 7) + 1)
|
||||
}
|
||||
function isLeapYear (d: Date) {
|
||||
const year = d.getFullYear()
|
||||
return !!((year & 3) === 0 && (year % 100 || (year % 400 === 0 && year)))
|
||||
}
|
||||
function getSuffix (d: Date) {
|
||||
const str = d.getDate().toString()
|
||||
const index = parseInt(str.slice(-1))
|
||||
return suffixes[index] || suffixes['default']
|
||||
}
|
||||
function century (d: Date) {
|
||||
return parseInt(d.getFullYear().toString().substring(0, 2), 10)
|
||||
}
|
||||
|
||||
// default to 0
|
||||
const padWidths = {
|
||||
d: 2,
|
||||
e: 2,
|
||||
H: 2,
|
||||
I: 2,
|
||||
j: 3,
|
||||
k: 2,
|
||||
l: 2,
|
||||
L: 3,
|
||||
m: 2,
|
||||
M: 2,
|
||||
S: 2,
|
||||
U: 2,
|
||||
W: 2
|
||||
}
|
||||
|
||||
// default to '0'
|
||||
const padChars = {
|
||||
a: ' ',
|
||||
A: ' ',
|
||||
b: ' ',
|
||||
B: ' ',
|
||||
c: ' ',
|
||||
e: ' ',
|
||||
k: ' ',
|
||||
l: ' ',
|
||||
p: ' ',
|
||||
P: ' '
|
||||
}
|
||||
const formatCodes = {
|
||||
a: function (d: Date) {
|
||||
return dayNamesShort[d.getDay()]
|
||||
a: (d: Date) => dayNamesShort[d.getDay()],
|
||||
A: (d: Date) => dayNames[d.getDay()],
|
||||
b: (d: Date) => monthNamesShort[d.getMonth()],
|
||||
B: (d: Date) => monthNames[d.getMonth()],
|
||||
c: (d: Date) => d.toLocaleString(),
|
||||
C: (d: Date) => century(d),
|
||||
d: (d: Date) => d.getDate(),
|
||||
e: (d: Date) => d.getDate(),
|
||||
H: (d: Date) => d.getHours(),
|
||||
I: (d: Date) => String(d.getHours() % 12 || 12),
|
||||
j: (d: Date) => getDayOfYear(d),
|
||||
k: (d: Date) => d.getHours(),
|
||||
l: (d: Date) => String(d.getHours() % 12 || 12),
|
||||
L: (d: Date) => d.getMilliseconds(),
|
||||
m: (d: Date) => d.getMonth() + 1,
|
||||
M: (d: Date) => d.getMinutes(),
|
||||
N: (d: Date, opts: FormatOptions) => {
|
||||
const width = Number(opts.width) || 9
|
||||
const str = String(d.getMilliseconds()).substr(0, width)
|
||||
return padEnd(str, width, '0')
|
||||
},
|
||||
A: function (d: Date) {
|
||||
return dayNames[d.getDay()]
|
||||
p: (d: Date) => (d.getHours() < 12 ? 'AM' : 'PM'),
|
||||
P: (d: Date) => (d.getHours() < 12 ? 'am' : 'pm'),
|
||||
q: (d: Date) => getSuffix(d),
|
||||
s: (d: Date) => Math.round(d.valueOf() / 1000),
|
||||
S: (d: Date) => d.getSeconds(),
|
||||
u: (d: Date) => d.getDay() || 7,
|
||||
U: (d: Date) => getWeekOfYear(d, 0),
|
||||
w: (d: Date) => d.getDay(),
|
||||
W: (d: Date) => getWeekOfYear(d, 1),
|
||||
x: (d: Date) => d.toLocaleDateString(),
|
||||
X: (d: Date) => d.toLocaleTimeString(),
|
||||
y: (d: Date) => d.getFullYear().toString().substring(2, 4),
|
||||
Y: (d: Date) => d.getFullYear(),
|
||||
z: (d: Date, opts: FormatOptions) => {
|
||||
const offset = d.getTimezoneOffset()
|
||||
const nOffset = Math.abs(offset)
|
||||
const h = Math.floor(nOffset / 60)
|
||||
const m = nOffset % 60
|
||||
return (offset > 0 ? '-' : '+') +
|
||||
padStart(h, 2, '0') +
|
||||
(opts.flags[':'] ? ':' : '') +
|
||||
padStart(m, 2, '0')
|
||||
},
|
||||
b: function (d: Date) {
|
||||
return monthNamesShort[d.getMonth()]
|
||||
},
|
||||
B: function (d: Date) {
|
||||
return monthNames[d.getMonth()]
|
||||
},
|
||||
c: function (d: Date) {
|
||||
return d.toLocaleString()
|
||||
},
|
||||
C: function (d: Date) {
|
||||
return _date.century(d)
|
||||
},
|
||||
d: function (d: Date) {
|
||||
return padStart(d.getDate(), 2, '0')
|
||||
},
|
||||
e: function (d: Date) {
|
||||
return padStart(d.getDate(), 2)
|
||||
},
|
||||
H: function (d: Date) {
|
||||
return padStart(d.getHours(), 2, '0')
|
||||
},
|
||||
I: function (d: Date) {
|
||||
return padStart(String(d.getHours() % 12 || 12), 2, '0')
|
||||
},
|
||||
j: function (d: Date) {
|
||||
return padStart(_date.getDayOfYear(d), 3, '0')
|
||||
},
|
||||
k: function (d: Date) {
|
||||
return padStart(d.getHours(), 2)
|
||||
},
|
||||
l: function (d: Date) {
|
||||
return padStart(String(d.getHours() % 12 || 12), 2)
|
||||
},
|
||||
L: function (d: Date) {
|
||||
return padStart(d.getMilliseconds(), 3, '0')
|
||||
},
|
||||
m: function (d: Date) {
|
||||
return padStart(d.getMonth() + 1, 2, '0')
|
||||
},
|
||||
M: function (d: Date) {
|
||||
return padStart(d.getMinutes(), 2, '0')
|
||||
},
|
||||
p: function (d: Date) {
|
||||
return (d.getHours() < 12 ? 'AM' : 'PM')
|
||||
},
|
||||
P: function (d: Date) {
|
||||
return (d.getHours() < 12 ? 'am' : 'pm')
|
||||
},
|
||||
q: function (d: Date) {
|
||||
return _date.getSuffix(d)
|
||||
},
|
||||
s: function (d: Date) {
|
||||
return Math.round(d.valueOf() / 1000)
|
||||
},
|
||||
S: function (d: Date) {
|
||||
return padStart(d.getSeconds(), 2, '0')
|
||||
},
|
||||
u: function (d: Date) {
|
||||
return d.getDay() || 7
|
||||
},
|
||||
U: function (d: Date) {
|
||||
return _date.getWeekOfYear(d, 0)
|
||||
},
|
||||
w: function (d: Date) {
|
||||
return d.getDay()
|
||||
},
|
||||
W: function (d: Date) {
|
||||
return _date.getWeekOfYear(d, 1)
|
||||
},
|
||||
x: function (d: Date) {
|
||||
return d.toLocaleDateString()
|
||||
},
|
||||
X: function (d: Date) {
|
||||
return d.toLocaleTimeString()
|
||||
},
|
||||
y: function (d: Date) {
|
||||
return d.getFullYear().toString().substring(2, 4)
|
||||
},
|
||||
Y: function (d: Date) {
|
||||
return d.getFullYear()
|
||||
},
|
||||
z: function (d: Date) {
|
||||
const tz = d.getTimezoneOffset() / 60 * 100
|
||||
return (tz > 0 ? '-' : '+') + padStart(String(Math.abs(tz)), 4, '0')
|
||||
},
|
||||
'%': function () {
|
||||
return '%'
|
||||
}
|
||||
't': () => '\t',
|
||||
'n': () => '\n',
|
||||
'%': () => '%'
|
||||
};
|
||||
(formatCodes as any).h = formatCodes.b;
|
||||
(formatCodes as any).N = formatCodes.L
|
||||
(formatCodes as any).h = formatCodes.b
|
||||
|
||||
export default function (d: Date, format: string) {
|
||||
export default function (d: Date, formatStr: string) {
|
||||
let output = ''
|
||||
let remaining = format
|
||||
|
||||
while (true) {
|
||||
const r = /%./g
|
||||
const results = r.exec(remaining)
|
||||
|
||||
// No more format codes. Add the remaining text and return
|
||||
if (!results) {
|
||||
return output + remaining
|
||||
}
|
||||
|
||||
// Add the preceding text
|
||||
output += remaining.slice(0, r.lastIndex - 2)
|
||||
remaining = remaining.slice(r.lastIndex)
|
||||
|
||||
// Add the format code
|
||||
const ch = results[0].charAt(1)
|
||||
const func = formatCodes[ch]
|
||||
output += func ? func(d) : '%' + ch
|
||||
let remaining = formatStr
|
||||
let match
|
||||
while ((match = rFormat.exec(remaining))) {
|
||||
output += remaining.slice(0, match.index)
|
||||
remaining = remaining.slice(match.index + match[0].length)
|
||||
output += format(d, match)
|
||||
}
|
||||
return output + remaining
|
||||
}
|
||||
|
||||
function format (d: Date, match: RegExpExecArray) {
|
||||
const [input, flagStr = '', width, modifier, conversion] = match
|
||||
const convert = formatCodes[conversion]
|
||||
if (!convert) return input
|
||||
const flags = {}
|
||||
for (const flag of flagStr) flags[flag] = true
|
||||
let ret = String(convert(d, { flags, width, modifier }))
|
||||
let padChar = padChars[conversion] || '0'
|
||||
let padWidth = width || padWidths[conversion] || 0
|
||||
if (flags['^']) ret = ret.toUpperCase()
|
||||
else if (flags['#']) ret = changeCase(ret)
|
||||
if (flags['_']) padChar = ' '
|
||||
else if (flags['0']) padChar = '0'
|
||||
if (flags['-']) padWidth = 0
|
||||
return padStart(ret, padWidth, padChar)
|
||||
}
|
||||
|
||||
+18
-1
@@ -101,8 +101,25 @@ export function range (start: number, stop: number, step = 1) {
|
||||
}
|
||||
|
||||
export function padStart (str: any, length: number, ch = ' ') {
|
||||
return pad(str, length, ch, (str, ch) => ch + str)
|
||||
}
|
||||
|
||||
export function padEnd (str: any, length: number, ch = ' ') {
|
||||
return pad(str, length, ch, (str, ch) => str + ch)
|
||||
}
|
||||
|
||||
export function pad (str: any, length: number, ch: string, add: (str: string, ch: string) => string) {
|
||||
str = String(str)
|
||||
let n = length - str.length
|
||||
while (n-- > 0) str = ch + str
|
||||
while (n-- > 0) str = add(str, ch)
|
||||
return str
|
||||
}
|
||||
|
||||
export function identify<T> (val: T): T {
|
||||
return val
|
||||
}
|
||||
|
||||
export function changeCase (str: string): string {
|
||||
const hasLowerCase = [...str].some(ch => ch >= 'a' && ch <= 'z')
|
||||
return hasLowerCase ? str.toUpperCase() : str.toLowerCase()
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user