mirror of
https://github.com/harttle/liquidjs.git
synced 2026-09-16 21:00:40 -07:00
feat: support not operator, #575
This commit is contained in:
+10
-10
@@ -1,8 +1,8 @@
|
||||
import { RangeToken, OperatorToken, Token, LiteralToken, NumberToken, PropertyAccessToken, QuotedToken } from '../tokens'
|
||||
import { RangeToken, OperatorToken, Token, LiteralToken, NumberToken, PropertyAccessToken, QuotedToken, OperatorType, operatorTypes } from '../tokens'
|
||||
import { isQuotedToken, isWordToken, isNumberToken, isLiteralToken, isRangeToken, isPropertyAccessToken, UndefinedVariableError, range, isOperatorToken, literalValues, assert } from '../util'
|
||||
import { parseStringLiteral } from '../parser'
|
||||
import { Context } from '../context'
|
||||
import { Operators } from '../render'
|
||||
import type { Context } from '../context'
|
||||
import type { UnaryOperatorHandler } from '../render'
|
||||
|
||||
export class Expression {
|
||||
private postfix: Token[]
|
||||
@@ -16,8 +16,13 @@ export class Expression {
|
||||
for (const token of this.postfix) {
|
||||
if (isOperatorToken(token)) {
|
||||
const r = operands.pop()
|
||||
const l = operands.pop()
|
||||
const result = yield evalOperatorToken(ctx.opts.operators, token, l, r, ctx)
|
||||
let result
|
||||
if (operatorTypes[token.operator] === OperatorType.Unary) {
|
||||
result = yield (ctx.opts.operators[token.operator] as UnaryOperatorHandler)(r, ctx)
|
||||
} else {
|
||||
const l = operands.pop()
|
||||
result = yield ctx.opts.operators[token.operator](l, r, ctx)
|
||||
}
|
||||
operands.push(result)
|
||||
} else {
|
||||
operands.push(yield evalToken(token, ctx, lenient && this.postfix.length === 1))
|
||||
@@ -58,11 +63,6 @@ export function evalQuotedToken (token: QuotedToken) {
|
||||
return parseStringLiteral(token.getText())
|
||||
}
|
||||
|
||||
function evalOperatorToken (operators: Operators, token: OperatorToken, lhs: any, rhs: any, ctx: Context) {
|
||||
const impl = operators[token.operator]
|
||||
return impl(lhs, rhs, ctx)
|
||||
}
|
||||
|
||||
function evalLiteralToken (token: LiteralToken) {
|
||||
return literalValues[token.literal]
|
||||
}
|
||||
|
||||
@@ -1,9 +1,11 @@
|
||||
import { isComparable } from '../drop/comparable'
|
||||
import { Context } from '../context'
|
||||
import { isFunction, toValue } from '../util'
|
||||
import { isTruthy } from '../render/boolean'
|
||||
import { isFalsy, isTruthy } from '../render/boolean'
|
||||
|
||||
export type OperatorHandler = (lhs: any, rhs: any, ctx: Context) => boolean;
|
||||
export type UnaryOperatorHandler = (operand: any, ctx: Context) => boolean;
|
||||
export type BinaryOperatorHandler = (lhs: any, rhs: any, ctx: Context) => boolean;
|
||||
export type OperatorHandler = UnaryOperatorHandler | BinaryOperatorHandler;
|
||||
export type Operators = Record<string, OperatorHandler>
|
||||
|
||||
export const defaultOperators: Operators = {
|
||||
@@ -42,6 +44,7 @@ export const defaultOperators: Operators = {
|
||||
r = toValue(r)
|
||||
return l && isFunction(l.indexOf) ? l.indexOf(r) > -1 : false
|
||||
},
|
||||
'not': (v: any, ctx: Context) => isFalsy(toValue(v), ctx),
|
||||
'and': (l: any, r: any, ctx: Context) => isTruthy(toValue(l), ctx) && isTruthy(toValue(r), ctx),
|
||||
'or': (l: any, r: any, ctx: Context) => isTruthy(toValue(l), ctx) || isTruthy(toValue(r), ctx)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user