fix: date from integer, #125

This commit is contained in:
harttle
2019-05-12 21:03:35 +08:00
parent bef290923c
commit fdf0043b9c
6 changed files with 42 additions and 4 deletions
+4 -2
View File
@@ -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
}
+2 -1
View File
@@ -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) {
@@ -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]
+2 -1
View File
@@ -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 {
@@ -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]
+4
View File
@@ -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