mirror of
https://github.com/harttle/liquidjs.git
synced 2026-09-15 04:10:40 -07:00
Compare commits
4
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
2331da37a9 | ||
|
|
52a3059727 | ||
|
|
1e4414950c | ||
|
|
9b0e61249e |
@@ -22,7 +22,7 @@ jobs:
|
|||||||
- name: Setup Node.js
|
- name: Setup Node.js
|
||||||
uses: actions/setup-node@v3
|
uses: actions/setup-node@v3
|
||||||
with:
|
with:
|
||||||
node-version: '14'
|
node-version: '20'
|
||||||
- name: Install dependencies
|
- name: Install dependencies
|
||||||
run: npm ci
|
run: npm ci
|
||||||
- name: Build
|
- name: Build
|
||||||
|
|||||||
@@ -12,7 +12,7 @@ jobs:
|
|||||||
- name: Setup Node.js
|
- name: Setup Node.js
|
||||||
uses: actions/setup-node@v3
|
uses: actions/setup-node@v3
|
||||||
with:
|
with:
|
||||||
node-version: '14'
|
node-version: '20'
|
||||||
- name: Install dependencies
|
- name: Install dependencies
|
||||||
run: npm ci
|
run: npm ci
|
||||||
- name: Test
|
- name: Test
|
||||||
|
|||||||
@@ -12,7 +12,7 @@ jobs:
|
|||||||
- name: Setup Node.js
|
- name: Setup Node.js
|
||||||
uses: actions/setup-node@v3
|
uses: actions/setup-node@v3
|
||||||
with:
|
with:
|
||||||
node-version: '14'
|
node-version: '20'
|
||||||
- name: Build
|
- name: Build
|
||||||
run: |
|
run: |
|
||||||
npm ci
|
npm ci
|
||||||
|
|||||||
@@ -12,7 +12,7 @@ jobs:
|
|||||||
- name: Setup Node.js
|
- name: Setup Node.js
|
||||||
uses: actions/setup-node@v3
|
uses: actions/setup-node@v3
|
||||||
with:
|
with:
|
||||||
node-version: '14'
|
node-version: '20'
|
||||||
- name: Install dependencies
|
- name: Install dependencies
|
||||||
run: npm ci
|
run: npm ci
|
||||||
- name: Lint
|
- name: Lint
|
||||||
|
|||||||
@@ -17,7 +17,7 @@ jobs:
|
|||||||
- name: Setup Node.js
|
- name: Setup Node.js
|
||||||
uses: actions/setup-node@v3
|
uses: actions/setup-node@v3
|
||||||
with:
|
with:
|
||||||
node-version: '14'
|
node-version: '20'
|
||||||
- name: Download artifacts
|
- name: Download artifacts
|
||||||
uses: actions/download-artifact@v3
|
uses: actions/download-artifact@v3
|
||||||
with:
|
with:
|
||||||
|
|||||||
@@ -16,7 +16,7 @@ jobs:
|
|||||||
- name: Setup Node.js
|
- name: Setup Node.js
|
||||||
uses: actions/setup-node@v3
|
uses: actions/setup-node@v3
|
||||||
with:
|
with:
|
||||||
node-version: '14'
|
node-version: '20'
|
||||||
- name: Install dependencies
|
- name: Install dependencies
|
||||||
run: npm ci
|
run: npm ci
|
||||||
- name: Download artifacts
|
- name: Download artifacts
|
||||||
|
|||||||
@@ -13,6 +13,7 @@ node_modules/
|
|||||||
# tmp
|
# tmp
|
||||||
docs/public/js/liquid.browser.min.js
|
docs/public/js/liquid.browser.min.js
|
||||||
docs/themes/navy/layout/partial/all-contributors.swig
|
docs/themes/navy/layout/partial/all-contributors.swig
|
||||||
|
docs/themes/navy/layout/partial/financial-contributors.swig
|
||||||
dist/
|
dist/
|
||||||
demo/*/yarn.json
|
demo/*/yarn.json
|
||||||
|
|
||||||
|
|||||||
@@ -91,6 +91,7 @@ Special thanks to the following sponsors!
|
|||||||
<td align="center" valign="top" width="14.28%"><a href="https://opencollective.com/khaled-salem"><img src="https://images.opencollective.com/khaled-salem/avatar/256.png?height=256" width="100px;" alt="Khaled Salem"/><br /><sub><b>Khaled Salem</b></sub></a></td>
|
<td align="center" valign="top" width="14.28%"><a href="https://opencollective.com/khaled-salem"><img src="https://images.opencollective.com/khaled-salem/avatar/256.png?height=256" width="100px;" alt="Khaled Salem"/><br /><sub><b>Khaled Salem</b></sub></a></td>
|
||||||
<td align="center" valign="top" width="14.28%"><a href="https://sentry.io/"><img src="https://avatars.githubusercontent.com/u/1396951?v=4?s=100" width="100px;" alt="Sentry"/><br /><sub><b>Sentry</b></sub></a></td>
|
<td align="center" valign="top" width="14.28%"><a href="https://sentry.io/"><img src="https://avatars.githubusercontent.com/u/1396951?v=4?s=100" width="100px;" alt="Sentry"/><br /><sub><b>Sentry</b></sub></a></td>
|
||||||
<td align="center" valign="top" width="14.28%"><a href="https://www.checkoutblocks.com/"><img src="https://avatars.githubusercontent.com/u/114603307?v=4?s=100" width="100px;" alt="Checkout Blocks"/><br /><sub><b>Checkout Blocks</b></sub></a></td>
|
<td align="center" valign="top" width="14.28%"><a href="https://www.checkoutblocks.com/"><img src="https://avatars.githubusercontent.com/u/114603307?v=4?s=100" width="100px;" alt="Checkout Blocks"/><br /><sub><b>Checkout Blocks</b></sub></a></td>
|
||||||
|
<td align="center" valign="top" width="14.28%"><a href="https://customer.io/"><img src="https://avatars.githubusercontent.com/u/1152079?v=4?s=100" width="100px;" alt="Customer IO"/><br /><sub><b>Customer IO</b></sub></a></td>
|
||||||
</tr>
|
</tr>
|
||||||
</tbody>
|
</tbody>
|
||||||
</table>
|
</table>
|
||||||
|
|||||||
@@ -11,7 +11,5 @@ sed -i \
|
|||||||
source/tutorials/changelog.md
|
source/tutorials/changelog.md
|
||||||
cp source/tutorials/changelog.md source/zh-cn/tutorials/changelog.md
|
cp source/tutorials/changelog.md source/zh-cn/tutorials/changelog.md
|
||||||
|
|
||||||
sed -i -e '1i\
|
sed -i -e '1i\---\ntitle: Changelog\nauto: true\n---\n' source/tutorials/changelog.md
|
||||||
---\ntitle: Changelog\nauto: true\n---\n' source/tutorials/changelog.md
|
sed -i -e '1i\---\ntitle: 更新日志\nauto: true\n---\n' source/zh-cn/tutorials/changelog.md
|
||||||
sed -i -e '1i\
|
|
||||||
---\ntitle: 更新日志\nauto: true\n---\n' source/zh-cn/tutorials/changelog.md
|
|
||||||
|
|||||||
@@ -10,15 +10,12 @@ sedi() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
# create docs/themes/navy/layout/partial/all-contributors.swig
|
# create docs/themes/navy/layout/partial/all-contributors.swig
|
||||||
cp .all-contributorsrc docs/.all-contributorsrc
|
awk '/ALL-CONTRIBUTORS-LIST:START/{flag=1;next}/ALL-CONTRIBUTORS-LIST:END/{flag=0}flag' README.md | \
|
||||||
sedi \
|
sed 's/<br \/>.*<\/td>/<\/a><\/td>/g' | \
|
||||||
-e 's/README.md/docs\/themes\/navy\/layout\/partial\/all-contributors.swig/g' \
|
sed 's/width="[^"]*"//g' | \
|
||||||
-e 's/"contributorsPerLine": 7/"contributorsPerLine": 65535/g' \
|
tr -d '\n' | \
|
||||||
docs/.all-contributorsrc
|
sed 's/<\/tr>\s*<tr>//g' \
|
||||||
|
> docs/themes/navy/layout/partial/all-contributors.swig
|
||||||
touch docs/themes/navy/layout/partial/all-contributors.swig
|
|
||||||
all-contributors --config docs/.all-contributorsrc generate
|
|
||||||
sedi 's/<br \/>.*<\/td>/<\/a><\/td>/g' docs/themes/navy/layout/partial/all-contributors.swig
|
|
||||||
|
|
||||||
# create docs/themes/navy/layout/partial/financial-contributors.swig
|
# create docs/themes/navy/layout/partial/financial-contributors.swig
|
||||||
awk '/FINANCIAL-CONTRIBUTORS-BEGIN/{flag=1;next}/FINANCIAL-CONTRIBUTORS-END/{flag=0}flag' README.md | \
|
awk '/FINANCIAL-CONTRIBUTORS-BEGIN/{flag=1;next}/FINANCIAL-CONTRIBUTORS-END/{flag=0}flag' README.md | \
|
||||||
|
|||||||
Generated
-1247
File diff suppressed because it is too large
Load Diff
@@ -28,7 +28,6 @@
|
|||||||
"hexo-uglify": "^1.0.0"
|
"hexo-uglify": "^1.0.0"
|
||||||
},
|
},
|
||||||
"devDependencies": {
|
"devDependencies": {
|
||||||
"all-contributors-cli": "^6.8.1",
|
|
||||||
"eslint": "^6.0.1",
|
"eslint": "^6.0.1",
|
||||||
"eslint-config-hexo": "^4.0.0"
|
"eslint-config-hexo": "^4.0.0"
|
||||||
},
|
},
|
||||||
|
|||||||
@@ -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
|
||||||
@@ -1 +0,0 @@
|
|||||||
<table> <tbody> <tr> <td align="center" valign="top" ><a href="https://www.opensense.com/"><img src="https://images.opencollective.com/opensense-inc/bf840ae/logo/256.png?height=100" alt="Opensense Inc."/></a></td> <td align="center" valign="top" ><a href="https://www.11ty.dev/"><img src="https://avatars.githubusercontent.com/u/35147177?v=4?s=100" alt="Eleventy"/></a></td> <td align="center" valign="top" ><a href="http://about.me/peterdehaan"><img src="https://avatars2.githubusercontent.com/u/557895?v=4?s=100" alt="Peter deHaan"/></a></td> <td align="center" valign="top" ><a href="https://opencollective.com/touchless"><img src="https://images.opencollective.com/touchless/273bc74/logo/256.png?height=100" alt="Touchless"/></a></td> <td align="center" valign="top" ><a href="https://www.dropkiq.com/"><img src="https://images.opencollective.com/1bertlol/43a8ea8/logo/256.png?height=100" alt="Adam Darrah"/></a></td> <td align="center" valign="top" ><a href="https://dailycontributors.com/"><img src="https://images.opencollective.com/dailycontributors/3c2e057/logo/256.png?height=100&width=100" alt="Dailycontributors"/></a></td> <td align="center" valign="top" ><a href="https://github.com/coni2k"><img src="https://avatars0.githubusercontent.com/u/1284601?v=4?s=100" alt="coni2k"/></a></td> <td align="center" valign="top" ><a href="https://github.com/amit777"><img src="https://avatars0.githubusercontent.com/u/2703309?v=4?s=100" alt="amit777"/></a></td> <td align="center" valign="top" ><a href="https://opencollective.com/khaled-salem"><img src="https://images.opencollective.com/khaled-salem/avatar/256.png?height=256" alt="Khaled Salem"/></a></td> <td align="center" valign="top" ><a href="https://sentry.io/"><img src="https://avatars.githubusercontent.com/u/1396951?v=4?s=100" alt="Sentry"/></a></td> <td align="center" valign="top" ><a href="https://www.checkoutblocks.com/"><img src="https://avatars.githubusercontent.com/u/114603307?v=4?s=100" alt="Checkout Blocks"/></a></td> </tr> </tbody></table>
|
|
||||||
@@ -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)
|
||||||
|
|||||||
+2
-1
@@ -1,4 +1,5 @@
|
|||||||
import { ValueToken, Liquid, toValue, evalToken, Value, Emitter, TagToken, TopLevelToken, Context, Template, Tag, ParseStream } from '..'
|
import { ValueToken, Liquid, toValue, evalToken, Value, Emitter, TagToken, TopLevelToken, Context, Template, Tag, ParseStream } from '..'
|
||||||
|
import { equals } from '../render'
|
||||||
|
|
||||||
export default class extends Tag {
|
export default class extends Tag {
|
||||||
value: Value
|
value: Value
|
||||||
@@ -58,7 +59,7 @@ export default class extends Tag {
|
|||||||
for (const branch of this.branches) {
|
for (const branch of this.branches) {
|
||||||
for (const valueToken of branch.values) {
|
for (const valueToken of branch.values) {
|
||||||
const value = yield evalToken(valueToken, ctx, ctx.opts.lenientIf)
|
const value = yield evalToken(valueToken, ctx, ctx.opts.lenientIf)
|
||||||
if (target === value) {
|
if (equals(target, value)) {
|
||||||
yield r.renderTemplates(branch.templates, ctx, emitter)
|
yield r.renderTemplates(branch.templates, ctx, emitter)
|
||||||
branchHit = true
|
branchHit = true
|
||||||
break
|
break
|
||||||
|
|||||||
@@ -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' },
|
||||||
|
|||||||
@@ -119,4 +119,17 @@ describe('tags/case', function () {
|
|||||||
'{%- endcase %}', {})
|
'{%- endcase %}', {})
|
||||||
expect(result).toEqual('show this')
|
expect(result).toEqual('show this')
|
||||||
})
|
})
|
||||||
|
it('should apply value equal for arrays', () => {
|
||||||
|
const engine = new Liquid()
|
||||||
|
const result = engine.parseAndRenderSync(`
|
||||||
|
{%- assign x = "a,b,c" | split: "," %}
|
||||||
|
{%- assign y = "a,b,c" | split: "," %}
|
||||||
|
{% case x %}{% when y %}TRUE{% else %}FALSE{% endcase %}
|
||||||
|
{% if x == y %}TRUE{% else %}FALSE{% endif %}
|
||||||
|
`)
|
||||||
|
expect(result).toEqual(`
|
||||||
|
TRUE
|
||||||
|
TRUE
|
||||||
|
`)
|
||||||
|
})
|
||||||
})
|
})
|
||||||
|
|||||||
Reference in New Issue
Block a user