Skip to content

Commit

Permalink
Merge pull request #17 from ambassify/bug/support-for-key-value
Browse files Browse the repository at this point in the history
[for] add support for key, value iteration
  • Loading branch information
JorgenEvens committed Mar 1, 2019
2 parents 83a0611 + d127f33 commit 21cc073
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 0 deletions.
11 changes: 11 additions & 0 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -425,6 +425,17 @@ function matchToken(config = {}, str, state = TEXT, offset = 0, skip = 0, parent
tok.closing = false;
return tok;

/**
* Iterating over keys and values using a for loop allows
* for two arguments separated by a comma.
*
* Example: {% for key, value in array %}
*/
} else if (isType(state, TAG_CONTROL) && cur == ',' && tok.name == 'for' && tok.children.length == 1) {
// jump over comma, next argument will be picked up by regular arguments clause
i++;
continue;

/**
* The sequence following the name of a control tag should be the
* arguments to the tag.
Expand Down
15 changes: 15 additions & 0 deletions test/control.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,4 +49,19 @@ describe('toAST', () => {

})

it('should handle for loop iterating both keys and values', () => {
const ast = toAST(`
{% for key, value in array %}
Hello Test
{% endfor %}
`);

assert.equal(take(ast, 1, 'name'), 'for');
assert.equal(take(ast, 1, 0, 0, 'name'), 'key');
assert.equal(take(ast, 1, 1, 0, 'name'), 'value');
assert.equal(take(ast, 1, 1, 1, 'value'), 'in');
assert.equal(take(ast, 1, 1, 2, 0, 'name'), 'array');
assert.equal(take(ast, 1, 2, 'type'), 'BLOCK');
});

});

0 comments on commit 21cc073

Please sign in to comment.