From bea5554bf0a0cccbc206ed517e853764c00a05d2 Mon Sep 17 00:00:00 2001 From: Travis Nelson Date: Mon, 16 Jan 2017 02:38:42 -0700 Subject: [PATCH] Handle undefined arrays better https://github.com/wycats/handlebars.js/issues/1227 [undefined, undefined] should have the same behavior as new Array(2), per https://github.com/wycats/handlebars.js/issues/1065 --- lib/handlebars/helpers/each.js | 2 +- spec/regressions.js | 7 +++++++ 2 files changed, 8 insertions(+), 1 deletion(-) diff --git a/lib/handlebars/helpers/each.js b/lib/handlebars/helpers/each.js index 914928d6d..3f8696ccb 100644 --- a/lib/handlebars/helpers/each.js +++ b/lib/handlebars/helpers/each.js @@ -36,7 +36,7 @@ export default function(instance) { if (context && typeof context === 'object') { if (isArray(context)) { for (let j = context.length; i < j; i++) { - if (i in context) { + if (context[i] !== undefined) { execIteration(i, i, i === context.length - 1); } } diff --git a/spec/regressions.js b/spec/regressions.js index 4dc2ac89c..ae0992d44 100644 --- a/spec/regressions.js +++ b/spec/regressions.js @@ -288,4 +288,11 @@ describe('Regressions', function() { shouldCompileTo('{{helpa length="foo"}}', [obj, helpers], 'foo'); }); + + it('GH-1227: Array of undefineds', function() { + var array = [undefined, undefined]; + shouldCompileTo('foo{{#each array}}{{@index}}{{.}}{{/each}}bar', {array: array}, 'foobar'); + var array2 = new Array(2); + shouldCompileTo('foo{{#each array}}{{@index}}{{.}}{{/each}}bar', {array: array2}, 'foobar'); + }); });