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

min function does not working as expected #466

Closed
lionel87 opened this issue Dec 12, 2019 · 1 comment
Closed

min function does not working as expected #466

lionel87 opened this issue Dec 12, 2019 · 1 comment
Labels
Milestone

Comments

@lionel87
Copy link

I tired to use the built-in min() function on an array containing negative numbers, but the result was wrong.

{% set a = [-5, 5, 10, -10, 20, -20] %}
{{ min(a) }}

Expexted: -20

Got: -10

This is because you do a.sort() then return a[0] and in JS the sort does this:

const a = [-5, 5, 10, -10, 20, -20];
a.sort();
// a => [-10, -20, -5, 10, 20, 5]

https://github.com/NightlyCommit/twing/blob/master/src/lib/extension/core/functions/min.ts#L10

Better (faster) approach would be:

return Math.min.apply(null, array);

The max function does not affected because you wrote a proper compare function there.

@ericmorand ericmorand added the bug label Dec 12, 2019
@ericmorand ericmorand added this to the 4.0.2 milestone Dec 16, 2019
ericmorand added a commit to ericmorand/twing that referenced this issue Dec 16, 2019
ericmorand added a commit to ericmorand/twing that referenced this issue Dec 16, 2019
ericmorand added a commit that referenced this issue Dec 16, 2019
@ericmorand
Copy link
Member

ericmorand commented Dec 16, 2019

Will be fixed in Twing@4.0.2 available later today. Thanks for the report. min and max functions were far from implementing the whole spectrum of TwigPHP equivalent. This is now fixed.

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

No branches or pull requests

2 participants