1 | var __extends = (this && this.__extends) || (function () {
|
2 | var extendStatics = function (d, b) {
|
3 | extendStatics = Object.setPrototypeOf ||
|
4 | ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
|
5 | function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; };
|
6 | return extendStatics(d, b);
|
7 | };
|
8 | return function (d, b) {
|
9 | extendStatics(d, b);
|
10 | function __() { this.constructor = d; }
|
11 | d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
|
12 | };
|
13 | })();
|
14 | var __assign = (this && this.__assign) || function () {
|
15 | __assign = Object.assign || function(t) {
|
16 | for (var s, i = 1, n = arguments.length; i < n; i++) {
|
17 | s = arguments[i];
|
18 | for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
|
19 | t[p] = s[p];
|
20 | }
|
21 | return t;
|
22 | };
|
23 | return __assign.apply(this, arguments);
|
24 | };
|
25 | var __spreadArrays = (this && this.__spreadArrays) || function () {
|
26 | for (var s = 0, i = 0, il = arguments.length; i < il; i++) s += arguments[i].length;
|
27 | for (var r = Array(s), k = 0, i = 0; i < il; i++)
|
28 | for (var a = arguments[i], j = 0, jl = a.length; j < jl; j++, k++)
|
29 | r[k] = a[j];
|
30 | return r;
|
31 | };
|
32 |
|
33 |
|
34 |
|
35 | import { isLeft, left, right } from 'fp-ts/es6/Either';
|
36 |
|
37 |
|
38 |
|
39 | var Type = (function () {
|
40 | function Type(
|
41 | /** a unique name for this codec */
|
42 | name,
|
43 | /** a custom type guard */
|
44 | is,
|
45 | /** succeeds if a value of type I can be decoded to a value of type A */
|
46 | validate,
|
47 | /** converts a value of type A to a value of type O */
|
48 | encode) {
|
49 | this.name = name;
|
50 | this.is = is;
|
51 | this.validate = validate;
|
52 | this.encode = encode;
|
53 | this.decode = this.decode.bind(this);
|
54 | }
|
55 | |
56 |
|
57 |
|
58 | Type.prototype.pipe = function (ab, name) {
|
59 | var _this = this;
|
60 | if (name === void 0) { name = "pipe(" + this.name + ", " + ab.name + ")"; }
|
61 | return new Type(name, ab.is, function (i, c) {
|
62 | var e = _this.validate(i, c);
|
63 | if (isLeft(e)) {
|
64 | return e;
|
65 | }
|
66 | return ab.validate(e.right, c);
|
67 | }, this.encode === identity && ab.encode === identity ? identity : function (b) { return _this.encode(ab.encode(b)); });
|
68 | };
|
69 | |
70 |
|
71 |
|
72 | Type.prototype.asDecoder = function () {
|
73 | return this;
|
74 | };
|
75 | |
76 |
|
77 |
|
78 | Type.prototype.asEncoder = function () {
|
79 | return this;
|
80 | };
|
81 | |
82 |
|
83 |
|
84 |
|
85 | Type.prototype.decode = function (i) {
|
86 | return this.validate(i, [{ key: '', type: this, actual: i }]);
|
87 | };
|
88 | return Type;
|
89 | }());
|
90 | export { Type };
|
91 |
|
92 |
|
93 |
|
94 | export var identity = function (a) { return a; };
|
95 |
|
96 |
|
97 |
|
98 | export var getFunctionName = function (f) {
|
99 | return f.displayName || f.name || "<function" + f.length + ">";
|
100 | };
|
101 |
|
102 |
|
103 |
|
104 | export var getContextEntry = function (key, decoder) { return ({ key: key, type: decoder }); };
|
105 |
|
106 |
|
107 |
|
108 | export var appendContext = function (c, key, decoder, actual) {
|
109 | var len = c.length;
|
110 | var r = Array(len + 1);
|
111 | for (var i = 0; i < len; i++) {
|
112 | r[i] = c[i];
|
113 | }
|
114 | r[len] = { key: key, type: decoder, actual: actual };
|
115 | return r;
|
116 | };
|
117 |
|
118 |
|
119 |
|
120 | export var failures = left;
|
121 |
|
122 |
|
123 |
|
124 | export var failure = function (value, context, message) {
|
125 | return failures([{ value: value, context: context, message: message }]);
|
126 | };
|
127 |
|
128 |
|
129 |
|
130 | export var success = right;
|
131 | var pushAll = function (xs, ys) {
|
132 | var l = ys.length;
|
133 | for (var i = 0; i < l; i++) {
|
134 | xs.push(ys[i]);
|
135 | }
|
136 | };
|
137 |
|
138 |
|
139 |
|
140 |
|
141 |
|
142 |
|
143 | var NullType = (function (_super) {
|
144 | __extends(NullType, _super);
|
145 | function NullType() {
|
146 | var _this = _super.call(this, 'null', function (u) { return u === null; }, function (u, c) { return (_this.is(u) ? success(u) : failure(u, c)); }, identity) || this;
|
147 | |
148 |
|
149 |
|
150 | _this._tag = 'NullType';
|
151 | return _this;
|
152 | }
|
153 | return NullType;
|
154 | }(Type));
|
155 | export { NullType };
|
156 |
|
157 |
|
158 |
|
159 | export var nullType = new NullType();
|
160 |
|
161 |
|
162 |
|
163 | var UndefinedType = (function (_super) {
|
164 | __extends(UndefinedType, _super);
|
165 | function UndefinedType() {
|
166 | var _this = _super.call(this, 'undefined', function (u) { return u === void 0; }, function (u, c) { return (_this.is(u) ? success(u) : failure(u, c)); }, identity) || this;
|
167 | |
168 |
|
169 |
|
170 | _this._tag = 'UndefinedType';
|
171 | return _this;
|
172 | }
|
173 | return UndefinedType;
|
174 | }(Type));
|
175 | export { UndefinedType };
|
176 | var undefinedType = new UndefinedType();
|
177 |
|
178 |
|
179 |
|
180 | var VoidType = (function (_super) {
|
181 | __extends(VoidType, _super);
|
182 | function VoidType() {
|
183 | var _this = _super.call(this, 'void', undefinedType.is, undefinedType.validate, identity) || this;
|
184 | |
185 |
|
186 |
|
187 | _this._tag = 'VoidType';
|
188 | return _this;
|
189 | }
|
190 | return VoidType;
|
191 | }(Type));
|
192 | export { VoidType };
|
193 |
|
194 |
|
195 |
|
196 | export var voidType = new VoidType();
|
197 |
|
198 |
|
199 |
|
200 | var UnknownType = (function (_super) {
|
201 | __extends(UnknownType, _super);
|
202 | function UnknownType() {
|
203 | var _this = _super.call(this, 'unknown', function (_) { return true; }, success, identity) || this;
|
204 | |
205 |
|
206 |
|
207 | _this._tag = 'UnknownType';
|
208 | return _this;
|
209 | }
|
210 | return UnknownType;
|
211 | }(Type));
|
212 | export { UnknownType };
|
213 |
|
214 |
|
215 |
|
216 | export var unknown = new UnknownType();
|
217 |
|
218 |
|
219 |
|
220 | var StringType = (function (_super) {
|
221 | __extends(StringType, _super);
|
222 | function StringType() {
|
223 | var _this = _super.call(this, 'string', function (u) { return typeof u === 'string'; }, function (u, c) { return (_this.is(u) ? success(u) : failure(u, c)); }, identity) || this;
|
224 | |
225 |
|
226 |
|
227 | _this._tag = 'StringType';
|
228 | return _this;
|
229 | }
|
230 | return StringType;
|
231 | }(Type));
|
232 | export { StringType };
|
233 |
|
234 |
|
235 |
|
236 | export var string = new StringType();
|
237 |
|
238 |
|
239 |
|
240 | var NumberType = (function (_super) {
|
241 | __extends(NumberType, _super);
|
242 | function NumberType() {
|
243 | var _this = _super.call(this, 'number', function (u) { return typeof u === 'number'; }, function (u, c) { return (_this.is(u) ? success(u) : failure(u, c)); }, identity) || this;
|
244 | |
245 |
|
246 |
|
247 | _this._tag = 'NumberType';
|
248 | return _this;
|
249 | }
|
250 | return NumberType;
|
251 | }(Type));
|
252 | export { NumberType };
|
253 |
|
254 |
|
255 |
|
256 | export var number = new NumberType();
|
257 |
|
258 |
|
259 |
|
260 | var BigIntType = (function (_super) {
|
261 | __extends(BigIntType, _super);
|
262 | function BigIntType() {
|
263 | var _this = _super.call(this, 'bigint',
|
264 |
|
265 | function (u) { return typeof u === 'bigint'; }, function (u, c) { return (_this.is(u) ? success(u) : failure(u, c)); }, identity) || this;
|
266 | |
267 |
|
268 |
|
269 | _this._tag = 'BigIntType';
|
270 | return _this;
|
271 | }
|
272 | return BigIntType;
|
273 | }(Type));
|
274 | export { BigIntType };
|
275 |
|
276 |
|
277 |
|
278 | export var bigint = new BigIntType();
|
279 |
|
280 |
|
281 |
|
282 | var BooleanType = (function (_super) {
|
283 | __extends(BooleanType, _super);
|
284 | function BooleanType() {
|
285 | var _this = _super.call(this, 'boolean', function (u) { return typeof u === 'boolean'; }, function (u, c) { return (_this.is(u) ? success(u) : failure(u, c)); }, identity) || this;
|
286 | |
287 |
|
288 |
|
289 | _this._tag = 'BooleanType';
|
290 | return _this;
|
291 | }
|
292 | return BooleanType;
|
293 | }(Type));
|
294 | export { BooleanType };
|
295 |
|
296 |
|
297 |
|
298 | export var boolean = new BooleanType();
|
299 |
|
300 |
|
301 |
|
302 | var AnyArrayType = (function (_super) {
|
303 | __extends(AnyArrayType, _super);
|
304 | function AnyArrayType() {
|
305 | var _this = _super.call(this, 'UnknownArray', Array.isArray, function (u, c) { return (_this.is(u) ? success(u) : failure(u, c)); }, identity) || this;
|
306 | |
307 |
|
308 |
|
309 | _this._tag = 'AnyArrayType';
|
310 | return _this;
|
311 | }
|
312 | return AnyArrayType;
|
313 | }(Type));
|
314 | export { AnyArrayType };
|
315 |
|
316 |
|
317 |
|
318 | export var UnknownArray = new AnyArrayType();
|
319 |
|
320 |
|
321 |
|
322 | var AnyDictionaryType = (function (_super) {
|
323 | __extends(AnyDictionaryType, _super);
|
324 | function AnyDictionaryType() {
|
325 | var _this = _super.call(this, 'UnknownRecord', function (u) {
|
326 | var s = Object.prototype.toString.call(u);
|
327 | return s === '[object Object]' || s === '[object Window]';
|
328 | }, function (u, c) { return (_this.is(u) ? success(u) : failure(u, c)); }, identity) || this;
|
329 | |
330 |
|
331 |
|
332 | _this._tag = 'AnyDictionaryType';
|
333 | return _this;
|
334 | }
|
335 | return AnyDictionaryType;
|
336 | }(Type));
|
337 | export { AnyDictionaryType };
|
338 |
|
339 |
|
340 |
|
341 | export var UnknownRecord = new AnyDictionaryType();
|
342 |
|
343 |
|
344 |
|
345 |
|
346 | var FunctionType = (function (_super) {
|
347 | __extends(FunctionType, _super);
|
348 | function FunctionType() {
|
349 | var _this = _super.call(this, 'Function',
|
350 |
|
351 | function (u) { return typeof u === 'function'; }, function (u, c) { return (_this.is(u) ? success(u) : failure(u, c)); }, identity) || this;
|
352 | |
353 |
|
354 |
|
355 | _this._tag = 'FunctionType';
|
356 | return _this;
|
357 | }
|
358 | return FunctionType;
|
359 | }(Type));
|
360 | export { FunctionType };
|
361 |
|
362 |
|
363 |
|
364 |
|
365 |
|
366 | export var Function = new FunctionType();
|
367 |
|
368 |
|
369 |
|
370 | var RefinementType = (function (_super) {
|
371 | __extends(RefinementType, _super);
|
372 | function RefinementType(name, is, validate, encode, type, predicate) {
|
373 | var _this = _super.call(this, name, is, validate, encode) || this;
|
374 | _this.type = type;
|
375 | _this.predicate = predicate;
|
376 | |
377 |
|
378 |
|
379 | _this._tag = 'RefinementType';
|
380 | return _this;
|
381 | }
|
382 | return RefinementType;
|
383 | }(Type));
|
384 | export { RefinementType };
|
385 |
|
386 |
|
387 |
|
388 | export var brand = function (codec, predicate, name) {
|
389 |
|
390 | return refinement(codec, predicate, name);
|
391 | };
|
392 |
|
393 |
|
394 |
|
395 |
|
396 | export var Int = brand(number, function (n) { return Number.isInteger(n); }, 'Int');
|
397 |
|
398 |
|
399 |
|
400 | var LiteralType = (function (_super) {
|
401 | __extends(LiteralType, _super);
|
402 | function LiteralType(name, is, validate, encode, value) {
|
403 | var _this = _super.call(this, name, is, validate, encode) || this;
|
404 | _this.value = value;
|
405 | |
406 |
|
407 |
|
408 | _this._tag = 'LiteralType';
|
409 | return _this;
|
410 | }
|
411 | return LiteralType;
|
412 | }(Type));
|
413 | export { LiteralType };
|
414 |
|
415 |
|
416 |
|
417 | export var literal = function (value, name) {
|
418 | if (name === void 0) { name = JSON.stringify(value); }
|
419 | var is = function (u) { return u === value; };
|
420 | return new LiteralType(name, is, function (u, c) { return (is(u) ? success(value) : failure(u, c)); }, identity, value);
|
421 | };
|
422 |
|
423 |
|
424 |
|
425 | var KeyofType = (function (_super) {
|
426 | __extends(KeyofType, _super);
|
427 | function KeyofType(name, is, validate, encode, keys) {
|
428 | var _this = _super.call(this, name, is, validate, encode) || this;
|
429 | _this.keys = keys;
|
430 | |
431 |
|
432 |
|
433 | _this._tag = 'KeyofType';
|
434 | return _this;
|
435 | }
|
436 | return KeyofType;
|
437 | }(Type));
|
438 | export { KeyofType };
|
439 | var hasOwnProperty = Object.prototype.hasOwnProperty;
|
440 |
|
441 |
|
442 |
|
443 | export var keyof = function (keys, name) {
|
444 | if (name === void 0) { name = Object.keys(keys)
|
445 | .map(function (k) { return JSON.stringify(k); })
|
446 | .join(' | '); }
|
447 | var is = function (u) { return string.is(u) && hasOwnProperty.call(keys, u); };
|
448 | return new KeyofType(name, is, function (u, c) { return (is(u) ? success(u) : failure(u, c)); }, identity, keys);
|
449 | };
|
450 |
|
451 |
|
452 |
|
453 | var RecursiveType = (function (_super) {
|
454 | __extends(RecursiveType, _super);
|
455 | function RecursiveType(name, is, validate, encode, runDefinition) {
|
456 | var _this = _super.call(this, name, is, validate, encode) || this;
|
457 | _this.runDefinition = runDefinition;
|
458 | |
459 |
|
460 |
|
461 | _this._tag = 'RecursiveType';
|
462 | return _this;
|
463 | }
|
464 | return RecursiveType;
|
465 | }(Type));
|
466 | export { RecursiveType };
|
467 | Object.defineProperty(RecursiveType.prototype, 'type', {
|
468 | get: function () {
|
469 | return this.runDefinition();
|
470 | },
|
471 | enumerable: true,
|
472 | configurable: true
|
473 | });
|
474 |
|
475 |
|
476 |
|
477 | export var recursion = function (name, definition) {
|
478 | var cache;
|
479 | var runDefinition = function () {
|
480 | if (!cache) {
|
481 | cache = definition(Self);
|
482 | cache.name = name;
|
483 | }
|
484 | return cache;
|
485 | };
|
486 | var Self = new RecursiveType(name, function (u) { return runDefinition().is(u); }, function (u, c) { return runDefinition().validate(u, c); }, function (a) { return runDefinition().encode(a); }, runDefinition);
|
487 | return Self;
|
488 | };
|
489 |
|
490 |
|
491 |
|
492 | var ArrayType = (function (_super) {
|
493 | __extends(ArrayType, _super);
|
494 | function ArrayType(name, is, validate, encode, type) {
|
495 | var _this = _super.call(this, name, is, validate, encode) || this;
|
496 | _this.type = type;
|
497 | |
498 |
|
499 |
|
500 | _this._tag = 'ArrayType';
|
501 | return _this;
|
502 | }
|
503 | return ArrayType;
|
504 | }(Type));
|
505 | export { ArrayType };
|
506 |
|
507 |
|
508 |
|
509 | export var array = function (codec, name) {
|
510 | if (name === void 0) { name = "Array<" + codec.name + ">"; }
|
511 | return new ArrayType(name, function (u) { return UnknownArray.is(u) && u.every(codec.is); }, function (u, c) {
|
512 | var e = UnknownArray.validate(u, c);
|
513 | if (isLeft(e)) {
|
514 | return e;
|
515 | }
|
516 | var us = e.right;
|
517 | var len = us.length;
|
518 | var as = us;
|
519 | var errors = [];
|
520 | for (var i = 0; i < len; i++) {
|
521 | var ui = us[i];
|
522 | var result = codec.validate(ui, appendContext(c, String(i), codec, ui));
|
523 | if (isLeft(result)) {
|
524 | pushAll(errors, result.left);
|
525 | }
|
526 | else {
|
527 | var ai = result.right;
|
528 | if (ai !== ui) {
|
529 | if (as === us) {
|
530 | as = us.slice();
|
531 | }
|
532 | as[i] = ai;
|
533 | }
|
534 | }
|
535 | }
|
536 | return errors.length > 0 ? failures(errors) : success(as);
|
537 | }, codec.encode === identity ? identity : function (a) { return a.map(codec.encode); }, codec);
|
538 | };
|
539 |
|
540 |
|
541 |
|
542 | var InterfaceType = (function (_super) {
|
543 | __extends(InterfaceType, _super);
|
544 | function InterfaceType(name, is, validate, encode, props) {
|
545 | var _this = _super.call(this, name, is, validate, encode) || this;
|
546 | _this.props = props;
|
547 | |
548 |
|
549 |
|
550 | _this._tag = 'InterfaceType';
|
551 | return _this;
|
552 | }
|
553 | return InterfaceType;
|
554 | }(Type));
|
555 | export { InterfaceType };
|
556 | var getNameFromProps = function (props) {
|
557 | return Object.keys(props)
|
558 | .map(function (k) { return k + ": " + props[k].name; })
|
559 | .join(', ');
|
560 | };
|
561 | var useIdentity = function (codecs) {
|
562 | for (var i = 0; i < codecs.length; i++) {
|
563 | if (codecs[i].encode !== identity) {
|
564 | return false;
|
565 | }
|
566 | }
|
567 | return true;
|
568 | };
|
569 | var getInterfaceTypeName = function (props) {
|
570 | return "{ " + getNameFromProps(props) + " }";
|
571 | };
|
572 |
|
573 |
|
574 |
|
575 | export var type = function (props, name) {
|
576 | if (name === void 0) { name = getInterfaceTypeName(props); }
|
577 | var keys = Object.keys(props);
|
578 | var types = keys.map(function (key) { return props[key]; });
|
579 | var len = keys.length;
|
580 | return new InterfaceType(name, function (u) {
|
581 | if (UnknownRecord.is(u)) {
|
582 | for (var i = 0; i < len; i++) {
|
583 | var k = keys[i];
|
584 | var uk = u[k];
|
585 | if ((uk === undefined && !hasOwnProperty.call(u, k)) || !types[i].is(uk)) {
|
586 | return false;
|
587 | }
|
588 | }
|
589 | return true;
|
590 | }
|
591 | return false;
|
592 | }, function (u, c) {
|
593 | var e = UnknownRecord.validate(u, c);
|
594 | if (isLeft(e)) {
|
595 | return e;
|
596 | }
|
597 | var o = e.right;
|
598 | var a = o;
|
599 | var errors = [];
|
600 | for (var i = 0; i < len; i++) {
|
601 | var k = keys[i];
|
602 | var ak = a[k];
|
603 | var type_1 = types[i];
|
604 | var result = type_1.validate(ak, appendContext(c, k, type_1, ak));
|
605 | if (isLeft(result)) {
|
606 | pushAll(errors, result.left);
|
607 | }
|
608 | else {
|
609 | var vak = result.right;
|
610 | if (vak !== ak || (vak === undefined && !hasOwnProperty.call(a, k))) {
|
611 |
|
612 | if (a === o) {
|
613 | a = __assign({}, o);
|
614 | }
|
615 | a[k] = vak;
|
616 | }
|
617 | }
|
618 | }
|
619 | return errors.length > 0 ? failures(errors) : success(a);
|
620 | }, useIdentity(types)
|
621 | ? identity
|
622 | : function (a) {
|
623 | var s = __assign({}, a);
|
624 | for (var i = 0; i < len; i++) {
|
625 | var k = keys[i];
|
626 | var encode = types[i].encode;
|
627 | if (encode !== identity) {
|
628 | s[k] = encode(a[k]);
|
629 | }
|
630 | }
|
631 | return s;
|
632 | }, props);
|
633 | };
|
634 |
|
635 |
|
636 |
|
637 | var PartialType = (function (_super) {
|
638 | __extends(PartialType, _super);
|
639 | function PartialType(name, is, validate, encode, props) {
|
640 | var _this = _super.call(this, name, is, validate, encode) || this;
|
641 | _this.props = props;
|
642 | |
643 |
|
644 |
|
645 | _this._tag = 'PartialType';
|
646 | return _this;
|
647 | }
|
648 | return PartialType;
|
649 | }(Type));
|
650 | export { PartialType };
|
651 | var getPartialTypeName = function (inner) {
|
652 | return "Partial<" + inner + ">";
|
653 | };
|
654 |
|
655 |
|
656 |
|
657 | export var partial = function (props, name) {
|
658 | if (name === void 0) { name = getPartialTypeName(getInterfaceTypeName(props)); }
|
659 | var keys = Object.keys(props);
|
660 | var types = keys.map(function (key) { return props[key]; });
|
661 | var len = keys.length;
|
662 | return new PartialType(name, function (u) {
|
663 | if (UnknownRecord.is(u)) {
|
664 | for (var i = 0; i < len; i++) {
|
665 | var k = keys[i];
|
666 | var uk = u[k];
|
667 | if (uk !== undefined && !props[k].is(uk)) {
|
668 | return false;
|
669 | }
|
670 | }
|
671 | return true;
|
672 | }
|
673 | return false;
|
674 | }, function (u, c) {
|
675 | var e = UnknownRecord.validate(u, c);
|
676 | if (isLeft(e)) {
|
677 | return e;
|
678 | }
|
679 | var o = e.right;
|
680 | var a = o;
|
681 | var errors = [];
|
682 | for (var i = 0; i < len; i++) {
|
683 | var k = keys[i];
|
684 | var ak = a[k];
|
685 | var type_2 = props[k];
|
686 | var result = type_2.validate(ak, appendContext(c, k, type_2, ak));
|
687 | if (isLeft(result)) {
|
688 | if (ak !== undefined) {
|
689 | pushAll(errors, result.left);
|
690 | }
|
691 | }
|
692 | else {
|
693 | var vak = result.right;
|
694 | if (vak !== ak) {
|
695 |
|
696 | if (a === o) {
|
697 | a = __assign({}, o);
|
698 | }
|
699 | a[k] = vak;
|
700 | }
|
701 | }
|
702 | }
|
703 | return errors.length > 0 ? failures(errors) : success(a);
|
704 | }, useIdentity(types)
|
705 | ? identity
|
706 | : function (a) {
|
707 | var s = __assign({}, a);
|
708 | for (var i = 0; i < len; i++) {
|
709 | var k = keys[i];
|
710 | var ak = a[k];
|
711 | if (ak !== undefined) {
|
712 | s[k] = types[i].encode(ak);
|
713 | }
|
714 | }
|
715 | return s;
|
716 | }, props);
|
717 | };
|
718 |
|
719 |
|
720 |
|
721 | var DictionaryType = (function (_super) {
|
722 | __extends(DictionaryType, _super);
|
723 | function DictionaryType(name, is, validate, encode, domain, codomain) {
|
724 | var _this = _super.call(this, name, is, validate, encode) || this;
|
725 | _this.domain = domain;
|
726 | _this.codomain = codomain;
|
727 | |
728 |
|
729 |
|
730 | _this._tag = 'DictionaryType';
|
731 | return _this;
|
732 | }
|
733 | return DictionaryType;
|
734 | }(Type));
|
735 | export { DictionaryType };
|
736 | function enumerableRecord(keys, domain, codomain, name) {
|
737 | if (name === void 0) { name = "{ [K in " + domain.name + "]: " + codomain.name + " }"; }
|
738 | var len = keys.length;
|
739 | return new DictionaryType(name, function (u) { return UnknownRecord.is(u) && keys.every(function (k) { return codomain.is(u[k]); }); }, function (u, c) {
|
740 | var e = UnknownRecord.validate(u, c);
|
741 | if (isLeft(e)) {
|
742 | return e;
|
743 | }
|
744 | var o = e.right;
|
745 | var a = {};
|
746 | var errors = [];
|
747 | var changed = false;
|
748 | for (var i = 0; i < len; i++) {
|
749 | var k = keys[i];
|
750 | var ok = o[k];
|
751 | var codomainResult = codomain.validate(ok, appendContext(c, k, codomain, ok));
|
752 | if (isLeft(codomainResult)) {
|
753 | pushAll(errors, codomainResult.left);
|
754 | }
|
755 | else {
|
756 | var vok = codomainResult.right;
|
757 | changed = changed || vok !== ok;
|
758 | a[k] = vok;
|
759 | }
|
760 | }
|
761 | return errors.length > 0 ? failures(errors) : success((changed || Object.keys(o).length !== len ? a : o));
|
762 | }, codomain.encode === identity
|
763 | ? identity
|
764 | : function (a) {
|
765 | var s = {};
|
766 | for (var i = 0; i < len; i++) {
|
767 | var k = keys[i];
|
768 | s[k] = codomain.encode(a[k]);
|
769 | }
|
770 | return s;
|
771 | }, domain, codomain);
|
772 | }
|
773 |
|
774 |
|
775 |
|
776 | export function getDomainKeys(domain) {
|
777 | var _a;
|
778 | if (isLiteralC(domain)) {
|
779 | var literal_1 = domain.value;
|
780 | if (string.is(literal_1)) {
|
781 | return _a = {}, _a[literal_1] = null, _a;
|
782 | }
|
783 | }
|
784 | else if (isKeyofC(domain)) {
|
785 | return domain.keys;
|
786 | }
|
787 | else if (isUnionC(domain)) {
|
788 | var keys = domain.types.map(function (type) { return getDomainKeys(type); });
|
789 | return keys.some(undefinedType.is) ? undefined : Object.assign.apply(Object, __spreadArrays([{}], keys));
|
790 | }
|
791 | return undefined;
|
792 | }
|
793 | function nonEnumerableRecord(domain, codomain, name) {
|
794 | if (name === void 0) { name = "{ [K in " + domain.name + "]: " + codomain.name + " }"; }
|
795 | return new DictionaryType(name, function (u) {
|
796 | if (UnknownRecord.is(u)) {
|
797 | return Object.keys(u).every(function (k) { return domain.is(k) && codomain.is(u[k]); });
|
798 | }
|
799 | return isAnyC(codomain) && Array.isArray(u);
|
800 | }, function (u, c) {
|
801 | if (UnknownRecord.is(u)) {
|
802 | var a = {};
|
803 | var errors = [];
|
804 | var keys = Object.keys(u);
|
805 | var len = keys.length;
|
806 | var changed = false;
|
807 | for (var i = 0; i < len; i++) {
|
808 | var k = keys[i];
|
809 | var ok = u[k];
|
810 | var domainResult = domain.validate(k, appendContext(c, k, domain, k));
|
811 | if (isLeft(domainResult)) {
|
812 | pushAll(errors, domainResult.left);
|
813 | }
|
814 | else {
|
815 | var vk = domainResult.right;
|
816 | changed = changed || vk !== k;
|
817 | k = vk;
|
818 | var codomainResult = codomain.validate(ok, appendContext(c, k, codomain, ok));
|
819 | if (isLeft(codomainResult)) {
|
820 | pushAll(errors, codomainResult.left);
|
821 | }
|
822 | else {
|
823 | var vok = codomainResult.right;
|
824 | changed = changed || vok !== ok;
|
825 | a[k] = vok;
|
826 | }
|
827 | }
|
828 | }
|
829 | return errors.length > 0 ? failures(errors) : success((changed ? a : u));
|
830 | }
|
831 | if (isAnyC(codomain) && Array.isArray(u)) {
|
832 | return success(u);
|
833 | }
|
834 | return failure(u, c);
|
835 | }, domain.encode === identity && codomain.encode === identity
|
836 | ? identity
|
837 | : function (a) {
|
838 | var s = {};
|
839 | var keys = Object.keys(a);
|
840 | var len = keys.length;
|
841 | for (var i = 0; i < len; i++) {
|
842 | var k = keys[i];
|
843 | s[String(domain.encode(k))] = codomain.encode(a[k]);
|
844 | }
|
845 | return s;
|
846 | }, domain, codomain);
|
847 | }
|
848 |
|
849 |
|
850 |
|
851 | export function record(domain, codomain, name) {
|
852 | var keys = getDomainKeys(domain);
|
853 | return keys
|
854 | ? enumerableRecord(Object.keys(keys), domain, codomain, name)
|
855 | : nonEnumerableRecord(domain, codomain, name);
|
856 | }
|
857 |
|
858 |
|
859 |
|
860 | var UnionType = (function (_super) {
|
861 | __extends(UnionType, _super);
|
862 | function UnionType(name, is, validate, encode, types) {
|
863 | var _this = _super.call(this, name, is, validate, encode) || this;
|
864 | _this.types = types;
|
865 | |
866 |
|
867 |
|
868 | _this._tag = 'UnionType';
|
869 | return _this;
|
870 | }
|
871 | return UnionType;
|
872 | }(Type));
|
873 | export { UnionType };
|
874 | var getUnionName = function (codecs) {
|
875 | return '(' + codecs.map(function (type) { return type.name; }).join(' | ') + ')';
|
876 | };
|
877 |
|
878 |
|
879 |
|
880 | export var union = function (codecs, name) {
|
881 | if (name === void 0) { name = getUnionName(codecs); }
|
882 | var index = getIndex(codecs);
|
883 | if (index !== undefined && codecs.length > 0) {
|
884 | var tag_1 = index[0], groups_1 = index[1];
|
885 | var len_1 = groups_1.length;
|
886 | var find_1 = function (value) {
|
887 | for (var i = 0; i < len_1; i++) {
|
888 | if (groups_1[i].indexOf(value) !== -1) {
|
889 | return i;
|
890 | }
|
891 | }
|
892 | return undefined;
|
893 | };
|
894 |
|
895 | return new TaggedUnionType(name, function (u) {
|
896 | if (UnknownRecord.is(u)) {
|
897 | var i = find_1(u[tag_1]);
|
898 | return i !== undefined ? codecs[i].is(u) : false;
|
899 | }
|
900 | return false;
|
901 | }, function (u, c) {
|
902 | var e = UnknownRecord.validate(u, c);
|
903 | if (isLeft(e)) {
|
904 | return e;
|
905 | }
|
906 | var r = e.right;
|
907 | var i = find_1(r[tag_1]);
|
908 | if (i === undefined) {
|
909 | return failure(u, c);
|
910 | }
|
911 | var codec = codecs[i];
|
912 | return codec.validate(r, appendContext(c, String(i), codec, r));
|
913 | }, useIdentity(codecs)
|
914 | ? identity
|
915 | : function (a) {
|
916 | var i = find_1(a[tag_1]);
|
917 | if (i === undefined) {
|
918 |
|
919 | throw new Error("no codec found to encode value in union codec " + name);
|
920 | }
|
921 | else {
|
922 | return codecs[i].encode(a);
|
923 | }
|
924 | }, codecs, tag_1);
|
925 | }
|
926 | else {
|
927 | return new UnionType(name, function (u) { return codecs.some(function (type) { return type.is(u); }); }, function (u, c) {
|
928 | var errors = [];
|
929 | for (var i = 0; i < codecs.length; i++) {
|
930 | var codec = codecs[i];
|
931 | var result = codec.validate(u, appendContext(c, String(i), codec, u));
|
932 | if (isLeft(result)) {
|
933 | pushAll(errors, result.left);
|
934 | }
|
935 | else {
|
936 | return success(result.right);
|
937 | }
|
938 | }
|
939 | return failures(errors);
|
940 | }, useIdentity(codecs)
|
941 | ? identity
|
942 | : function (a) {
|
943 | for (var _i = 0, codecs_1 = codecs; _i < codecs_1.length; _i++) {
|
944 | var codec = codecs_1[_i];
|
945 | if (codec.is(a)) {
|
946 | return codec.encode(a);
|
947 | }
|
948 | }
|
949 |
|
950 | throw new Error("no codec found to encode value in union type " + name);
|
951 | }, codecs);
|
952 | }
|
953 | };
|
954 |
|
955 |
|
956 |
|
957 | var IntersectionType = (function (_super) {
|
958 | __extends(IntersectionType, _super);
|
959 | function IntersectionType(name, is, validate, encode, types) {
|
960 | var _this = _super.call(this, name, is, validate, encode) || this;
|
961 | _this.types = types;
|
962 | |
963 |
|
964 |
|
965 | _this._tag = 'IntersectionType';
|
966 | return _this;
|
967 | }
|
968 | return IntersectionType;
|
969 | }(Type));
|
970 | export { IntersectionType };
|
971 | var mergeAll = function (base, us) {
|
972 | var equal = true;
|
973 | var primitive = true;
|
974 | for (var _i = 0, us_1 = us; _i < us_1.length; _i++) {
|
975 | var u = us_1[_i];
|
976 | if (u !== base) {
|
977 | equal = false;
|
978 | }
|
979 | if (UnknownRecord.is(u)) {
|
980 | primitive = false;
|
981 | }
|
982 | }
|
983 | if (equal) {
|
984 | return base;
|
985 | }
|
986 | else if (primitive) {
|
987 | return us[us.length - 1];
|
988 | }
|
989 | var r = {};
|
990 | for (var _a = 0, us_2 = us; _a < us_2.length; _a++) {
|
991 | var u = us_2[_a];
|
992 | for (var k in u) {
|
993 | if (u[k] !== base[k] || !r.hasOwnProperty(k)) {
|
994 | r[k] = u[k];
|
995 | }
|
996 | }
|
997 | }
|
998 | return r;
|
999 | };
|
1000 | export function intersection(codecs, name) {
|
1001 | if (name === void 0) { name = "(" + codecs.map(function (type) { return type.name; }).join(' & ') + ")"; }
|
1002 | var len = codecs.length;
|
1003 | return new IntersectionType(name, function (u) { return codecs.every(function (type) { return type.is(u); }); }, codecs.length === 0
|
1004 | ? success
|
1005 | : function (u, c) {
|
1006 | var us = [];
|
1007 | var errors = [];
|
1008 | for (var i = 0; i < len; i++) {
|
1009 | var codec = codecs[i];
|
1010 | var result = codec.validate(u, appendContext(c, String(i), codec, u));
|
1011 | if (isLeft(result)) {
|
1012 | pushAll(errors, result.left);
|
1013 | }
|
1014 | else {
|
1015 | us.push(result.right);
|
1016 | }
|
1017 | }
|
1018 | return errors.length > 0 ? failures(errors) : success(mergeAll(u, us));
|
1019 | }, codecs.length === 0
|
1020 | ? identity
|
1021 | : function (a) {
|
1022 | return mergeAll(a, codecs.map(function (codec) { return codec.encode(a); }));
|
1023 | }, codecs);
|
1024 | }
|
1025 |
|
1026 |
|
1027 |
|
1028 | var TupleType = (function (_super) {
|
1029 | __extends(TupleType, _super);
|
1030 | function TupleType(name, is, validate, encode, types) {
|
1031 | var _this = _super.call(this, name, is, validate, encode) || this;
|
1032 | _this.types = types;
|
1033 | |
1034 |
|
1035 |
|
1036 | _this._tag = 'TupleType';
|
1037 | return _this;
|
1038 | }
|
1039 | return TupleType;
|
1040 | }(Type));
|
1041 | export { TupleType };
|
1042 | export function tuple(codecs, name) {
|
1043 | if (name === void 0) { name = "[" + codecs.map(function (type) { return type.name; }).join(', ') + "]"; }
|
1044 | var len = codecs.length;
|
1045 | return new TupleType(name, function (u) { return UnknownArray.is(u) && u.length === len && codecs.every(function (type, i) { return type.is(u[i]); }); }, function (u, c) {
|
1046 | var e = UnknownArray.validate(u, c);
|
1047 | if (isLeft(e)) {
|
1048 | return e;
|
1049 | }
|
1050 | var us = e.right;
|
1051 | var as = us.length > len ? us.slice(0, len) : us;
|
1052 | var errors = [];
|
1053 | for (var i = 0; i < len; i++) {
|
1054 | var a = us[i];
|
1055 | var type_3 = codecs[i];
|
1056 | var result = type_3.validate(a, appendContext(c, String(i), type_3, a));
|
1057 | if (isLeft(result)) {
|
1058 | pushAll(errors, result.left);
|
1059 | }
|
1060 | else {
|
1061 | var va = result.right;
|
1062 | if (va !== a) {
|
1063 |
|
1064 | if (as === us) {
|
1065 | as = us.slice();
|
1066 | }
|
1067 | as[i] = va;
|
1068 | }
|
1069 | }
|
1070 | }
|
1071 | return errors.length > 0 ? failures(errors) : success(as);
|
1072 | }, useIdentity(codecs) ? identity : function (a) { return codecs.map(function (type, i) { return type.encode(a[i]); }); }, codecs);
|
1073 | }
|
1074 |
|
1075 |
|
1076 |
|
1077 | var ReadonlyType = (function (_super) {
|
1078 | __extends(ReadonlyType, _super);
|
1079 | function ReadonlyType(name, is, validate, encode, type) {
|
1080 | var _this = _super.call(this, name, is, validate, encode) || this;
|
1081 | _this.type = type;
|
1082 | |
1083 |
|
1084 |
|
1085 | _this._tag = 'ReadonlyType';
|
1086 | return _this;
|
1087 | }
|
1088 | return ReadonlyType;
|
1089 | }(Type));
|
1090 | export { ReadonlyType };
|
1091 |
|
1092 |
|
1093 |
|
1094 | export var readonly = function (codec, name) {
|
1095 | if (name === void 0) { name = "Readonly<" + codec.name + ">"; }
|
1096 | return new ReadonlyType(name, codec.is, function (u, c) {
|
1097 | var e = codec.validate(u, c);
|
1098 | if (isLeft(e)) {
|
1099 | return e;
|
1100 | }
|
1101 | var x = e.right;
|
1102 | if (process.env.NODE_ENV !== 'production') {
|
1103 | return right(Object.freeze(x));
|
1104 | }
|
1105 | return right(x);
|
1106 | }, codec.encode === identity ? identity : codec.encode, codec);
|
1107 | };
|
1108 |
|
1109 |
|
1110 |
|
1111 | var ReadonlyArrayType = (function (_super) {
|
1112 | __extends(ReadonlyArrayType, _super);
|
1113 | function ReadonlyArrayType(name, is, validate, encode, type) {
|
1114 | var _this = _super.call(this, name, is, validate, encode) || this;
|
1115 | _this.type = type;
|
1116 | |
1117 |
|
1118 |
|
1119 | _this._tag = 'ReadonlyArrayType';
|
1120 | return _this;
|
1121 | }
|
1122 | return ReadonlyArrayType;
|
1123 | }(Type));
|
1124 | export { ReadonlyArrayType };
|
1125 |
|
1126 |
|
1127 |
|
1128 | export var readonlyArray = function (codec, name) {
|
1129 | if (name === void 0) { name = "ReadonlyArray<" + codec.name + ">"; }
|
1130 | var arrayType = array(codec);
|
1131 | return new ReadonlyArrayType(name, arrayType.is, function (u, c) {
|
1132 | var e = arrayType.validate(u, c);
|
1133 | if (isLeft(e)) {
|
1134 | return e;
|
1135 | }
|
1136 | var x = e.right;
|
1137 | if (process.env.NODE_ENV !== 'production') {
|
1138 | return right(Object.freeze(x));
|
1139 | }
|
1140 | return right(x);
|
1141 | }, arrayType.encode, codec);
|
1142 | };
|
1143 |
|
1144 |
|
1145 |
|
1146 |
|
1147 | export var strict = function (props, name) {
|
1148 | return exact(type(props), name);
|
1149 | };
|
1150 |
|
1151 |
|
1152 |
|
1153 |
|
1154 | var TaggedUnionType = (function (_super) {
|
1155 | __extends(TaggedUnionType, _super);
|
1156 | function TaggedUnionType(name,
|
1157 | // tslint:disable-next-line: deprecation
|
1158 | is,
|
1159 | // tslint:disable-next-line: deprecation
|
1160 | validate,
|
1161 | // tslint:disable-next-line: deprecation
|
1162 | encode, codecs, tag) {
|
1163 | var _this = _super.call(this, name, is, validate, encode, codecs)
|
1164 | || this;
|
1165 | _this.tag = tag;
|
1166 | return _this;
|
1167 | }
|
1168 | return TaggedUnionType;
|
1169 | }(UnionType));
|
1170 | export { TaggedUnionType };
|
1171 |
|
1172 |
|
1173 |
|
1174 |
|
1175 |
|
1176 |
|
1177 | export var taggedUnion = function (tag, codecs, name
|
1178 | // tslint:disable-next-line: deprecation
|
1179 | ) {
|
1180 | if (name === void 0) { name = getUnionName(codecs); }
|
1181 | var U = union(codecs, name);
|
1182 |
|
1183 | if (U instanceof TaggedUnionType) {
|
1184 | return U;
|
1185 | }
|
1186 | else {
|
1187 | console.warn("[io-ts] Cannot build a tagged union for " + name + ", returning a de-optimized union");
|
1188 |
|
1189 | return new TaggedUnionType(name, U.is, U.validate, U.encode, codecs, tag);
|
1190 | }
|
1191 | };
|
1192 |
|
1193 |
|
1194 |
|
1195 | var ExactType = (function (_super) {
|
1196 | __extends(ExactType, _super);
|
1197 | function ExactType(name, is, validate, encode, type) {
|
1198 | var _this = _super.call(this, name, is, validate, encode) || this;
|
1199 | _this.type = type;
|
1200 | |
1201 |
|
1202 |
|
1203 | _this._tag = 'ExactType';
|
1204 | return _this;
|
1205 | }
|
1206 | return ExactType;
|
1207 | }(Type));
|
1208 | export { ExactType };
|
1209 | var getProps = function (codec) {
|
1210 | switch (codec._tag) {
|
1211 | case 'RefinementType':
|
1212 | case 'ReadonlyType':
|
1213 | return getProps(codec.type);
|
1214 | case 'InterfaceType':
|
1215 | case 'StrictType':
|
1216 | case 'PartialType':
|
1217 | return codec.props;
|
1218 | case 'IntersectionType':
|
1219 | return codec.types.reduce(function (props, type) { return Object.assign(props, getProps(type)); }, {});
|
1220 | }
|
1221 | };
|
1222 | var stripKeys = function (o, props) {
|
1223 | var keys = Object.getOwnPropertyNames(o);
|
1224 | var shouldStrip = false;
|
1225 | var r = {};
|
1226 | for (var i = 0; i < keys.length; i++) {
|
1227 | var key = keys[i];
|
1228 | if (!hasOwnProperty.call(props, key)) {
|
1229 | shouldStrip = true;
|
1230 | }
|
1231 | else {
|
1232 | r[key] = o[key];
|
1233 | }
|
1234 | }
|
1235 | return shouldStrip ? r : o;
|
1236 | };
|
1237 | var getExactTypeName = function (codec) {
|
1238 | if (isTypeC(codec)) {
|
1239 | return "{| " + getNameFromProps(codec.props) + " |}";
|
1240 | }
|
1241 | else if (isPartialC(codec)) {
|
1242 | return getPartialTypeName("{| " + getNameFromProps(codec.props) + " |}");
|
1243 | }
|
1244 | return "Exact<" + codec.name + ">";
|
1245 | };
|
1246 |
|
1247 |
|
1248 |
|
1249 |
|
1250 | export var exact = function (codec, name) {
|
1251 | if (name === void 0) { name = getExactTypeName(codec); }
|
1252 | var props = getProps(codec);
|
1253 | return new ExactType(name, codec.is, function (u, c) {
|
1254 | var e = UnknownRecord.validate(u, c);
|
1255 | if (isLeft(e)) {
|
1256 | return e;
|
1257 | }
|
1258 | var ce = codec.validate(u, c);
|
1259 | if (isLeft(ce)) {
|
1260 | return ce;
|
1261 | }
|
1262 | return right(stripKeys(ce.right, props));
|
1263 | }, function (a) { return codec.encode(stripKeys(a, props)); }, codec);
|
1264 | };
|
1265 | export {
|
1266 |
|
1267 |
|
1268 |
|
1269 | nullType as null };
|
1270 | export {
|
1271 |
|
1272 |
|
1273 |
|
1274 | undefinedType as undefined };
|
1275 | export {
|
1276 |
|
1277 |
|
1278 |
|
1279 |
|
1280 |
|
1281 | UnknownArray as Array };
|
1282 | export {
|
1283 |
|
1284 |
|
1285 |
|
1286 |
|
1287 |
|
1288 | type as interface };
|
1289 | export {
|
1290 |
|
1291 |
|
1292 |
|
1293 | voidType as void };
|
1294 |
|
1295 |
|
1296 |
|
1297 |
|
1298 | export var getValidationError = function (value, context) { return ({
|
1299 | value: value,
|
1300 | context: context
|
1301 | }); };
|
1302 |
|
1303 |
|
1304 |
|
1305 |
|
1306 | export var getDefaultContext = function (decoder) { return [
|
1307 | { key: '', type: decoder }
|
1308 | ]; };
|
1309 |
|
1310 |
|
1311 |
|
1312 |
|
1313 | var NeverType = (function (_super) {
|
1314 | __extends(NeverType, _super);
|
1315 | function NeverType() {
|
1316 | var _this = _super.call(this, 'never', function (_) { return false; }, function (u, c) { return failure(u, c); },
|
1317 |
|
1318 | function () {
|
1319 | throw new Error('cannot encode never');
|
1320 | }) || this;
|
1321 | |
1322 |
|
1323 |
|
1324 | _this._tag = 'NeverType';
|
1325 | return _this;
|
1326 | }
|
1327 | return NeverType;
|
1328 | }(Type));
|
1329 | export { NeverType };
|
1330 |
|
1331 |
|
1332 |
|
1333 |
|
1334 |
|
1335 | export var never = new NeverType();
|
1336 |
|
1337 |
|
1338 |
|
1339 |
|
1340 | var AnyType = (function (_super) {
|
1341 | __extends(AnyType, _super);
|
1342 | function AnyType() {
|
1343 | var _this = _super.call(this, 'any', function (_) { return true; }, success, identity) || this;
|
1344 | |
1345 |
|
1346 |
|
1347 | _this._tag = 'AnyType';
|
1348 | return _this;
|
1349 | }
|
1350 | return AnyType;
|
1351 | }(Type));
|
1352 | export { AnyType };
|
1353 |
|
1354 |
|
1355 |
|
1356 |
|
1357 |
|
1358 |
|
1359 | export var any = new AnyType();
|
1360 |
|
1361 |
|
1362 |
|
1363 |
|
1364 |
|
1365 | export var Dictionary = UnknownRecord;
|
1366 |
|
1367 |
|
1368 |
|
1369 |
|
1370 | var ObjectType = (function (_super) {
|
1371 | __extends(ObjectType, _super);
|
1372 | function ObjectType() {
|
1373 | var _this = _super.call(this, 'object', function (u) { return u !== null && typeof u === 'object'; }, function (u, c) { return (_this.is(u) ? success(u) : failure(u, c)); }, identity) || this;
|
1374 | |
1375 |
|
1376 |
|
1377 | _this._tag = 'ObjectType';
|
1378 | return _this;
|
1379 | }
|
1380 | return ObjectType;
|
1381 | }(Type));
|
1382 | export { ObjectType };
|
1383 |
|
1384 |
|
1385 |
|
1386 |
|
1387 |
|
1388 |
|
1389 | export var object = new ObjectType();
|
1390 |
|
1391 |
|
1392 |
|
1393 |
|
1394 |
|
1395 | export function refinement(codec, predicate, name) {
|
1396 | if (name === void 0) { name = "(" + codec.name + " | " + getFunctionName(predicate) + ")"; }
|
1397 | return new RefinementType(name, function (u) { return codec.is(u) && predicate(u); }, function (i, c) {
|
1398 | var e = codec.validate(i, c);
|
1399 | if (isLeft(e)) {
|
1400 | return e;
|
1401 | }
|
1402 | var a = e.right;
|
1403 | return predicate(a) ? success(a) : failure(a, c);
|
1404 | }, codec.encode, codec, predicate);
|
1405 | }
|
1406 |
|
1407 |
|
1408 |
|
1409 |
|
1410 |
|
1411 |
|
1412 | export var Integer = refinement(number, Number.isInteger, 'Integer');
|
1413 |
|
1414 |
|
1415 |
|
1416 |
|
1417 |
|
1418 | export var dictionary = record;
|
1419 |
|
1420 |
|
1421 |
|
1422 |
|
1423 | var StrictType = (function (_super) {
|
1424 | __extends(StrictType, _super);
|
1425 | function StrictType(name,
|
1426 | // tslint:disable-next-line: deprecation
|
1427 | is,
|
1428 | // tslint:disable-next-line: deprecation
|
1429 | validate,
|
1430 | // tslint:disable-next-line: deprecation
|
1431 | encode, props) {
|
1432 | var _this = _super.call(this, name, is, validate, encode) || this;
|
1433 | _this.props = props;
|
1434 | |
1435 |
|
1436 |
|
1437 | _this._tag = 'StrictType';
|
1438 | return _this;
|
1439 | }
|
1440 | return StrictType;
|
1441 | }(Type));
|
1442 | export { StrictType };
|
1443 |
|
1444 |
|
1445 |
|
1446 |
|
1447 |
|
1448 | export function clean(codec) {
|
1449 | return codec;
|
1450 | }
|
1451 | export function alias(codec) {
|
1452 | return function () { return codec; };
|
1453 | }
|
1454 | var isNonEmpty = function (as) { return as.length > 0; };
|
1455 |
|
1456 |
|
1457 |
|
1458 | export var emptyTags = {};
|
1459 | function intersect(a, b) {
|
1460 | var r = [];
|
1461 | for (var _i = 0, a_1 = a; _i < a_1.length; _i++) {
|
1462 | var v = a_1[_i];
|
1463 | if (b.indexOf(v) !== -1) {
|
1464 | r.push(v);
|
1465 | }
|
1466 | }
|
1467 | return r;
|
1468 | }
|
1469 | function mergeTags(a, b) {
|
1470 | if (a === emptyTags) {
|
1471 | return b;
|
1472 | }
|
1473 | if (b === emptyTags) {
|
1474 | return a;
|
1475 | }
|
1476 | var r = Object.assign({}, a);
|
1477 | for (var k in b) {
|
1478 | if (a.hasOwnProperty(k)) {
|
1479 | var intersection_1 = intersect(a[k], b[k]);
|
1480 | if (isNonEmpty(intersection_1)) {
|
1481 | r[k] = intersection_1;
|
1482 | }
|
1483 | else {
|
1484 | r = emptyTags;
|
1485 | break;
|
1486 | }
|
1487 | }
|
1488 | else {
|
1489 | r[k] = b[k];
|
1490 | }
|
1491 | }
|
1492 | return r;
|
1493 | }
|
1494 | function intersectTags(a, b) {
|
1495 | if (a === emptyTags || b === emptyTags) {
|
1496 | return emptyTags;
|
1497 | }
|
1498 | var r = emptyTags;
|
1499 | for (var k in a) {
|
1500 | if (b.hasOwnProperty(k)) {
|
1501 | var intersection_2 = intersect(a[k], b[k]);
|
1502 | if (intersection_2.length === 0) {
|
1503 | if (r === emptyTags) {
|
1504 | r = {};
|
1505 | }
|
1506 | r[k] = a[k].concat(b[k]);
|
1507 | }
|
1508 | }
|
1509 | }
|
1510 | return r;
|
1511 | }
|
1512 |
|
1513 | function isAnyC(codec) {
|
1514 | return codec._tag === 'AnyType';
|
1515 | }
|
1516 | function isLiteralC(codec) {
|
1517 | return codec._tag === 'LiteralType';
|
1518 | }
|
1519 | function isKeyofC(codec) {
|
1520 | return codec._tag === 'KeyofType';
|
1521 | }
|
1522 | function isTypeC(codec) {
|
1523 | return codec._tag === 'InterfaceType';
|
1524 | }
|
1525 | function isPartialC(codec) {
|
1526 | return codec._tag === 'PartialType';
|
1527 | }
|
1528 |
|
1529 | function isStrictC(codec) {
|
1530 | return codec._tag === 'StrictType';
|
1531 | }
|
1532 | function isExactC(codec) {
|
1533 | return codec._tag === 'ExactType';
|
1534 | }
|
1535 |
|
1536 | function isRefinementC(codec) {
|
1537 | return codec._tag === 'RefinementType';
|
1538 | }
|
1539 | function isIntersectionC(codec) {
|
1540 | return codec._tag === 'IntersectionType';
|
1541 | }
|
1542 | function isUnionC(codec) {
|
1543 | return codec._tag === 'UnionType';
|
1544 | }
|
1545 | function isRecursiveC(codec) {
|
1546 | return codec._tag === 'RecursiveType';
|
1547 | }
|
1548 | var lazyCodecs = [];
|
1549 |
|
1550 |
|
1551 |
|
1552 | export function getTags(codec) {
|
1553 | if (lazyCodecs.indexOf(codec) !== -1) {
|
1554 | return emptyTags;
|
1555 | }
|
1556 | if (isTypeC(codec) || isStrictC(codec)) {
|
1557 | var index = emptyTags;
|
1558 |
|
1559 | for (var k in codec.props) {
|
1560 | var prop = codec.props[k];
|
1561 | if (isLiteralC(prop)) {
|
1562 | if (index === emptyTags) {
|
1563 | index = {};
|
1564 | }
|
1565 | index[k] = [prop.value];
|
1566 | }
|
1567 | }
|
1568 | return index;
|
1569 | }
|
1570 | else if (isExactC(codec) || isRefinementC(codec)) {
|
1571 | return getTags(codec.type);
|
1572 | }
|
1573 | else if (isIntersectionC(codec)) {
|
1574 | return codec.types.reduce(function (tags, codec) { return mergeTags(tags, getTags(codec)); }, emptyTags);
|
1575 | }
|
1576 | else if (isUnionC(codec)) {
|
1577 | return codec.types.slice(1).reduce(function (tags, codec) { return intersectTags(tags, getTags(codec)); }, getTags(codec.types[0]));
|
1578 | }
|
1579 | else if (isRecursiveC(codec)) {
|
1580 | lazyCodecs.push(codec);
|
1581 | var tags = getTags(codec.type);
|
1582 | lazyCodecs.pop();
|
1583 | return tags;
|
1584 | }
|
1585 | return emptyTags;
|
1586 | }
|
1587 |
|
1588 |
|
1589 |
|
1590 | export function getIndex(codecs) {
|
1591 | var tags = getTags(codecs[0]);
|
1592 | var keys = Object.keys(tags);
|
1593 | var len = codecs.length;
|
1594 | var _loop_1 = function (k) {
|
1595 | var all = tags[k].slice();
|
1596 | var index = [tags[k]];
|
1597 | for (var i = 1; i < len; i++) {
|
1598 | var codec = codecs[i];
|
1599 | var ctags = getTags(codec);
|
1600 | var values = ctags[k];
|
1601 |
|
1602 | if (values === undefined) {
|
1603 | return "continue-keys";
|
1604 | }
|
1605 | else {
|
1606 | if (values.some(function (v) { return all.indexOf(v) !== -1; })) {
|
1607 | return "continue-keys";
|
1608 | }
|
1609 | else {
|
1610 | all.push.apply(all, values);
|
1611 | index.push(values);
|
1612 | }
|
1613 | }
|
1614 | }
|
1615 | return { value: [k, index] };
|
1616 | };
|
1617 | keys: for (var _i = 0, keys_1 = keys; _i < keys_1.length; _i++) {
|
1618 | var k = keys_1[_i];
|
1619 | var state_1 = _loop_1(k);
|
1620 | if (typeof state_1 === "object")
|
1621 | return state_1.value;
|
1622 | switch (state_1) {
|
1623 | case "continue-keys": continue keys;
|
1624 | }
|
1625 | }
|
1626 | return undefined;
|
1627 | }
|