function orExpr()

in packages/sqrl/src/ast/AstExpr.ts [225:261]


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

  if (hadTrue) {
    return constantExpr(true);
  } else if (exprs.length === 0) {
    return constantExpr(hadNull ? null : false);
  } else if (hadNull) {
    // If we saw a null but still have other values, add it back
    exprs.push(constantExpr(null));
  } else if (exprs.length === 1) {
    return boolExpr(state, exprs[0]);
  }

  const sortedExprs = state.sortExprsByCostAsc(exprs);
  return makeCall(
    "_orSequential",
    [{ type: "state" }, ...sortedExprs],
    exprOrderedMinimalLoad(sortedExprs)
  );
}