UNPKG

9.1 kBJavaScriptView Raw
1"use strict";
2var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3 if (k2 === undefined) k2 = k;
4 Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } });
5}) : (function(o, m, k, k2) {
6 if (k2 === undefined) k2 = k;
7 o[k2] = m[k];
8}));
9var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
10 Object.defineProperty(o, "default", { enumerable: true, value: v });
11}) : function(o, v) {
12 o["default"] = v;
13});
14var __importStar = (this && this.__importStar) || function (mod) {
15 if (mod && mod.__esModule) return mod;
16 var result = {};
17 if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
18 __setModuleDefault(result, mod);
19 return result;
20};
21Object.defineProperty(exports, "__esModule", { value: true });
22exports.Invariant = exports.URI = exports.imap = exports.compose = exports.readonly = exports.lazy = exports.sum = exports.fromSum = exports.intersect = exports.tuple = exports.fromTuple = exports.record = exports.fromRecord = exports.array = exports.fromArray = exports.partial = exports.fromPartial = exports.type = exports.struct = exports.fromType = exports.fromStruct = exports.nullable = exports.refine = exports.mapLeftWithInput = exports.UnknownRecord = exports.UnknownArray = exports.boolean = exports.number = exports.string = exports.literal = exports.fromDecoder = exports.make = void 0;
23/**
24 * **This module is experimental**
25 *
26 * Experimental features are published in order to get early feedback from the community, see these tracking
27 * [issues](https://github.com/gcanti/io-ts/issues?q=label%3Av2.2+) for further discussions and enhancements.
28 *
29 * A feature tagged as _Experimental_ is in a high state of flux, you're at risk of it changing without notice.
30 *
31 * @since 2.2.3
32 */
33var function_1 = require("fp-ts/lib/function");
34var pipeable_1 = require("fp-ts/lib/pipeable");
35var D = __importStar(require("./Decoder"));
36var E = __importStar(require("./Encoder"));
37// -------------------------------------------------------------------------------------
38// constructors
39// -------------------------------------------------------------------------------------
40/**
41 * @category constructors
42 * @since 2.2.3
43 */
44function make(decoder, encoder) {
45 return {
46 decode: decoder.decode,
47 encode: encoder.encode
48 };
49}
50exports.make = make;
51/**
52 * @category constructors
53 * @since 2.2.3
54 */
55function fromDecoder(decoder) {
56 return {
57 decode: decoder.decode,
58 encode: function_1.identity
59 };
60}
61exports.fromDecoder = fromDecoder;
62/**
63 * @category constructors
64 * @since 2.2.3
65 */
66function literal() {
67 var values = [];
68 for (var _i = 0; _i < arguments.length; _i++) {
69 values[_i] = arguments[_i];
70 }
71 return fromDecoder(D.literal.apply(D, values));
72}
73exports.literal = literal;
74// -------------------------------------------------------------------------------------
75// primitives
76// -------------------------------------------------------------------------------------
77/**
78 * @category primitives
79 * @since 2.2.3
80 */
81exports.string =
82/*#__PURE__*/
83fromDecoder(D.string);
84/**
85 * @category primitives
86 * @since 2.2.3
87 */
88exports.number =
89/*#__PURE__*/
90fromDecoder(D.number);
91/**
92 * @category primitives
93 * @since 2.2.3
94 */
95exports.boolean =
96/*#__PURE__*/
97fromDecoder(D.boolean);
98/**
99 * @category primitives
100 * @since 2.2.3
101 */
102exports.UnknownArray =
103/*#__PURE__*/
104fromDecoder(D.UnknownArray);
105/**
106 * @category primitives
107 * @since 2.2.3
108 */
109exports.UnknownRecord =
110/*#__PURE__*/
111fromDecoder(D.UnknownRecord);
112// -------------------------------------------------------------------------------------
113// combinators
114// -------------------------------------------------------------------------------------
115/**
116 * @category combinators
117 * @since 2.2.3
118 */
119var mapLeftWithInput = function (f) { return function (codec) { return make(pipeable_1.pipe(codec, D.mapLeftWithInput(f)), codec); }; };
120exports.mapLeftWithInput = mapLeftWithInput;
121/**
122 * @category combinators
123 * @since 2.2.3
124 */
125var refine = function (refinement, id) {
126 var refine = D.refine(refinement, id);
127 return function (from) { return make(refine(from), from); };
128};
129exports.refine = refine;
130/**
131 * @category combinators
132 * @since 2.2.3
133 */
134function nullable(or) {
135 return make(D.nullable(or), E.nullable(or));
136}
137exports.nullable = nullable;
138/**
139 * @category combinators
140 * @since 2.2.15
141 */
142function fromStruct(properties) {
143 return make(D.fromStruct(properties), E.struct(properties));
144}
145exports.fromStruct = fromStruct;
146/**
147 * Use `fromStruct` instead.
148 *
149 * @category combinators
150 * @since 2.2.8
151 * @deprecated
152 */
153exports.fromType = fromStruct;
154/**
155 * @category combinators
156 * @since 2.2.15
157 */
158function struct(properties) {
159 return pipeable_1.pipe(exports.UnknownRecord, exports.compose(fromStruct(properties)));
160}
161exports.struct = struct;
162/**
163 * Use `struct` instead.
164 *
165 * @category combinators
166 * @since 2.2.3
167 * @deprecated
168 */
169exports.type = struct;
170/**
171 * @category combinators
172 * @since 2.2.8
173 */
174function fromPartial(properties) {
175 return make(D.fromPartial(properties), E.partial(properties));
176}
177exports.fromPartial = fromPartial;
178/**
179 * @category combinators
180 * @since 2.2.3
181 */
182function partial(properties) {
183 return pipeable_1.pipe(exports.UnknownRecord, exports.compose(fromPartial(properties)));
184}
185exports.partial = partial;
186/**
187 * @category combinators
188 * @since 2.2.3
189 */
190function fromArray(item) {
191 return make(D.fromArray(item), E.array(item));
192}
193exports.fromArray = fromArray;
194/**
195 * @category combinators
196 * @since 2.2.3
197 */
198function array(item) {
199 return pipeable_1.pipe(exports.UnknownArray, exports.compose(fromArray(item)));
200}
201exports.array = array;
202/**
203 * @category combinators
204 * @since 2.2.3
205 */
206function fromRecord(codomain) {
207 return make(D.fromRecord(codomain), E.record(codomain));
208}
209exports.fromRecord = fromRecord;
210/**
211 * @category combinators
212 * @since 2.2.3
213 */
214function record(codomain) {
215 return pipeable_1.pipe(exports.UnknownRecord, exports.compose(fromRecord(codomain)));
216}
217exports.record = record;
218/**
219 * @category combinators
220 * @since 2.2.8
221 */
222var fromTuple = function () {
223 var components = [];
224 for (var _i = 0; _i < arguments.length; _i++) {
225 components[_i] = arguments[_i];
226 }
227 return make(D.fromTuple.apply(D, components), E.tuple.apply(E, components));
228};
229exports.fromTuple = fromTuple;
230/**
231 * @category combinators
232 * @since 2.2.3
233 */
234function tuple() {
235 var components = [];
236 for (var _i = 0; _i < arguments.length; _i++) {
237 components[_i] = arguments[_i];
238 }
239 return pipeable_1.pipe(exports.UnknownArray, exports.compose(exports.fromTuple.apply(void 0, components)));
240}
241exports.tuple = tuple;
242/**
243 * @category combinators
244 * @since 2.2.3
245 */
246var intersect = function (right) {
247 var intersectD = D.intersect(right);
248 var intersectE = E.intersect(right);
249 return function (left) { return make(intersectD(left), intersectE(left)); };
250};
251exports.intersect = intersect;
252/**
253 * @category combinators
254 * @since 2.2.8
255 */
256var fromSum = function (tag) {
257 var decoder = D.fromSum(tag);
258 var encoder = E.sum(tag);
259 return function (members) { return make(decoder(members), encoder(members)); };
260};
261exports.fromSum = fromSum;
262/**
263 * @category combinators
264 * @since 2.2.3
265 */
266function sum(tag) {
267 var sum = exports.fromSum(tag);
268 return function (members) { return pipeable_1.pipe(exports.UnknownRecord, exports.compose(sum(members))); };
269}
270exports.sum = sum;
271/**
272 * @category combinators
273 * @since 2.2.3
274 */
275function lazy(id, f) {
276 return make(D.lazy(id, f), E.lazy(f));
277}
278exports.lazy = lazy;
279/**
280 * @category combinators
281 * @since 2.2.16
282 */
283exports.readonly = function_1.identity;
284/**
285 * @category combinators
286 * @since 2.2.8
287 */
288var compose = function (to) { return function (from) { return make(D.compose(to)(from), E.compose(from)(to)); }; };
289exports.compose = compose;
290// -------------------------------------------------------------------------------------
291// non-pipeables
292// -------------------------------------------------------------------------------------
293var imap_ = function (fa, f, g) { return make(D.Functor.map(fa, f), E.Contravariant.contramap(fa, g)); };
294// -------------------------------------------------------------------------------------
295// pipeables
296// -------------------------------------------------------------------------------------
297/**
298 * @category Invariant
299 * @since 2.2.3
300 */
301var imap = function (f, g) { return function (fa) { return imap_(fa, f, g); }; };
302exports.imap = imap;
303// -------------------------------------------------------------------------------------
304// instances
305// -------------------------------------------------------------------------------------
306/**
307 * @category instances
308 * @since 2.2.3
309 */
310exports.URI = 'io-ts/Codec';
311/**
312 * @category instances
313 * @since 2.2.8
314 */
315exports.Invariant = {
316 URI: exports.URI,
317 imap: imap_
318};