Skip to content

Commit

Permalink
Add support for eq, ne
Browse files Browse the repository at this point in the history
  • Loading branch information
ultraq committed Sep 20, 2020
1 parent 7bbdb8b commit 997947d
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 6 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Expand Up @@ -4,6 +4,7 @@ Changelog

### 0.20.2
- Add support for logical operators: `&&`/`and`, `||`/`or`
- Add support for equality operator keywords: `eq`, `ne`

### 0.20.1
- Fix `Node is not defined` errors when using Thymeleaf in a NodeJS environment,
Expand Down
4 changes: 3 additions & 1 deletion source/standard/expressions/ThymeleafExpressionLanguage.js
Expand Up @@ -360,15 +360,17 @@ const ThymeleafExpressionLanguage = new Grammar('Thymeleaf Expression Language',
let rhs = rightOperand(context);
switch (operator(context)) {
case '==': return lhs == rhs; // eslint-disable-line
case 'eq':
case '===': return lhs === rhs;
case '!=': return lhs != rhs; // eslint-disable-line
case 'ne':
case '!==': return lhs !== rhs;
}
return false;
}
),
new ThymeleafRule('EqualityOperator',
OrderedChoice(/[=!]==?/)
OrderedChoice(/[=!]==?/, /eq/, /ne/)
),


Expand Down
20 changes: 15 additions & 5 deletions test/standard/expressions/ThymeleafExpressionLanguage.js
Expand Up @@ -263,14 +263,24 @@ describe('standard/expressions/ThymeleafExpressionLanguage', function() {
});
});


describe('#Condition', function() {
describe('#EqualityOperation', function() {

test('${var} === literal', function() {
let result = expressionProcessor.process('${number} === 3', {
number: 3
['===', 'eq'].forEach(symbol => {
let result = expressionProcessor.process(`\${number} ${symbol} 3`, {
number: 3
});
expect(result).toBe(true);
});
});

test('${var} !== literal', function() {
['!==', 'ne'].forEach(symbol => {
let result = expressionProcessor.process(`\${number} ${symbol} 3`, {
number: 3
});
expect(result).toBe(false);
});
expect(result).toBe(true);
});
});

Expand Down

0 comments on commit 997947d

Please sign in to comment.