mirror of
https://github.com/harttle/liquidjs.git
synced 2026-09-16 21:00:40 -07:00
feat: introduce where_exp filter from Jekyll
This commit is contained in:
@@ -88,6 +88,7 @@ filters:
|
|||||||
url_decode: url_decode.html
|
url_decode: url_decode.html
|
||||||
url_encode: url_encode.html
|
url_encode: url_encode.html
|
||||||
where: where.html
|
where: where.html
|
||||||
|
where_exp: where_exp.html
|
||||||
|
|
||||||
tags:
|
tags:
|
||||||
overview: overview.html
|
overview: overview.html
|
||||||
|
|||||||
@@ -12,7 +12,7 @@ Categories | Filters
|
|||||||
Math | plus, minus, modulo, times, floor, ceil, round, divided_by, abs, at_least, at_most
|
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
|
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
|
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
|
Date | date
|
||||||
Misc | default, json, raw
|
Misc | default, json, raw
|
||||||
|
|
||||||
|
|||||||
@@ -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
|
||||||
@@ -12,7 +12,7 @@ LiquidJS 共支持 40+ 个过滤器,可以分为如下几类:
|
|||||||
数学 | plus, minus, modulo, times, floor, ceil, round, divided_by, abs, at_least, at_most
|
数学 | 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
|
字符串 | 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
|
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
|
日期 | date
|
||||||
其他 | default, json
|
其他 | default, json
|
||||||
|
|
||||||
|
|||||||
@@ -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
|
||||||
@@ -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> {
|
export function * group_by<T extends object> (arr: T[], property: string): IterableIterator<unknown> {
|
||||||
const map = new Map()
|
const map = new Map()
|
||||||
arr = toArray(arr)
|
arr = toArray(arr)
|
||||||
|
|||||||
@@ -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 () {
|
describe('group_by', function () {
|
||||||
const members = [
|
const members = [
|
||||||
{ graduation_year: 2003, name: 'Jay' },
|
{ graduation_year: 2003, name: 'Jay' },
|
||||||
|
|||||||
Reference in New Issue
Block a user