Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for arrow functions #871

Closed

Conversation

antoineveldhoven
Copy link
Contributor

@antoineveldhoven antoineveldhoven commented Sep 26, 2023

Aiming to resolve #652

I'm not (yet) quite convinced if this is the way to go; would really like to get some discussion going on how to approach this and eventually get this fixed and merged.

Implementation of arrow functions in filters:

  • sort
  • filter
  • map
  • reduce

Object manipulation is not really supported; this implementation accepts a callbackFunction (Twig syntax) which is compiled and parsed as a new (runtime) template. The return values (e.g. output) of the callBackFunctions are delegated to their respective JS functions.

As previously stated: this is merely a functional (read dirty) workaround; but for most of the use cases it will do the trick.

@antoineveldhoven antoineveldhoven marked this pull request as ready for review September 27, 2023 10:52
@antoineveldhoven antoineveldhoven changed the title [WIP] Filter callbacks Filter callbacks Sep 27, 2023
@willrowe willrowe changed the title Filter callbacks Add support for arrow functions Oct 11, 2023
src/twig.expression.js Outdated Show resolved Hide resolved
src/twig.expression.js Outdated Show resolved Hide resolved
@@ -212,9 +214,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
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remove this change and submit as separate PR.

// 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)|^\.\.)/,
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remove this change and submit as separate PR.

src/twig.expression.js Outdated Show resolved Hide resolved
token.precidence = 9;
token.associativity = Twig.expression.operator.leftToRight;
break;

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remove this change and submit as separate PR.

case '<=>':
stack.push(a === b ? 0 : (a < b ? -1 : 1));
break;

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remove this change and submit as separate PR.

const template = Twig.exports.twig({data: params.body});
return template.render(data);
}, params.args || 0);
}
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remove these changes and submit as a separate PR after this is one merged.

const output = testTemplate.render(pair);
output.should.equal((pair.a === pair.b ? 0 : (pair.a < pair.b ? -1 : 1)).toString());
});
});
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remove this change and submit as separate PR.

testTemplate.render().should.equal('18');
});
});

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remove these changes and submit as a separate PR after this is one merged.

antoineveldhoven and others added 6 commits October 11, 2023 21:39
Co-authored-by: Will Rowe <willrowe@users.noreply.github.com>
Co-authored-by: Will Rowe <willrowe@users.noreply.github.com>
Co-authored-by: Will Rowe <willrowe@users.noreply.github.com>
Co-authored-by: Will Rowe <willrowe@users.noreply.github.com>
Co-authored-by: Will Rowe <willrowe@users.noreply.github.com>
Co-authored-by: Will Rowe <willrowe@users.noreply.github.com>
@antoineveldhoven antoineveldhoven deleted the filter-callbacks branch October 11, 2023 19:43
@antoineveldhoven
Copy link
Contributor Author

Thanks for the review and the suggestions.
Please take a look at #873 & #874

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Add filter, map and reduce filters with support for arrow functions
2 participants