feat: introduce where_exp filter from Jekyll

This commit is contained in:
Yang Jun
2024-04-28 23:17:01 +08:00
committed by Jun Yang
parent 303a0d44b5
commit 8c7cef9f95
7 changed files with 109 additions and 2 deletions
+1
View File
@@ -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
+1 -1
View File
@@ -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
+37
View File
@@ -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
+1 -1
View File
@@ -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
+37
View File
@@ -0,0 +1,37 @@
---
title: where_exp
---
{% since %}v10.12.0{% endsince %}
从数组中选择所有表达式值为真的对象。下面的例子中,假设你要从产品列表中筛选出来厨房用品。利用 `where_exp` 可以创建一个只包含 `"type"``"kitchen"` 的列表。
输入
```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 %}
```
输出
```text
All products:
- Vacuum
- Spatula
- Television
- Garlic press
Kitchen products:
- Spatula
- Garlic press
```
[truthy]: ../tutorials/truthy-and-falsy.html
+10
View File
@@ -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)
+22
View File
@@ -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' },