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

Math.floor blocks float in divided_by filter #59

Closed
tim-phillips opened this issue Jan 13, 2018 · 5 comments
Closed

Math.floor blocks float in divided_by filter #59

tim-phillips opened this issue Jan 13, 2018 · 5 comments
Assignees
Labels

Comments

@tim-phillips
Copy link

In filters.js, divided_by is defined as 'divided_by': (v, arg) => Math.floor(v / arg).

However in the Liquid documentation it says that if given a float it will return a float (under the section "Controlling rounding"). Math.floor prevents that.

https://shopify.github.io/liquid/filters/divided_by/

@harttle harttle added the bug label Jan 13, 2018
@harttle
Copy link
Owner

harttle commented Jan 14, 2018

It seems JavaScript Number does not distinguish float and integer, which means it's may be not possible to be compliant with shopify.
AFAIK, either Number.isInteger(x), x%1, or x.toString() cannot test float numbers like 1.0. Any ideas?

@tim-phillips
Copy link
Author

You're right, it seems that javascript cannot handle the 1.0 case.

@harttle
Copy link
Owner

harttle commented Jan 17, 2018

Since we have floor and ceil, can we always return the float result as it is? This is not backward-compatible but makes sense. What do you think?

@tim-phillips
Copy link
Author

I already added this to my project:

liquid.registerFilter('divided_by', (v, arg) => v / arg)

So yes, I agree with returning the float as is.

@harttle harttle self-assigned this Jan 17, 2018
@harttle
Copy link
Owner

harttle commented May 12, 2018

Now divided_by always return the number as it is. When integers are expected, useceil, floor and round filters. Since this fix is NOT backward-compatible, we bump a major version to @4.0.0.

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