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

Support for modulus operator in swig #155

Merged
merged 2 commits into from
Dec 8, 2012
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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