8000 feat: add find_index, has, and reject filters by brunodccarvalho · Pull Request #799 · harttle/liquidjs · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

feat: add find_index, has, and reject filters #799

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

Merged
merged 4 commits into from
Feb 23, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions docs/source/_data/sidebar.yml
Original file line number Diff line number Diff line change
Expand Up @@ -51,10 +51,14 @@ filters:
escape_once: escape_once.html
find: find.html
find_exp: find_exp.html
find_index: find_index.html
find_index_exp: find_index_exp.html
first: first.html
floor: floor.html
group_by: group_by.html
group_by_exp: group_by_exp.html
has: has.html
has_exp: has_exp.html
inspect: inspect.html
join: join.html
json: json.html
Expand All @@ -72,6 +76,8 @@ filters:
push: push.html
prepend: prepend.html
raw: raw.html
reject: reject.html
reject_exp: reject_exp.html
remove: remove.html
remove_first: remove_first.html
remove_last: remove_last.html
Expand Down
25 changes: 25 additions & 0 deletions docs/source/filters/find_index.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
---
title: find_index
---

{% since %}v10.21.0{% endsince %}

Return the 0-based index of the first object in an array for which the queried attribute has the given value or return `nil` if no item in the array satisfies the given criteria. For the following `members` array:

```javascript
const members = [
{ graduation_year: 2013, name: 'Jay' },
{ graduation_year: 2014, name: 'John' },
{ graduation_year: 2014, name: 'Jack' }
]
```

Input
```liquid
{{ members | find_index: "graduation_year", 2014 | json }}
```

Output
```text
1
```
25 changes: 25 additions & 0 deletions docs/source/filters/find_index_exp.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
---
title: find_index_exp
---

{% since %}v10.21.0{% endsince %}

Return the 0-based index of the first object in an array for which the given expression evaluates to true or return `nil` if no item in the array satisfies the evaluated expression.

```javascript
const members = [
{ graduation_year: 2013, name: 'Jay' },
{ graduation_year: 2014, name: 'John' },
{ graduation_year: 2014, name: 'Jack' }
]
```

Input
```liquid
{{ members | find_index_exp: "item", "item.graduation_year == 2014" | json }}
```

Output
```text
1
```
25 changes: 25 additions & 0 deletions docs/source/filters/has.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
---
title: has
---

{% since %}v10.21.0{% endsince %}

Return `true` if the array includes an item for which the queried attribute has the given value or return `false` if no item in the array satisfies the given criteria. For the following `members` array:

```javascript
const members = [
{ graduation_year: 2013, name: 'Jay' },
{ graduation_year: 2014, name: 'John' },
{ graduation_year: 2014, name: 'Jack' }
]
```

Input
```liquid
{{ members | has: "graduation_year", 2014 | json }}
```

Output
```text
true
```
25 changes: 25 additions & 0 deletions docs/source/filters/has_exp.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
---
title: has_exp
---

{% since %}v10.21.0{% endsince %}

Return `true` if an item exists in an array for which the given expression evaluates to true or return `false` if no item in the array satisfies the evaluated expression.

```javascript
const members = [
{ graduation_year: 2013, name: 'Jay' },
{ graduation_year: 2014, name: 'John' },
{ graduation_year: 2014, name: 'Jack' }
]
```

Input
```liquid
{{ members | has_exp: "item", "item.graduation_year == 2014" | json }}
```

Output
```text
true
```
118 changes: 118 additions & 0 deletions docs/source/filters/reject.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
---
title: reject
---

{% since %}v10.21.0{% endsince %}

Creates an array excluding the objects with a given property value, or excluding [truthy][truthy] values by default when a property is not given.

In this example, assume you have a list of products and you want to filter out kitchen products. Using `reject`, you can create an array excluding only the products that have a `"type"` of `"kitchen"`.

Input
```liquid
All products:
{% for product in products %}
- {{ product.title }}
{% endfor %}

{% assign non_kitchen_products = products | reject: "type", "kitchen" %}

Kitchen products:
{% for product in non_kitchen_products %}
- {{ product.title }}
{% endfor %}
```

Output
```text
All products:
- Vacuum
- Spatula
- Television
- Garlic press

Kitchen products:
- Vacuum
- Television
```

Say instead you have a list of products and you want to exclude taxable products. You can `reject` with a property name but no target value to reject all products with a [truthy][truthy] `"taxable"` value.

Input
```liquid
All products:
{% for product in products %}
- {{ product.title }}
{% endfor %}

{% assign not_taxed_products = products | reject: "taxable" %}

Available products:
{% for product in not_taxed_products %}
- {{ product.title }}
{% endfor %}
```

Output
```text
All products:
- Vacuum
- Spatula
- Television
- Garlic press

Available products:
- Spatula
- Television
```

Additionally, `property` can be any valid Liquid variable expression as used in output syntax, except that the scope of this expression is within each item. For the following `products` array:

```javascript
const products = [
{ meta: { details: { class: 'A' } }, order: 1 },
{ meta: { details: { class: 'B' } }, order: 2 },
{ meta: { details: { class: 'B' } }, order: 3 }
]
```

Input
```liquid
{% assign selected = products | reject: 'meta.details["class"]', "B" %}
{% for item in selected -%}
- {{ item.order }}
{% endfor %}
```

Output
```text
- 1
```

## Jekyll style

{% since %}v10.21.0{% endsince %}

For Liquid users migrating from Jekyll, there's a `jekyllWhere` option to mimic the behavior of Jekyll's `where` filter. This option is set to `false` by default. When enabled, if `property` is an array, the target value is matched using `Array.includes` instead of `==`, which is particularly useful for excluding tags.

```javascript
const pages = [
{ tags: ["cat", "food"], title: 'Cat Food' },
{ tags: ["dog", "food"], title: 'Dog Food' },
]
```

Input
```liquid
{% assign selected = pages | reject: 'tags', "cat" %}
{% for item in selected -%}
- {{ item.title }}
{% endfor %}
```

Output
```text
Dog Food
```

[truthy]: ../tutorials/truthy-and-falsy.html
37 changes: 37 additions & 0 deletions docs/source/filters/reject_exp.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
---
title: reject_exp
---

{% since %}v10.21.0{% endsince %}

Select all the objects in an array where the expression is false. In this example, assume you have a list of products and you want to hide your kitchen products. Using `reject_exp`, you can create an array that omits only the products that have a `"type"` of `"kitchen"`.

Input
```liquid
All products:
{% for product in products %}
- {{ product.title }}
{% endfor %}

{% assign non_kitchen_products = products | reject_exp: "item", "item.type == 'kitchen'" %}

Kitchen products:
{% for product in non_kitchen_products %}
- {{ product.title }}
{% endfor %}
```

Output
```text
All products:
- Vacuum
- Spatula
- Television
- Garlic press

Kitchen products:
- Vacuum
- Television
```

[truthy]: ../tutorials/truthy-and-falsy.html
8 changes: 5 additions & 3 deletions docs/source/filters/where.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ Kitchen products:
```

Say instead you have a list of products and you only want to show those that are available to buy. You can `where` with a property name but no target value to include all products with a [truthy][truthy] `"available"` value.
As a special case, the same will happen if the target value is given but evaluates to `undefined`.

Input
```liquid
Expand Down Expand Up @@ -70,7 +71,6 @@ The `where` filter can also be used to find a single object in an array when com
Input
```liquid
{% assign new_shirt = products | where: "type", "shirt" | first %}

Featured product: {{ new_shirt.title }}
```

Expand Down Expand Up @@ -105,9 +105,11 @@ Output

## Jekyll style

{% since %}v10.19.0{% endsince %}
{% since %}v10.21.0{% endsince %}

For Liquid users migrating from Jekyll, there's a `jekyllWhere` option to mimic the behavior of Jekyll's `where` filter. This option is set to `false` by default. When enabled, if `property` is an array, the target value is matched using `Array.includes` instead of `==`, which is particularly useful for filtering tags. Additionally, a target value of `undefined` is treated normally, entries matched are exactly those which are themselves `undefined`.

For Liquid users migrating from Jekyll, there's a `jekyllWhere` option to mimic the behavior of Jekyll's `where` filter. This option is set to `false` by default. When enabled, if `property` is an array, the target value is matched using `Array.includes` instead of `==`, which is particularly useful for filtering tags.
This option affects other array selection filters as well, such as `reject` and `find`.

```javascript
const pages = [
Expand Down
Loading
Loading
0