mirror of
https://github.com/harttle/liquidjs.git
synced 2026-09-15 20:30:39 -07:00
Added jsTruthy flag to constuctor in order to enable JS style truthiness evaluation
This commit is contained in:
+13
-4
@@ -1,6 +1,15 @@
|
||||
export function isTruthy (val: any): boolean {
|
||||
return !isFalsy(val)
|
||||
import { Context } from '../context/context'
|
||||
|
||||
export function isTruthy (val: any, ctx: Context): boolean {
|
||||
if ((val === null) && ctx && ctx.opts && ctx.opts.jsTruthy ) return false;
|
||||
return !isFalsy(val, ctx)
|
||||
}
|
||||
export function isFalsy (val: any): boolean {
|
||||
return val === false || undefined === val || val === null
|
||||
|
||||
export function isFalsy (val: any, ctx: Context): boolean {
|
||||
if(ctx && ctx.opts && ctx.opts.jsTruthy) {
|
||||
return val == false
|
||||
}
|
||||
else {
|
||||
return val === false || undefined === val || val === null
|
||||
}
|
||||
}
|
||||
|
||||
@@ -26,7 +26,7 @@ export class Expression {
|
||||
if (TypeGuards.isOperatorToken(token)) {
|
||||
const r = this.operands.pop()
|
||||
const l = this.operands.pop()
|
||||
const result = evalOperatorToken(token, l, r)
|
||||
const result = evalOperatorToken(token, l, r, ctx)
|
||||
this.operands.push(result)
|
||||
} else {
|
||||
this.operands.push(evalToken(token, ctx))
|
||||
@@ -62,9 +62,9 @@ export function evalQuotedToken (token: QuotedToken) {
|
||||
return parseStringLiteral(token.getText())
|
||||
}
|
||||
|
||||
function evalOperatorToken (token: OperatorToken, lhs: any, rhs: any) {
|
||||
function evalOperatorToken (token: OperatorToken, lhs: any, rhs: any, ctx: Context) {
|
||||
const impl = operatorImpls[token.operator]
|
||||
return impl(lhs, rhs)
|
||||
return impl(lhs, rhs, ctx)
|
||||
}
|
||||
|
||||
function evalLiteralToken (token: LiteralToken) {
|
||||
|
||||
@@ -1,8 +1,10 @@
|
||||
import { isComparable } from '../drop/comparable'
|
||||
import { Context } from '../context/context'
|
||||
import { isFunction } from '../util/underscore'
|
||||
import { isTruthy } from '../render/boolean'
|
||||
|
||||
export const operatorImpls: {[key: string]: (lhs: any, rhs: any) => boolean} = {
|
||||
|
||||
export const operatorImpls: {[key: string]: (lhs: any, rhs: any, ctx: Context) => boolean} = {
|
||||
'==': (l: any, r: any) => {
|
||||
if (isComparable(l)) return l.equals(r)
|
||||
if (isComparable(r)) return r.equals(l)
|
||||
@@ -36,6 +38,6 @@ export const operatorImpls: {[key: string]: (lhs: any, rhs: any) => boolean} = {
|
||||
'contains': (l: any, r: any) => {
|
||||
return l && isFunction(l.indexOf) ? l.indexOf(r) > -1 : false
|
||||
},
|
||||
'and': (l: any, r: any) => isTruthy(l) && isTruthy(r),
|
||||
'or': (l: any, r: any) => isTruthy(l) || isTruthy(r)
|
||||
'and': (l: any, r: any, ctx: Context) => isTruthy(l, ctx) && isTruthy(r, ctx),
|
||||
'or': (l: any, r: any, ctx: Context) => isTruthy(l, ctx) || isTruthy(r, ctx)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user