Skip to content

Commit

Permalink
fix: considering empty arrays as falsy values (#5)
Browse files Browse the repository at this point in the history
  • Loading branch information
jmjuanes authored Apr 8, 2024
1 parent d3195c6 commit 6d0e232
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 2 deletions.
4 changes: 2 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@ const compile = (tokens, output, ctx, opt, index, section) => {
const negate = tokens[i].startsWith("^");
if (!negate && value && Array.isArray(value)) {
const j = i + 1;
value.forEach(item => {
i = compile(tokens, output, item, opt, j, t);
(value.length > 0 ? value : [""]).forEach(item => {
i = compile(tokens, value.length > 0 ? output : [], item, opt, j, t);
});
}
else {
Expand Down
7 changes: 7 additions & 0 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,13 @@ describe("{{# xyz }}", () => {
assert.equal(m("{{#items}}{{name}}-{{/items}}", data), "Susan-Bob-");
});

it("should not iterate over an empty array", () => {
const data = {
items: [],
};
assert.equal(m("List of items: {{#items}}{{.}},{{/items}}", data), "List of items: ");
});

it("should throw an error for unmatched end of section", () => {
const data = {name: "Bob"};
try {
Expand Down

0 comments on commit 6d0e232

Please sign in to comment.