UNPKG

jaywalk

Version:
89 lines 3.5 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 promises_1 = require('../support/promises'); var utils_1 = require('../utils'); var Intersection = (function (_super) { __extends(Intersection, _super); function Intersection(options) { _super.call(this, options); this.type = 'Intersection'; this.types = options.types; this._types = Intersection.flatten(this.types); this._tests.push(toIntersectionTest(this._types)); } Intersection.flatten = function (schemas) { return schemas .reduce(function (result, schema) { if (schema) { for (var i = 0; i < result.length; i++) { var cur = result[i]; if (cur._typeOf(schema)) { result[i] = utils_1.extendSchema(schema, cur); return result; } if (schema._typeOf(cur)) { result[i] = utils_1.extendSchema(cur, schema); return result; } } result.push(schema); } return result; }, []); }; Intersection.intersect = function () { var schemas = []; for (var _i = 0; _i < arguments.length; _i++) { schemas[_i - 0] = arguments[_i]; } var types = Intersection.flatten(schemas); return types.length > 1 ? new Intersection({ types: types }) : types[0]; }; Intersection.prototype._isType = function (value, path, context) { var _this = this; return utils_1.wrapIsType(this, value, path, context, _super.prototype._isType, function (value, path, context) { var res = 0; for (var _i = 0, _a = _this._types; _i < _a.length; _i++) { var type = _a[_i]; res += type._isType(value, path, context); } return res; }); }; Intersection.prototype._extend = function (options) { var res = _super.prototype._extend.call(this, options); if (options.types) { res.types = this.types.concat(options.types); } return res; }; Intersection.prototype.toJSON = function () { var json = _super.prototype.toJSON.call(this); json.types = this.types.map(function (x) { return x.toJSON(); }); delete json._types; return json; }; return Intersection; }(any_1.Any)); exports.Intersection = Intersection; function toIntersectionTest(types) { var tests = types.map(function (type) { return type._compile(); }); return function (value, path, context, next) { var result = promises_1.promiseEvery(tests.map(function (test) { return function () { return test(value, path, context, utils_1.identity) .then(function (result) { value = utils_1.merge(value, result); return result; }); }; })); return result.then(function (values) { return utils_1.merge.apply(void 0, values); }).then(function (res) { return next(res); }); }; } //# sourceMappingURL=intersection.js.map