Implement lenientIf option (resolve #265; default off)

See description of the option in liquid-options.ts.
This commit is contained in:
sschuldenzucker
2020-12-06 22:58:08 +08:00
committed by Jun Yang
parent c920ebb282
commit 768fb79e32
5 changed files with 62 additions and 7 deletions
+5 -1
View File
@@ -4,6 +4,7 @@ import { FilterMap } from '../template/filter/filter-map'
import { Filter } from './filter/filter'
import { Context } from '../context/context'
import { ValueToken } from '../tokens/value-token'
import { assert } from '../util/assert'
export class Value {
public readonly filters: Filter[] = []
@@ -18,7 +19,10 @@ export class Value {
this.filters = tokenizer.readFilters().map(({ name, args }) => new Filter(name, this.filterMap.get(name), args))
}
public * value (ctx: Context) {
let val = yield evalToken(this.initial, ctx)
assert(ctx, () => 'unable to evaluate: context not defined')
const lenient = ctx.opts.lenientIf && this.filters.length > 0 && this.filters[0].name == "default"
let val = yield evalToken(this.initial, ctx, lenient)
for (const filter of this.filters) {
val = yield filter.render(val, ctx)
}