Skip to content

Commit

Permalink
fix: date from integer, #125
Browse files Browse the repository at this point in the history
  • Loading branch information
harttle committed May 12, 2019
1 parent bef2909 commit fdf0043
Show file tree
Hide file tree
Showing 6 changed files with 42 additions and 4 deletions.
6 changes: 4 additions & 2 deletions src/builtin/filters/date.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
import strftime from '../../util/strftime'
import { isString } from '../../util/underscore'
import { isString, isNumber } from '../../util/underscore'

export default {
'date': (v: string | Date, arg: string) => {
let date = v
if (v === 'now') {
date = new Date()
} else if (isNumber(v)) {
date = new Date(v * 1000)
} 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
}
Expand Down
3 changes: 2 additions & 1 deletion src/builtin/tags/decrement.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { identifier } from '../../parser/lexical'
import TagToken from '../../parser/tag-token'
import Context from '../../context/context'
import ITagImplOptions from '../../template/tag/itag-impl-options'
import { isNumber } from '../../util/underscore'

export default {
parse: function (token: TagToken) {
Expand All @@ -12,7 +13,7 @@ export default {
},
render: function (context: Context) {
const scope = context.environments
if (typeof scope[this.variable] !== 'number') {
if (!isNumber(scope[this.variable])) {
scope[this.variable] = 0
}
return --scope[this.variable]
Expand Down
3 changes: 2 additions & 1 deletion src/builtin/tags/increment.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import assert from '../../util/assert'
import { identifier } from '../../parser/lexical'
import { isNumber } from '../../util/underscore'
import ITagImplOptions from '../../template/tag/itag-impl-options'

export default {
Expand All @@ -10,7 +11,7 @@ export default {
},
render: function (context) {
const scope = context.environments
if (typeof scope[this.variable] !== 'number') {
if (!isNumber(scope[this.variable])) {
scope[this.variable] = 0
}
const val = scope[this.variable]
Expand Down
4 changes: 4 additions & 0 deletions src/util/underscore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,10 @@ export function toValue (value: any): any {
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 {
if (isFunction(value.toLiquid)) return toLiquid(value.toLiquid())
return value
Expand Down
22 changes: 22 additions & 0 deletions test/integration/builtin/filters/date.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
import { test, ctx } from '../../../stub/render'
import { expect } from 'chai'
import Liquid from '../../../../src/liquid'

describe('filters/date', function () {
let liquid: Liquid
beforeEach(function () {
liquid = new Liquid()
})
it('should support date: %a %b %d %Y', function () {
const str = ctx.date.toDateString()
return test('{{ date | date:"%a %b %d %Y"}}', str)
Expand All @@ -17,4 +23,20 @@ describe('filters/date', function () {
it('should render object as string if not valid', function () {
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)
})
})
8 changes: 8 additions & 0 deletions test/unit/util/underscore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,14 @@ describe('util/underscore', function () {
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 () {
it('should respect to toLiquid() method', function () {
expect(_.stringify({ toLiquid: () => 'foo' })).to.equal('foo')
Expand Down

0 comments on commit fdf0043

Please sign in to comment.