UNPKG

jaywalk

Version:
67 lines 2.38 kB
"use strict"; 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 any_1 = require('./any'); var utils_1 = require('../utils'); var Union = (function (_super) { __extends(Union, _super); function Union(options) { _super.call(this, options); this.type = 'Union'; this.types = options.types; this._tests.push(toItemsTest(this.types)); } Union.prototype._extend = function (options) { var res = _super.prototype._extend.call(this, options); if (options.types) { res.types = this.types.concat(options.types); } return res; }; Union.prototype._isType = function (value, path, context) { var _this = this; return utils_1.wrapIsType(this, value, path, context, _super.prototype._isType, function (value) { var res = 0; for (var _i = 0, _a = _this.types; _i < _a.length; _i++) { var type = _a[_i]; var check = utils_1.isType(type, value, path, context); if (check > res) { res = check; } } if (res === 0) { throw context.error(path, 'Union', 'types', _this.types, value); } return res; }); }; Union.prototype.toJSON = function () { var json = _super.prototype.toJSON.call(this); json.types = this.types.map(function (x) { return x.toJSON(); }); return json; }; return Union; }(any_1.Any)); exports.Union = Union; function toItemsTest(types) { var tests = types.map(function (type) { return type._compile(); }); return function (value, path, context, next) { var res = 0; var test; for (var i = 0; i < tests.length; i++) { var check = utils_1.isType(types[i], value, path, context); if (check > res) { res = check; test = tests[i]; } } if (res === 0) { throw context.error(path, 'Union', 'types', types, value); } return test(value, path, context, next); }; } //# sourceMappingURL=union.js.map