Skip to content

Commit

Permalink
Merge pull request #746 from oughter/support-type-check-comparisons
Browse files Browse the repository at this point in the history
Add support for strict type check comparisons
  • Loading branch information
carljm committed May 6, 2016
2 parents fd49151 + 28ffedb commit 0331bf1
Show file tree
Hide file tree
Showing 8 changed files with 23 additions and 2 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ Changelog
2.x (unreleased)
----------------

* Add support for strict type check comparisons (=== and !===)
* Allow full expressions (incl. filters) in import and from tags. Thanks legutierr.
Merge of [#710](https://github.com/mozilla/nunjucks/pull/710).

Expand Down
2 changes: 2 additions & 0 deletions docs/cn/templating.md
Original file line number Diff line number Diff line change
Expand Up @@ -586,7 +586,9 @@ Nunjucks 支持运算 (但尽量少用,把逻辑放在代码中),可使用
### 比较 (Comparisons)

* `==`
* `===`
* `!=`
* `!==`
* `>`
* `>=`
* `<`
Expand Down
2 changes: 2 additions & 0 deletions docs/fr/templating.md
Original file line number Diff line number Diff line change
Expand Up @@ -699,7 +699,9 @@ Vous pouvez les utiliser ainsi :
### Comparaisons

* `==`
* `===`
* `!=`
* `!==`
* `>`
* `>=`
* `<`
Expand Down
2 changes: 2 additions & 0 deletions docs/templating.md
Original file line number Diff line number Diff line change
Expand Up @@ -699,7 +699,9 @@ You can use them like this:
### Comparisons

* `==`
* `===`
* `!=`
* `!==`
* `>`
* `>=`
* `<`
Expand Down
2 changes: 2 additions & 0 deletions src/compiler.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,9 @@ var Frame = require('./runtime').Frame;
// through
var compareOps = {
'==': '==',
'===': '===',
'!=': '!=',
'!==': '!==',
'<': '<',
'>': '>',
'<=': '<=',
Expand Down
8 changes: 7 additions & 1 deletion src/lexer.js
Original file line number Diff line number Diff line change
Expand Up @@ -163,13 +163,19 @@ Tokenizer.prototype.nextToken = function() {
else if(delimChars.indexOf(cur) !== -1) {
// We've hit a delimiter (a special char like a bracket)
this.forward();
var complexOps = ['==', '!=', '<=', '>=', '//', '**'];
var complexOps = ['==', '===', '!=', '!==', '<=', '>=', '//', '**'];
var curComplex = cur + this.current();
var type;

if(lib.indexOf(complexOps, curComplex) !== -1) {
this.forward();
cur = curComplex;

// See if this is a strict equality/inequality comparator
if(lib.indexOf(complexOps, curComplex + this.current()) !== -1) {
cur = curComplex + this.current();
this.forward();
}
}

switch(cur) {
Expand Down
2 changes: 1 addition & 1 deletion src/parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -760,7 +760,7 @@ var Parser = Object.extend({
},

parseCompare: function() {
var compareOps = ['==', '!=', '<', '>', '<=', '>='];
var compareOps = ['==', '===', '!=', '!==', '<', '>', '<=', '>='];
var expr = this.parseConcat();
var ops = [];

Expand Down
6 changes: 6 additions & 0 deletions tests/compiler.js
Original file line number Diff line number Diff line change
Expand Up @@ -399,6 +399,12 @@
equal('{% if 10 != 10 %}yes{% endif %}', '');
equal('{% if 10 == 10 %}yes{% endif %}', 'yes');

equal('{% if "0" == 0 %}yes{% endif %}', 'yes');
equal('{% if "0" === 0 %}yes{% endif %}', '');
equal('{% if "0" !== 0 %}yes{% endif %}', 'yes');
equal('{% if 0 == false %}yes{% endif %}', 'yes');
equal('{% if 0 === false %}yes{% endif %}', '');

equal('{% if foo(20) > bar %}yes{% endif %}',
{ foo: function(n) { return n - 1; },
bar: 15 },
Expand Down

0 comments on commit 0331bf1

Please sign in to comment.