Skip to content
This repository has been archived by the owner on Apr 11, 2018. It is now read-only.

Commit

Permalink
Merge pull request #155 from fbardelli/master
Browse files Browse the repository at this point in the history
Support for modulus operator in swig
  • Loading branch information
paularmstrong committed Dec 8, 2012
2 parents 46b2df7 + 290c4a0 commit f18b753
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 2 deletions.
4 changes: 2 additions & 2 deletions lib/helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -374,7 +374,7 @@ exports.setVar = function (varName, argument) {
};

exports.parseIfArgs = function (args, parser) {
var operators = ['==', '<', '>', '!=', '<=', '>=', '===', '!==', '&&', '||', 'in', 'and', 'or'],
var operators = ['==', '<', '>', '!=', '<=', '>=', '===', '!==', '&&', '||', 'in', 'and', 'or', 'mod', '%'],
errorString = 'Bad if-syntax in `{% if ' + args.join(' ') + ' %}...',
tokens = [],
prevType,
Expand Down Expand Up @@ -416,7 +416,7 @@ exports.parseIfArgs = function (args, parser) {
if (prevType === 'operator') {
throw new Error(errorString);
}
value = value.replace('and', '&&').replace('or', '||');
value = value.replace('and', '&&').replace('or', '||').replace('mod', '%');
tokens.push({
value: value
});
Expand Down
31 changes: 31 additions & 0 deletions tests/node/tags/if.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,37 @@ describe('Tag: if', function () {
expect(tpl({ foo: 'b', bar: ['a', 'b', 'c'] })).to.equal('hi!');
});

it('can use the "%" operator', function () {
var tpl = swig.compile('{% if foo % 2 == 0 %}hi!{% endif %}');
expect(tpl({ foo: 4 })).to.equal('hi!');

tpl = swig.compile('{% if foo % 2 == 0 %}hi!{% endif %}');
expect(tpl({ foo: 5 })).to.equal('');

tpl = swig.compile('{% if foo % 2 %}hi!{% endif %}');
expect(tpl({ foo: 4 })).to.equal('');

tpl = swig.compile('{% if foo % 2 %}hi!{% endif %}');
expect(tpl({ foo: 3 })).to.equal('hi!');

});

it('can use the "mod" operator', function () {
var tpl = swig.compile('{% if foo mod 2 == 0 %}hi!{% endif %}');
expect(tpl({ foo: 4 })).to.equal('hi!');

tpl = swig.compile('{% if foo mod 2 == 0 %}hi!{% endif %}');
expect(tpl({ foo: 5 })).to.equal('');

tpl = swig.compile('{% if foo mod 2 %}hi!{% endif %}');
expect(tpl({ foo: 4 })).to.equal('');

tpl = swig.compile('{% if foo mod 2 %}hi!{% endif %}');
expect(tpl({ foo: 3 })).to.equal('hi!');

});


it('throws on bad conditional syntax', function () {
var fn1 = function () {
swig.compile('{% if foo bar %}{% endif %}');
Expand Down

0 comments on commit f18b753

Please sign in to comment.