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:
harttle
2019-12-15 02:28:33 +00:00
parent c69ad538d4
commit ba5ff3f41c
7 changed files with 368 additions and 253 deletions
+1 -1
View File
@@ -12,7 +12,7 @@ describe('LiquidOptions#fs', function () {
engine = new Liquid({
root: '/root/',
fs
})
} as any)
})
it('should be used to read templates', function () {
return engine.renderFile('files/foo')
+2 -2
View File
@@ -18,8 +18,8 @@ describe('Liquid', function () {
})
it('should call plugin with Liquid', async function () {
const engine = new Liquid()
engine.plugin(function (Liquid) {
this.registerFilter('t', x => isFalsy(x))
engine.plugin(function () {
this.registerFilter('t', isFalsy)
})
const html = await engine.parseAndRender('{{false|t}}')
expect(html).to.equal('true')
+195 -96
View File
@@ -7,114 +7,213 @@ describe('util/strftime', function () {
const now = new Date('2016-01-04 13:15:23')
const then = new Date('2016-03-06 03:05:03')
it('should format detailed datetime', function () {
expect(t(now, '%Y-%m-%d %H:%M:%S')).to.equal('2016-01-04 13:15:23')
})
it('should format %A as Monday', function () {
expect(t(now, '%A')).to.equal('Monday')
})
it('should format %B as month name', function () {
expect(t(now, '%B')).to.equal('January')
})
it('should format %C as century', function () {
expect(t(now, '%C')).to.equal('20')
})
it('should format %c as local string', function () {
expect(t(now, '%c')).to.equal(now.toLocaleString())
})
it('should format %e as space padded date', function () {
expect(t(now, '%e')).to.equal(' 4')
})
it('should format %I as 0 padded hour12', function () {
expect(t(now, '%I')).to.equal('01')
})
it('should format %I as 12 for 00:00', function () {
const date = new Date('2016-01-01 00:00:00')
expect(t(date, '%I')).to.equal('12')
})
describe('%j', function () {
it('should format %j as day of year', function () {
expect(t(then, '%j')).to.equal('066')
describe('Date (Year, Month, Day)', () => {
it('should format %C as century', function () {
expect(t(now, '%C')).to.equal('20')
})
it('should take count of leap years', function () {
const date = new Date('2001-03-01')
expect(t(date, '%j')).to.equal('060')
it('should format %B as month name', function () {
expect(t(now, '%B')).to.equal('January')
})
it('should take count of leap years', function () {
const date = new Date('2000-03-01')
expect(t(date, '%j')).to.equal('061')
it('should format %e as space padded date', function () {
expect(t(now, '%e')).to.equal(' 4')
})
it('should format %y as 2-digit year', function () {
expect(t(now, '%y')).to.equal('16')
})
describe('%j', function () {
it('should format %j as day of year', function () {
expect(t(then, '%j')).to.equal('066')
})
it('should take count of leap years', function () {
const date = new Date('2001-03-01')
expect(t(date, '%j')).to.equal('060')
})
it('should take count of leap years', function () {
const date = new Date('2000-03-01')
expect(t(date, '%j')).to.equal('061')
})
})
it('should format %q as date suffix', function () {
const st = new Date('2016-03-01T03:05:03.000Z')
const nd = new Date('2016-03-02T03:05:03.000Z')
const rd = new Date('2016-03-03T03:05:03.000Z')
expect(t(st, '%q')).to.equal('st')
expect(t(nd, '%q')).to.equal('nd')
expect(t(rd, '%q')).to.equal('rd')
expect(t(now, '%q')).to.equal('th')
})
})
it('should format %k as space padded hour', function () {
expect(t(then, '%k')).to.equal(' 3')
describe('Time (Hour, Minute, Second, Subsecond)', function () {
it('should format %I as 0 padded hour12', function () {
expect(t(now, '%I')).to.equal('01')
})
it('should format %I as 12 for 00:00', function () {
const date = new Date('2016-01-01 00:00:00')
expect(t(date, '%I')).to.equal('12')
})
it('should format %k as space padded hour', function () {
expect(t(then, '%k')).to.equal(' 3')
})
it('should format %l as space padded hour12', function () {
expect(t(now, '%l')).to.equal(' 1')
})
it('should format %l as 12 for 00:00', function () {
const date = new Date('2016-01-01 00:00:00')
expect(t(date, '%l')).to.equal('12')
})
it('should format %L as 0 padded millisecond', function () {
expect(t(then, '%L')).to.equal('000')
})
it('should format %N as fractional seconds digits', function () {
const time = new Date('2019-12-15 01:21:00.129')
expect(t(time, '%N')).to.equal('129000000')
expect(t(time, '%2N')).to.equal('12')
expect(t(time, '%10N')).to.equal('1290000000')
expect(t(time, '%0N')).to.equal('129000000')
})
it('should format %p as upper cased am/pm', function () {
expect(t(now, '%p')).to.equal('PM')
expect(t(then, '%p')).to.equal('AM')
})
it('should format %P as lower cased am/pm', function () {
expect(t(now, '%P')).to.equal('pm')
expect(t(now, '%^8P')).to.equal(' PM')
expect(t(then, '%P')).to.equal('am')
})
})
it('should format %l as space padded hour12', function () {
expect(t(now, '%l')).to.equal(' 1')
describe('Weekday', function () {
it('should format %A as Monday', function () {
expect(t(now, '%A')).to.equal('Monday')
expect(t(now, '%^A')).to.equal('MONDAY')
expect(t(now, '%#A')).to.equal('MONDAY')
})
it('should format %a as Mon', function () {
expect(t(now, '%a')).to.equal('Mon')
expect(t(now, '%^a')).to.equal('MON')
})
it('should format %u as day of week(1-7)', function () {
expect(t(now, '%u')).to.be.equal('1')
expect(t(then, '%u')).to.be.equal('7')
})
it('should format %w as day of week(0-7)', function () {
expect(t(now, '%w')).to.be.equal('1')
expect(t(then, '%w')).to.be.equal('0')
})
})
it('should format %l as 12 for 00:00', function () {
const date = new Date('2016-01-01 00:00:00')
expect(t(date, '%l')).to.equal('12')
describe('Seconds since the Unix Epoch', () => {
it('should format %s as UNIX seconds', function () {
expect(t(now, '%s')).to.be.match(/\d+/)
})
})
it('should format %L as 0 padded millisecond', function () {
expect(t(then, '%L')).to.equal('000')
describe('Week number', () => {
it('should format %U as week of year, starts with 0', function () {
expect(t(now, '%U')).to.equal('01')
})
it('should format %W as week of year, starts with 1', function () {
expect(t(now, '%W')).to.be.equal('01')
})
})
it('should format %p as upper cased am/pm', function () {
expect(t(now, '%p')).to.equal('PM')
expect(t(then, '%p')).to.equal('AM')
describe('Time zone', () => {
it('should format %z as time zone', function () {
const now = new Date('2016-01-04 13:15:23')
now.getTimezoneOffset = () => -480 // suppose we're in +8:00
expect(t(now, '%z')).to.equal('+0800')
})
it('should format %z as negative time zone', function () {
const date = new Date('2016-01-04T13:15:23.000Z')
date.getTimezoneOffset = () => 480 // suppose we're in -8:00
expect(t(date, '%z')).to.equal('-0800')
})
})
it('should format %P as lower cased am/pm', function () {
expect(t(now, '%P')).to.equal('pm')
expect(t(then, '%P')).to.equal('am')
describe('combination', () => {
it('should format %x as local date string', function () {
expect(t(now, '%x')).to.equal(now.toLocaleDateString())
})
it('should format %X as local time string', function () {
expect(t(now, '%X')).to.equal(now.toLocaleTimeString())
})
it('should format detailed datetime', function () {
expect(t(now, '%Y-%m-%d %H:%M:%S')).to.equal('2016-01-04 13:15:23')
})
it('should format %c as local string', function () {
expect(t(now, '%c')).to.equal(now.toLocaleString())
})
})
it('should format %q as date suffix', function () {
const st = new Date('2016-03-01T03:05:03.000Z')
const nd = new Date('2016-03-02T03:05:03.000Z')
const rd = new Date('2016-03-03T03:05:03.000Z')
expect(t(st, '%q')).to.equal('st')
expect(t(nd, '%q')).to.equal('nd')
expect(t(rd, '%q')).to.equal('rd')
expect(t(now, '%q')).to.equal('th')
describe('literal strings', () => {
it('should escape %% as %', function () {
expect(t(now, '%%')).to.equal('%')
})
it('should escape %n as \\n', function () {
expect(t(now, '%n')).to.equal('\n')
})
it('should escape %t as \\t', function () {
expect(t(now, '%t')).to.equal('\t')
})
it('should retain un-recognized formaters', function () {
expect(t(now, '%o')).to.equal('%o')
})
})
it('should format %s as UNIX seconds', function () {
expect(t(now, '%s')).to.be.match(/\d+/)
describe('width field', () => {
it('should support width field', () => {
expect(t(now, '%8Y')).to.equal('00002016')
})
it('should ignore invalid width', () => {
expect(t(then, '%1Y')).to.equal('2016')
expect(t(then, '%1H')).to.equal('3')
})
it('should have higher priority than H', () => {
expect(t(then, '%0H')).to.equal('03')
})
})
it('should format %u as day of week(1-7)', function () {
expect(t(now, '%u')).to.be.equal('1')
expect(t(then, '%u')).to.be.equal('7')
describe('modifier field', () => {
it('should ignore E modifier', () => {
expect(t(now, '%EY')).to.equal('2016')
})
it('should ignore O modifier', () => {
expect(t(now, '%OY')).to.equal('2016')
})
it('should support modifier with width field', () => {
expect(t(now, '%8EY')).to.equal('00002016')
})
})
it('should format %U as week of year, starts with 0', function () {
expect(t(now, '%U')).to.equal('01')
})
it('should format %w as day of month(0-7)', function () {
expect(t(now, '%w')).to.be.equal('1')
expect(t(then, '%w')).to.be.equal('0')
})
it('should format %W as week of year, starts with 1', function () {
expect(t(now, '%W')).to.be.equal('01')
})
it('should format %x as local date string', function () {
expect(t(now, '%x')).to.equal(now.toLocaleDateString())
})
it('should format %X as local time string', function () {
expect(t(now, '%X')).to.equal(now.toLocaleTimeString())
})
it('should format %y as 2-digit year', function () {
expect(t(now, '%y')).to.equal('16')
})
it('should format %z as time zone', function () {
const now = new Date('2016-01-04 13:15:23')
now.getTimezoneOffset = () => -480 // suppose we're in +8:00
expect(t(now, '%z')).to.equal('+0800')
})
it('should format %z as negative time zone', function () {
const date = new Date('2016-01-04T13:15:23.000Z')
date.getTimezoneOffset = () => 480 // suppose we're in -8:00
expect(t(date, '%z')).to.equal('-0800')
})
it('should escape %% as %', function () {
expect(t(now, '%%')).to.equal('%')
})
it('should retain un-recognized formaters', function () {
expect(t(now, '%o')).to.equal('%o')
describe('flags field', () => {
it('should support - flag', () => {
expect(t(now, '%-m')).to.equal('1')
})
it('should support _ flag', () => {
expect(t(now, '%_m')).to.equal(' 1')
})
it('should support 0 flag', () => {
expect(t(now, '%0m')).to.equal('01')
})
it('should support ^ flag', () => {
expect(t(now, '%^B')).to.equal('JANUARY')
})
it('should respect to specific conversion', () => {
expect(t(now, '%^P')).to.equal('PM')
expect(t(now, '%P')).to.equal('pm')
})
it('should support # flag', () => {
expect(t(now, '%#B')).to.equal('JANUARY')
expect(t(now, '%#P')).to.equal('PM')
})
it('should support : flag', () => {
const date = new Date('2016-01-04T13:15:23.000Z')
date.getTimezoneOffset = () => -480 // suppose we're in +8:00
expect(t(date, '%:z')).to.equal('+08:00')
expect(t(date, '%z')).to.equal('+0800')
})
it('should support multiple flags', () => {
expect(t(now, '%^08P')).to.equal('000000PM')
})
})
})
+13
View File
@@ -95,4 +95,17 @@ describe('util/underscore', function () {
expect(_.isObject(2)).to.be.false
})
})
describe('.padEnd()', function () {
it('should default ch to " "', () => {
expect(_.padEnd('foo', 5)).to.equal('foo ')
})
})
describe('.changeCase()', function () {
it('should to upper case if there is one lowercase', () => {
expect(_.changeCase('fooA')).to.equal('FOOA')
})
it('should to lower case if all upper case', () => {
expect(_.changeCase('FOOA')).to.equal('fooa')
})
})
})