Iterate keys of object when used with for...in loop

This commit is contained in:
aleclarson
2017-07-12 15:33:48 -04:00
parent ed20fe6072
commit 2a16800305
+19 -2
View File
@@ -38,8 +38,10 @@ module.exports = function (liquid) {
render: function (scope, hash) { render: function (scope, hash) {
var collection = Liquid.evalExp(this.collection, scope) var collection = Liquid.evalExp(this.collection, scope)
if (!Array.isArray(collection) || if (isObject(collection)) {
(Array.isArray(collection) && collection.length === 0)) { collection = Object.keys(collection)
}
if (isEmpty(collection)) {
return liquid.renderer.renderTemplates(this.elseTemplates, scope) return liquid.renderer.renderTemplates(this.elseTemplates, scope)
} }
@@ -90,3 +92,18 @@ module.exports = function (liquid) {
} }
}) })
} }
function isObject(collection) {
if (collection) {
const ctr = collection.constructor
return ctr === Object || ctr == null
}
return false
}
function isEmpty(collection) {
if (Array.isArray(collection)) {
return !collection.length
}
return true
}