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

Liquidjs divided_by not compatible with Ruby/Shopify Liquid #465

Closed
Nowaker opened this issue Feb 5, 2022 · 12 comments · May be fixed by ASISBusiness/docs#17 or ReneMaetzschker/docs#2
Closed

Liquidjs divided_by not compatible with Ruby/Shopify Liquid #465

Nowaker opened this issue Feb 5, 2022 · 12 comments · May be fixed by ASISBusiness/docs#17 or ReneMaetzschker/docs#2

Comments

@Nowaker
Copy link
Contributor

Nowaker commented Feb 5, 2022

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

The result is rounded down to the nearest integer (that is, the floor) if the divisor is an integer.Input

{{ 16 | divided_by: 4 }} => 4
{{ 5 | divided_by: 3 }} => 1

Liquidjs:

{{ 5 | divided_by: 3 }} => 1.6666666666666667
@harttle
Copy link
Owner

harttle commented Feb 5, 2022

As stated here: https://liquidjs.com/tutorials/differences.html, in JavaScript we cannot distinguish integer and float, they're just numbers. For example, I can't find a way to implement this:

{{ 5 | divided_by: 3 }} // expect 1
{{ 5.0 | divided_by: 3 }} // expect 1.6667

@Nowaker
Copy link
Contributor Author

Nowaker commented Feb 6, 2022

@harttle I'll provide a conforming implementation this week.

@Nowaker
Copy link
Contributor Author

Nowaker commented Feb 8, 2022

@harttle Using lodash:

  engine.registerFilter('divided_by', (dividend, divisor) => {
    if (_.isInteger(divisor)) {
      return _.floor(dividend, divisor)
    } else {
      return dividend / divisor
    }
  })

@Nowaker
Copy link
Contributor Author

Nowaker commented Feb 8, 2022

I can submit a PR at a later point (1-3 weeks) or you can use transform it to TS yourself right away. Up to you :)

@harttle
Copy link
Owner

harttle commented Feb 8, 2022 via email

@Nowaker
Copy link
Contributor Author

Nowaker commented Feb 8, 2022

@harttle Yeah, you're right... I just found this definition of divided_by broke other parts of my code.

@Nowaker
Copy link
Contributor Author

Nowaker commented Feb 8, 2022

Actually, your example is incorrect. The rule is if the divisor is an integer. What you want is:

{{ 5.0 | divided_by: 3 }} // 1
{{ 5 | divided_by: 3 }} // 1
{{ 5.0 | divided_by: 3.0 }} // 1.6667
{{ 5 | divided_by: 3.0 }} // 1.6667

However, my code still fails:

1 // 1
1 // 1
1 // 1.6667
1 // 1.6667

Still working on it though. I'll keep you updated.

@Nowaker
Copy link
Contributor Author

Nowaker commented Feb 8, 2022

Okay. There's no way to fix it on the filter level. We'd have to detect 5.0 on the parsing stage, and use our own wrapping type to differentiate between float and integer. I'm not that good with JavaScript to do it for you.

@harttle
Copy link
Owner

harttle commented Feb 8, 2022

What if the divider is a variable?

I mean we can keep it simple and imperfect, then enable this detection only when heuristicInteger option is set. If this imperfect detection is useful for you.

@Nowaker
Copy link
Contributor Author

Nowaker commented Feb 8, 2022

...Or maybe - rather than introduce value wrapping, why don't we introduce two extra arguments that are passed to divided_by?

  engine.registerFilter('divided_by', (dividend, divisor, dividendType, divisorType) => {

with dividendType and divisorType returning float or int for informational purpose, so that divided_by can do its job.

And yes, if divider is a variable, it doesn't work. But I think it's still worth it. It gets us a little closer to Liquid compatibility.

github-actions bot pushed a commit that referenced this issue Apr 21, 2022
# [9.37.0](v9.36.2...v9.37.0) (2022-04-21)

### Bug Fixes

* support integer arithmetic for `divided_by`, closes [#465](#465) ([e69a510](e69a510))

### Features

* automatic output escaping, closes [#500](#500) ([f88490c](f88490c))
@github-actions
Copy link

🎉 This issue has been resolved in version 9.37.0 🎉

The release is available on:

Your semantic-release bot 📦🚀

@harttle
Copy link
Owner

harttle commented Apr 21, 2022

Supported another argument to enable integer arithmetic:

{{ 5 | divided_by: 3, true }}

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