UNPKG

jaywalk

Version:
65 lines 2.67 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 assert = require('assert'); var any_1 = require('./any'); var intersection_1 = require('./intersection'); var promises_1 = require('../support/promises'); var utils_1 = require('../utils'); var Tuple = (function (_super) { __extends(Tuple, _super); function Tuple(options) { _super.call(this, options); this.type = 'Tuple'; assert.ok(Array.isArray(options.tuple), 'Expected "tuple" to be a list of types'); this.tuple = options.tuple; this._tests.push(toTupleTest(options.tuple)); } Tuple.prototype._isType = function (value, path, context) { var _this = this; return utils_1.wrapIsType(this, value, path, context, _super.prototype._isType, function (value) { if (value.length === _this.tuple.length) { var res = 1; for (var i = 0; i < _this.tuple.length; i++) { res += _this.tuple[i]._isType(value[i], path.concat(String(i)), context); } return res; } throw context.error(path, 'Tuple', 'tuple', _this.tuple.length, value); }); }; Tuple.prototype._extend = function (options) { var res = _super.prototype._extend.call(this, options); if (options.tuple) { var len = Math.max(this.tuple.length, options.tuple.length); res.tuple = new Array(len); for (var i = 0; i < len; i++) { res.tuple[i] = intersection_1.Intersection.intersect(this.tuple[i], options.tuple[i]); } } return res; }; Tuple.prototype.toJSON = function () { var json = _super.prototype.toJSON.call(this); json.tuple = this.tuple.map(function (x) { return x.toJSON(); }); return json; }; return Tuple; }(any_1.Any)); exports.Tuple = Tuple; function toTupleTest(tuple) { var tests = tuple.map(function (type) { return type._compile(); }); return function (values, path, context, next) { return promises_1.promiseEvery(tests.map(function (test, index) { return function () { var value = values[index]; var valuePath = path.concat(String(index)); return test(value, valuePath, context, utils_1.identity); }; })).then(function (res) { return next(res); }); }; } //# sourceMappingURL=tuple.js.map