decoders 2.0.0-beta1 → 2.0.0-beta5
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +4 -0
- package/{es → _esm}/_guard.js +0 -0
- package/{cjs → _esm}/_guard.js.flow +0 -0
- package/{es → _esm}/_types.js +0 -0
- package/{cjs → _esm}/_types.js.flow +0 -0
- package/{es → _esm}/_utils.js +0 -0
- package/{cjs → _esm}/_utils.js.flow +0 -0
- package/{es → _esm}/annotate.js +0 -0
- package/{cjs → _esm}/annotate.js.flow +0 -0
- package/{es/stdlib → _esm/core}/array.js +0 -0
- package/{cjs/stdlib → _esm/core}/array.js.flow +0 -0
- package/{es/stdlib → _esm/core}/boolean.js +0 -0
- package/{cjs/stdlib → _esm/core}/boolean.js.flow +0 -0
- package/{es/stdlib → _esm/core}/composition.js +0 -0
- package/{cjs/stdlib → _esm/core}/composition.js.flow +0 -0
- package/{es/stdlib → _esm/core}/constants.js +0 -0
- package/{cjs/stdlib → _esm/core}/constants.js.flow +0 -0
- package/{es/stdlib → _esm/core}/date.js +0 -0
- package/{cjs/stdlib → _esm/core}/date.js.flow +0 -0
- package/{es/stdlib → _esm/core}/describe.js +0 -0
- package/{cjs/stdlib → _esm/core}/describe.js.flow +0 -0
- package/{es/stdlib → _esm/core}/dispatch.js +0 -0
- package/{cjs/stdlib → _esm/core}/dispatch.js.flow +0 -0
- package/{es/stdlib → _esm/core}/either.js +0 -0
- package/{cjs/stdlib → _esm/core}/either.js.flow +0 -0
- package/{es/stdlib → _esm/core}/fail.js +0 -0
- package/{cjs/stdlib → _esm/core}/fail.js.flow +0 -0
- package/{es/stdlib → _esm/core}/instanceOf.js +0 -0
- package/{cjs/stdlib → _esm/core}/instanceOf.js.flow +0 -0
- package/{es/stdlib → _esm/core}/json.js +0 -0
- package/{cjs/stdlib → _esm/core}/json.js.flow +0 -0
- package/{es/stdlib → _esm/core}/lazy.js +0 -0
- package/{cjs/stdlib → _esm/core}/lazy.js.flow +0 -0
- package/{es/stdlib → _esm/core}/mapping.js +0 -0
- package/{cjs/stdlib → _esm/core}/mapping.js.flow +0 -0
- package/{es/stdlib → _esm/core}/number.js +0 -0
- package/{cjs/stdlib → _esm/core}/number.js.flow +0 -0
- package/{es/stdlib → _esm/core}/object.js +0 -0
- package/{cjs/stdlib → _esm/core}/object.js.flow +0 -0
- package/{es/stdlib → _esm/core}/optional.js +0 -0
- package/{cjs/stdlib → _esm/core}/optional.js.flow +0 -0
- package/{es/stdlib → _esm/core}/string.js +0 -0
- package/{cjs/stdlib → _esm/core}/string.js.flow +0 -0
- package/{es/stdlib → _esm/core}/tuple.js +7 -7
- package/{cjs/stdlib → _esm/core}/tuple.js.flow +21 -26
- package/{es → _esm}/format/index.js +0 -0
- package/{cjs → _esm}/format/index.js.flow +0 -0
- package/{es → _esm}/format/inline.js +0 -0
- package/{cjs → _esm}/format/inline.js.flow +0 -0
- package/{es → _esm}/format/short.js +0 -0
- package/{cjs → _esm}/format/short.js.flow +0 -0
- package/{es → _esm}/index.js +18 -18
- package/{cjs → _esm}/index.js.flow +29 -29
- package/{es → _esm}/result.js +14 -5
- package/{cjs → _esm}/result.js.flow +14 -6
- package/{cjs/_guard.js → _guard.js} +0 -0
- package/_guard.js.flow +20 -0
- package/{cjs/_types.js → _types.js} +0 -0
- package/_types.js.flow +20 -0
- package/{cjs/_utils.js → _utils.js} +0 -0
- package/_utils.js.flow +97 -0
- package/{cjs/annotate.js → annotate.js} +0 -0
- package/annotate.js.flow +218 -0
- package/{cjs/stdlib → core}/array.js +0 -0
- package/core/array.js.flow +103 -0
- package/{cjs/stdlib → core}/boolean.js +0 -0
- package/core/boolean.js.flow +29 -0
- package/{cjs/stdlib → core}/composition.js +0 -0
- package/core/composition.js.flow +43 -0
- package/{cjs/stdlib → core}/constants.js +0 -0
- package/core/constants.js.flow +46 -0
- package/{cjs/stdlib → core}/date.js +0 -0
- package/core/date.js.flow +40 -0
- package/{cjs/stdlib → core}/describe.js +0 -0
- package/core/describe.js.flow +17 -0
- package/{cjs/stdlib → core}/dispatch.js +0 -0
- package/core/dispatch.js.flow +58 -0
- package/{cjs/stdlib → core}/either.js +0 -0
- package/core/either.js.flow +151 -0
- package/{cjs/stdlib → core}/fail.js +0 -0
- package/core/fail.js.flow +12 -0
- package/{cjs/stdlib → core}/instanceOf.js +0 -0
- package/core/instanceOf.js.flow +20 -0
- package/{cjs/stdlib → core}/json.js +0 -0
- package/core/json.js.flow +28 -0
- package/{cjs/stdlib → core}/lazy.js +0 -0
- package/core/lazy.js.flow +15 -0
- package/{cjs/stdlib → core}/mapping.js +0 -0
- package/core/mapping.js.flow +54 -0
- package/{cjs/stdlib → core}/number.js +0 -0
- package/core/number.js.flow +34 -0
- package/{cjs/stdlib → core}/object.js +0 -0
- package/core/object.js.flow +203 -0
- package/{cjs/stdlib → core}/optional.js +0 -0
- package/core/optional.js.flow +41 -0
- package/{cjs/stdlib → core}/string.js +0 -0
- package/core/string.js.flow +82 -0
- package/{cjs/stdlib → core}/tuple.js +6 -6
- package/core/tuple.js.flow +215 -0
- package/{cjs/format → format}/index.js +0 -0
- package/format/index.js.flow +4 -0
- package/{cjs/format → format}/inline.js +0 -0
- package/format/inline.js.flow +122 -0
- package/{cjs/format → format}/short.js +0 -0
- package/format/short.js.flow +8 -0
- package/{cjs/index.js → index.js} +18 -18
- package/index.js.flow +63 -0
- package/package.json +3 -12
- package/{cjs/result.js → result.js} +18 -7
- package/result.js.flow +174 -0
- package/ts/_guard.d.ts +0 -7
- package/ts/_helpers.d.ts +0 -79
- package/ts/_types.d.ts +0 -16
- package/ts/_utils.d.ts +0 -13
- package/ts/annotate.d.ts +0 -58
- package/ts/array.d.ts +0 -5
- package/ts/boolean.d.ts +0 -5
- package/ts/constants.d.ts +0 -11
- package/ts/date.d.ts +0 -4
- package/ts/describe.d.ts +0 -3
- package/ts/dispatch.d.ts +0 -8
- package/ts/either.d.ts +0 -61
- package/ts/fail.d.ts +0 -3
- package/ts/index.d.ts +0 -42
- package/ts/inline.d.ts +0 -3
- package/ts/instanceOf.d.ts +0 -3
- package/ts/json.d.ts +0 -11
- package/ts/lazy.d.ts +0 -3
- package/ts/mapping.d.ts +0 -4
- package/ts/number.d.ts +0 -6
- package/ts/object.d.ts +0 -33
- package/ts/optional.d.ts +0 -5
- package/ts/result.d.ts +0 -39
- package/ts/short.d.ts +0 -3
- package/ts/string.d.ts +0 -7
- package/ts/tuple.d.ts +0 -30
|
@@ -37,7 +37,7 @@ function tuple1(decoder1) {
|
|
|
37
37
|
} catch (e) {
|
|
38
38
|
// If a decoder error has happened while unwrapping all the
|
|
39
39
|
// results, try to construct a good error message
|
|
40
|
-
return (0, _result.err)((0, _annotate.annotate)((0, _result.
|
|
40
|
+
return (0, _result.err)((0, _annotate.annotate)([(0, _result.okOrErrValue)(result1)]));
|
|
41
41
|
}
|
|
42
42
|
});
|
|
43
43
|
}
|
|
@@ -59,7 +59,7 @@ function tuple2(decoder1, decoder2) {
|
|
|
59
59
|
} catch (e) {
|
|
60
60
|
// If a decoder error has happened while unwrapping all the
|
|
61
61
|
// results, try to construct a good error message
|
|
62
|
-
return (0, _result.err)((0, _annotate.annotate)([(0, _result.
|
|
62
|
+
return (0, _result.err)((0, _annotate.annotate)([(0, _result.okOrErrValue)(result1), (0, _result.okOrErrValue)(result2)]));
|
|
63
63
|
}
|
|
64
64
|
});
|
|
65
65
|
}
|
|
@@ -83,7 +83,7 @@ function tuple3(decoder1, decoder2, decoder3) {
|
|
|
83
83
|
} catch (e) {
|
|
84
84
|
// If a decoder error has happened while unwrapping all the
|
|
85
85
|
// results, try to construct a good error message
|
|
86
|
-
return (0, _result.err)((0, _annotate.annotate)([(0, _result.
|
|
86
|
+
return (0, _result.err)((0, _annotate.annotate)([(0, _result.okOrErrValue)(result1), (0, _result.okOrErrValue)(result2), (0, _result.okOrErrValue)(result3)]));
|
|
87
87
|
}
|
|
88
88
|
});
|
|
89
89
|
}
|
|
@@ -109,7 +109,7 @@ function tuple4(decoder1, decoder2, decoder3, decoder4) {
|
|
|
109
109
|
} catch (e) {
|
|
110
110
|
// If a decoder error has happened while unwrapping all the
|
|
111
111
|
// results, try to construct a good error message
|
|
112
|
-
return (0, _result.err)((0, _annotate.annotate)([(0, _result.
|
|
112
|
+
return (0, _result.err)((0, _annotate.annotate)([(0, _result.okOrErrValue)(result1), (0, _result.okOrErrValue)(result2), (0, _result.okOrErrValue)(result3), (0, _result.okOrErrValue)(result4)]));
|
|
113
113
|
}
|
|
114
114
|
});
|
|
115
115
|
}
|
|
@@ -137,7 +137,7 @@ function tuple5(decoder1, decoder2, decoder3, decoder4, decoder5) {
|
|
|
137
137
|
} catch (e) {
|
|
138
138
|
// If a decoder error has happened while unwrapping all the
|
|
139
139
|
// results, try to construct a good error message
|
|
140
|
-
return (0, _result.err)((0, _annotate.annotate)([(0, _result.
|
|
140
|
+
return (0, _result.err)((0, _annotate.annotate)([(0, _result.okOrErrValue)(result1), (0, _result.okOrErrValue)(result2), (0, _result.okOrErrValue)(result3), (0, _result.okOrErrValue)(result4), (0, _result.okOrErrValue)(result5)]));
|
|
141
141
|
}
|
|
142
142
|
});
|
|
143
143
|
}
|
|
@@ -167,7 +167,7 @@ function tuple6(decoder1, decoder2, decoder3, decoder4, decoder5, decoder6) {
|
|
|
167
167
|
} catch (e) {
|
|
168
168
|
// If a decoder error has happened while unwrapping all the
|
|
169
169
|
// results, try to construct a good error message
|
|
170
|
-
return (0, _result.err)((0, _annotate.annotate)([(0, _result.
|
|
170
|
+
return (0, _result.err)((0, _annotate.annotate)([(0, _result.okOrErrValue)(result1), (0, _result.okOrErrValue)(result2), (0, _result.okOrErrValue)(result3), (0, _result.okOrErrValue)(result4), (0, _result.okOrErrValue)(result5), (0, _result.okOrErrValue)(result6)]));
|
|
171
171
|
}
|
|
172
172
|
});
|
|
173
173
|
}
|
|
@@ -0,0 +1,215 @@
|
|
|
1
|
+
// @flow strict
|
|
2
|
+
|
|
3
|
+
import { annotate } from '../annotate';
|
|
4
|
+
import { compose, predicate } from './composition';
|
|
5
|
+
import { err, ok, okOrErrValue, unwrap } from '../result';
|
|
6
|
+
import { poja } from './array';
|
|
7
|
+
import type { Decoder } from '../_types';
|
|
8
|
+
|
|
9
|
+
const ntuple = (n: number) =>
|
|
10
|
+
compose(
|
|
11
|
+
poja,
|
|
12
|
+
predicate((arr) => arr.length === n, `Must be a ${n}-tuple`),
|
|
13
|
+
);
|
|
14
|
+
|
|
15
|
+
/**
|
|
16
|
+
* Builds a Decoder that returns Ok for 1-tuple of [T], given a Decoder for T.
|
|
17
|
+
* Err otherwise.
|
|
18
|
+
*/
|
|
19
|
+
export function tuple1<T>(decoder1: Decoder<T>): Decoder<[T]> {
|
|
20
|
+
return compose(ntuple(1), (blobs: $ReadOnlyArray<mixed>) => {
|
|
21
|
+
const [blob1] = blobs;
|
|
22
|
+
|
|
23
|
+
const result1 = decoder1(blob1);
|
|
24
|
+
try {
|
|
25
|
+
return ok([unwrap(result1)]);
|
|
26
|
+
} catch (e) {
|
|
27
|
+
// If a decoder error has happened while unwrapping all the
|
|
28
|
+
// results, try to construct a good error message
|
|
29
|
+
return err(annotate([okOrErrValue(result1)]));
|
|
30
|
+
}
|
|
31
|
+
});
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
/**
|
|
35
|
+
* Builds a Decoder that returns Ok for 2-tuples of [T1, T2], given Decoders
|
|
36
|
+
* for T1 and T2. Err otherwise.
|
|
37
|
+
*/
|
|
38
|
+
export function tuple2<T1, T2>(
|
|
39
|
+
decoder1: Decoder<T1>,
|
|
40
|
+
decoder2: Decoder<T2>,
|
|
41
|
+
): Decoder<[T1, T2]> {
|
|
42
|
+
return compose(ntuple(2), (blobs: $ReadOnlyArray<mixed>) => {
|
|
43
|
+
const [blob1, blob2] = blobs;
|
|
44
|
+
|
|
45
|
+
const result1 = decoder1(blob1);
|
|
46
|
+
const result2 = decoder2(blob2);
|
|
47
|
+
try {
|
|
48
|
+
return ok([unwrap(result1), unwrap(result2)]);
|
|
49
|
+
} catch (e) {
|
|
50
|
+
// If a decoder error has happened while unwrapping all the
|
|
51
|
+
// results, try to construct a good error message
|
|
52
|
+
return err(annotate([okOrErrValue(result1), okOrErrValue(result2)]));
|
|
53
|
+
}
|
|
54
|
+
});
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
/**
|
|
58
|
+
* Builds a Decoder that returns Ok for 3-tuples of [T1, T2, T3], given
|
|
59
|
+
* Decoders for T1, T2, and T3. Err otherwise.
|
|
60
|
+
*/
|
|
61
|
+
export function tuple3<T1, T2, T3>(
|
|
62
|
+
decoder1: Decoder<T1>,
|
|
63
|
+
decoder2: Decoder<T2>,
|
|
64
|
+
decoder3: Decoder<T3>,
|
|
65
|
+
): Decoder<[T1, T2, T3]> {
|
|
66
|
+
return compose(ntuple(3), (blobs: $ReadOnlyArray<mixed>) => {
|
|
67
|
+
const [blob1, blob2, blob3] = blobs;
|
|
68
|
+
|
|
69
|
+
const result1 = decoder1(blob1);
|
|
70
|
+
const result2 = decoder2(blob2);
|
|
71
|
+
const result3 = decoder3(blob3);
|
|
72
|
+
try {
|
|
73
|
+
return ok([unwrap(result1), unwrap(result2), unwrap(result3)]);
|
|
74
|
+
} catch (e) {
|
|
75
|
+
// If a decoder error has happened while unwrapping all the
|
|
76
|
+
// results, try to construct a good error message
|
|
77
|
+
return err(
|
|
78
|
+
annotate([
|
|
79
|
+
okOrErrValue(result1),
|
|
80
|
+
okOrErrValue(result2),
|
|
81
|
+
okOrErrValue(result3),
|
|
82
|
+
]),
|
|
83
|
+
);
|
|
84
|
+
}
|
|
85
|
+
});
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
/**
|
|
89
|
+
* Builds a Decoder that returns Ok for 4-tuples of [T1, T2, T3, T4], given
|
|
90
|
+
* Decoders for T1, T2, T3, and T4. Err otherwise.
|
|
91
|
+
*/
|
|
92
|
+
export function tuple4<T1, T2, T3, T4>(
|
|
93
|
+
decoder1: Decoder<T1>,
|
|
94
|
+
decoder2: Decoder<T2>,
|
|
95
|
+
decoder3: Decoder<T3>,
|
|
96
|
+
decoder4: Decoder<T4>,
|
|
97
|
+
): Decoder<[T1, T2, T3, T4]> {
|
|
98
|
+
return compose(ntuple(4), (blobs: $ReadOnlyArray<mixed>) => {
|
|
99
|
+
const [blob1, blob2, blob3, blob4] = blobs;
|
|
100
|
+
|
|
101
|
+
const result1 = decoder1(blob1);
|
|
102
|
+
const result2 = decoder2(blob2);
|
|
103
|
+
const result3 = decoder3(blob3);
|
|
104
|
+
const result4 = decoder4(blob4);
|
|
105
|
+
try {
|
|
106
|
+
return ok([
|
|
107
|
+
unwrap(result1),
|
|
108
|
+
unwrap(result2),
|
|
109
|
+
unwrap(result3),
|
|
110
|
+
unwrap(result4),
|
|
111
|
+
]);
|
|
112
|
+
} catch (e) {
|
|
113
|
+
// If a decoder error has happened while unwrapping all the
|
|
114
|
+
// results, try to construct a good error message
|
|
115
|
+
return err(
|
|
116
|
+
annotate([
|
|
117
|
+
okOrErrValue(result1),
|
|
118
|
+
okOrErrValue(result2),
|
|
119
|
+
okOrErrValue(result3),
|
|
120
|
+
okOrErrValue(result4),
|
|
121
|
+
]),
|
|
122
|
+
);
|
|
123
|
+
}
|
|
124
|
+
});
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
/**
|
|
128
|
+
* Builds a Decoder that returns Ok for 5-tuples of [T1, T2, T3, T4, T5], given
|
|
129
|
+
* Decoders for T1, T2, T3, T4, and T5. Err otherwise.
|
|
130
|
+
*/
|
|
131
|
+
export function tuple5<T1, T2, T3, T4, T5>(
|
|
132
|
+
decoder1: Decoder<T1>,
|
|
133
|
+
decoder2: Decoder<T2>,
|
|
134
|
+
decoder3: Decoder<T3>,
|
|
135
|
+
decoder4: Decoder<T4>,
|
|
136
|
+
decoder5: Decoder<T5>,
|
|
137
|
+
): Decoder<[T1, T2, T3, T4, T5]> {
|
|
138
|
+
return compose(ntuple(5), (blobs: $ReadOnlyArray<mixed>) => {
|
|
139
|
+
const [blob1, blob2, blob3, blob4, blob5] = blobs;
|
|
140
|
+
|
|
141
|
+
const result1 = decoder1(blob1);
|
|
142
|
+
const result2 = decoder2(blob2);
|
|
143
|
+
const result3 = decoder3(blob3);
|
|
144
|
+
const result4 = decoder4(blob4);
|
|
145
|
+
const result5 = decoder5(blob5);
|
|
146
|
+
try {
|
|
147
|
+
return ok([
|
|
148
|
+
unwrap(result1),
|
|
149
|
+
unwrap(result2),
|
|
150
|
+
unwrap(result3),
|
|
151
|
+
unwrap(result4),
|
|
152
|
+
unwrap(result5),
|
|
153
|
+
]);
|
|
154
|
+
} catch (e) {
|
|
155
|
+
// If a decoder error has happened while unwrapping all the
|
|
156
|
+
// results, try to construct a good error message
|
|
157
|
+
return err(
|
|
158
|
+
annotate([
|
|
159
|
+
okOrErrValue(result1),
|
|
160
|
+
okOrErrValue(result2),
|
|
161
|
+
okOrErrValue(result3),
|
|
162
|
+
okOrErrValue(result4),
|
|
163
|
+
okOrErrValue(result5),
|
|
164
|
+
]),
|
|
165
|
+
);
|
|
166
|
+
}
|
|
167
|
+
});
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
/**
|
|
171
|
+
* Builds a Decoder that returns Ok for 5-tuples of [T1, T2, T3, T4, T5], given
|
|
172
|
+
* Decoders for T1, T2, T3, T4, T5, and T6. Err otherwise.
|
|
173
|
+
*/
|
|
174
|
+
export function tuple6<T1, T2, T3, T4, T5, T6>(
|
|
175
|
+
decoder1: Decoder<T1>,
|
|
176
|
+
decoder2: Decoder<T2>,
|
|
177
|
+
decoder3: Decoder<T3>,
|
|
178
|
+
decoder4: Decoder<T4>,
|
|
179
|
+
decoder5: Decoder<T5>,
|
|
180
|
+
decoder6: Decoder<T6>,
|
|
181
|
+
): Decoder<[T1, T2, T3, T4, T5, T6]> {
|
|
182
|
+
return compose(ntuple(6), (blobs: $ReadOnlyArray<mixed>) => {
|
|
183
|
+
const [blob1, blob2, blob3, blob4, blob5, blob6] = blobs;
|
|
184
|
+
|
|
185
|
+
const result1 = decoder1(blob1);
|
|
186
|
+
const result2 = decoder2(blob2);
|
|
187
|
+
const result3 = decoder3(blob3);
|
|
188
|
+
const result4 = decoder4(blob4);
|
|
189
|
+
const result5 = decoder5(blob5);
|
|
190
|
+
const result6 = decoder6(blob6);
|
|
191
|
+
try {
|
|
192
|
+
return ok([
|
|
193
|
+
unwrap(result1),
|
|
194
|
+
unwrap(result2),
|
|
195
|
+
unwrap(result3),
|
|
196
|
+
unwrap(result4),
|
|
197
|
+
unwrap(result5),
|
|
198
|
+
unwrap(result6),
|
|
199
|
+
]);
|
|
200
|
+
} catch (e) {
|
|
201
|
+
// If a decoder error has happened while unwrapping all the
|
|
202
|
+
// results, try to construct a good error message
|
|
203
|
+
return err(
|
|
204
|
+
annotate([
|
|
205
|
+
okOrErrValue(result1),
|
|
206
|
+
okOrErrValue(result2),
|
|
207
|
+
okOrErrValue(result3),
|
|
208
|
+
okOrErrValue(result4),
|
|
209
|
+
okOrErrValue(result5),
|
|
210
|
+
okOrErrValue(result6),
|
|
211
|
+
]),
|
|
212
|
+
);
|
|
213
|
+
}
|
|
214
|
+
});
|
|
215
|
+
}
|
|
File without changes
|
|
File without changes
|
|
@@ -0,0 +1,122 @@
|
|
|
1
|
+
// @flow strict
|
|
2
|
+
|
|
3
|
+
import { asDate, indent, INDENT, isMultiline } from '../_utils';
|
|
4
|
+
import type { Annotation, ArrayAnnotation, ObjectAnnotation } from '../annotate';
|
|
5
|
+
|
|
6
|
+
function serializeString(s: string, width: number = 80): string {
|
|
7
|
+
// Full string
|
|
8
|
+
// Abbreviated to $maxlen i.e. "Vincent Driess..." [truncated]
|
|
9
|
+
let ser = JSON.stringify(s);
|
|
10
|
+
if (ser.length <= width) {
|
|
11
|
+
return ser;
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
// Cut off a bit
|
|
15
|
+
const truncated = s.substring(0, width - 15) + '...';
|
|
16
|
+
ser = JSON.stringify(truncated) + ' [truncated]';
|
|
17
|
+
return ser;
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
function serializeArray(annotation: ArrayAnnotation, prefix: string): string {
|
|
21
|
+
const { items } = annotation;
|
|
22
|
+
if (items.length === 0) {
|
|
23
|
+
return '[]';
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
const result = [];
|
|
27
|
+
items.forEach((item) => {
|
|
28
|
+
const [ser, ann] = serializeAnnotation(item, prefix + INDENT);
|
|
29
|
+
result.push(prefix + INDENT + ser + ',');
|
|
30
|
+
if (ann !== undefined) {
|
|
31
|
+
result.push(indent(ann, prefix + INDENT));
|
|
32
|
+
}
|
|
33
|
+
});
|
|
34
|
+
return ['[', ...result, prefix + ']'].join('\n');
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
function serializeObject(annotation: ObjectAnnotation, prefix: string): string {
|
|
38
|
+
const { fields } = annotation;
|
|
39
|
+
|
|
40
|
+
const fieldNames = Object.keys(fields);
|
|
41
|
+
if (fieldNames.length === 0) {
|
|
42
|
+
return '{}';
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
const result = [];
|
|
46
|
+
fieldNames.forEach((key) => {
|
|
47
|
+
const valueAnnotation = fields[key];
|
|
48
|
+
const kser = serializeValue(key);
|
|
49
|
+
|
|
50
|
+
const valPrefix = prefix + INDENT + ' '.repeat(kser.length + 2);
|
|
51
|
+
const [vser, vann] = serializeAnnotation(valueAnnotation, prefix + INDENT);
|
|
52
|
+
|
|
53
|
+
result.push(prefix + INDENT + kser + ': ' + vser + ',');
|
|
54
|
+
if (vann !== undefined) {
|
|
55
|
+
result.push(indent(vann, valPrefix));
|
|
56
|
+
}
|
|
57
|
+
});
|
|
58
|
+
return ['{', ...result, prefix + '}'].join('\n');
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
export function serializeValue(value: mixed): string {
|
|
62
|
+
// istanbul ignore else
|
|
63
|
+
if (typeof value === 'string') {
|
|
64
|
+
return serializeString(value);
|
|
65
|
+
} else if (typeof value === 'number' || typeof value === 'boolean') {
|
|
66
|
+
return value.toString();
|
|
67
|
+
} else if (value === null) {
|
|
68
|
+
return 'null';
|
|
69
|
+
} else if (value === undefined) {
|
|
70
|
+
return 'undefined';
|
|
71
|
+
} else {
|
|
72
|
+
const valueAsDate = asDate(value);
|
|
73
|
+
if (valueAsDate !== null) {
|
|
74
|
+
return `new Date(${JSON.stringify(valueAsDate.toISOString())})`;
|
|
75
|
+
} else if (value instanceof Date) {
|
|
76
|
+
// NOTE: Using `instanceof Date` is unreliable way of checking dates.
|
|
77
|
+
// If this case occurs (and it didn't pass the prior isDate())
|
|
78
|
+
// check, then this must be the case where it's an invalid date.
|
|
79
|
+
return '(Invalid Date)';
|
|
80
|
+
} else {
|
|
81
|
+
return '(unserializable)';
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
export function serializeAnnotation(
|
|
87
|
+
ann: Annotation,
|
|
88
|
+
prefix: string = '',
|
|
89
|
+
): [string, string | void] {
|
|
90
|
+
// The serialized data (the input object echoed back)
|
|
91
|
+
let serialized;
|
|
92
|
+
if (ann.type === 'array') {
|
|
93
|
+
serialized = serializeArray(ann, prefix);
|
|
94
|
+
} else if (ann.type === 'object') {
|
|
95
|
+
serialized = serializeObject(ann, prefix);
|
|
96
|
+
} else if (ann.type === 'function') {
|
|
97
|
+
serialized = '<function>';
|
|
98
|
+
} else if (ann.type === 'circular-ref') {
|
|
99
|
+
serialized = '<circular ref>';
|
|
100
|
+
} else if (ann.type === 'unknown') {
|
|
101
|
+
serialized = '???';
|
|
102
|
+
} else {
|
|
103
|
+
serialized = serializeValue(ann.value);
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
const text = ann.text;
|
|
107
|
+
if (text !== undefined) {
|
|
108
|
+
const sep = '^'.repeat(isMultiline(serialized) ? 1 : serialized.length);
|
|
109
|
+
return [serialized, [sep, text].join(isMultiline(text) ? '\n' : ' ')];
|
|
110
|
+
} else {
|
|
111
|
+
return [serialized, undefined];
|
|
112
|
+
}
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
export function formatInline(ann: Annotation): string {
|
|
116
|
+
const [serialized, annotation] = serializeAnnotation(ann);
|
|
117
|
+
if (annotation !== undefined) {
|
|
118
|
+
return serialized + '\n' + annotation;
|
|
119
|
+
} else {
|
|
120
|
+
return serialized;
|
|
121
|
+
}
|
|
122
|
+
}
|
|
File without changes
|
|
@@ -7,25 +7,25 @@ var _guard = require("./_guard");
|
|
|
7
7
|
|
|
8
8
|
exports.guard = _guard.guard;
|
|
9
9
|
|
|
10
|
-
var _composition = require("./
|
|
10
|
+
var _composition = require("./core/composition");
|
|
11
11
|
|
|
12
12
|
exports.compose = _composition.compose;
|
|
13
13
|
exports.map = _composition.map;
|
|
14
14
|
exports.predicate = _composition.predicate;
|
|
15
15
|
|
|
16
|
-
var _array = require("./
|
|
16
|
+
var _array = require("./core/array");
|
|
17
17
|
|
|
18
18
|
exports.array = _array.array;
|
|
19
19
|
exports.nonEmptyArray = _array.nonEmptyArray;
|
|
20
20
|
exports.poja = _array.poja;
|
|
21
21
|
|
|
22
|
-
var _boolean = require("./
|
|
22
|
+
var _boolean = require("./core/boolean");
|
|
23
23
|
|
|
24
24
|
exports["boolean"] = _boolean["boolean"];
|
|
25
25
|
exports.numericBoolean = _boolean.numericBoolean;
|
|
26
26
|
exports.truthy = _boolean.truthy;
|
|
27
27
|
|
|
28
|
-
var _constants = require("./
|
|
28
|
+
var _constants = require("./core/constants");
|
|
29
29
|
|
|
30
30
|
exports.constant = _constants.constant;
|
|
31
31
|
exports.hardcoded = _constants.hardcoded;
|
|
@@ -34,20 +34,20 @@ exports.null_ = _constants.null_;
|
|
|
34
34
|
exports.undefined_ = _constants.undefined_;
|
|
35
35
|
exports.unknown = _constants.unknown;
|
|
36
36
|
|
|
37
|
-
var _date = require("./
|
|
37
|
+
var _date = require("./core/date");
|
|
38
38
|
|
|
39
39
|
exports.date = _date.date;
|
|
40
40
|
exports.iso8601 = _date.iso8601;
|
|
41
41
|
|
|
42
|
-
var _describe = require("./
|
|
42
|
+
var _describe = require("./core/describe");
|
|
43
43
|
|
|
44
44
|
exports.describe = _describe.describe;
|
|
45
45
|
|
|
46
|
-
var _dispatch = require("./
|
|
46
|
+
var _dispatch = require("./core/dispatch");
|
|
47
47
|
|
|
48
48
|
exports.dispatch = _dispatch.dispatch;
|
|
49
49
|
|
|
50
|
-
var _either = require("./
|
|
50
|
+
var _either = require("./core/either");
|
|
51
51
|
|
|
52
52
|
exports.either = _either.either;
|
|
53
53
|
exports.either3 = _either.either3;
|
|
@@ -59,50 +59,50 @@ exports.either8 = _either.either8;
|
|
|
59
59
|
exports.either9 = _either.either9;
|
|
60
60
|
exports.oneOf = _either.oneOf;
|
|
61
61
|
|
|
62
|
-
var _fail = require("./
|
|
62
|
+
var _fail = require("./core/fail");
|
|
63
63
|
|
|
64
64
|
exports.fail = _fail.fail;
|
|
65
65
|
|
|
66
|
-
var _instanceOf = require("./
|
|
66
|
+
var _instanceOf = require("./core/instanceOf");
|
|
67
67
|
|
|
68
68
|
exports.instanceOf = _instanceOf.instanceOf;
|
|
69
69
|
|
|
70
|
-
var _json = require("./
|
|
70
|
+
var _json = require("./core/json");
|
|
71
71
|
|
|
72
72
|
exports.json = _json.json;
|
|
73
73
|
exports.jsonObject = _json.jsonObject;
|
|
74
74
|
exports.jsonArray = _json.jsonArray;
|
|
75
75
|
|
|
76
|
-
var _lazy = require("./
|
|
76
|
+
var _lazy = require("./core/lazy");
|
|
77
77
|
|
|
78
78
|
exports.lazy = _lazy.lazy;
|
|
79
79
|
|
|
80
|
-
var _mapping = require("./
|
|
80
|
+
var _mapping = require("./core/mapping");
|
|
81
81
|
|
|
82
82
|
exports.mapping = _mapping.mapping;
|
|
83
83
|
exports.dict = _mapping.dict;
|
|
84
84
|
|
|
85
|
-
var _number = require("./
|
|
85
|
+
var _number = require("./core/number");
|
|
86
86
|
|
|
87
87
|
exports.integer = _number.integer;
|
|
88
88
|
exports.number = _number.number;
|
|
89
89
|
exports.positiveInteger = _number.positiveInteger;
|
|
90
90
|
exports.positiveNumber = _number.positiveNumber;
|
|
91
91
|
|
|
92
|
-
var _object = require("./
|
|
92
|
+
var _object = require("./core/object");
|
|
93
93
|
|
|
94
94
|
exports.exact = _object.exact;
|
|
95
95
|
exports.inexact = _object.inexact;
|
|
96
96
|
exports.object = _object.object;
|
|
97
97
|
exports.pojo = _object.pojo;
|
|
98
98
|
|
|
99
|
-
var _optional = require("./
|
|
99
|
+
var _optional = require("./core/optional");
|
|
100
100
|
|
|
101
101
|
exports.maybe = _optional.maybe;
|
|
102
102
|
exports.nullable = _optional.nullable;
|
|
103
103
|
exports.optional = _optional.optional;
|
|
104
104
|
|
|
105
|
-
var _string = require("./
|
|
105
|
+
var _string = require("./core/string");
|
|
106
106
|
|
|
107
107
|
exports.email = _string.email;
|
|
108
108
|
exports.nonEmptyString = _string.nonEmptyString;
|
|
@@ -110,7 +110,7 @@ exports.regex = _string.regex;
|
|
|
110
110
|
exports.string = _string.string;
|
|
111
111
|
exports.url = _string.url;
|
|
112
112
|
|
|
113
|
-
var _tuple = require("./
|
|
113
|
+
var _tuple = require("./core/tuple");
|
|
114
114
|
|
|
115
115
|
exports.tuple1 = _tuple.tuple1;
|
|
116
116
|
exports.tuple2 = _tuple.tuple2;
|
package/index.js.flow
ADDED
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
// @flow strict
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Elm-like JSON decoders, for use with Flow.
|
|
5
|
+
* See http://elmplayground.com/decoding-json-in-elm-1 for an introduction.
|
|
6
|
+
*
|
|
7
|
+
* Why? All JSON responses coming from our API endpoints are just that: free-form
|
|
8
|
+
* JSON data. To Flow, the only type classification possilbe is "any" -- effectively
|
|
9
|
+
* turning off all type checks for anything related to JSON. To the receiving end
|
|
10
|
+
* (our frontend), the structure of that data is completely opaque to any type
|
|
11
|
+
* checkers since JSON values can be anything: an object, an array, null, a string,
|
|
12
|
+
* a bool, etc. Our type system is not a runtime type system, so we need a way of
|
|
13
|
+
* "converting" an any-type JSON value into a type that we want to work with in our
|
|
14
|
+
* frontend code base.
|
|
15
|
+
*
|
|
16
|
+
* Elm's solution to this problem is to define composable decoders: functions that
|
|
17
|
+
* take anything and either fail with an error, or guarantee to return the expected
|
|
18
|
+
* type. In our case, it's fine to fail with a runtime error.
|
|
19
|
+
*
|
|
20
|
+
*/
|
|
21
|
+
export type {
|
|
22
|
+
Decoder,
|
|
23
|
+
DecodeResult,
|
|
24
|
+
DecoderType,
|
|
25
|
+
Guard,
|
|
26
|
+
GuardType,
|
|
27
|
+
Predicate,
|
|
28
|
+
Scalar,
|
|
29
|
+
} from './_types';
|
|
30
|
+
export type { Result } from './result';
|
|
31
|
+
export type { JSONValue, JSONObject, JSONArray } from './core/json';
|
|
32
|
+
|
|
33
|
+
export { guard } from './_guard';
|
|
34
|
+
|
|
35
|
+
export { compose, map, predicate } from './core/composition';
|
|
36
|
+
|
|
37
|
+
export { array, nonEmptyArray, poja } from './core/array';
|
|
38
|
+
export { boolean, numericBoolean, truthy } from './core/boolean';
|
|
39
|
+
export { constant, hardcoded, mixed, null_, undefined_, unknown } from './core/constants';
|
|
40
|
+
export { date, iso8601 } from './core/date';
|
|
41
|
+
export { describe } from './core/describe';
|
|
42
|
+
export { dispatch } from './core/dispatch';
|
|
43
|
+
export {
|
|
44
|
+
either,
|
|
45
|
+
either3,
|
|
46
|
+
either4,
|
|
47
|
+
either5,
|
|
48
|
+
either6,
|
|
49
|
+
either7,
|
|
50
|
+
either8,
|
|
51
|
+
either9,
|
|
52
|
+
oneOf,
|
|
53
|
+
} from './core/either';
|
|
54
|
+
export { fail } from './core/fail';
|
|
55
|
+
export { instanceOf } from './core/instanceOf';
|
|
56
|
+
export { json, jsonObject, jsonArray } from './core/json';
|
|
57
|
+
export { lazy } from './core/lazy';
|
|
58
|
+
export { mapping, dict } from './core/mapping';
|
|
59
|
+
export { integer, number, positiveInteger, positiveNumber } from './core/number';
|
|
60
|
+
export { exact, inexact, object, pojo } from './core/object';
|
|
61
|
+
export { maybe, nullable, optional } from './core/optional';
|
|
62
|
+
export { email, nonEmptyString, regex, string, url } from './core/string';
|
|
63
|
+
export { tuple1, tuple2, tuple3, tuple4, tuple5, tuple6 } from './core/tuple';
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "decoders",
|
|
3
|
-
"version": "2.0.0-
|
|
3
|
+
"version": "2.0.0-beta5",
|
|
4
4
|
"description": "Elegant and battle-tested validation library for type-safe input data (for TypeScript and Flow)",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"repository": {
|
|
@@ -12,16 +12,8 @@
|
|
|
12
12
|
"bugs": {
|
|
13
13
|
"url": "https://github.com/nvie/decoders/issues"
|
|
14
14
|
},
|
|
15
|
-
"main": "./
|
|
16
|
-
"module": "./
|
|
17
|
-
"type": "module",
|
|
18
|
-
"exports": {
|
|
19
|
-
"require": "./cjs/index.js",
|
|
20
|
-
"import": "./es/index.js"
|
|
21
|
-
},
|
|
22
|
-
"engines": {
|
|
23
|
-
"node": ">=12"
|
|
24
|
-
},
|
|
15
|
+
"main": "./index.js",
|
|
16
|
+
"module": "./_esm/index.js",
|
|
25
17
|
"keywords": [
|
|
26
18
|
"decoder",
|
|
27
19
|
"decoders",
|
|
@@ -48,7 +40,6 @@
|
|
|
48
40
|
"map",
|
|
49
41
|
"predicate"
|
|
50
42
|
],
|
|
51
|
-
"types": "./ts/index.d.ts",
|
|
52
43
|
"githubUrl": "https://github.com/nvie/decoders",
|
|
53
44
|
"sideEffects": false
|
|
54
45
|
}
|