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