import { PinLike } from '../pin/pin-like' ; import { filter , FilterFunc , FilterFuncSync , FilterFuncAsync } from '../pin/filter' ; import { map } from '../pin/map' ; import { Agent } from './agent' ; /**
 *
 * Represents [check](https://connective.dev/docs/check) agents.
 *
 */ export class Check extends Agent { private core : PinLike ; param predicate constructor ( readonly predicate ) { super ( { inputs : [ 'value' ] , outputs : [ 'pass' , 'fail' ] } ) ; if ( predicate . length <= 1 ) this . core = this . input . to ( map ( ( v ) => [ v , ( predicate as FilterFuncSync ) ( v ) ] ) ) ; else this . core = this . input . to ( map ( ( v , done , error , context ) => predicate ( v , res => done ( [ v , res ] ) , error , context ) ) ) ; } /**
     *
     * Shortcut for `.in('value')`, the main value input for this check.
     * [Read this](https://connective.dev/docs/check#signature) for more details.
     *
     */ public get input ( ) : PinLike { return this . in ( 'value' ) ; } /**
     *
     * Shortcut for `.out('pass')`, the output for values passing the criteria outline by given predicate.
     * [Read this](https://connective.dev/docs/check#signature) for more details.
     *
     */ public get pass ( ) : PinLike { return this . out ( 'pass' ) ; } /**
     *
     * Shortcut for `.out('fail')`, the output for values failing the criteria outline by given predicate.
     * [Read this](https://connective.dev/docs/check#signature) for more details.
     *
     */ public get fail ( ) : PinLike { return this . out ( 'fail' ) ; } protected createOutput ( label ) { this . checkOutput ( label ) ; if ( label == 'pass' ) { return this . core . to ( filter ( ( [ _ , v ] ) => v ) ) . to ( map ( ( [ v , _ ] ) => v ) ) ; } else { return this . core . to ( filter ( ( [ _ , v ] ) => ! v ) ) . to ( map ( ( [ v , _ ] ) => v ) ) ; } } protected createEntries ( ) { return [ this . input ] ; } protected createExits ( ) { return [ this . pass , this . fail ] ; } } param func export function check ( func ) { return new Check ( func ) ; } export default check ;