From 2a16800305a075ff76ea3a6c9b9ce6d4c5fdcd48 Mon Sep 17 00:00:00 2001 From: aleclarson Date: Wed, 12 Jul 2017 15:28:59 -0400 Subject: [PATCH] Iterate keys of object when used with `for...in` loop --- tags/for.js | 21 +++++++++++++++++++-- 1 file changed, 19 insertions(+), 2 deletions(-) diff --git a/tags/for.js b/tags/for.js index de94a7207..a16350718 100644 --- a/tags/for.js +++ b/tags/for.js @@ -38,8 +38,10 @@ module.exports = function (liquid) { render: function (scope, hash) { var collection = Liquid.evalExp(this.collection, scope) - if (!Array.isArray(collection) || - (Array.isArray(collection) && collection.length === 0)) { + if (isObject(collection)) { + collection = Object.keys(collection) + } + if (isEmpty(collection)) { 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 +}