UNPKG

@connectv/core

Version:

agent-based reactive programming library for typescript/javascript

75 lines 2.44 kB
import { filter } 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 { /** * * @param predicate the predicate function to pass or fail incoming values against. * */ constructor(predicate) { super({ inputs: ['value'], outputs: ['pass', 'fail'] }); this.predicate = predicate; if (predicate.length <= 1) this.core = this.input.to(map((v) => [v, predicate(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. * */ get input() { 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. * */ get pass() { 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. * */ get fail() { return this.out('fail'); } 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)); } } createEntries() { return [this.input]; } createExits() { return [this.pass, this.fail]; } } /** * * Creates a [check](https://connective.dev/docs/check) agent. A check agent * will pass or fail incoming values based on given predicate, passing them through * the corresponding outputs. * [Checkout the docs](https://connective.dev/docs/check) for examples and further information. * * @param func the predicate to test incoming values against * */ export function check(func) { return new Check(func); } export default check; //# sourceMappingURL=check.js.map