Skip to content

Commit

Permalink
Handle undefined arrays better
Browse files Browse the repository at this point in the history
handlebars-lang#1227

[undefined, undefined] should have the same behavior as new Array(2),
per handlebars-lang#1065
  • Loading branch information
travnels committed Jan 16, 2017
1 parent a647673 commit bea5554
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 1 deletion.
2 changes: 1 addition & 1 deletion lib/handlebars/helpers/each.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
Expand Down
7 changes: 7 additions & 0 deletions spec/regressions.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');
});
});

0 comments on commit bea5554

Please # to comment.