mirror of
https://github.com/harttle/liquidjs.git
synced 2026-09-15 04:10:40 -07:00
feat: support allow_false for default filter, see #435
This commit is contained in:
@@ -44,4 +44,23 @@ Output
|
||||
2.99
|
||||
```
|
||||
|
||||
## Allowing `false`
|
||||
|
||||
{% since %}v9.32.0{% endsince %}
|
||||
|
||||
To allow variables to return `false` instead of the default value, you can use the `allow_false` parameter.
|
||||
|
||||
Input
|
||||
|
||||
```liquid
|
||||
{% assign display_price = false %}
|
||||
{{ display_price | default: true, allow_false: true }}
|
||||
```
|
||||
|
||||
Output
|
||||
|
||||
```text
|
||||
false
|
||||
```
|
||||
|
||||
[falsy]: ../tutorials/truthy-and-falsy.html
|
||||
|
||||
@@ -42,4 +42,23 @@ title: default
|
||||
2.99
|
||||
```
|
||||
|
||||
## 允许 `false`
|
||||
|
||||
{% since %}v9.32.0{% endsince %}
|
||||
|
||||
为了允许让 `false` 直接输出而不是用默认值,可以用 `allow_false` 参数。
|
||||
|
||||
输入
|
||||
|
||||
```liquid
|
||||
{% assign display_price = false %}
|
||||
{{ display_price | default: true, allow_false: true }}
|
||||
```
|
||||
|
||||
输出
|
||||
|
||||
```text
|
||||
false
|
||||
```
|
||||
|
||||
[falsy]: ../tutorials/truthy-and-falsy.html
|
||||
|
||||
@@ -2,10 +2,13 @@ import { isFalsy } from '../../render/boolean'
|
||||
import { isArray, isString, toValue } from '../../util/underscore'
|
||||
import { FilterImpl } from '../../template/filter/filter-impl'
|
||||
|
||||
export function Default<T1, T2> (this: FilterImpl, v: string | T1, arg: T2): string | T1 | T2 {
|
||||
if (isArray(v) || isString(v)) return v.length ? v : arg
|
||||
return isFalsy(toValue(v), this.context) ? arg : v
|
||||
export function Default<T1 extends boolean, T2> (this: FilterImpl, value: T1, defaultValue: T2, ...args: Array<[string, any]>): T1 | T2 {
|
||||
if (isArray(value) || isString(value)) return value.length ? value : defaultValue
|
||||
value = toValue(value)
|
||||
if (value === false && (new Map(args)).get('allow_false')) return false as T1
|
||||
return isFalsy(value, this.context) ? defaultValue : value
|
||||
}
|
||||
export function json (v: any) {
|
||||
return JSON.stringify(v)
|
||||
|
||||
export function json (value: any) {
|
||||
return JSON.stringify(value)
|
||||
}
|
||||
|
||||
@@ -12,6 +12,9 @@ describe('filters/object', function () {
|
||||
it('undefined should use default', async () => expect(await liquid.parseAndRender('{{not_defined | default: "a"}}')).to.equal('a'))
|
||||
it('true should not use default', async () => expect(await liquid.parseAndRender('{{true | default: "a"}}')).to.equal('true'))
|
||||
it('0 should not use default', async () => expect(await liquid.parseAndRender('{{0 | default: "a"}}')).to.equal('0'))
|
||||
it('should output false when allow_false=true', async () => expect(await liquid.parseAndRender('{{false | default: true, allow_false: true}}')).to.equal('false'))
|
||||
it('should output default without allow_false', async () => expect(await liquid.parseAndRender('{{false | default: true}}')).to.equal('true'))
|
||||
it('should output default when allow_false=false', async () => expect(await liquid.parseAndRender('{{false | default: true, allow_false: false}}')).to.equal('true'))
|
||||
})
|
||||
describe('json', function () {
|
||||
it('should stringify string', async () => expect(await liquid.parseAndRender('{{"foo" | json}}')).to.equal('"foo"'))
|
||||
|
||||
Reference in New Issue
Block a user