feature: variable lookup respects to to_liquid, #81

This commit is contained in:
harttle
2018-08-14 00:11:50 +08:00
committed by Jun Yang
parent ea38a9e1f5
commit 5b43520d78
4 changed files with 35 additions and 22 deletions
+9 -4
View File
@@ -58,12 +58,17 @@ var Scope = {
},
readProperty: function (obj, key) {
let val
if (key === 'size' && (_.isArray(obj) || _.isString(obj))) {
val = obj.length
} else if (_.isNil(obj)) {
if (_.isNil(obj)) {
val = undefined
} else {
val = obj[key]
if (typeof obj.to_liquid === 'function') {
obj = obj.to_liquid()
}
if (key === 'size' && (_.isArray(obj) || _.isString(obj))) {
val = obj.length
} else {
val = obj[key]
}
}
if (_.isNil(val) && this.opts.strict_variables) {
throw new TypeError(`undefined variable: ${key}`)
+4 -3
View File
@@ -1,3 +1,4 @@
'use strict'
const toStr = Object.prototype.toString
/*
@@ -10,12 +11,12 @@ function isString (value) {
}
function stringify (value) {
if (value && typeof value.to_liquid === 'function') {
return stringify(value.to_liquid())
}
if (isString(value)) {
return value
}
if (value && typeof value.to_liquid === 'function') {
return value.to_liquid()
}
let cache = []
return JSON.stringify(value, (key, value) => {