decoders 2.0.0-beta6 → 2.0.0-beta7
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/NotSupportedTSVersion.d.ts +1 -0
- package/_guard.d.ts +7 -0
- package/_guard.js +2 -6
- package/_guard.js.flow +3 -3
- package/{_esm/_guard.js → _guard.mjs} +3 -3
- package/_types.d.ts +13 -0
- package/{_esm/_types.js → _types.mjs} +0 -0
- package/_utils.d.ts +10 -0
- package/{_esm/_utils.js → _utils.mjs} +0 -0
- package/annotate.d.ts +62 -0
- package/{_esm/annotate.js → annotate.mjs} +0 -0
- package/core/_helpers.d.ts +79 -0
- package/core/array.d.ts +5 -0
- package/core/array.js +5 -9
- package/core/array.js.flow +5 -5
- package/{_esm/core/array.js → core/array.mjs} +7 -7
- package/core/boolean.d.ts +5 -0
- package/core/boolean.js +4 -8
- package/core/boolean.js.flow +3 -5
- package/{_esm/core/boolean.js → core/boolean.mjs} +6 -6
- package/core/composition.d.ts +12 -0
- package/core/composition.js +6 -10
- package/core/composition.js.flow +6 -6
- package/{_esm/core/composition.js → core/composition.mjs} +7 -7
- package/core/constants.d.ts +11 -0
- package/core/constants.js +6 -10
- package/core/constants.js.flow +7 -9
- package/{_esm/core/constants.js → core/constants.mjs} +7 -7
- package/core/date.d.ts +4 -0
- package/core/date.js +3 -7
- package/core/date.js.flow +2 -4
- package/{_esm/core/date.js → core/date.mjs} +6 -6
- package/core/describe.d.ts +3 -0
- package/core/describe.js +2 -6
- package/core/describe.js.flow +2 -2
- package/{_esm/core/describe.js → core/describe.mjs} +3 -3
- package/core/dispatch.d.ts +8 -0
- package/core/dispatch.js +2 -6
- package/core/dispatch.js.flow +2 -2
- package/{_esm/core/dispatch.js → core/dispatch.mjs} +4 -4
- package/core/either.d.ts +61 -0
- package/core/either.js +7 -11
- package/core/either.js.flow +6 -6
- package/{_esm/core/either.js → core/either.mjs} +8 -8
- package/core/fail.d.ts +3 -0
- package/core/fail.js +2 -6
- package/core/fail.js.flow +2 -2
- package/{_esm/core/fail.js → core/fail.mjs} +3 -3
- package/core/instanceOf.d.ts +3 -0
- package/core/instanceOf.js +2 -6
- package/core/instanceOf.js.flow +3 -3
- package/core/instanceOf.mjs +8 -0
- package/core/json.d.ts +11 -0
- package/core/json.mjs +15 -0
- package/core/lazy.d.ts +3 -0
- package/{_esm/core/lazy.js → core/lazy.mjs} +0 -0
- package/core/mapping.d.ts +6 -0
- package/core/mapping.js +4 -8
- package/core/mapping.js.flow +3 -3
- package/{_esm/core/mapping.js → core/mapping.mjs} +7 -7
- package/core/number.d.ts +6 -0
- package/core/number.js +2 -6
- package/core/number.js.flow +3 -3
- package/{_esm/core/number.js → core/number.mjs} +4 -4
- package/core/object.d.ts +33 -0
- package/core/object.js +7 -11
- package/core/object.js.flow +7 -7
- package/{_esm/core/object.js → core/object.mjs} +9 -9
- package/core/optional.d.ts +5 -0
- package/core/optional.js +4 -8
- package/core/optional.js.flow +3 -3
- package/{_esm/core/optional.js → core/optional.mjs} +6 -6
- package/core/string.d.ts +7 -0
- package/core/string.js +5 -9
- package/core/string.js.flow +5 -7
- package/{_esm/core/string.js → core/string.mjs} +7 -7
- package/core/tuple.d.ts +30 -0
- package/{_esm/core/tuple.js → core/tuple.mjs} +4 -4
- package/format.d.ts +4 -0
- package/{format/inline.js → format.js} +6 -1
- package/{_esm/format/inline.js.flow → format.js.flow} +6 -2
- package/{_esm/format/inline.js → format.mjs} +4 -1
- package/{_esm/index.js.flow → index.d.ts} +3 -23
- package/{_esm/index.js → index.mjs} +19 -19
- package/package.json +15 -3
- package/result.d.ts +39 -0
- package/result.js +0 -62
- package/result.js.flow +0 -60
- package/{_esm/result.js → result.mjs} +0 -54
- package/_esm/_guard.js.flow +0 -20
- package/_esm/_types.js.flow +0 -20
- package/_esm/_utils.js.flow +0 -97
- package/_esm/annotate.js.flow +0 -218
- package/_esm/core/array.js.flow +0 -103
- package/_esm/core/boolean.js.flow +0 -29
- package/_esm/core/composition.js.flow +0 -43
- package/_esm/core/constants.js.flow +0 -46
- package/_esm/core/date.js.flow +0 -40
- package/_esm/core/describe.js.flow +0 -17
- package/_esm/core/dispatch.js.flow +0 -58
- package/_esm/core/either.js.flow +0 -151
- package/_esm/core/fail.js.flow +0 -12
- package/_esm/core/instanceOf.js +0 -8
- package/_esm/core/instanceOf.js.flow +0 -20
- package/_esm/core/json.js +0 -15
- package/_esm/core/json.js.flow +0 -28
- package/_esm/core/lazy.js.flow +0 -15
- package/_esm/core/mapping.js.flow +0 -54
- package/_esm/core/number.js.flow +0 -34
- package/_esm/core/object.js.flow +0 -203
- package/_esm/core/optional.js.flow +0 -41
- package/_esm/core/string.js.flow +0 -82
- package/_esm/core/tuple.js.flow +0 -214
- package/_esm/format/index.js +0 -2
- package/_esm/format/index.js.flow +0 -4
- package/_esm/format/short.js +0 -4
- package/_esm/format/short.js.flow +0 -8
- package/_esm/result.js.flow +0 -158
- package/format/index.js +0 -12
- package/format/index.js.flow +0 -4
- package/format/inline.js.flow +0 -122
- package/format/short.js +0 -10
- package/format/short.js.flow +0 -8
package/_esm/core/tuple.js.flow
DELETED
|
@@ -1,214 +0,0 @@
|
|
|
1
|
-
// @flow strict
|
|
2
|
-
|
|
3
|
-
import { annotate } from '../annotate';
|
|
4
|
-
import { compose, predicate } from './composition';
|
|
5
|
-
import { err, ok, unwrap } from '../result';
|
|
6
|
-
import { poja } from './array';
|
|
7
|
-
import type { Decoder } from '../_types';
|
|
8
|
-
import type { Result } from '../result';
|
|
9
|
-
|
|
10
|
-
function okOrErr<T, E>(result: Result<T, E>): T | E {
|
|
11
|
-
return result.type === 'ok' ? result.value : result.error;
|
|
12
|
-
}
|
|
13
|
-
|
|
14
|
-
const ntuple = (n: number) =>
|
|
15
|
-
compose(
|
|
16
|
-
poja,
|
|
17
|
-
predicate((arr) => arr.length === n, `Must be a ${n}-tuple`),
|
|
18
|
-
);
|
|
19
|
-
|
|
20
|
-
/**
|
|
21
|
-
* Builds a Decoder that returns Ok for 1-tuple of [T], given a Decoder for T.
|
|
22
|
-
* Err otherwise.
|
|
23
|
-
*/
|
|
24
|
-
export function tuple1<T>(decoder1: Decoder<T>): Decoder<[T]> {
|
|
25
|
-
return compose(ntuple(1), (blobs: $ReadOnlyArray<mixed>) => {
|
|
26
|
-
const [blob1] = blobs;
|
|
27
|
-
|
|
28
|
-
const result1 = decoder1(blob1);
|
|
29
|
-
try {
|
|
30
|
-
return ok([unwrap(result1)]);
|
|
31
|
-
} catch (e) {
|
|
32
|
-
// If a decoder error has happened while unwrapping all the
|
|
33
|
-
// results, try to construct a good error message
|
|
34
|
-
return err(annotate([okOrErr(result1)]));
|
|
35
|
-
}
|
|
36
|
-
});
|
|
37
|
-
}
|
|
38
|
-
|
|
39
|
-
/**
|
|
40
|
-
* Builds a Decoder that returns Ok for 2-tuples of [T1, T2], given Decoders
|
|
41
|
-
* for T1 and T2. Err otherwise.
|
|
42
|
-
*/
|
|
43
|
-
export function tuple2<T1, T2>(
|
|
44
|
-
decoder1: Decoder<T1>,
|
|
45
|
-
decoder2: Decoder<T2>,
|
|
46
|
-
): Decoder<[T1, T2]> {
|
|
47
|
-
return compose(ntuple(2), (blobs: $ReadOnlyArray<mixed>) => {
|
|
48
|
-
const [blob1, blob2] = blobs;
|
|
49
|
-
|
|
50
|
-
const result1 = decoder1(blob1);
|
|
51
|
-
const result2 = decoder2(blob2);
|
|
52
|
-
try {
|
|
53
|
-
return ok([unwrap(result1), unwrap(result2)]);
|
|
54
|
-
} catch (e) {
|
|
55
|
-
// If a decoder error has happened while unwrapping all the
|
|
56
|
-
// results, try to construct a good error message
|
|
57
|
-
return err(annotate([okOrErr(result1), okOrErr(result2)]));
|
|
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(annotate([okOrErr(result1), okOrErr(result2), okOrErr(result3)]));
|
|
83
|
-
}
|
|
84
|
-
});
|
|
85
|
-
}
|
|
86
|
-
|
|
87
|
-
/**
|
|
88
|
-
* Builds a Decoder that returns Ok for 4-tuples of [T1, T2, T3, T4], given
|
|
89
|
-
* Decoders for T1, T2, T3, and T4. Err otherwise.
|
|
90
|
-
*/
|
|
91
|
-
export function tuple4<T1, T2, T3, T4>(
|
|
92
|
-
decoder1: Decoder<T1>,
|
|
93
|
-
decoder2: Decoder<T2>,
|
|
94
|
-
decoder3: Decoder<T3>,
|
|
95
|
-
decoder4: Decoder<T4>,
|
|
96
|
-
): Decoder<[T1, T2, T3, T4]> {
|
|
97
|
-
return compose(ntuple(4), (blobs: $ReadOnlyArray<mixed>) => {
|
|
98
|
-
const [blob1, blob2, blob3, blob4] = blobs;
|
|
99
|
-
|
|
100
|
-
const result1 = decoder1(blob1);
|
|
101
|
-
const result2 = decoder2(blob2);
|
|
102
|
-
const result3 = decoder3(blob3);
|
|
103
|
-
const result4 = decoder4(blob4);
|
|
104
|
-
try {
|
|
105
|
-
return ok([
|
|
106
|
-
unwrap(result1),
|
|
107
|
-
unwrap(result2),
|
|
108
|
-
unwrap(result3),
|
|
109
|
-
unwrap(result4),
|
|
110
|
-
]);
|
|
111
|
-
} catch (e) {
|
|
112
|
-
// If a decoder error has happened while unwrapping all the
|
|
113
|
-
// results, try to construct a good error message
|
|
114
|
-
return err(
|
|
115
|
-
annotate([
|
|
116
|
-
okOrErr(result1),
|
|
117
|
-
okOrErr(result2),
|
|
118
|
-
okOrErr(result3),
|
|
119
|
-
okOrErr(result4),
|
|
120
|
-
]),
|
|
121
|
-
);
|
|
122
|
-
}
|
|
123
|
-
});
|
|
124
|
-
}
|
|
125
|
-
|
|
126
|
-
/**
|
|
127
|
-
* Builds a Decoder that returns Ok for 5-tuples of [T1, T2, T3, T4, T5], given
|
|
128
|
-
* Decoders for T1, T2, T3, T4, and T5. Err otherwise.
|
|
129
|
-
*/
|
|
130
|
-
export function tuple5<T1, T2, T3, T4, T5>(
|
|
131
|
-
decoder1: Decoder<T1>,
|
|
132
|
-
decoder2: Decoder<T2>,
|
|
133
|
-
decoder3: Decoder<T3>,
|
|
134
|
-
decoder4: Decoder<T4>,
|
|
135
|
-
decoder5: Decoder<T5>,
|
|
136
|
-
): Decoder<[T1, T2, T3, T4, T5]> {
|
|
137
|
-
return compose(ntuple(5), (blobs: $ReadOnlyArray<mixed>) => {
|
|
138
|
-
const [blob1, blob2, blob3, blob4, blob5] = blobs;
|
|
139
|
-
|
|
140
|
-
const result1 = decoder1(blob1);
|
|
141
|
-
const result2 = decoder2(blob2);
|
|
142
|
-
const result3 = decoder3(blob3);
|
|
143
|
-
const result4 = decoder4(blob4);
|
|
144
|
-
const result5 = decoder5(blob5);
|
|
145
|
-
try {
|
|
146
|
-
return ok([
|
|
147
|
-
unwrap(result1),
|
|
148
|
-
unwrap(result2),
|
|
149
|
-
unwrap(result3),
|
|
150
|
-
unwrap(result4),
|
|
151
|
-
unwrap(result5),
|
|
152
|
-
]);
|
|
153
|
-
} catch (e) {
|
|
154
|
-
// If a decoder error has happened while unwrapping all the
|
|
155
|
-
// results, try to construct a good error message
|
|
156
|
-
return err(
|
|
157
|
-
annotate([
|
|
158
|
-
okOrErr(result1),
|
|
159
|
-
okOrErr(result2),
|
|
160
|
-
okOrErr(result3),
|
|
161
|
-
okOrErr(result4),
|
|
162
|
-
okOrErr(result5),
|
|
163
|
-
]),
|
|
164
|
-
);
|
|
165
|
-
}
|
|
166
|
-
});
|
|
167
|
-
}
|
|
168
|
-
|
|
169
|
-
/**
|
|
170
|
-
* Builds a Decoder that returns Ok for 5-tuples of [T1, T2, T3, T4, T5], given
|
|
171
|
-
* Decoders for T1, T2, T3, T4, T5, and T6. Err otherwise.
|
|
172
|
-
*/
|
|
173
|
-
export function tuple6<T1, T2, T3, T4, T5, T6>(
|
|
174
|
-
decoder1: Decoder<T1>,
|
|
175
|
-
decoder2: Decoder<T2>,
|
|
176
|
-
decoder3: Decoder<T3>,
|
|
177
|
-
decoder4: Decoder<T4>,
|
|
178
|
-
decoder5: Decoder<T5>,
|
|
179
|
-
decoder6: Decoder<T6>,
|
|
180
|
-
): Decoder<[T1, T2, T3, T4, T5, T6]> {
|
|
181
|
-
return compose(ntuple(6), (blobs: $ReadOnlyArray<mixed>) => {
|
|
182
|
-
const [blob1, blob2, blob3, blob4, blob5, blob6] = blobs;
|
|
183
|
-
|
|
184
|
-
const result1 = decoder1(blob1);
|
|
185
|
-
const result2 = decoder2(blob2);
|
|
186
|
-
const result3 = decoder3(blob3);
|
|
187
|
-
const result4 = decoder4(blob4);
|
|
188
|
-
const result5 = decoder5(blob5);
|
|
189
|
-
const result6 = decoder6(blob6);
|
|
190
|
-
try {
|
|
191
|
-
return ok([
|
|
192
|
-
unwrap(result1),
|
|
193
|
-
unwrap(result2),
|
|
194
|
-
unwrap(result3),
|
|
195
|
-
unwrap(result4),
|
|
196
|
-
unwrap(result5),
|
|
197
|
-
unwrap(result6),
|
|
198
|
-
]);
|
|
199
|
-
} catch (e) {
|
|
200
|
-
// If a decoder error has happened while unwrapping all the
|
|
201
|
-
// results, try to construct a good error message
|
|
202
|
-
return err(
|
|
203
|
-
annotate([
|
|
204
|
-
okOrErr(result1),
|
|
205
|
-
okOrErr(result2),
|
|
206
|
-
okOrErr(result3),
|
|
207
|
-
okOrErr(result4),
|
|
208
|
-
okOrErr(result5),
|
|
209
|
-
okOrErr(result6),
|
|
210
|
-
]),
|
|
211
|
-
);
|
|
212
|
-
}
|
|
213
|
-
});
|
|
214
|
-
}
|
package/_esm/format/index.js
DELETED
package/_esm/format/short.js
DELETED
package/_esm/result.js.flow
DELETED
|
@@ -1,158 +0,0 @@
|
|
|
1
|
-
// @flow strict
|
|
2
|
-
|
|
3
|
-
/**
|
|
4
|
-
* Result <value> <error>
|
|
5
|
-
* = Ok <value>
|
|
6
|
-
* | Err <error>
|
|
7
|
-
*/
|
|
8
|
-
|
|
9
|
-
type Ok<+T> = {| +type: 'ok', +value: T, +error: void |};
|
|
10
|
-
type Err<+E> = {| +type: 'err', +value: void, +error: E |};
|
|
11
|
-
|
|
12
|
-
export type Result<+T, +E> = Ok<T> | Err<E>;
|
|
13
|
-
|
|
14
|
-
/**
|
|
15
|
-
* Create a new Result instance representing a successful computation.
|
|
16
|
-
*/
|
|
17
|
-
export function ok<T>(value: T): Ok<T> {
|
|
18
|
-
return { type: 'ok', value, error: undefined };
|
|
19
|
-
}
|
|
20
|
-
|
|
21
|
-
/**
|
|
22
|
-
* Create a new Result instance representing a failed computation.
|
|
23
|
-
*/
|
|
24
|
-
export function err<E>(error: E): Err<E> {
|
|
25
|
-
return { type: 'err', value: undefined, error };
|
|
26
|
-
}
|
|
27
|
-
|
|
28
|
-
export function toString(result: Result<mixed, mixed>): string {
|
|
29
|
-
return result.type === 'ok'
|
|
30
|
-
? `Ok(${String(result.value)})`
|
|
31
|
-
: `Err(${String(result.error)})`;
|
|
32
|
-
}
|
|
33
|
-
|
|
34
|
-
export function isOk(result: Result<mixed, mixed>): boolean {
|
|
35
|
-
return result.type === 'ok';
|
|
36
|
-
}
|
|
37
|
-
|
|
38
|
-
export function isErr(result: Result<mixed, mixed>): boolean {
|
|
39
|
-
return result.type === 'err';
|
|
40
|
-
}
|
|
41
|
-
|
|
42
|
-
export function withDefault<T>(result: Result<T, mixed>, defaultValue: T): T {
|
|
43
|
-
return result.type === 'ok' ? result.value : defaultValue;
|
|
44
|
-
}
|
|
45
|
-
|
|
46
|
-
/**
|
|
47
|
-
* Unwrap the value from this Result instance if this is an "Ok" result.
|
|
48
|
-
* Otherwise, will throw the "Err" error via a runtime exception.
|
|
49
|
-
*/
|
|
50
|
-
export function unwrap<T>(result: Result<T, mixed>): T {
|
|
51
|
-
if (result.type === 'ok') {
|
|
52
|
-
return result.value;
|
|
53
|
-
} else {
|
|
54
|
-
throw result.error;
|
|
55
|
-
}
|
|
56
|
-
}
|
|
57
|
-
|
|
58
|
-
export function expect<T>(result: Result<T, mixed>, message: string | Error): T {
|
|
59
|
-
if (result.type === 'ok') {
|
|
60
|
-
return result.value;
|
|
61
|
-
} else {
|
|
62
|
-
throw message instanceof Error ? message : new Error(message);
|
|
63
|
-
}
|
|
64
|
-
}
|
|
65
|
-
|
|
66
|
-
export function dispatch<T, E, O>(
|
|
67
|
-
result: Result<T, E>,
|
|
68
|
-
okCallback: (value: T) => O,
|
|
69
|
-
errCallback: (error: E) => O,
|
|
70
|
-
): O {
|
|
71
|
-
return result.type === 'ok' ? okCallback(result.value) : errCallback(result.error);
|
|
72
|
-
}
|
|
73
|
-
|
|
74
|
-
/**
|
|
75
|
-
* If the given result is OK, defers to the other result. Otherwise returns the
|
|
76
|
-
* error result.
|
|
77
|
-
*
|
|
78
|
-
* It's like saying A && B, but on Result.
|
|
79
|
-
*
|
|
80
|
-
* Examples:
|
|
81
|
-
*
|
|
82
|
-
* Result.ok(42) && Result.ok('hi') // => Ok('hi')
|
|
83
|
-
* Result.err('boo') && Result.ok('hi') // => Err('boo')
|
|
84
|
-
* Result.ok(42) && Result.err('boo') // => Err('boo')
|
|
85
|
-
* Result.err('boo') && Result.err('boo') // => Err('boo')
|
|
86
|
-
*
|
|
87
|
-
*/
|
|
88
|
-
// export function and<T, E, T2>(
|
|
89
|
-
// result1: Result<T, E>,
|
|
90
|
-
// result2: Result<T2, E>,
|
|
91
|
-
// ): Result<T2, E> {
|
|
92
|
-
// return result1.type === 'ok' ? result2 : result1;
|
|
93
|
-
// }
|
|
94
|
-
|
|
95
|
-
/**
|
|
96
|
-
* If the given result is OK, return that result. Otherwise, defers to the
|
|
97
|
-
* other result.
|
|
98
|
-
*
|
|
99
|
-
* It's like saying A || B, but on Result.
|
|
100
|
-
*
|
|
101
|
-
* Examples:
|
|
102
|
-
*
|
|
103
|
-
* Result.ok(42) || Result.ok('hi') // => Ok(42)
|
|
104
|
-
* Result.err('boo') || Result.ok('hi') // => Ok('hi')
|
|
105
|
-
* Result.ok(42) || Result.err('boo') // => Ok(42)
|
|
106
|
-
* Result.err('bleh') || Result.err('boo') // => Err('boo')
|
|
107
|
-
*
|
|
108
|
-
*/
|
|
109
|
-
// export function or<T, E, E2>(
|
|
110
|
-
// result1: Result<T, E>,
|
|
111
|
-
// result2: Result<T, E2>,
|
|
112
|
-
// ): Result<T, E2> {
|
|
113
|
-
// return result1.type === 'ok' ? result1 : result2;
|
|
114
|
-
// }
|
|
115
|
-
|
|
116
|
-
/**
|
|
117
|
-
* Like .and(), aka &&, but the second argument gets evaluated lazily only if
|
|
118
|
-
* the first result is an Ok result. If so, it has access to the Ok value from
|
|
119
|
-
* the first argument.
|
|
120
|
-
*/
|
|
121
|
-
export function andThen<T, E, T2>(
|
|
122
|
-
result1: Result<T, E>,
|
|
123
|
-
lazyResult2: (value: T) => Result<T2, E>,
|
|
124
|
-
): Result<T2, E> {
|
|
125
|
-
return result1.type === 'ok' ? lazyResult2(result1.value) : result1;
|
|
126
|
-
}
|
|
127
|
-
|
|
128
|
-
/**
|
|
129
|
-
* Like .or(), aka ||, but the second argument gets evaluated lazily only if
|
|
130
|
-
* the first result is an Err result. If so, it has access to the Err value
|
|
131
|
-
* from the first argument.
|
|
132
|
-
*/
|
|
133
|
-
export function orElse<T, E, E2>(
|
|
134
|
-
result1: Result<T, E>,
|
|
135
|
-
lazyResult2: (errValue: E) => Result<T, E2>,
|
|
136
|
-
): Result<T, E2> {
|
|
137
|
-
return result1.type === 'ok' ? result1 : lazyResult2(result1.error);
|
|
138
|
-
}
|
|
139
|
-
|
|
140
|
-
/**
|
|
141
|
-
* Transform an Ok result. Will not touch Err results.
|
|
142
|
-
*/
|
|
143
|
-
export function mapOk<T, E, T2>(
|
|
144
|
-
result: Result<T, E>,
|
|
145
|
-
mapper: (value: T) => T2,
|
|
146
|
-
): Result<T2, E> {
|
|
147
|
-
return result.type === 'ok' ? ok(mapper(result.value)) : result;
|
|
148
|
-
}
|
|
149
|
-
|
|
150
|
-
/**
|
|
151
|
-
* Transform an Err value. Will not touch Ok results.
|
|
152
|
-
*/
|
|
153
|
-
export function mapError<T, E, E2>(
|
|
154
|
-
result: Result<T, E>,
|
|
155
|
-
mapper: (error: E) => E2,
|
|
156
|
-
): Result<T, E2> {
|
|
157
|
-
return result.type === 'ok' ? result : err(mapper(result.error));
|
|
158
|
-
}
|
package/format/index.js
DELETED
|
@@ -1,12 +0,0 @@
|
|
|
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/index.js.flow
DELETED
package/format/inline.js.flow
DELETED
|
@@ -1,122 +0,0 @@
|
|
|
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
DELETED