Skip to content

Commit

Permalink
✨ Add support for gte and lte operator, which are aliases for `ge…
Browse files Browse the repository at this point in the history
…` and `le`
  • Loading branch information
skerit committed Nov 26, 2024
1 parent 6408cfd commit 5ec6f0e
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 5 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
## 2.4.1 (WIP)

* Fix variables not being available for custom elements that require synchronous rendering
* Add support for `gte` and `lte` operator, which are aliases for `ge` and `le`

## 2.4.0 (2024-08-12)

Expand Down
6 changes: 3 additions & 3 deletions lib/expression/expression.js
Original file line number Diff line number Diff line change
Expand Up @@ -578,7 +578,7 @@ Expression.setMethod(function branch(name, pieces, fnc) {
*
* @author Jelle De Loecker <[email protected]>
* @since 1.2.9
* @version 2.4.0
* @version 2.4.1
*
* @param {Array} tokens An array of tokens
* @param {Object} vars An object of variables
Expand Down Expand Up @@ -743,11 +743,11 @@ Expression.setMethod(function parseExpression(tokens, vars, call_chain) {
}
} else if (op == 'gt') {
result = a > b;
} else if (op == 'ge') {
} else if (op == 'ge' || op == 'gte') {
result = a >= b;
} else if (op == 'lt') {
result = a < b;
} else if (op == 'le') {
} else if (op == 'le' || op == 'lte') {
result = a <= b;
} else if (op == 'plus') {
result = a + b;
Expand Down
6 changes: 4 additions & 2 deletions lib/parser/expressions_parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -701,7 +701,7 @@ Eparser.setMethod(function getConditionalExpression(condition, level) {
*
* @author Jelle De Loecker <[email protected]>
* @since 1.2.9
* @version 2.4.0
* @version 2.4.1
*
* @param {Number} level
* @param {Symbol} state Are we in some kind of literal ({}, [])
Expand All @@ -710,7 +710,7 @@ Eparser.setMethod(function getConditionalExpression(condition, level) {
*/
Eparser.setMethod(function getExpression(level, state) {

var pre_variable_index,
let pre_variable_index,
variable_count = 0,
variable,
has_array,
Expand Down Expand Up @@ -885,6 +885,8 @@ Eparser.setMethod(function getExpression(level, state) {
case 'gt':
case 'le':
case 'ge':
case 'gte':
case 'lte':
// Set the "keyword" property to true,
// in case `previous_token` is used
token.keyword = true;
Expand Down
5 changes: 5 additions & 0 deletions test/10-expressions.js
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,11 @@ describe('Expressions', function() {
['{% if my_obj.c eq "a" or my_obj.b eq "a" %}ERR{% elseif my_obj.a eq "a" and my_obj.b eq "b" %}AB{% else %}ERRTOO{% /if %}', 'AB'],
['{% if opt_str %}{{ opt_str }}{% /if %}', 'truthy'],
['{% if opt_empty %}OOPS{% /if %}', ''],
['{% if Math.max(1, 2) gte 2 %}ALWAYSTRUE{% else %}WRONG{% / if %}', 'ALWAYSTRUE'],
['{% if Math.max(0, 0) gte 2 %}WRONG{% else %}ALWAYSELSE{% / if %}', 'ALWAYSELSE'],
['{% if (Math.max(0, 0) gte 2) %}WRONG{% else %}ALWAYSELSE{% / if %}', 'ALWAYSELSE'],
['{% if (Math.max(0, 0)) gte 2 %}WRONG{% else %}ALWAYSELSE{% / if %}', 'ALWAYSELSE'],
['{% if ((Math.max(0, 0) gte 2) %}WRONG{% else %}ALWAYSELSE{% / if %}', 'ALWAYSELSE'],
// @TODO: ['{% if 1 emptyhtml %}WRONG{% /if %}', ''],
];

Expand Down

0 comments on commit 5ec6f0e

Please sign in to comment.