Repository: twigjs/twig.js (js)

Issue:
Add support for spacehip operator
Add support for spaceship operator.

[PHP](https://www.php.net/manual/en/language.operators.comparison.php)
[Usage in Twig](https://twig.symfony.com/doc/3.x/filters/sort.html)

This is needed for resolving #652 

Pull request:
Add support for spaceship operator
Fixes #872 

Patch:
diff --git a/src/twig.expression.js b/src/twig.expression.js
index 80a8b4d..0e2ea9f 100644
--- a/src/twig.expression.js
+++ b/src/twig.expression.js
@@ -212,9 +212,9 @@ module.exports = function (Twig) {
         },
         {
             type: Twig.expression.type.operator.binary,
-            // Match any of ??, ?:, +, *, /, -, %, ~, <, <=, >, >=, !=, ==, **, ?, :, and, b-and, or, b-or, b-xor, in, not in
+            // Match any of ??, ?:, +, *, /, -, %, ~, <=>, <, <=, >, >=, !=, ==, **, ?, :, and, b-and, or, b-or, b-xor, in, not in
             // and, or, in, not in, matches, starts with, ends with can be followed by a space or parenthesis
-            regex: /(^\?\?|^\?:|^(b-and)|^(b-or)|^(b-xor)|^[+\-~%?]|^[:](?!\d\])|^[!=]==?|^[!<>]=?|^\*\*?|^\/\/?|^(and)[(|\s+]|^(or)[(|\s+]|^(in)[(|\s+]|^(not in)[(|\s+]|^(matches)|^(starts with)|^(ends with)|^\.\.)/,
+            regex: /(^\?\?|^\?:|^(b-and)|^(b-or)|^(b-xor)|^[+\-~%?]|^(<=>)|^[:](?!\d\])|^[!=]==?|^[!<>]=?|^\*\*?|^\/\/?|^(and)[(|\s+]|^(or)[(|\s+]|^(in)[(|\s+]|^(not in)[(|\s+]|^(matches)|^(starts with)|^(ends with)|^\.\.)/,
             next: Twig.expression.set.expressions,
             transform(match, tokens) {
                 switch (match[0]) {
diff --git a/src/twig.expression.operator.js b/src/twig.expression.operator.js
index 452add4..027382e 100644
--- a/src/twig.expression.operator.js
+++ b/src/twig.expression.operator.js
@@ -93,6 +93,11 @@ module.exports = function (Twig) {
                 token.associativity = Twig.expression.operator.leftToRight;
                 break;
 
+            case '<=>':
+                token.precidence = 9;
+                token.associativity = Twig.expression.operator.leftToRight;
+                break;
+
             case '<':
             case '<=':
             case '>':
@@ -275,6 +280,10 @@ module.exports = function (Twig) {
                 stack.push(!Twig.lib.boolval(b));
                 break;
 
+            case '<=>':
+                stack.push(a === b ? 0 : (a < b ? -1 : 1));
+                break;
+
             case '<':
                 stack.push(a < b);
                 break;


Test patch:
diff --git a/test/test.expressions.js b/test/test.expressions.js
index fb41428..6620479 100644
--- a/test/test.expressions.js
+++ b/test/test.expressions.js
@@ -173,6 +173,13 @@ describe('Twig.js Expressions ->', function () {
             {a: false, b: true},
             {a: false, b: false}
         ];
+        it('should support spaceship operator', function () {
+            const testTemplate = twig({data: '{{ a <=> b }}'});
+            numericTestData.forEach(pair => {
+                const output = testTemplate.render(pair);
+                output.should.equal((pair.a === pair.b ? 0 : (pair.a < pair.b ? -1 : 1)).toString());
+            });
+        });
         it('should support less then', function () {
             const testTemplate = twig({data: '{{ a < b }}'});
             numericTestData.forEach(pair => {