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

Commit

Permalink
Added support for '%' operator as an alternative to 'mod'
Browse files Browse the repository at this point in the history
  • Loading branch information
fbardelli-shutterstock committed Dec 6, 2012
1 parent 5cf528a commit 290c4a0
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 7 deletions.
5 changes: 2 additions & 3 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', 'mod'],
var operators = ['==', '<', '>', '!=', '<=', '>=', '===', '!==', '&&', '||', 'in', 'and', 'or', 'mod', '%'],
errorString = 'Bad if-syntax in `{% if ' + args.join(' ') + ' %}...',
tokens = [],
prevType,
Expand Down Expand Up @@ -416,8 +416,7 @@ exports.parseIfArgs = function (args, parser) {
if (prevType === 'operator') {
throw new Error(errorString);
}
value = value.replace('and', '&&').replace('or', '||');
value = value.replace('mod', '%');
value = value.replace('and', '&&').replace('or', '||').replace('mod', '%');
tokens.push({
value: value
});
Expand Down
24 changes: 20 additions & 4 deletions tests/node/tags/if.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,18 +43,34 @@ 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!');
expect(tpl({ foo: 4 })).to.equal('hi!');

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

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

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

});


Expand Down

0 comments on commit 290c4a0

Please sign in to comment.