decoders 1.25.5 → 2.0.0-beta2
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 +23 -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/{array.js.flow → _esm/core/array.js.flow} +22 -25
- package/_esm/core/boolean.js +28 -0
- package/{boolean.js.flow → _esm/core/boolean.js.flow} +8 -7
- package/_esm/core/composition.js +42 -0
- package/{utils.js.flow → _esm/core/composition.js.flow} +7 -22
- package/_esm/core/constants.js +46 -0
- package/{constants.js.flow → _esm/core/constants.js.flow} +13 -12
- package/_esm/core/date.js +28 -0
- package/{date.js.flow → _esm/core/date.js.flow} +9 -7
- package/{describe.js → _esm/core/describe.js} +5 -11
- package/{describe.js.flow → _esm/core/describe.js.flow} +4 -4
- package/{dispatch.js → _esm/core/dispatch.js} +8 -15
- package/{dispatch.js.flow → _esm/core/dispatch.js.flow} +6 -5
- package/_esm/core/either.js +90 -0
- package/{either.js.flow → _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/{instanceOf.js.flow → _esm/core/instanceOf.js.flow} +7 -8
- package/_esm/core/json.js +15 -0
- package/{json.js.flow → _esm/core/json.js.flow} +4 -4
- package/_esm/core/lazy.js +11 -0
- package/{lazy.js.flow → _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/{number.js.flow → _esm/core/number.js.flow} +10 -11
- package/_esm/core/object.js +175 -0
- package/{object.js.flow → _esm/core/object.js.flow} +44 -52
- package/_esm/core/optional.js +38 -0
- package/{optional.js.flow → _esm/core/optional.js.flow} +6 -7
- package/{string.js → _esm/core/string.js} +18 -35
- package/{string.js.flow → _esm/core/string.js.flow} +13 -12
- package/_esm/core/tuple.js +155 -0
- package/{tuple.js.flow → _esm/core/tuple.js.flow} +62 -63
- 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/_esm/index.js.flow +56 -0
- package/_esm/result.js +139 -0
- package/_esm/result.js.flow +166 -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/{json.js → core/json.js} +3 -5
- package/core/json.js.flow +28 -0
- package/{lazy.js → 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 +220 -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 +118 -395
- package/index.js.flow +24 -24
- package/package.json +2 -6
- package/result.js +172 -0
- package/result.js.flow +166 -0
- package/array.d.ts +0 -5
- package/array.js +0 -133
- package/boolean.d.ts +0 -5
- package/boolean.js +0 -42
- package/constants.d.ts +0 -11
- package/constants.js +0 -67
- package/date.d.ts +0 -4
- package/date.js +0 -42
- package/describe.d.ts +0 -3
- package/dispatch.d.ts +0 -8
- package/either.d.ts +0 -61
- package/either.js +0 -85
- package/fail.d.ts +0 -3
- package/fail.js +0 -19
- package/fail.js.flow +0 -13
- package/guard.d.ts +0 -7
- package/guard.js +0 -30
- package/guard.js.flow +0 -36
- package/helpers.d.ts +0 -79
- package/index.d.ts +0 -38
- package/instanceOf.d.ts +0 -3
- package/instanceOf.js +0 -17
- package/json.d.ts +0 -11
- package/lazy.d.ts +0 -3
- package/mapping.d.ts +0 -4
- package/mapping.js +0 -113
- package/mapping.js.flow +0 -71
- package/number.d.ts +0 -6
- package/number.js +0 -38
- package/object.d.ts +0 -33
- package/object.js +0 -254
- package/optional.d.ts +0 -5
- package/optional.js +0 -52
- package/string.d.ts +0 -7
- package/tuple.d.ts +0 -30
- package/tuple.js +0 -199
- package/types.d.ts +0 -18
- package/types.js +0 -5
- package/types.js.flow +0 -26
- package/utils.d.ts +0 -13
- package/utils.js +0 -70
|
@@ -0,0 +1,155 @@
|
|
|
1
|
+
import { annotate } from '../annotate';
|
|
2
|
+
import { compose, predicate } from './composition';
|
|
3
|
+
import { err, errValue, isErr, ok, unwrap, value } 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(errValue(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([isErr(result1) ? errValue(result1) : value(result1), isErr(result2) ? errValue(result2) : value(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([isErr(result1) ? errValue(result1) : value(result1), isErr(result2) ? errValue(result2) : value(result2), isErr(result3) ? errValue(result3) : value(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([isErr(result1) ? errValue(result1) : value(result1), isErr(result2) ? errValue(result2) : value(result2), isErr(result3) ? errValue(result3) : value(result3), isErr(result4) ? errValue(result4) : value(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([isErr(result1) ? errValue(result1) : value(result1), isErr(result2) ? errValue(result2) : value(result2), isErr(result3) ? errValue(result3) : value(result3), isErr(result4) ? errValue(result4) : value(result4), isErr(result5) ? errValue(result5) : value(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([isErr(result1) ? errValue(result1) : value(result1), isErr(result2) ? errValue(result2) : value(result2), isErr(result3) ? errValue(result3) : value(result3), isErr(result4) ? errValue(result4) : value(result4), isErr(result5) ? errValue(result5) : value(result5), isErr(result6) ? errValue(result6) : value(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, errValue, isErr, ok, unwrap, value } 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(errValue(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,15 +45,15 @@ 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
|
|
52
|
+
return err(
|
|
54
53
|
annotate([
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
])
|
|
54
|
+
isErr(result1) ? errValue(result1) : value(result1),
|
|
55
|
+
isErr(result2) ? errValue(result2) : value(result2),
|
|
56
|
+
]),
|
|
58
57
|
);
|
|
59
58
|
}
|
|
60
59
|
});
|
|
@@ -67,7 +66,7 @@ export function tuple2<T1, T2>(
|
|
|
67
66
|
export function tuple3<T1, T2, T3>(
|
|
68
67
|
decoder1: Decoder<T1>,
|
|
69
68
|
decoder2: Decoder<T2>,
|
|
70
|
-
decoder3: Decoder<T3
|
|
69
|
+
decoder3: Decoder<T3>,
|
|
71
70
|
): Decoder<[T1, T2, T3]> {
|
|
72
71
|
return compose(ntuple(3), (blobs: $ReadOnlyArray<mixed>) => {
|
|
73
72
|
const [blob1, blob2, blob3] = blobs;
|
|
@@ -76,16 +75,16 @@ export function tuple3<T1, T2, T3>(
|
|
|
76
75
|
const result2 = decoder2(blob2);
|
|
77
76
|
const result3 = decoder3(blob3);
|
|
78
77
|
try {
|
|
79
|
-
return
|
|
78
|
+
return ok([unwrap(result1), unwrap(result2), unwrap(result3)]);
|
|
80
79
|
} catch (e) {
|
|
81
80
|
// If a decoder error has happened while unwrapping all the
|
|
82
81
|
// results, try to construct a good error message
|
|
83
|
-
return
|
|
82
|
+
return err(
|
|
84
83
|
annotate([
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
])
|
|
84
|
+
isErr(result1) ? errValue(result1) : value(result1),
|
|
85
|
+
isErr(result2) ? errValue(result2) : value(result2),
|
|
86
|
+
isErr(result3) ? errValue(result3) : value(result3),
|
|
87
|
+
]),
|
|
89
88
|
);
|
|
90
89
|
}
|
|
91
90
|
});
|
|
@@ -99,7 +98,7 @@ export function tuple4<T1, T2, T3, T4>(
|
|
|
99
98
|
decoder1: Decoder<T1>,
|
|
100
99
|
decoder2: Decoder<T2>,
|
|
101
100
|
decoder3: Decoder<T3>,
|
|
102
|
-
decoder4: Decoder<T4
|
|
101
|
+
decoder4: Decoder<T4>,
|
|
103
102
|
): Decoder<[T1, T2, T3, T4]> {
|
|
104
103
|
return compose(ntuple(4), (blobs: $ReadOnlyArray<mixed>) => {
|
|
105
104
|
const [blob1, blob2, blob3, blob4] = blobs;
|
|
@@ -109,22 +108,22 @@ export function tuple4<T1, T2, T3, T4>(
|
|
|
109
108
|
const result3 = decoder3(blob3);
|
|
110
109
|
const result4 = decoder4(blob4);
|
|
111
110
|
try {
|
|
112
|
-
return
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
111
|
+
return ok([
|
|
112
|
+
unwrap(result1),
|
|
113
|
+
unwrap(result2),
|
|
114
|
+
unwrap(result3),
|
|
115
|
+
unwrap(result4),
|
|
117
116
|
]);
|
|
118
117
|
} catch (e) {
|
|
119
118
|
// If a decoder error has happened while unwrapping all the
|
|
120
119
|
// results, try to construct a good error message
|
|
121
|
-
return
|
|
120
|
+
return err(
|
|
122
121
|
annotate([
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
])
|
|
122
|
+
isErr(result1) ? errValue(result1) : value(result1),
|
|
123
|
+
isErr(result2) ? errValue(result2) : value(result2),
|
|
124
|
+
isErr(result3) ? errValue(result3) : value(result3),
|
|
125
|
+
isErr(result4) ? errValue(result4) : value(result4),
|
|
126
|
+
]),
|
|
128
127
|
);
|
|
129
128
|
}
|
|
130
129
|
});
|
|
@@ -139,7 +138,7 @@ export function tuple5<T1, T2, T3, T4, T5>(
|
|
|
139
138
|
decoder2: Decoder<T2>,
|
|
140
139
|
decoder3: Decoder<T3>,
|
|
141
140
|
decoder4: Decoder<T4>,
|
|
142
|
-
decoder5: Decoder<T5
|
|
141
|
+
decoder5: Decoder<T5>,
|
|
143
142
|
): Decoder<[T1, T2, T3, T4, T5]> {
|
|
144
143
|
return compose(ntuple(5), (blobs: $ReadOnlyArray<mixed>) => {
|
|
145
144
|
const [blob1, blob2, blob3, blob4, blob5] = blobs;
|
|
@@ -150,24 +149,24 @@ export function tuple5<T1, T2, T3, T4, T5>(
|
|
|
150
149
|
const result4 = decoder4(blob4);
|
|
151
150
|
const result5 = decoder5(blob5);
|
|
152
151
|
try {
|
|
153
|
-
return
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
152
|
+
return ok([
|
|
153
|
+
unwrap(result1),
|
|
154
|
+
unwrap(result2),
|
|
155
|
+
unwrap(result3),
|
|
156
|
+
unwrap(result4),
|
|
157
|
+
unwrap(result5),
|
|
159
158
|
]);
|
|
160
159
|
} catch (e) {
|
|
161
160
|
// If a decoder error has happened while unwrapping all the
|
|
162
161
|
// results, try to construct a good error message
|
|
163
|
-
return
|
|
162
|
+
return err(
|
|
164
163
|
annotate([
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
])
|
|
164
|
+
isErr(result1) ? errValue(result1) : value(result1),
|
|
165
|
+
isErr(result2) ? errValue(result2) : value(result2),
|
|
166
|
+
isErr(result3) ? errValue(result3) : value(result3),
|
|
167
|
+
isErr(result4) ? errValue(result4) : value(result4),
|
|
168
|
+
isErr(result5) ? errValue(result5) : value(result5),
|
|
169
|
+
]),
|
|
171
170
|
);
|
|
172
171
|
}
|
|
173
172
|
});
|
|
@@ -183,7 +182,7 @@ export function tuple6<T1, T2, T3, T4, T5, T6>(
|
|
|
183
182
|
decoder3: Decoder<T3>,
|
|
184
183
|
decoder4: Decoder<T4>,
|
|
185
184
|
decoder5: Decoder<T5>,
|
|
186
|
-
decoder6: Decoder<T6
|
|
185
|
+
decoder6: Decoder<T6>,
|
|
187
186
|
): Decoder<[T1, T2, T3, T4, T5, T6]> {
|
|
188
187
|
return compose(ntuple(6), (blobs: $ReadOnlyArray<mixed>) => {
|
|
189
188
|
const [blob1, blob2, blob3, blob4, blob5, blob6] = blobs;
|
|
@@ -195,26 +194,26 @@ export function tuple6<T1, T2, T3, T4, T5, T6>(
|
|
|
195
194
|
const result5 = decoder5(blob5);
|
|
196
195
|
const result6 = decoder6(blob6);
|
|
197
196
|
try {
|
|
198
|
-
return
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
197
|
+
return ok([
|
|
198
|
+
unwrap(result1),
|
|
199
|
+
unwrap(result2),
|
|
200
|
+
unwrap(result3),
|
|
201
|
+
unwrap(result4),
|
|
202
|
+
unwrap(result5),
|
|
203
|
+
unwrap(result6),
|
|
205
204
|
]);
|
|
206
205
|
} catch (e) {
|
|
207
206
|
// If a decoder error has happened while unwrapping all the
|
|
208
207
|
// results, try to construct a good error message
|
|
209
|
-
return
|
|
208
|
+
return err(
|
|
210
209
|
annotate([
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
])
|
|
210
|
+
isErr(result1) ? errValue(result1) : value(result1),
|
|
211
|
+
isErr(result2) ? errValue(result2) : value(result2),
|
|
212
|
+
isErr(result3) ? errValue(result3) : value(result3),
|
|
213
|
+
isErr(result4) ? errValue(result4) : value(result4),
|
|
214
|
+
isErr(result5) ? errValue(result5) : value(result5),
|
|
215
|
+
isErr(result6) ? errValue(result6) : value(result6),
|
|
216
|
+
]),
|
|
218
217
|
);
|
|
219
218
|
}
|
|
220
219
|
});
|
|
@@ -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
|
+
}
|