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

Request for suggestions: Support negative integers in the slice filter #1850

Open
karreiro opened this issue Nov 15, 2024 · 2 comments
Open

Comments

@karreiro
Copy link
Contributor

karreiro commented Nov 15, 2024

Author: Shopify
Expected end date: December 5, 2024

Background

Today, the slice filter is limited to counting items from the start index, with no option to count backwards from the end. This creates unnecessary toil, affecting the developer experience, as seen in the following examples.

It's straightforward counting from the index:

{% assign fruits = 'apple, banana, cherry, tomato, fig' | split: ", " %}

{{ fruits | slice: 1, 3 }}
{% # => [ "banana", "cherry", "tomato" ] %}

However, operations such as getting all array elements except the last two can become verbose:

{% assign end_index = fruits.size | minus: 2 %}

{{ fruits | slice: 0, end_index }}
{% # => [ "apple", "banana", "cherry" ] %}

Proposal

Inspired by slice operations in other languages, we propose supporting negative numbers in the second argument, making operations more intuitive by controlling the direction of elements being taken.

With this enhancement, that example becomes simpler and clearer:

{{ fruits | slice: 0, -2 }}
{% # => [ "apple", "banana", "cherry" ] %}

Here's how the negative number influences the direction of the second argument:

{{ fruits | slice: 1, 3 }}
{{ fruits | slice: 1, -3 }}
Example of the slice filter, equivalent to the proposal below.

Limitations

This proposal is not currently backward compatible:

{{ fruits | slice: 0, -2 }}
{% # => [] %}

Therefore, we will conduct an impact assessment to evaluate the feasibility and ensure a smooth transition if this change is implemented.

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.

@galenking
Copy link

This seems low-risk since old themes wouldn’t use negatives, right?

@TeamDijon
Copy link

Do not forget that the first arguments accepts negative values:

{% liquid
  assign fruit_list = 'apple,banana,cherry,tomato,fig' | split: ','
  
  # Similar to | slice: 1, -3
  assign sliced_fruit_list = fruit_list | slice: -4, 1

  echo sliced_fruit_list | append: '<br>'
  # => ["banana"]
%}

As it can reasonably be expected that you will calculate either the -3 or the -4, I'm not sure if it is really necessary ?

Nice use-case though, I don't think I ever used the slice filter on arrays ! Thanks for sharing

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

No branches or pull requests

3 participants