1 | import * as D from './Decoder';
|
2 | import * as E from './Encoder';
|
3 | import { identity } from 'fp-ts/es6/function';
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 | export function make(decoder, encoder) {
|
11 | return {
|
12 | decode: decoder.decode,
|
13 | encode: encoder.encode
|
14 | };
|
15 | }
|
16 |
|
17 |
|
18 |
|
19 | export function fromDecoder(decoder) {
|
20 | return {
|
21 | decode: decoder.decode,
|
22 | encode: identity
|
23 | };
|
24 | }
|
25 |
|
26 |
|
27 |
|
28 | export function literal() {
|
29 | var values = [];
|
30 | for (var _i = 0; _i < arguments.length; _i++) {
|
31 | values[_i] = arguments[_i];
|
32 | }
|
33 | return fromDecoder(D.literal.apply(D, values));
|
34 | }
|
35 |
|
36 |
|
37 |
|
38 |
|
39 |
|
40 |
|
41 | export var string = fromDecoder(D.string);
|
42 |
|
43 |
|
44 |
|
45 | export var number = fromDecoder(D.number);
|
46 |
|
47 |
|
48 |
|
49 | export var boolean = fromDecoder(D.boolean);
|
50 |
|
51 |
|
52 |
|
53 | export var UnknownArray = fromDecoder(D.UnknownArray);
|
54 |
|
55 |
|
56 |
|
57 | export var UnknownRecord = fromDecoder(D.UnknownRecord);
|
58 |
|
59 |
|
60 |
|
61 |
|
62 |
|
63 |
|
64 | export function withExpected(codec, expected) {
|
65 | return make(D.withExpected(codec, expected), codec);
|
66 | }
|
67 |
|
68 |
|
69 |
|
70 | export function refinement(from, refinement, expected) {
|
71 | return make(D.refinement(from, refinement, expected), from);
|
72 | }
|
73 |
|
74 |
|
75 |
|
76 | export function nullable(or) {
|
77 | return make(D.nullable(or), E.nullable(or));
|
78 | }
|
79 |
|
80 |
|
81 |
|
82 | export function type(properties) {
|
83 | var decoder = D.type(properties);
|
84 | return make(decoder, E.type(properties));
|
85 | }
|
86 |
|
87 |
|
88 |
|
89 | export function partial(properties) {
|
90 |
|
91 |
|
92 | return make(D.partial(properties), E.partial(properties));
|
93 | }
|
94 |
|
95 |
|
96 |
|
97 | export function record(codomain) {
|
98 | return make(D.record(codomain), E.record(codomain));
|
99 | }
|
100 |
|
101 |
|
102 |
|
103 | export function array(items) {
|
104 | return make(D.array(items), E.array(items));
|
105 | }
|
106 |
|
107 |
|
108 |
|
109 | export function tuple() {
|
110 | var components = [];
|
111 | for (var _i = 0; _i < arguments.length; _i++) {
|
112 | components[_i] = arguments[_i];
|
113 | }
|
114 | var decoder = D.tuple.apply(D, components);
|
115 | var encoder = E.tuple.apply(E, components);
|
116 | return make(decoder, encoder);
|
117 | }
|
118 |
|
119 |
|
120 |
|
121 | export function intersection(left, right) {
|
122 | return make(D.intersection(left, right), E.intersection(left, right));
|
123 | }
|
124 |
|
125 |
|
126 |
|
127 | export function sum(tag) {
|
128 | var sumD = D.sum(tag);
|
129 | var sumE = E.sum(tag);
|
130 | return function (members) { return make(sumD(members), sumE(members)); };
|
131 | }
|
132 |
|
133 |
|
134 |
|
135 | export function lazy(id, f) {
|
136 | return make(D.lazy(id, f), E.lazy(f));
|
137 | }
|
138 |
|
139 |
|
140 |
|
141 |
|
142 |
|
143 |
|
144 | export var imap = function (f, g) { return function (fa) {
|
145 | return imap_(fa, f, g);
|
146 | }; };
|
147 | var imap_ = function (fa, f, g) {
|
148 | return make(D.functorDecoder.map(fa, f), E.contravariantEncoder.contramap(fa, g));
|
149 | };
|
150 |
|
151 |
|
152 |
|
153 |
|
154 |
|
155 |
|
156 | export var URI = 'io-ts/Codec';
|
157 |
|
158 |
|
159 |
|
160 | export var invariantCodec = {
|
161 | URI: URI,
|
162 | imap: imap_
|
163 | };
|