mirror of
https://github.com/Comfy-Org/ComfyUI_frontend.git
synced 2026-04-29 10:42:44 +00:00
Extract toBoolean helper
This commit is contained in:
@@ -133,16 +133,28 @@ export function parseAST(tokens: Token[]): ASTNode {
|
|||||||
return ast
|
return ast
|
||||||
}
|
}
|
||||||
|
|
||||||
function getNodeRawValue(
|
/**
|
||||||
|
* Converts a ContextValue or undefined to boolean.
|
||||||
|
*/
|
||||||
|
function toBoolean(val: ContextValue | undefined): boolean {
|
||||||
|
if (val === undefined) return false
|
||||||
|
if (typeof val === 'boolean') return val
|
||||||
|
if (typeof val === 'number') return val !== 0
|
||||||
|
if (typeof val === 'string') return val.length > 0
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Retrieves raw value of an AST node for equality checks.
|
||||||
|
*/
|
||||||
|
function getRawValue(
|
||||||
node: ASTNode,
|
node: ASTNode,
|
||||||
getContextKey: (key: string) => ContextValue | undefined
|
getContextKey: (key: string) => ContextValue | undefined
|
||||||
): ContextValue | boolean {
|
): ContextValue | boolean {
|
||||||
if (node.type === 'Literal') {
|
if (node.type === 'Literal') return node.value
|
||||||
return node.value
|
|
||||||
}
|
|
||||||
if (node.type === 'Identifier') {
|
if (node.type === 'Identifier') {
|
||||||
const raw = getContextKey(node.name)
|
const val = getContextKey(node.name)
|
||||||
return raw === undefined ? false : raw
|
return val === undefined ? false : val
|
||||||
}
|
}
|
||||||
return evalAst(node, getContextKey)
|
return evalAst(node, getContextKey)
|
||||||
}
|
}
|
||||||
@@ -159,21 +171,10 @@ export function evalAst(
|
|||||||
getContextKey: (key: string) => ContextValue | undefined
|
getContextKey: (key: string) => ContextValue | undefined
|
||||||
): boolean {
|
): boolean {
|
||||||
switch (node.type) {
|
switch (node.type) {
|
||||||
case 'Literal': {
|
case 'Literal':
|
||||||
const v = node.value
|
return toBoolean(node.value)
|
||||||
if (typeof v === 'boolean') return v
|
case 'Identifier':
|
||||||
if (typeof v === 'string') return v.length > 0
|
return toBoolean(getContextKey(node.name))
|
||||||
if (typeof v === 'number') return v !== 0
|
|
||||||
return false
|
|
||||||
}
|
|
||||||
case 'Identifier': {
|
|
||||||
const raw = getContextKey(node.name)
|
|
||||||
if (raw === undefined) return false
|
|
||||||
if (typeof raw === 'boolean') return raw
|
|
||||||
if (typeof raw === 'string') return raw.length > 0
|
|
||||||
if (typeof raw === 'number') return raw !== 0
|
|
||||||
return false
|
|
||||||
}
|
|
||||||
case 'Unary':
|
case 'Unary':
|
||||||
return !evalAst(node.arg, getContextKey)
|
return !evalAst(node.arg, getContextKey)
|
||||||
case 'Binary': {
|
case 'Binary': {
|
||||||
@@ -183,8 +184,8 @@ export function evalAst(
|
|||||||
const r = evalAst(right, getContextKey)
|
const r = evalAst(right, getContextKey)
|
||||||
return op === '&&' ? l && r : l || r
|
return op === '&&' ? l && r : l || r
|
||||||
}
|
}
|
||||||
const lRaw = getNodeRawValue(left, getContextKey)
|
const lRaw = getRawValue(left, getContextKey)
|
||||||
const rRaw = getNodeRawValue(right, getContextKey)
|
const rRaw = getRawValue(right, getContextKey)
|
||||||
return op === '==' ? lRaw === rRaw : lRaw !== rRaw
|
return op === '==' ? lRaw === rRaw : lRaw !== rRaw
|
||||||
}
|
}
|
||||||
default:
|
default:
|
||||||
|
|||||||
Reference in New Issue
Block a user