import { CallExpr , Expr } from "../Expr" ; import { ExprEvaluatorContext , OperatorDescriptorMap } from "../ExprEvaluator" ; function conditionalCast ( context , type , args ) { switch ( type ) { case "boolean" : case "number" : case "string" : for ( const childExpr of args ) { const value = context . evaluate ( childExpr ) ; if ( typeof value === type ) { return value ; } } throw new Error ( `expected a '${ type }'` ) ; default : throw new Error ( `invalid type '${ type }'` ) ; } } const operators = { all : { call : ( context , call ) => { for ( const childExpr of call . args ) { if ( ! context . evaluate ( childExpr ) ) { return false ; } } return true ; } } , any : { call : ( context , call ) => { for ( const childExpr of call . args ) { if ( context . evaluate ( childExpr ) ) { return true ; } } return false ; } } , none : { call : ( context , call ) => { for ( const childExpr of call . args ) { if ( context . evaluate ( childExpr ) ) { return false ; } } return true ; } } , boolean : { call : ( context , call ) => { return conditionalCast ( context , "boolean" , call . args ) ; } } , number : { call : ( context , call ) => { return conditionalCast ( context , "number" , call . args ) ; } } , string : { call : ( context , call ) => { return conditionalCast ( context , "string" , call . args ) ; } } } ; export const FlowOperators = operators ; export type FlowOperatorNames = keyof typeof operators ;