-
Notifications
You must be signed in to change notification settings - Fork 1.4k
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
Request for suggestions: Add find
, find_index
, has
, and reject
filters to arrays
#1849
Comments
👍
Would Have you considered |
Would adding the ability to work with nested keys be a trivial add? i.e. {{ section.blocks | find: "settings.product.id", product.id }} |
I feel like IMHO, |
These all are pretty useful! An addition to boolean support idea of @galenking, comparison filters would also be really useful. {% assign collections_to_show = collections | where_compare: 'products_count', '>=', 0 %} or {% assign collections_to_show = collections | greater: 'products_count', 0 %}
{% assign collections_to_show = collections | greater_or_equal: 'products_count', 0 %} or {% assign collections_to_show = collections | where_gt: 'products_count', 0 %}
{% assign collections_to_show = collections | where_gte: 'products_count', 0 %} |
|
Pending PR for |
Should |
👋 Hey everyone, Thank you so much for sharing these thoughts!
Precisely, @jg-rp, this makes runtimes potentially more efficient and also makes the code less verbose.
We considered it also because of the String API, but it was looking a bit verbose, and we do have some prior art (with Ruby, Rust, JavaScript) where the concept of Regarding Regarding Thanks a lot your thoughts, @jg-rp!
Thank you, @andershagbard and @dan-gamble. This is an excellent suggestion and really resonates with so many use cases. We're planning to incorporate this as part of the implementation. I don't think we're introducing support for multiple separators, but we may expect that on the Array API.
We've identified some contexts where the Thinking about naming, many different folks were leaning on Please, if you consider For sure we have some paths where a more robust All query-filters ( {{
collections.first.products
| where: 'price', greater: 3000
| map: 'title'
}}
{{
collections.first.products
| reject: 'title', contains: "Shirt"
| map: 'title'
}} But we're still validating how that plays with other long-term items to identify the limitations of this approach, so we don't feel this is a point to be in an RFC for now. Thanks again, everyone, for all your input. We read and reflect on every thought you share here and in the other issues. They are really helpful in calibrating the right direction for the language! |
👋 Hey everyone, Thank you so much for your thoughts here. I'm closing this issue in favor of this PR: If you have any extra thoughts, feel free to drop them there. This way, we can keep a single thread about this :) Thanks again for your involvement! |
Author: Shopify
Expected end date: December 5, 2024
Background
Handling arrays in Liquid is challenging.
Developers often need to iterate over arrays to find items or check for their presence. This pattern results in imperative code and verbose templates, which detract from the simplicity and declarative nature that Liquid aims to promote.
⏭️ Upcoming: In a different proposal, we'll tackle improvements to the
where
filter and its limitation of only filtering based on equality. That's so crucial, we aim to handle that separately.Proposal
We should introduce new filters to make it easier to perform common tasks such as extracting or checking the presence of an item in an array. This will make Liquid templates simpler, less verbose, and more declarative.
All filters proposed here do not mutate the source object and return a new instance as a result of the operation:
1.
find
andfind_index
The
find
andfind_index
filters return, respectively, the item and the index of the item with the specified property value; otherwise, they returnnil
.2.
has
Returns
true
when the array includes an item with the specified property value; otherwise, returnsfalse
.Source
3.
reject
Works almost like the where filter, but returns an array excluding items with the specified property value.
Source
Call for suggestions
We welcome any feedback or opinions on this proposal. Please share your thoughts by December 5, 2024. Your input is valuable as we prepare to begin active development on this initiative.
The text was updated successfully, but these errors were encountered: