decoders 1.26.0-test1 → 2.0.0-beta4
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 +27 -3
- package/_esm/_guard.js +15 -0
- package/_esm/_guard.js.flow +20 -0
- package/_esm/_types.js +0 -0
- package/_esm/_types.js.flow +20 -0
- package/_esm/_utils.js +93 -0
- package/_esm/_utils.js.flow +97 -0
- package/_esm/annotate.js +144 -0
- package/_esm/annotate.js.flow +218 -0
- package/_esm/core/array.js +91 -0
- package/{cjs → _esm/core}/array.js.flow +22 -25
- package/_esm/core/boolean.js +28 -0
- package/{cjs → _esm/core}/boolean.js.flow +8 -7
- package/_esm/core/composition.js +42 -0
- package/{cjs/utils.js.flow → _esm/core/composition.js.flow} +7 -22
- package/_esm/core/constants.js +46 -0
- package/{cjs → _esm/core}/constants.js.flow +13 -12
- package/_esm/core/date.js +28 -0
- package/{cjs → _esm/core}/date.js.flow +9 -7
- package/{cjs → _esm/core}/describe.js +5 -11
- package/{cjs → _esm/core}/describe.js.flow +4 -4
- package/{cjs → _esm/core}/dispatch.js +8 -15
- package/{cjs → _esm/core}/dispatch.js.flow +6 -5
- package/_esm/core/either.js +90 -0
- package/{cjs → _esm/core}/either.js.flow +55 -35
- package/_esm/core/fail.js +11 -0
- package/_esm/core/fail.js.flow +12 -0
- package/_esm/core/instanceOf.js +8 -0
- package/{cjs → _esm/core}/instanceOf.js.flow +7 -8
- package/_esm/core/json.js +15 -0
- package/{cjs → _esm/core}/json.js.flow +4 -4
- package/_esm/core/lazy.js +11 -0
- package/{cjs → _esm/core}/lazy.js.flow +1 -1
- package/_esm/core/mapping.js +54 -0
- package/_esm/core/mapping.js.flow +54 -0
- package/_esm/core/number.js +25 -0
- package/{cjs → _esm/core}/number.js.flow +10 -11
- package/_esm/core/object.js +175 -0
- package/{cjs → _esm/core}/object.js.flow +44 -52
- package/_esm/core/optional.js +38 -0
- package/{cjs → _esm/core}/optional.js.flow +6 -7
- package/{cjs → _esm/core}/string.js +18 -35
- package/{cjs → _esm/core}/string.js.flow +13 -12
- package/_esm/core/tuple.js +155 -0
- package/{cjs → _esm/core}/tuple.js.flow +59 -65
- package/_esm/format/index.js +2 -0
- package/_esm/format/index.js.flow +4 -0
- package/_esm/format/inline.js +137 -0
- package/_esm/format/inline.js.flow +122 -0
- package/_esm/format/short.js +4 -0
- package/_esm/format/short.js.flow +8 -0
- package/_esm/index.js +37 -0
- package/{cjs → _esm}/index.js.flow +31 -24
- package/_esm/result.js +142 -0
- package/_esm/result.js.flow +170 -0
- package/_guard.js +26 -0
- package/_guard.js.flow +20 -0
- package/_types.js +1 -0
- package/_types.js.flow +20 -0
- package/_utils.js +108 -0
- package/_utils.js.flow +97 -0
- package/annotate.js +161 -0
- package/annotate.js.flow +218 -0
- package/core/array.js +108 -0
- package/core/array.js.flow +103 -0
- package/core/boolean.js +44 -0
- package/core/boolean.js.flow +29 -0
- package/core/composition.js +56 -0
- package/core/composition.js.flow +43 -0
- package/core/constants.js +69 -0
- package/core/constants.js.flow +46 -0
- package/core/date.js +46 -0
- package/core/date.js.flow +40 -0
- package/core/describe.js +26 -0
- package/core/describe.js.flow +17 -0
- package/core/dispatch.js +62 -0
- package/core/dispatch.js.flow +58 -0
- package/core/either.js +117 -0
- package/core/either.js.flow +151 -0
- package/core/fail.js +21 -0
- package/core/fail.js.flow +12 -0
- package/core/instanceOf.js +19 -0
- package/core/instanceOf.js.flow +20 -0
- package/{cjs → core}/json.js +3 -5
- package/core/json.js.flow +28 -0
- package/{cjs → core}/lazy.js +1 -3
- package/core/lazy.js.flow +15 -0
- package/core/mapping.js +67 -0
- package/core/mapping.js.flow +54 -0
- package/core/number.js +40 -0
- package/core/number.js.flow +34 -0
- package/core/object.js +194 -0
- package/core/object.js.flow +203 -0
- package/core/optional.js +54 -0
- package/core/optional.js.flow +41 -0
- package/core/string.js +98 -0
- package/core/string.js.flow +82 -0
- package/core/tuple.js +173 -0
- package/core/tuple.js.flow +215 -0
- package/format/index.js +12 -0
- package/format/index.js.flow +4 -0
- package/format/inline.js +146 -0
- package/format/inline.js.flow +122 -0
- package/format/short.js +10 -0
- package/format/short.js.flow +8 -0
- package/index.js +120 -0
- package/index.js.flow +63 -0
- package/package.json +3 -8
- package/result.js +177 -0
- package/result.js.flow +170 -0
- package/cjs/array.js +0 -133
- package/cjs/boolean.js +0 -42
- package/cjs/constants.js +0 -67
- package/cjs/date.js +0 -42
- package/cjs/either.js +0 -85
- package/cjs/fail.js +0 -19
- package/cjs/fail.js.flow +0 -13
- package/cjs/guard.js +0 -30
- package/cjs/guard.js.flow +0 -36
- package/cjs/index.js +0 -397
- package/cjs/instanceOf.js +0 -17
- package/cjs/mapping.js +0 -113
- package/cjs/mapping.js.flow +0 -71
- package/cjs/number.js +0 -38
- package/cjs/object.js +0 -254
- package/cjs/optional.js +0 -52
- package/cjs/tuple.js +0 -199
- package/cjs/types.js +0 -5
- package/cjs/types.js.flow +0 -26
- package/cjs/utils.js +0 -70
- package/es/index.js +0 -1212
- 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/guard.d.ts +0 -7
- package/ts/helpers.d.ts +0 -79
- package/ts/index.d.ts +0 -38
- 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/string.d.ts +0 -7
- package/ts/tuple.d.ts +0 -30
- package/ts/types.d.ts +0 -18
- package/ts/utils.d.ts +0 -13
|
@@ -0,0 +1,155 @@
|
|
|
1
|
+
import { annotate } from '../annotate';
|
|
2
|
+
import { compose, predicate } from './composition';
|
|
3
|
+
import { err, ok, okOrErrValue, unwrap } from '../result';
|
|
4
|
+
import { poja } from './array';
|
|
5
|
+
|
|
6
|
+
var ntuple = function ntuple(n) {
|
|
7
|
+
return compose(poja, predicate(function (arr) {
|
|
8
|
+
return arr.length === n;
|
|
9
|
+
}, "Must be a " + n + "-tuple"));
|
|
10
|
+
};
|
|
11
|
+
/**
|
|
12
|
+
* Builds a Decoder that returns Ok for 1-tuple of [T], given a Decoder for T.
|
|
13
|
+
* Err otherwise.
|
|
14
|
+
*/
|
|
15
|
+
|
|
16
|
+
|
|
17
|
+
export function tuple1(decoder1) {
|
|
18
|
+
return compose(ntuple(1), function (blobs) {
|
|
19
|
+
var blob1 = blobs[0];
|
|
20
|
+
var result1 = decoder1(blob1);
|
|
21
|
+
|
|
22
|
+
try {
|
|
23
|
+
return ok([unwrap(result1)]);
|
|
24
|
+
} catch (e) {
|
|
25
|
+
// If a decoder error has happened while unwrapping all the
|
|
26
|
+
// results, try to construct a good error message
|
|
27
|
+
return err(annotate([okOrErrValue(result1)]));
|
|
28
|
+
}
|
|
29
|
+
});
|
|
30
|
+
}
|
|
31
|
+
/**
|
|
32
|
+
* Builds a Decoder that returns Ok for 2-tuples of [T1, T2], given Decoders
|
|
33
|
+
* for T1 and T2. Err otherwise.
|
|
34
|
+
*/
|
|
35
|
+
|
|
36
|
+
export function tuple2(decoder1, decoder2) {
|
|
37
|
+
return compose(ntuple(2), function (blobs) {
|
|
38
|
+
var blob1 = blobs[0],
|
|
39
|
+
blob2 = blobs[1];
|
|
40
|
+
var result1 = decoder1(blob1);
|
|
41
|
+
var result2 = decoder2(blob2);
|
|
42
|
+
|
|
43
|
+
try {
|
|
44
|
+
return ok([unwrap(result1), unwrap(result2)]);
|
|
45
|
+
} catch (e) {
|
|
46
|
+
// If a decoder error has happened while unwrapping all the
|
|
47
|
+
// results, try to construct a good error message
|
|
48
|
+
return err(annotate([okOrErrValue(result1), okOrErrValue(result2)]));
|
|
49
|
+
}
|
|
50
|
+
});
|
|
51
|
+
}
|
|
52
|
+
/**
|
|
53
|
+
* Builds a Decoder that returns Ok for 3-tuples of [T1, T2, T3], given
|
|
54
|
+
* Decoders for T1, T2, and T3. Err otherwise.
|
|
55
|
+
*/
|
|
56
|
+
|
|
57
|
+
export function tuple3(decoder1, decoder2, decoder3) {
|
|
58
|
+
return compose(ntuple(3), function (blobs) {
|
|
59
|
+
var blob1 = blobs[0],
|
|
60
|
+
blob2 = blobs[1],
|
|
61
|
+
blob3 = blobs[2];
|
|
62
|
+
var result1 = decoder1(blob1);
|
|
63
|
+
var result2 = decoder2(blob2);
|
|
64
|
+
var result3 = decoder3(blob3);
|
|
65
|
+
|
|
66
|
+
try {
|
|
67
|
+
return ok([unwrap(result1), unwrap(result2), unwrap(result3)]);
|
|
68
|
+
} catch (e) {
|
|
69
|
+
// If a decoder error has happened while unwrapping all the
|
|
70
|
+
// results, try to construct a good error message
|
|
71
|
+
return err(annotate([okOrErrValue(result1), okOrErrValue(result2), okOrErrValue(result3)]));
|
|
72
|
+
}
|
|
73
|
+
});
|
|
74
|
+
}
|
|
75
|
+
/**
|
|
76
|
+
* Builds a Decoder that returns Ok for 4-tuples of [T1, T2, T3, T4], given
|
|
77
|
+
* Decoders for T1, T2, T3, and T4. Err otherwise.
|
|
78
|
+
*/
|
|
79
|
+
|
|
80
|
+
export function tuple4(decoder1, decoder2, decoder3, decoder4) {
|
|
81
|
+
return compose(ntuple(4), function (blobs) {
|
|
82
|
+
var blob1 = blobs[0],
|
|
83
|
+
blob2 = blobs[1],
|
|
84
|
+
blob3 = blobs[2],
|
|
85
|
+
blob4 = blobs[3];
|
|
86
|
+
var result1 = decoder1(blob1);
|
|
87
|
+
var result2 = decoder2(blob2);
|
|
88
|
+
var result3 = decoder3(blob3);
|
|
89
|
+
var result4 = decoder4(blob4);
|
|
90
|
+
|
|
91
|
+
try {
|
|
92
|
+
return ok([unwrap(result1), unwrap(result2), unwrap(result3), unwrap(result4)]);
|
|
93
|
+
} catch (e) {
|
|
94
|
+
// If a decoder error has happened while unwrapping all the
|
|
95
|
+
// results, try to construct a good error message
|
|
96
|
+
return err(annotate([okOrErrValue(result1), okOrErrValue(result2), okOrErrValue(result3), okOrErrValue(result4)]));
|
|
97
|
+
}
|
|
98
|
+
});
|
|
99
|
+
}
|
|
100
|
+
/**
|
|
101
|
+
* Builds a Decoder that returns Ok for 5-tuples of [T1, T2, T3, T4, T5], given
|
|
102
|
+
* Decoders for T1, T2, T3, T4, and T5. Err otherwise.
|
|
103
|
+
*/
|
|
104
|
+
|
|
105
|
+
export function tuple5(decoder1, decoder2, decoder3, decoder4, decoder5) {
|
|
106
|
+
return compose(ntuple(5), function (blobs) {
|
|
107
|
+
var blob1 = blobs[0],
|
|
108
|
+
blob2 = blobs[1],
|
|
109
|
+
blob3 = blobs[2],
|
|
110
|
+
blob4 = blobs[3],
|
|
111
|
+
blob5 = blobs[4];
|
|
112
|
+
var result1 = decoder1(blob1);
|
|
113
|
+
var result2 = decoder2(blob2);
|
|
114
|
+
var result3 = decoder3(blob3);
|
|
115
|
+
var result4 = decoder4(blob4);
|
|
116
|
+
var result5 = decoder5(blob5);
|
|
117
|
+
|
|
118
|
+
try {
|
|
119
|
+
return ok([unwrap(result1), unwrap(result2), unwrap(result3), unwrap(result4), unwrap(result5)]);
|
|
120
|
+
} catch (e) {
|
|
121
|
+
// If a decoder error has happened while unwrapping all the
|
|
122
|
+
// results, try to construct a good error message
|
|
123
|
+
return err(annotate([okOrErrValue(result1), okOrErrValue(result2), okOrErrValue(result3), okOrErrValue(result4), okOrErrValue(result5)]));
|
|
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, T5, and T6. Err otherwise.
|
|
130
|
+
*/
|
|
131
|
+
|
|
132
|
+
export function tuple6(decoder1, decoder2, decoder3, decoder4, decoder5, decoder6) {
|
|
133
|
+
return compose(ntuple(6), function (blobs) {
|
|
134
|
+
var blob1 = blobs[0],
|
|
135
|
+
blob2 = blobs[1],
|
|
136
|
+
blob3 = blobs[2],
|
|
137
|
+
blob4 = blobs[3],
|
|
138
|
+
blob5 = blobs[4],
|
|
139
|
+
blob6 = blobs[5];
|
|
140
|
+
var result1 = decoder1(blob1);
|
|
141
|
+
var result2 = decoder2(blob2);
|
|
142
|
+
var result3 = decoder3(blob3);
|
|
143
|
+
var result4 = decoder4(blob4);
|
|
144
|
+
var result5 = decoder5(blob5);
|
|
145
|
+
var result6 = decoder6(blob6);
|
|
146
|
+
|
|
147
|
+
try {
|
|
148
|
+
return ok([unwrap(result1), unwrap(result2), unwrap(result3), unwrap(result4), unwrap(result5), unwrap(result6)]);
|
|
149
|
+
} catch (e) {
|
|
150
|
+
// If a decoder error has happened while unwrapping all the
|
|
151
|
+
// results, try to construct a good error message
|
|
152
|
+
return err(annotate([okOrErrValue(result1), okOrErrValue(result2), okOrErrValue(result3), okOrErrValue(result4), okOrErrValue(result5), okOrErrValue(result6)]));
|
|
153
|
+
}
|
|
154
|
+
});
|
|
155
|
+
}
|
|
@@ -1,16 +1,15 @@
|
|
|
1
1
|
// @flow strict
|
|
2
2
|
|
|
3
|
-
import { annotate } from '
|
|
4
|
-
import {
|
|
5
|
-
|
|
3
|
+
import { annotate } from '../annotate';
|
|
4
|
+
import { compose, predicate } from './composition';
|
|
5
|
+
import { err, ok, okOrErrValue, unwrap } from '../result';
|
|
6
6
|
import { poja } from './array';
|
|
7
|
-
import type { Decoder } from '
|
|
8
|
-
import { compose, predicate } from './utils';
|
|
7
|
+
import type { Decoder } from '../_types';
|
|
9
8
|
|
|
10
9
|
const ntuple = (n: number) =>
|
|
11
10
|
compose(
|
|
12
11
|
poja,
|
|
13
|
-
predicate((arr) => arr.length === n, `Must be a ${n}-tuple`)
|
|
12
|
+
predicate((arr) => arr.length === n, `Must be a ${n}-tuple`),
|
|
14
13
|
);
|
|
15
14
|
|
|
16
15
|
/**
|
|
@@ -23,11 +22,11 @@ export function tuple1<T>(decoder1: Decoder<T>): Decoder<[T]> {
|
|
|
23
22
|
|
|
24
23
|
const result1 = decoder1(blob1);
|
|
25
24
|
try {
|
|
26
|
-
return
|
|
25
|
+
return ok([unwrap(result1)]);
|
|
27
26
|
} catch (e) {
|
|
28
27
|
// If a decoder error has happened while unwrapping all the
|
|
29
28
|
// results, try to construct a good error message
|
|
30
|
-
return
|
|
29
|
+
return err(annotate([okOrErrValue(result1)]));
|
|
31
30
|
}
|
|
32
31
|
});
|
|
33
32
|
}
|
|
@@ -38,7 +37,7 @@ export function tuple1<T>(decoder1: Decoder<T>): Decoder<[T]> {
|
|
|
38
37
|
*/
|
|
39
38
|
export function tuple2<T1, T2>(
|
|
40
39
|
decoder1: Decoder<T1>,
|
|
41
|
-
decoder2: Decoder<T2
|
|
40
|
+
decoder2: Decoder<T2>,
|
|
42
41
|
): Decoder<[T1, T2]> {
|
|
43
42
|
return compose(ntuple(2), (blobs: $ReadOnlyArray<mixed>) => {
|
|
44
43
|
const [blob1, blob2] = blobs;
|
|
@@ -46,16 +45,11 @@ export function tuple2<T1, T2>(
|
|
|
46
45
|
const result1 = decoder1(blob1);
|
|
47
46
|
const result2 = decoder2(blob2);
|
|
48
47
|
try {
|
|
49
|
-
return
|
|
48
|
+
return ok([unwrap(result1), unwrap(result2)]);
|
|
50
49
|
} catch (e) {
|
|
51
50
|
// If a decoder error has happened while unwrapping all the
|
|
52
51
|
// results, try to construct a good error message
|
|
53
|
-
return
|
|
54
|
-
annotate([
|
|
55
|
-
result1.isErr() ? result1.errValue() : result1.value(),
|
|
56
|
-
result2.isErr() ? result2.errValue() : result2.value(),
|
|
57
|
-
])
|
|
58
|
-
);
|
|
52
|
+
return err(annotate([okOrErrValue(result1), okOrErrValue(result2)]));
|
|
59
53
|
}
|
|
60
54
|
});
|
|
61
55
|
}
|
|
@@ -67,7 +61,7 @@ export function tuple2<T1, T2>(
|
|
|
67
61
|
export function tuple3<T1, T2, T3>(
|
|
68
62
|
decoder1: Decoder<T1>,
|
|
69
63
|
decoder2: Decoder<T2>,
|
|
70
|
-
decoder3: Decoder<T3
|
|
64
|
+
decoder3: Decoder<T3>,
|
|
71
65
|
): Decoder<[T1, T2, T3]> {
|
|
72
66
|
return compose(ntuple(3), (blobs: $ReadOnlyArray<mixed>) => {
|
|
73
67
|
const [blob1, blob2, blob3] = blobs;
|
|
@@ -76,16 +70,16 @@ export function tuple3<T1, T2, T3>(
|
|
|
76
70
|
const result2 = decoder2(blob2);
|
|
77
71
|
const result3 = decoder3(blob3);
|
|
78
72
|
try {
|
|
79
|
-
return
|
|
73
|
+
return ok([unwrap(result1), unwrap(result2), unwrap(result3)]);
|
|
80
74
|
} catch (e) {
|
|
81
75
|
// If a decoder error has happened while unwrapping all the
|
|
82
76
|
// results, try to construct a good error message
|
|
83
|
-
return
|
|
77
|
+
return err(
|
|
84
78
|
annotate([
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
])
|
|
79
|
+
okOrErrValue(result1),
|
|
80
|
+
okOrErrValue(result2),
|
|
81
|
+
okOrErrValue(result3),
|
|
82
|
+
]),
|
|
89
83
|
);
|
|
90
84
|
}
|
|
91
85
|
});
|
|
@@ -99,7 +93,7 @@ export function tuple4<T1, T2, T3, T4>(
|
|
|
99
93
|
decoder1: Decoder<T1>,
|
|
100
94
|
decoder2: Decoder<T2>,
|
|
101
95
|
decoder3: Decoder<T3>,
|
|
102
|
-
decoder4: Decoder<T4
|
|
96
|
+
decoder4: Decoder<T4>,
|
|
103
97
|
): Decoder<[T1, T2, T3, T4]> {
|
|
104
98
|
return compose(ntuple(4), (blobs: $ReadOnlyArray<mixed>) => {
|
|
105
99
|
const [blob1, blob2, blob3, blob4] = blobs;
|
|
@@ -109,22 +103,22 @@ export function tuple4<T1, T2, T3, T4>(
|
|
|
109
103
|
const result3 = decoder3(blob3);
|
|
110
104
|
const result4 = decoder4(blob4);
|
|
111
105
|
try {
|
|
112
|
-
return
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
106
|
+
return ok([
|
|
107
|
+
unwrap(result1),
|
|
108
|
+
unwrap(result2),
|
|
109
|
+
unwrap(result3),
|
|
110
|
+
unwrap(result4),
|
|
117
111
|
]);
|
|
118
112
|
} catch (e) {
|
|
119
113
|
// If a decoder error has happened while unwrapping all the
|
|
120
114
|
// results, try to construct a good error message
|
|
121
|
-
return
|
|
115
|
+
return err(
|
|
122
116
|
annotate([
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
])
|
|
117
|
+
okOrErrValue(result1),
|
|
118
|
+
okOrErrValue(result2),
|
|
119
|
+
okOrErrValue(result3),
|
|
120
|
+
okOrErrValue(result4),
|
|
121
|
+
]),
|
|
128
122
|
);
|
|
129
123
|
}
|
|
130
124
|
});
|
|
@@ -139,7 +133,7 @@ export function tuple5<T1, T2, T3, T4, T5>(
|
|
|
139
133
|
decoder2: Decoder<T2>,
|
|
140
134
|
decoder3: Decoder<T3>,
|
|
141
135
|
decoder4: Decoder<T4>,
|
|
142
|
-
decoder5: Decoder<T5
|
|
136
|
+
decoder5: Decoder<T5>,
|
|
143
137
|
): Decoder<[T1, T2, T3, T4, T5]> {
|
|
144
138
|
return compose(ntuple(5), (blobs: $ReadOnlyArray<mixed>) => {
|
|
145
139
|
const [blob1, blob2, blob3, blob4, blob5] = blobs;
|
|
@@ -150,24 +144,24 @@ export function tuple5<T1, T2, T3, T4, T5>(
|
|
|
150
144
|
const result4 = decoder4(blob4);
|
|
151
145
|
const result5 = decoder5(blob5);
|
|
152
146
|
try {
|
|
153
|
-
return
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
147
|
+
return ok([
|
|
148
|
+
unwrap(result1),
|
|
149
|
+
unwrap(result2),
|
|
150
|
+
unwrap(result3),
|
|
151
|
+
unwrap(result4),
|
|
152
|
+
unwrap(result5),
|
|
159
153
|
]);
|
|
160
154
|
} catch (e) {
|
|
161
155
|
// If a decoder error has happened while unwrapping all the
|
|
162
156
|
// results, try to construct a good error message
|
|
163
|
-
return
|
|
157
|
+
return err(
|
|
164
158
|
annotate([
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
])
|
|
159
|
+
okOrErrValue(result1),
|
|
160
|
+
okOrErrValue(result2),
|
|
161
|
+
okOrErrValue(result3),
|
|
162
|
+
okOrErrValue(result4),
|
|
163
|
+
okOrErrValue(result5),
|
|
164
|
+
]),
|
|
171
165
|
);
|
|
172
166
|
}
|
|
173
167
|
});
|
|
@@ -183,7 +177,7 @@ export function tuple6<T1, T2, T3, T4, T5, T6>(
|
|
|
183
177
|
decoder3: Decoder<T3>,
|
|
184
178
|
decoder4: Decoder<T4>,
|
|
185
179
|
decoder5: Decoder<T5>,
|
|
186
|
-
decoder6: Decoder<T6
|
|
180
|
+
decoder6: Decoder<T6>,
|
|
187
181
|
): Decoder<[T1, T2, T3, T4, T5, T6]> {
|
|
188
182
|
return compose(ntuple(6), (blobs: $ReadOnlyArray<mixed>) => {
|
|
189
183
|
const [blob1, blob2, blob3, blob4, blob5, blob6] = blobs;
|
|
@@ -195,26 +189,26 @@ export function tuple6<T1, T2, T3, T4, T5, T6>(
|
|
|
195
189
|
const result5 = decoder5(blob5);
|
|
196
190
|
const result6 = decoder6(blob6);
|
|
197
191
|
try {
|
|
198
|
-
return
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
192
|
+
return ok([
|
|
193
|
+
unwrap(result1),
|
|
194
|
+
unwrap(result2),
|
|
195
|
+
unwrap(result3),
|
|
196
|
+
unwrap(result4),
|
|
197
|
+
unwrap(result5),
|
|
198
|
+
unwrap(result6),
|
|
205
199
|
]);
|
|
206
200
|
} catch (e) {
|
|
207
201
|
// If a decoder error has happened while unwrapping all the
|
|
208
202
|
// results, try to construct a good error message
|
|
209
|
-
return
|
|
203
|
+
return err(
|
|
210
204
|
annotate([
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
])
|
|
205
|
+
okOrErrValue(result1),
|
|
206
|
+
okOrErrValue(result2),
|
|
207
|
+
okOrErrValue(result3),
|
|
208
|
+
okOrErrValue(result4),
|
|
209
|
+
okOrErrValue(result5),
|
|
210
|
+
okOrErrValue(result6),
|
|
211
|
+
]),
|
|
218
212
|
);
|
|
219
213
|
}
|
|
220
214
|
});
|
|
@@ -0,0 +1,137 @@
|
|
|
1
|
+
import { asDate, indent, INDENT, isMultiline } from '../_utils';
|
|
2
|
+
|
|
3
|
+
function serializeString(s, width) {
|
|
4
|
+
if (width === void 0) {
|
|
5
|
+
width = 80;
|
|
6
|
+
}
|
|
7
|
+
|
|
8
|
+
// Full string
|
|
9
|
+
// Abbreviated to $maxlen i.e. "Vincent Driess..." [truncated]
|
|
10
|
+
var ser = JSON.stringify(s);
|
|
11
|
+
|
|
12
|
+
if (ser.length <= width) {
|
|
13
|
+
return ser;
|
|
14
|
+
} // Cut off a bit
|
|
15
|
+
|
|
16
|
+
|
|
17
|
+
var truncated = s.substring(0, width - 15) + '...';
|
|
18
|
+
ser = JSON.stringify(truncated) + ' [truncated]';
|
|
19
|
+
return ser;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
function serializeArray(annotation, prefix) {
|
|
23
|
+
var items = annotation.items;
|
|
24
|
+
|
|
25
|
+
if (items.length === 0) {
|
|
26
|
+
return '[]';
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
var result = [];
|
|
30
|
+
items.forEach(function (item) {
|
|
31
|
+
var _serializeAnnotation = serializeAnnotation(item, prefix + INDENT),
|
|
32
|
+
ser = _serializeAnnotation[0],
|
|
33
|
+
ann = _serializeAnnotation[1];
|
|
34
|
+
|
|
35
|
+
result.push(prefix + INDENT + ser + ',');
|
|
36
|
+
|
|
37
|
+
if (ann !== undefined) {
|
|
38
|
+
result.push(indent(ann, prefix + INDENT));
|
|
39
|
+
}
|
|
40
|
+
});
|
|
41
|
+
return ['['].concat(result, [prefix + ']']).join('\n');
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
function serializeObject(annotation, prefix) {
|
|
45
|
+
var fields = annotation.fields;
|
|
46
|
+
var fieldNames = Object.keys(fields);
|
|
47
|
+
|
|
48
|
+
if (fieldNames.length === 0) {
|
|
49
|
+
return '{}';
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
var result = [];
|
|
53
|
+
fieldNames.forEach(function (key) {
|
|
54
|
+
var valueAnnotation = fields[key];
|
|
55
|
+
var kser = serializeValue(key);
|
|
56
|
+
var valPrefix = prefix + INDENT + ' '.repeat(kser.length + 2);
|
|
57
|
+
|
|
58
|
+
var _serializeAnnotation2 = serializeAnnotation(valueAnnotation, prefix + INDENT),
|
|
59
|
+
vser = _serializeAnnotation2[0],
|
|
60
|
+
vann = _serializeAnnotation2[1];
|
|
61
|
+
|
|
62
|
+
result.push(prefix + INDENT + kser + ': ' + vser + ',');
|
|
63
|
+
|
|
64
|
+
if (vann !== undefined) {
|
|
65
|
+
result.push(indent(vann, valPrefix));
|
|
66
|
+
}
|
|
67
|
+
});
|
|
68
|
+
return ['{'].concat(result, [prefix + '}']).join('\n');
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
export function serializeValue(value) {
|
|
72
|
+
// istanbul ignore else
|
|
73
|
+
if (typeof value === 'string') {
|
|
74
|
+
return serializeString(value);
|
|
75
|
+
} else if (typeof value === 'number' || typeof value === 'boolean') {
|
|
76
|
+
return value.toString();
|
|
77
|
+
} else if (value === null) {
|
|
78
|
+
return 'null';
|
|
79
|
+
} else if (value === undefined) {
|
|
80
|
+
return 'undefined';
|
|
81
|
+
} else {
|
|
82
|
+
var valueAsDate = asDate(value);
|
|
83
|
+
|
|
84
|
+
if (valueAsDate !== null) {
|
|
85
|
+
return "new Date(" + JSON.stringify(valueAsDate.toISOString()) + ")";
|
|
86
|
+
} else if (value instanceof Date) {
|
|
87
|
+
// NOTE: Using `instanceof Date` is unreliable way of checking dates.
|
|
88
|
+
// If this case occurs (and it didn't pass the prior isDate())
|
|
89
|
+
// check, then this must be the case where it's an invalid date.
|
|
90
|
+
return '(Invalid Date)';
|
|
91
|
+
} else {
|
|
92
|
+
return '(unserializable)';
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
export function serializeAnnotation(ann, prefix) {
|
|
97
|
+
if (prefix === void 0) {
|
|
98
|
+
prefix = '';
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
// The serialized data (the input object echoed back)
|
|
102
|
+
var serialized;
|
|
103
|
+
|
|
104
|
+
if (ann.type === 'array') {
|
|
105
|
+
serialized = serializeArray(ann, prefix);
|
|
106
|
+
} else if (ann.type === 'object') {
|
|
107
|
+
serialized = serializeObject(ann, prefix);
|
|
108
|
+
} else if (ann.type === 'function') {
|
|
109
|
+
serialized = '<function>';
|
|
110
|
+
} else if (ann.type === 'circular-ref') {
|
|
111
|
+
serialized = '<circular ref>';
|
|
112
|
+
} else if (ann.type === 'unknown') {
|
|
113
|
+
serialized = '???';
|
|
114
|
+
} else {
|
|
115
|
+
serialized = serializeValue(ann.value);
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
var text = ann.text;
|
|
119
|
+
|
|
120
|
+
if (text !== undefined) {
|
|
121
|
+
var sep = '^'.repeat(isMultiline(serialized) ? 1 : serialized.length);
|
|
122
|
+
return [serialized, [sep, text].join(isMultiline(text) ? '\n' : ' ')];
|
|
123
|
+
} else {
|
|
124
|
+
return [serialized, undefined];
|
|
125
|
+
}
|
|
126
|
+
}
|
|
127
|
+
export function formatInline(ann) {
|
|
128
|
+
var _serializeAnnotation3 = serializeAnnotation(ann),
|
|
129
|
+
serialized = _serializeAnnotation3[0],
|
|
130
|
+
annotation = _serializeAnnotation3[1];
|
|
131
|
+
|
|
132
|
+
if (annotation !== undefined) {
|
|
133
|
+
return serialized + '\n' + annotation;
|
|
134
|
+
} else {
|
|
135
|
+
return serialized;
|
|
136
|
+
}
|
|
137
|
+
}
|
|
@@ -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
|
+
}
|