mirror of
https://github.com/harttle/liquidjs.git
synced 2026-09-15 04:10:40 -07:00
fix: date from integer, #125
This commit is contained in:
@@ -1,13 +1,15 @@
|
|||||||
import strftime from '../../util/strftime'
|
import strftime from '../../util/strftime'
|
||||||
import { isString } from '../../util/underscore'
|
import { isString, isNumber } from '../../util/underscore'
|
||||||
|
|
||||||
export default {
|
export default {
|
||||||
'date': (v: string | Date, arg: string) => {
|
'date': (v: string | Date, arg: string) => {
|
||||||
let date = v
|
let date = v
|
||||||
if (v === 'now') {
|
if (v === 'now') {
|
||||||
date = new Date()
|
date = new Date()
|
||||||
|
} else if (isNumber(v)) {
|
||||||
|
date = new Date(v * 1000)
|
||||||
} else if (isString(v)) {
|
} else if (isString(v)) {
|
||||||
date = new Date(v)
|
date = /^\d+$/.test(v) ? new Date(+v * 1000) : new Date(v)
|
||||||
}
|
}
|
||||||
return isValidDate(date) ? strftime(date, arg) : v
|
return isValidDate(date) ? strftime(date, arg) : v
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -3,6 +3,7 @@ import { identifier } from '../../parser/lexical'
|
|||||||
import TagToken from '../../parser/tag-token'
|
import TagToken from '../../parser/tag-token'
|
||||||
import Context from '../../context/context'
|
import Context from '../../context/context'
|
||||||
import ITagImplOptions from '../../template/tag/itag-impl-options'
|
import ITagImplOptions from '../../template/tag/itag-impl-options'
|
||||||
|
import { isNumber } from '../../util/underscore'
|
||||||
|
|
||||||
export default {
|
export default {
|
||||||
parse: function (token: TagToken) {
|
parse: function (token: TagToken) {
|
||||||
@@ -12,7 +13,7 @@ export default {
|
|||||||
},
|
},
|
||||||
render: function (context: Context) {
|
render: function (context: Context) {
|
||||||
const scope = context.environments
|
const scope = context.environments
|
||||||
if (typeof scope[this.variable] !== 'number') {
|
if (!isNumber(scope[this.variable])) {
|
||||||
scope[this.variable] = 0
|
scope[this.variable] = 0
|
||||||
}
|
}
|
||||||
return --scope[this.variable]
|
return --scope[this.variable]
|
||||||
|
|||||||
@@ -1,5 +1,6 @@
|
|||||||
import assert from '../../util/assert'
|
import assert from '../../util/assert'
|
||||||
import { identifier } from '../../parser/lexical'
|
import { identifier } from '../../parser/lexical'
|
||||||
|
import { isNumber } from '../../util/underscore'
|
||||||
import ITagImplOptions from '../../template/tag/itag-impl-options'
|
import ITagImplOptions from '../../template/tag/itag-impl-options'
|
||||||
|
|
||||||
export default {
|
export default {
|
||||||
@@ -10,7 +11,7 @@ export default {
|
|||||||
},
|
},
|
||||||
render: function (context) {
|
render: function (context) {
|
||||||
const scope = context.environments
|
const scope = context.environments
|
||||||
if (typeof scope[this.variable] !== 'number') {
|
if (!isNumber(scope[this.variable])) {
|
||||||
scope[this.variable] = 0
|
scope[this.variable] = 0
|
||||||
}
|
}
|
||||||
const val = scope[this.variable]
|
const val = scope[this.variable]
|
||||||
|
|||||||
@@ -37,6 +37,10 @@ export function toValue (value: any): any {
|
|||||||
return value instanceof Drop ? value.valueOf() : value
|
return value instanceof Drop ? value.valueOf() : value
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export function isNumber (value: any): value is number {
|
||||||
|
return typeof value === 'number'
|
||||||
|
}
|
||||||
|
|
||||||
export function toLiquid (value: any): any {
|
export function toLiquid (value: any): any {
|
||||||
if (isFunction(value.toLiquid)) return toLiquid(value.toLiquid())
|
if (isFunction(value.toLiquid)) return toLiquid(value.toLiquid())
|
||||||
return value
|
return value
|
||||||
|
|||||||
@@ -1,6 +1,12 @@
|
|||||||
import { test, ctx } from '../../../stub/render'
|
import { test, ctx } from '../../../stub/render'
|
||||||
|
import { expect } from 'chai'
|
||||||
|
import Liquid from '../../../../src/liquid'
|
||||||
|
|
||||||
describe('filters/date', function () {
|
describe('filters/date', function () {
|
||||||
|
let liquid: Liquid
|
||||||
|
beforeEach(function () {
|
||||||
|
liquid = new Liquid()
|
||||||
|
})
|
||||||
it('should support date: %a %b %d %Y', function () {
|
it('should support date: %a %b %d %Y', function () {
|
||||||
const str = ctx.date.toDateString()
|
const str = ctx.date.toDateString()
|
||||||
return test('{{ date | date:"%a %b %d %Y"}}', str)
|
return test('{{ date | date:"%a %b %d %Y"}}', str)
|
||||||
@@ -17,4 +23,20 @@ describe('filters/date', function () {
|
|||||||
it('should render object as string if not valid', function () {
|
it('should render object as string if not valid', function () {
|
||||||
return test('{{ obj | date: "%Y"}}', '[object Object]')
|
return test('{{ obj | date: "%Y"}}', '[object Object]')
|
||||||
})
|
})
|
||||||
|
it('should create from number-like string', async function () {
|
||||||
|
const src = '{{ "1488859200" | date: "%Y-%m-%dT%H:%M:%S" }}'
|
||||||
|
const dst = '2017-03-07T12:00:00'
|
||||||
|
expect(await liquid.parseAndRender(src)).to.equal(dst)
|
||||||
|
})
|
||||||
|
it('should create from number', async function () {
|
||||||
|
const src = '{{ 1488859200 | date: "%Y-%m-%dT%H:%M:%S" }}'
|
||||||
|
const dst = '2017-03-07T12:00:00'
|
||||||
|
expect(await liquid.parseAndRender(src)).to.equal(dst)
|
||||||
|
})
|
||||||
|
it('should support manipulation', async function () {
|
||||||
|
const src = '{{ date | date: "%s" | minus : 604800 | date: "%Y-%m-%dT%H:%M:%S"}}'
|
||||||
|
const ctx = { date: new Date('2017-03-07 12:00:00.000+8:00') }
|
||||||
|
const dst = '2017-02-28T12:00:00'
|
||||||
|
expect(await liquid.parseAndRender(src, ctx)).to.equal(dst)
|
||||||
|
})
|
||||||
})
|
})
|
||||||
|
|||||||
@@ -18,6 +18,14 @@ describe('util/underscore', function () {
|
|||||||
expect(_.isString(123)).to.be.false
|
expect(_.isString(123)).to.be.false
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
describe('.isNumber()', function () {
|
||||||
|
it('should return false for "foo"', function () {
|
||||||
|
expect(_.isNumber('foo')).to.be.false
|
||||||
|
})
|
||||||
|
it('should return true for 0', function () {
|
||||||
|
expect(_.isNumber(0)).to.be.true
|
||||||
|
})
|
||||||
|
})
|
||||||
describe('.stringify()', function () {
|
describe('.stringify()', function () {
|
||||||
it('should respect to toLiquid() method', function () {
|
it('should respect to toLiquid() method', function () {
|
||||||
expect(_.stringify({ toLiquid: () => 'foo' })).to.equal('foo')
|
expect(_.stringify({ toLiquid: () => 'foo' })).to.equal('foo')
|
||||||
|
|||||||
Reference in New Issue
Block a user