Skip to content

Commit

Permalink
feat: introduce where_exp filter from Jekyll
Browse files Browse the repository at this point in the history
  • Loading branch information
Yang Jun committed Apr 28, 2024
1 parent 9b0e612 commit dd9bbc5
Show file tree
Hide file tree
Showing 7 changed files with 109 additions and 2 deletions.
1 change: 1 addition & 0 deletions docs/source/_data/sidebar.yml
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ filters:
url_decode: url_decode.html
url_encode: url_encode.html
where: where.html
where_exp: where_exp.html

tags:
overview: overview.html
Expand Down
2 changes: 1 addition & 1 deletion docs/source/filters/overview.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ Categories | Filters
Math | plus, minus, modulo, times, floor, ceil, round, divided_by, abs, at_least, at_most
String | append, prepend, capitalize, upcase, downcase, strip, lstrip, rstrip, strip_newlines, split, replace, replace_first, replace_last,remove, remove_first, remove_last, truncate, truncatewords
HTML/URI | escape, escape_once, url_encode, url_decode, strip_html, newline_to_br
Array | slice, map, sort, sort_natural, uniq, where, group_by, group_by_exp, find, find_exp, first, last, join, reverse, concat, compact, size, push, pop, shift, unshift
Array | slice, map, sort, sort_natural, uniq, where, where_exp, group_by, group_by_exp, find, find_exp, first, last, join, reverse, concat, compact, size, push, pop, shift, unshift
Date | date
Misc | default, json, raw

Expand Down
37 changes: 37 additions & 0 deletions docs/source/filters/where_exp.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
---
title: where_exp
---

{% since %}v10.12.0{% endsince %}

Select all the objects in an array where the expression is true. In this example, assume you have a list of products and you want to show your kitchen products separately. Using `where_exp`, you can create an array containing only the products that have a `"type"` of `"kitchen"`.

Input
```liquid
All products:
{% for product in products %}
- {{ product.title }}
{% endfor %}
{% assign kitchen_products = products | where_exp: "item", "item.type == 'kitchen'" %}
Kitchen products:
{% for product in kitchen_products %}
- {{ product.title }}
{% endfor %}
```

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

[truthy]: ../tutorials/truthy-and-falsy.html
2 changes: 1 addition & 1 deletion docs/source/zh-cn/filters/overview.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ LiquidJS 共支持 40+ 个过滤器,可以分为如下几类:
数学 | plus, minus, modulo, times, floor, ceil, round, divided_by, abs, at_least, at_most
字符串 | append, prepend, capitalize, upcase, downcase, strip, lstrip, rstrip, strip_newlines, split, replace, replace_first, replace_last, remove, remove_first, remove_last, truncate, truncatewords
HTML/URI | escape, escape_once, url_encode, url_decode, strip_html, newline_to_br
数组 | slice, map, sort, sort_natural, uniq, where, group_by, group_by_exp, find, find_exp, first, last, join, reverse, concat, compact, size, push, pop, shift, unshift
数组 | slice, map, sort, sort_natural, uniq, where, where_exp, group_by, group_by_exp, find, find_exp, first, last, join, reverse, concat, compact, size, push, pop, shift, unshift
日期 | date
其他 | default, json

Expand Down
37 changes: 37 additions & 0 deletions docs/source/zh-cn/filters/where_exp.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
---
title: where_exp
---

{% since %}v10.12.0{% endsince %}

从数组中选择所有表达式值为真的对象。下面的例子中,假设你要从产品列表中筛选出来厨房用品。利用 `where_exp` 可以创建一个只包含 `"type"``"kitchen"` 的列表。

Input
```liquid
All products:
{% for product in products %}
- {{ product.title }}
{% endfor %}
{% assign kitchen_products = products | where_exp: "item", "item.type == 'kitchen'" %}
Kitchen products:
{% for product in kitchen_products %}
- {{ product.title }}
{% endfor %}
```

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

[truthy]: ../tutorials/truthy-and-falsy.html
10 changes: 10 additions & 0 deletions src/filters/array.ts
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,16 @@ export function * where<T extends object> (this: FilterImpl, arr: T[], property:
})
}

export function * where_exp<T extends object> (this: FilterImpl, arr: T[], itemName: string, exp: string): IterableIterator<unknown> {
const filtered: unknown[] = []
const keyTemplate = new Value(stringify(exp), this.liquid)
for (const item of toArray(arr)) {
const value = yield keyTemplate.value(new Context({ [itemName]: item }))
if (value) filtered.push(item)
}
return filtered
}

export function * group_by<T extends object> (arr: T[], property: string): IterableIterator<unknown> {
const map = new Map()
arr = toArray(arr)
Expand Down
22 changes: 22 additions & 0 deletions test/integration/filters/array.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -476,6 +476,28 @@ describe('filters/array', function () {
`)
})
})
describe('where_exp', function () {
const products = [
{ title: 'Vacuum', type: 'living room' },
{ title: 'Spatula', type: 'kitchen' },
{ title: 'Television', type: 'living room' },
{ title: 'Garlic press', type: 'kitchen' },
{ title: 'Coffee mug', available: true },
{ title: 'Limited edition sneakers', available: false },
{ title: 'Boring sneakers', available: true }
]
it('should support filter by exp', function () {
return test(`{% assign kitchen_products = products | where_exp: "item", "item.type == 'kitchen'" %}
Kitchen products:
{% for product in kitchen_products -%}
- {{ product.title }}
{% endfor %}`, { products }, `
Kitchen products:
- Spatula
- Garlic press
`)
})
})
describe('group_by', function () {
const members = [
{ graduation_year: 2003, name: 'Jay' },
Expand Down

0 comments on commit dd9bbc5

Please sign in to comment.