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