MCPcopy Create free account
hub / github.com/formkit/formkit / evaluate

Function evaluate

packages/core/src/compiler.ts:464–545  ·  view source on GitHub ↗

* Given a string like '$name==bobby' evaluate it to true or false * @param operand - A left or right boolean operand — usually conditions * @param step - The current order of operation * @param fnToken - The token (string) representation of a function being called * @returns

(
    operand: Operand,
    step: number,
    fnToken?: string,
    tail?: string //eslint-disable-line
  )

Source from the content-addressed store, hash-verified

462 * @returns
463 */
464 function evaluate(
465 operand: Operand,
466 step: number,
467 fnToken?: string,
468 tail?: string //eslint-disable-line
469 ): Operand {
470 if (fnToken) {
471 const fn = evaluate(fnToken, operatorRegistry.length)
472 let userFuncReturn: unknown
473 // "Tail calls" are dot accessors after a function $foo().value. We need
474 // to compile tail calls, and then provide the function result to the
475 // exposed tokens.
476 let tailCall: false | FormKitCompilerOutput = tail
477 ? compile(`$${tail}`)
478 : false
479 if (typeof fn === 'function') {
480 const args = parseArgs(String(operand)).map((arg: string) =>
481 evaluate(arg, -1)
482 )
483 return (tokens: Record<string, any>) => {
484 const userFunc = fn(tokens)
485 if (typeof userFunc !== 'function') {
486 warn(150, fnToken)
487 return userFunc
488 }
489 userFuncReturn = userFunc(
490 ...args.map((arg) =>
491 typeof arg === 'function' ? arg(tokens) : arg
492 )
493 )
494 if (tailCall) {
495 tailCall = tailCall.provide((subTokens) => {
496 const rootTokens = provideTokens(subTokens)
497 const t = subTokens.reduce(
498 (tokenSet: Record<string, any>, token: string) => {
499 const isTail = token === tail || tail?.startsWith(`${token}(`)
500 if (isTail) {
501 const value = getTailValue(userFuncReturn, token)
502 tokenSet[token] = () => value
503 } else {
504 tokenSet[token] = rootTokens[token]
505 }
506 return tokenSet
507 },
508 {} as Record<string, any>
509 )
510 return t
511 })
512 }
513 return tailCall ? tailCall() : (userFuncReturn as string)
514 }
515 }
516 } else if (typeof operand === 'string') {
517 // the word true or false will never contain further operations
518 if (operand === 'true') return true
519 if (operand === 'false') return false
520 if (operand === 'undefined') return undefined
521

Callers 2

parseLogicalsFunction · 0.85
compiler.spec.tsFile · 0.85

Calls 7

parseArgsFunction · 0.90
warnFunction · 0.90
isQuotedStringFunction · 0.90
rmEscapesFunction · 0.90
compileFunction · 0.85
getTailValueFunction · 0.85
parseLogicalsFunction · 0.85

Tested by

no test coverage detected