fix string type checking and circular reference, working on #81

This commit is contained in:
harttle
2018-08-14 00:11:50 +08:00
committed by Jun Yang
parent 96829b5293
commit fa5b731daf
3 changed files with 71 additions and 11 deletions
+8 -8
View File
@@ -1,6 +1,7 @@
const Syntax = require('./syntax.js')
const mapSeries = require('./util/promise.js').mapSeries
const RenderBreakError = require('./util/error.js').RenderBreakError
const _ = require('./util/underscore.js')
const RenderError = require('./util/error.js').RenderError
const assert = require('./util/assert.js')
@@ -27,9 +28,7 @@ var render = {
return this.renderTag(template, scope)
.then(partial => partial === undefined ? '' : partial)
} else if (template.type === 'value') {
return Promise.resolve()
.then(() => this.evalValue(template, scope))
.then(partial => partial === undefined ? '' : stringify(partial))
return this.renderValue(template, scope)
} else { // template.type === 'html'
return Promise.resolve(template.value)
}
@@ -46,6 +45,12 @@ var render = {
return template.render(scope)
},
renderValue: function (template, scope) {
return Promise.resolve()
.then(() => this.evalValue(template, scope))
.then(partial => partial === undefined ? '' : _.stringify(partial))
},
evalValue: function (template, scope) {
assert(scope, 'unable to evalValue: scope undefined')
return template.filters.reduce(
@@ -59,9 +64,4 @@ function factory () {
return instance
}
function stringify (val) {
if (typeof val === 'string') return val
return JSON.stringify(val)
}
module.exports = factory
+22 -1
View File
@@ -6,7 +6,27 @@ const toStr = Object.prototype.toString
* @return {Boolean} Returns true if value is a string, else false.
*/
function isString (value) {
return value instanceof String || typeof value === 'string'
return toStr.call(value) === '[object String]'
}
function stringify (value) {
if (isString(value)) {
return value
}
if (value && typeof value.to_liquid === 'function') {
return value.to_liquid()
}
let cache = []
return JSON.stringify(value, (key, value) => {
if (isObject(value)) {
if (cache.indexOf(value) !== -1) {
return
}
cache.push(value)
}
return value
})
}
function isNil (value) {
@@ -126,3 +146,4 @@ exports.last = last
exports.forOwn = forOwn
exports.assign = assign
exports.uniq = uniq
exports.stringify = stringify