mirror of
https://github.com/harttle/liquidjs.git
synced 2026-09-16 04:40:39 -07:00
feat: add operators option for custom operators
This commit is contained in:
committed by
Jun Yang
parent
5901611af5
commit
75591cdbfc
@@ -26,10 +26,10 @@ export default {
|
||||
|
||||
render: function * (ctx: Context, emitter: Emitter) {
|
||||
const r = this.liquid.renderer
|
||||
const cond = yield new Expression(this.cond).value(ctx)
|
||||
const cond = yield new Expression(this.cond, this.liquid.options.operators).value(ctx)
|
||||
for (let i = 0; i < this.cases.length; i++) {
|
||||
const branch = this.cases[i]
|
||||
const val = yield new Expression(branch.val).value(ctx)
|
||||
const val = yield new Expression(branch.val, this.liquid.options.operators).value(ctx)
|
||||
if (val === cond) {
|
||||
yield r.renderTemplates(branch.templates, ctx, emitter)
|
||||
return
|
||||
|
||||
@@ -31,7 +31,7 @@ export default {
|
||||
const r = this.liquid.renderer
|
||||
|
||||
for (const branch of this.branches) {
|
||||
const cond = yield new Expression(branch.cond, ctx.opts.lenientIf).value(ctx)
|
||||
const cond = yield new Expression(branch.cond, this.liquid.options.operators, ctx.opts.lenientIf).value(ctx)
|
||||
if (isTruthy(cond, ctx)) {
|
||||
yield r.renderTemplates(branch.templates, ctx, emitter)
|
||||
return
|
||||
|
||||
@@ -29,7 +29,7 @@ export default {
|
||||
|
||||
render: function * (ctx: Context, emitter: Emitter) {
|
||||
const r = this.liquid.renderer
|
||||
const cond = yield new Expression(this.cond, ctx.opts.lenientIf).value(ctx)
|
||||
const cond = yield new Expression(this.cond, this.liquid.options.operators, ctx.opts.lenientIf).value(ctx)
|
||||
|
||||
if (isFalsy(cond, ctx)) {
|
||||
yield r.renderTemplates(this.templates, ctx, emitter)
|
||||
@@ -37,7 +37,7 @@ export default {
|
||||
}
|
||||
|
||||
for (const branch of this.branches) {
|
||||
const cond = yield new Expression(branch.cond, ctx.opts.lenientIf).value(ctx)
|
||||
const cond = yield new Expression(branch.cond, this.liquid.options.operators, ctx.opts.lenientIf).value(ctx)
|
||||
if (isTruthy(cond, ctx)) {
|
||||
yield r.renderTemplates(branch.templates, ctx, emitter)
|
||||
return
|
||||
|
||||
@@ -3,6 +3,7 @@ import { Template } from './template/template'
|
||||
import { Cache } from './cache/cache'
|
||||
import { LRU } from './cache/lru'
|
||||
import { FS } from './fs/fs'
|
||||
import { operatorImpls, OperatorMap } from './render/operator'
|
||||
|
||||
export interface LiquidOptions {
|
||||
/** A directory or an array of directories from where to resolve layout and include templates, and the filename passed to `.renderFile()`. If it's an array, the files are looked up in the order they occur in the array. Defaults to `["."]` */
|
||||
@@ -47,6 +48,8 @@ export interface LiquidOptions {
|
||||
globals?: object;
|
||||
/** Whether or not to keep value type when writing the Output. Defaults to `false`. */
|
||||
keepOutputType?: boolean;
|
||||
/** An object of operators for conditional statements. Defaults to the regular Liquid operators. */
|
||||
operators?: OperatorMap;
|
||||
}
|
||||
|
||||
interface NormalizedOptions extends LiquidOptions {
|
||||
@@ -75,6 +78,7 @@ export interface NormalizedFullOptions extends NormalizedOptions {
|
||||
greedy: boolean;
|
||||
globals: object;
|
||||
keepOutputType: boolean;
|
||||
operators: OperatorMap;
|
||||
}
|
||||
|
||||
export const defaultOptions: NormalizedFullOptions = {
|
||||
@@ -97,7 +101,8 @@ export const defaultOptions: NormalizedFullOptions = {
|
||||
strictVariables: false,
|
||||
lenientIf: false,
|
||||
globals: {},
|
||||
keepOutputType: false
|
||||
keepOutputType: false,
|
||||
operators: operatorImpls
|
||||
}
|
||||
|
||||
export function normalize (options?: LiquidOptions): NormalizedOptions {
|
||||
|
||||
@@ -11,25 +11,27 @@ import { parseStringLiteral } from '../parser/parse-string-literal'
|
||||
import { Context } from '../context/context'
|
||||
import { range, toValue } from '../util/underscore'
|
||||
import { Tokenizer } from '../parser/tokenizer'
|
||||
import { operatorImpls } from '../render/operator'
|
||||
import { OperatorMap } from '../render/operator'
|
||||
import { UndefinedVariableError, InternalUndefinedVariableError } from '../util/error'
|
||||
|
||||
export class Expression {
|
||||
private operands: any[] = []
|
||||
private postfix: Token[]
|
||||
private lenient: boolean
|
||||
private operators: OperatorMap
|
||||
|
||||
public constructor (str: string, lenient = false) {
|
||||
public constructor (str: string, operators: OperatorMap, lenient = false) {
|
||||
const tokenizer = new Tokenizer(str)
|
||||
this.postfix = [...toPostfix(tokenizer.readExpression())]
|
||||
this.lenient = lenient
|
||||
this.operators = operators
|
||||
}
|
||||
public evaluate (ctx: Context): any {
|
||||
for (const token of this.postfix) {
|
||||
if (TypeGuards.isOperatorToken(token)) {
|
||||
const r = this.operands.pop()
|
||||
const l = this.operands.pop()
|
||||
const result = evalOperatorToken(token, l, r, ctx)
|
||||
const result = evalOperatorToken(this.operators, token, l, r, ctx)
|
||||
this.operands.push(result)
|
||||
} else {
|
||||
this.operands.push(evalToken(token, ctx, this.lenient && this.postfix.length === 1))
|
||||
@@ -73,8 +75,8 @@ export function evalQuotedToken (token: QuotedToken) {
|
||||
return parseStringLiteral(token.getText())
|
||||
}
|
||||
|
||||
function evalOperatorToken (token: OperatorToken, lhs: any, rhs: any, ctx: Context) {
|
||||
const impl = operatorImpls[token.operator]
|
||||
function evalOperatorToken (operators: OperatorMap, token: OperatorToken, lhs: any, rhs: any, ctx: Context) {
|
||||
const impl = operators[token.operator]
|
||||
return impl(lhs, rhs, ctx)
|
||||
}
|
||||
|
||||
|
||||
@@ -3,7 +3,11 @@ import { Context } from '../context/context'
|
||||
import { isFunction } from '../util/underscore'
|
||||
import { isTruthy } from '../render/boolean'
|
||||
|
||||
export const operatorImpls: {[key: string]: (lhs: any, rhs: any, ctx: Context) => boolean} = {
|
||||
export interface OperatorMap {
|
||||
[key: string]: (lhs: any, rhs: any, ctx: Context) => boolean;
|
||||
}
|
||||
|
||||
export const operatorImpls: OperatorMap = {
|
||||
'==': (l: any, r: any) => {
|
||||
if (isComparable(l)) return l.equals(r)
|
||||
if (isComparable(r)) return r.equals(l)
|
||||
|
||||
Reference in New Issue
Block a user