@connectv/core
Version:
agent-based reactive programming library for typescript/javascript
121 lines • 4.28 kB
JavaScript
;
var __extends = (this && this.__extends) || (function () {
var extendStatics = function (d, b) {
extendStatics = Object.setPrototypeOf ||
({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; };
return extendStatics(d, b);
};
return function (d, b) {
extendStatics(d, b);
function __() { this.constructor = d; }
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
};
})();
Object.defineProperty(exports, "__esModule", { value: true });
var filter_1 = require("../pin/filter");
var map_1 = require("../pin/map");
var agent_1 = require("./agent");
/**
*
* Represents [check](https://connective.dev/docs/check) agents.
*
*/
var Check = /** @class */ (function (_super) {
__extends(Check, _super);
/**
*
* @param predicate the predicate function to pass or fail incoming values against.
*
*/
function Check(predicate) {
var _this = _super.call(this, {
inputs: ['value'],
outputs: ['pass', 'fail']
}) || this;
_this.predicate = predicate;
if (predicate.length <= 1)
_this.core = _this.input.to(map_1.map(function (v) { return [v, predicate(v)]; }));
else
_this.core = _this.input.to(map_1.map(function (v, done, error, context) {
return predicate(v, function (res) { return done([v, res]); }, error, context);
}));
return _this;
}
Object.defineProperty(Check.prototype, "input", {
/**
*
* Shortcut for `.in('value')`, the main value input for this check.
* [Read this](https://connective.dev/docs/check#signature) for more details.
*
*/
get: function () { return this.in('value'); },
enumerable: true,
configurable: true
});
Object.defineProperty(Check.prototype, "pass", {
/**
*
* 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: function () { return this.out('pass'); },
enumerable: true,
configurable: true
});
Object.defineProperty(Check.prototype, "fail", {
/**
*
* 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: function () { return this.out('fail'); },
enumerable: true,
configurable: true
});
Check.prototype.createOutput = function (label) {
this.checkOutput(label);
if (label == 'pass') {
return this.core
.to(filter_1.filter(function (_a) {
var _ = _a[0], v = _a[1];
return v;
}))
.to(map_1.map(function (_a) {
var v = _a[0], _ = _a[1];
return v;
}));
}
else {
return this.core
.to(filter_1.filter(function (_a) {
var _ = _a[0], v = _a[1];
return !v;
}))
.to(map_1.map(function (_a) {
var v = _a[0], _ = _a[1];
return v;
}));
}
};
Check.prototype.createEntries = function () { return [this.input]; };
Check.prototype.createExits = function () { return [this.pass, this.fail]; };
return Check;
}(agent_1.Agent));
exports.Check = Check;
/**
*
* 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
*
*/
function check(func) { return new Check(func); }
exports.check = check;
exports.default = check;
//# sourceMappingURL=check.js.map