function andExpr()

in packages/sqrl/src/ast/AstExpr.ts [187:223]


function andExpr(state: AstExprState, args: Ast[]): Expr {
  let hadFalse = false;
  let hadNull = false;
  const exprs = args
    .map((arg) => _astToExpr(arg, state))
    .filter((expr) => {
      if (expr.type === "constant") {
        if (SqrlObject.isTruthy(expr.value)) {
          // Filter out truthy values
          return false;
        } else if (expr.value === null) {
          hadNull = true;
        } else {
          hadFalse = true;
        }
      }
      return true;
    });

  if (hadFalse) {
    return constantExpr(false);
  } else if (hadNull) {
    return constantExpr(null);
  } else if (exprs.length === 0) {
    return constantExpr(true);
  } else if (exprs.length === 1) {
    return boolExpr(state, exprs[0]);
  }

  const sortedExprs = state.sortExprsByCostAsc(exprs);
  return {
    type: "call",
    func: "_andSequential",
    exprs: [{ type: "state" }, ...sortedExprs],
    load: exprOrderedMinimalLoad(sortedExprs),
  };
}