mirror of
https://github.com/harttle/liquidjs.git
synced 2026-09-16 12:50:38 -07:00
feat: passing liquid to FilterImpl, closes #277
This commit is contained in:
@@ -5,6 +5,7 @@ import { Filter } from './filter/filter'
|
||||
import { Context } from '../context/context'
|
||||
import { ValueToken } from '../tokens/value-token'
|
||||
import { assert } from '../util/assert'
|
||||
import { Liquid } from '../liquid'
|
||||
|
||||
export class Value {
|
||||
public readonly filters: Filter[] = []
|
||||
@@ -13,15 +14,15 @@ export class Value {
|
||||
/**
|
||||
* @param str the value to be valuated, eg.: "foobar" | truncate: 3
|
||||
*/
|
||||
public constructor (str: string, private readonly filterMap: FilterMap) {
|
||||
public constructor (str: string, private readonly filterMap: FilterMap, liquid: Liquid) {
|
||||
const tokenizer = new Tokenizer(str)
|
||||
this.initial = tokenizer.readValue()
|
||||
this.filters = tokenizer.readFilters().map(({ name, args }) => new Filter(name, this.filterMap.get(name), args))
|
||||
this.filters = tokenizer.readFilters().map(({ name, args }) => new Filter(name, this.filterMap.get(name), args, liquid))
|
||||
}
|
||||
public * value (ctx: Context) {
|
||||
assert(ctx, () => 'unable to evaluate: context not defined')
|
||||
const lenient = ctx.opts.lenientIf && this.filters.length > 0 && this.filters[0].name == "default"
|
||||
|
||||
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)
|
||||
|
||||
Reference in New Issue
Block a user