-
-
Notifications
You must be signed in to change notification settings - Fork 231
/
value.ts
28 lines (25 loc) · 1.04 KB
/
value.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
import { Expression } from '../render/expression'
import { Tokenizer } from '../parser/tokenizer'
import { Filter } from './filter/filter'
import { Context } from '../context/context'
import { Liquid } from '../liquid'
export class Value {
public readonly filters: Filter[] = []
public readonly initial: Expression
/**
* @param str the value to be valuated, eg.: "foobar" | truncate: 3
*/
public constructor (str: string, liquid: Liquid) {
const tokenizer = new Tokenizer(str, liquid.options.operators)
this.initial = tokenizer.readExpression()
this.filters = tokenizer.readFilters().map(({ name, args }) => new Filter(name, liquid.filters.get(name), args, liquid))
}
public * value (ctx: Context, lenient: boolean): Generator<unknown, unknown, unknown> {
lenient = lenient || (ctx.opts.lenientIf && this.filters.length > 0 && this.filters[0].name === 'default')
let val = yield this.initial.evaluate(ctx, lenient)
for (const filter of this.filters) {
val = yield filter.render(val, ctx)
}
return val
}
}