jaywalk
Version:
Runtime type validation
62 lines • 2.36 kB
JavaScript
;
var __extends = (this && this.__extends) || function (d, b) {
for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p];
function __() { this.constructor = d; }
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
};
var rule_1 = require('./rule');
var utils_1 = require('../utils');
var Condition = (function (_super) {
__extends(Condition, _super);
function Condition(options) {
_super.call(this, options);
this.type = 'Condition';
this.left = options.left;
this.right = options.right;
this.comparator = options.comparator || '==';
this._tests.push(toConditionTest(this.left, this.right, this.comparator));
}
Condition.prototype._extend = function (options) {
return _super.prototype._extend.call(this, options);
};
return Condition;
}(rule_1.Rule));
exports.Condition = Condition;
function toConditionTest(left, right, comparator) {
var leftValue = utils_1.toValue(left);
var rightValue = utils_1.toValue(right);
if (comparator === '==') {
return function (value, path, context, next) {
if (leftValue(path, context) === rightValue(path, context)) {
return next(value);
}
throw context.error(path, 'Condition', null, this, value);
};
}
if (comparator === '!=') {
return function (value, path, context, next) {
if (leftValue(path, context) !== rightValue(path, context)) {
return next(value);
}
throw context.error(path, 'Condition', null, this, value);
};
}
if (comparator === '>=') {
return function (value, path, context, next) {
if (leftValue(path, context) >= rightValue(path, context)) {
return next(value);
}
throw context.error(path, 'Condition', null, this, value);
};
}
if (comparator === '<=') {
return function (value, path, context, next) {
if (leftValue(path, context) <= rightValue(path, context)) {
return next(value);
}
throw context.error(path, 'Condition', null, this, value);
};
}
throw new TypeError("Unknown Condition comparator: " + comparator);
}
//# sourceMappingURL=condition.js.map