decoders 2.0.0-beta2 → 2.0.0-beta6
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +4 -0
- package/_esm/core/tuple.js +11 -7
- package/_esm/core/tuple.js.flow +24 -30
- package/_esm/index.js.flow +10 -3
- package/_esm/result.js +4 -8
- package/_esm/result.js.flow +5 -13
- package/core/tuple.js +10 -6
- package/core/tuple.js.flow +24 -30
- package/index.js.flow +10 -3
- package/package.json +1 -1
- package/result.js +5 -13
- package/result.js.flow +5 -13
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,9 @@
|
|
|
1
1
|
## v2.0.0-beta
|
|
2
2
|
|
|
3
|
+
Upgrading to v2 _can_, but doesn't _have_ to be a breaking change for you. If upgrading
|
|
4
|
+
causes errors for you, please see the [migration guide](./MIGRATING-v2.md) for
|
|
5
|
+
instructions.
|
|
6
|
+
|
|
3
7
|
Potentially breaking changes:
|
|
4
8
|
|
|
5
9
|
- Drop support for all Node versions below 12.x
|
package/_esm/core/tuple.js
CHANGED
|
@@ -1,8 +1,12 @@
|
|
|
1
1
|
import { annotate } from '../annotate';
|
|
2
2
|
import { compose, predicate } from './composition';
|
|
3
|
-
import { err,
|
|
3
|
+
import { err, ok, unwrap } from '../result';
|
|
4
4
|
import { poja } from './array';
|
|
5
5
|
|
|
6
|
+
function okOrErr(result) {
|
|
7
|
+
return result.type === 'ok' ? result.value : result.error;
|
|
8
|
+
}
|
|
9
|
+
|
|
6
10
|
var ntuple = function ntuple(n) {
|
|
7
11
|
return compose(poja, predicate(function (arr) {
|
|
8
12
|
return arr.length === n;
|
|
@@ -24,7 +28,7 @@ export function tuple1(decoder1) {
|
|
|
24
28
|
} catch (e) {
|
|
25
29
|
// If a decoder error has happened while unwrapping all the
|
|
26
30
|
// results, try to construct a good error message
|
|
27
|
-
return err(annotate(
|
|
31
|
+
return err(annotate([okOrErr(result1)]));
|
|
28
32
|
}
|
|
29
33
|
});
|
|
30
34
|
}
|
|
@@ -45,7 +49,7 @@ export function tuple2(decoder1, decoder2) {
|
|
|
45
49
|
} catch (e) {
|
|
46
50
|
// If a decoder error has happened while unwrapping all the
|
|
47
51
|
// results, try to construct a good error message
|
|
48
|
-
return err(annotate([
|
|
52
|
+
return err(annotate([okOrErr(result1), okOrErr(result2)]));
|
|
49
53
|
}
|
|
50
54
|
});
|
|
51
55
|
}
|
|
@@ -68,7 +72,7 @@ export function tuple3(decoder1, decoder2, decoder3) {
|
|
|
68
72
|
} catch (e) {
|
|
69
73
|
// If a decoder error has happened while unwrapping all the
|
|
70
74
|
// results, try to construct a good error message
|
|
71
|
-
return err(annotate([
|
|
75
|
+
return err(annotate([okOrErr(result1), okOrErr(result2), okOrErr(result3)]));
|
|
72
76
|
}
|
|
73
77
|
});
|
|
74
78
|
}
|
|
@@ -93,7 +97,7 @@ export function tuple4(decoder1, decoder2, decoder3, decoder4) {
|
|
|
93
97
|
} catch (e) {
|
|
94
98
|
// If a decoder error has happened while unwrapping all the
|
|
95
99
|
// results, try to construct a good error message
|
|
96
|
-
return err(annotate([
|
|
100
|
+
return err(annotate([okOrErr(result1), okOrErr(result2), okOrErr(result3), okOrErr(result4)]));
|
|
97
101
|
}
|
|
98
102
|
});
|
|
99
103
|
}
|
|
@@ -120,7 +124,7 @@ export function tuple5(decoder1, decoder2, decoder3, decoder4, decoder5) {
|
|
|
120
124
|
} catch (e) {
|
|
121
125
|
// If a decoder error has happened while unwrapping all the
|
|
122
126
|
// results, try to construct a good error message
|
|
123
|
-
return err(annotate([
|
|
127
|
+
return err(annotate([okOrErr(result1), okOrErr(result2), okOrErr(result3), okOrErr(result4), okOrErr(result5)]));
|
|
124
128
|
}
|
|
125
129
|
});
|
|
126
130
|
}
|
|
@@ -149,7 +153,7 @@ export function tuple6(decoder1, decoder2, decoder3, decoder4, decoder5, decoder
|
|
|
149
153
|
} catch (e) {
|
|
150
154
|
// If a decoder error has happened while unwrapping all the
|
|
151
155
|
// results, try to construct a good error message
|
|
152
|
-
return err(annotate([
|
|
156
|
+
return err(annotate([okOrErr(result1), okOrErr(result2), okOrErr(result3), okOrErr(result4), okOrErr(result5), okOrErr(result6)]));
|
|
153
157
|
}
|
|
154
158
|
});
|
|
155
159
|
}
|
package/_esm/core/tuple.js.flow
CHANGED
|
@@ -2,9 +2,14 @@
|
|
|
2
2
|
|
|
3
3
|
import { annotate } from '../annotate';
|
|
4
4
|
import { compose, predicate } from './composition';
|
|
5
|
-
import { err,
|
|
5
|
+
import { err, ok, unwrap } from '../result';
|
|
6
6
|
import { poja } from './array';
|
|
7
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
|
+
}
|
|
8
13
|
|
|
9
14
|
const ntuple = (n: number) =>
|
|
10
15
|
compose(
|
|
@@ -26,7 +31,7 @@ export function tuple1<T>(decoder1: Decoder<T>): Decoder<[T]> {
|
|
|
26
31
|
} catch (e) {
|
|
27
32
|
// If a decoder error has happened while unwrapping all the
|
|
28
33
|
// results, try to construct a good error message
|
|
29
|
-
return err(annotate(
|
|
34
|
+
return err(annotate([okOrErr(result1)]));
|
|
30
35
|
}
|
|
31
36
|
});
|
|
32
37
|
}
|
|
@@ -49,12 +54,7 @@ export function tuple2<T1, T2>(
|
|
|
49
54
|
} catch (e) {
|
|
50
55
|
// If a decoder error has happened while unwrapping all the
|
|
51
56
|
// 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
|
-
);
|
|
57
|
+
return err(annotate([okOrErr(result1), okOrErr(result2)]));
|
|
58
58
|
}
|
|
59
59
|
});
|
|
60
60
|
}
|
|
@@ -79,13 +79,7 @@ export function tuple3<T1, T2, T3>(
|
|
|
79
79
|
} catch (e) {
|
|
80
80
|
// If a decoder error has happened while unwrapping all the
|
|
81
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
|
-
);
|
|
82
|
+
return err(annotate([okOrErr(result1), okOrErr(result2), okOrErr(result3)]));
|
|
89
83
|
}
|
|
90
84
|
});
|
|
91
85
|
}
|
|
@@ -119,10 +113,10 @@ export function tuple4<T1, T2, T3, T4>(
|
|
|
119
113
|
// results, try to construct a good error message
|
|
120
114
|
return err(
|
|
121
115
|
annotate([
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
116
|
+
okOrErr(result1),
|
|
117
|
+
okOrErr(result2),
|
|
118
|
+
okOrErr(result3),
|
|
119
|
+
okOrErr(result4),
|
|
126
120
|
]),
|
|
127
121
|
);
|
|
128
122
|
}
|
|
@@ -161,11 +155,11 @@ export function tuple5<T1, T2, T3, T4, T5>(
|
|
|
161
155
|
// results, try to construct a good error message
|
|
162
156
|
return err(
|
|
163
157
|
annotate([
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
158
|
+
okOrErr(result1),
|
|
159
|
+
okOrErr(result2),
|
|
160
|
+
okOrErr(result3),
|
|
161
|
+
okOrErr(result4),
|
|
162
|
+
okOrErr(result5),
|
|
169
163
|
]),
|
|
170
164
|
);
|
|
171
165
|
}
|
|
@@ -207,12 +201,12 @@ export function tuple6<T1, T2, T3, T4, T5, T6>(
|
|
|
207
201
|
// results, try to construct a good error message
|
|
208
202
|
return err(
|
|
209
203
|
annotate([
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
204
|
+
okOrErr(result1),
|
|
205
|
+
okOrErr(result2),
|
|
206
|
+
okOrErr(result3),
|
|
207
|
+
okOrErr(result4),
|
|
208
|
+
okOrErr(result5),
|
|
209
|
+
okOrErr(result6),
|
|
216
210
|
]),
|
|
217
211
|
);
|
|
218
212
|
}
|
package/_esm/index.js.flow
CHANGED
|
@@ -18,9 +18,16 @@
|
|
|
18
18
|
* type. In our case, it's fine to fail with a runtime error.
|
|
19
19
|
*
|
|
20
20
|
*/
|
|
21
|
-
export type {
|
|
22
|
-
|
|
23
|
-
|
|
21
|
+
export type {
|
|
22
|
+
Decoder,
|
|
23
|
+
DecodeResult,
|
|
24
|
+
DecoderType,
|
|
25
|
+
Guard,
|
|
26
|
+
GuardType,
|
|
27
|
+
Predicate,
|
|
28
|
+
Scalar,
|
|
29
|
+
} from './_types';
|
|
30
|
+
export type { Result } from './result';
|
|
24
31
|
export type { JSONValue, JSONObject, JSONArray } from './core/json';
|
|
25
32
|
|
|
26
33
|
export { guard } from './_guard';
|
package/_esm/result.js
CHANGED
|
@@ -10,7 +10,8 @@
|
|
|
10
10
|
export function ok(value) {
|
|
11
11
|
return {
|
|
12
12
|
type: 'ok',
|
|
13
|
-
value: value
|
|
13
|
+
value: value,
|
|
14
|
+
error: undefined
|
|
14
15
|
};
|
|
15
16
|
}
|
|
16
17
|
/**
|
|
@@ -20,6 +21,7 @@ export function ok(value) {
|
|
|
20
21
|
export function err(error) {
|
|
21
22
|
return {
|
|
22
23
|
type: 'err',
|
|
24
|
+
value: undefined,
|
|
23
25
|
error: error
|
|
24
26
|
};
|
|
25
27
|
}
|
|
@@ -35,12 +37,6 @@ export function isErr(result) {
|
|
|
35
37
|
export function withDefault(result, defaultValue) {
|
|
36
38
|
return result.type === 'ok' ? result.value : defaultValue;
|
|
37
39
|
}
|
|
38
|
-
export function value(result) {
|
|
39
|
-
return result.type === 'ok' ? result.value : undefined;
|
|
40
|
-
}
|
|
41
|
-
export function errValue(result) {
|
|
42
|
-
return result.type === 'err' ? result.error : undefined;
|
|
43
|
-
}
|
|
44
40
|
/**
|
|
45
41
|
* Unwrap the value from this Result instance if this is an "Ok" result.
|
|
46
42
|
* Otherwise, will throw the "Err" error via a runtime exception.
|
|
@@ -127,7 +123,7 @@ export function orElse(result1, lazyResult2) {
|
|
|
127
123
|
* Transform an Ok result. Will not touch Err results.
|
|
128
124
|
*/
|
|
129
125
|
|
|
130
|
-
export function
|
|
126
|
+
export function mapOk(result, mapper) {
|
|
131
127
|
return result.type === 'ok' ? ok(mapper(result.value)) : result;
|
|
132
128
|
}
|
|
133
129
|
/**
|
package/_esm/result.js.flow
CHANGED
|
@@ -6,8 +6,8 @@
|
|
|
6
6
|
* | Err <error>
|
|
7
7
|
*/
|
|
8
8
|
|
|
9
|
-
type Ok<+T> = {| +type: 'ok', +value: T |};
|
|
10
|
-
type Err<+E> = {| +type: 'err', +error: E |};
|
|
9
|
+
type Ok<+T> = {| +type: 'ok', +value: T, +error: void |};
|
|
10
|
+
type Err<+E> = {| +type: 'err', +value: void, +error: E |};
|
|
11
11
|
|
|
12
12
|
export type Result<+T, +E> = Ok<T> | Err<E>;
|
|
13
13
|
|
|
@@ -15,14 +15,14 @@ export type Result<+T, +E> = Ok<T> | Err<E>;
|
|
|
15
15
|
* Create a new Result instance representing a successful computation.
|
|
16
16
|
*/
|
|
17
17
|
export function ok<T>(value: T): Ok<T> {
|
|
18
|
-
return { type: 'ok', value };
|
|
18
|
+
return { type: 'ok', value, error: undefined };
|
|
19
19
|
}
|
|
20
20
|
|
|
21
21
|
/**
|
|
22
22
|
* Create a new Result instance representing a failed computation.
|
|
23
23
|
*/
|
|
24
24
|
export function err<E>(error: E): Err<E> {
|
|
25
|
-
return { type: 'err', error };
|
|
25
|
+
return { type: 'err', value: undefined, error };
|
|
26
26
|
}
|
|
27
27
|
|
|
28
28
|
export function toString(result: Result<mixed, mixed>): string {
|
|
@@ -43,14 +43,6 @@ export function withDefault<T>(result: Result<T, mixed>, defaultValue: T): T {
|
|
|
43
43
|
return result.type === 'ok' ? result.value : defaultValue;
|
|
44
44
|
}
|
|
45
45
|
|
|
46
|
-
export function value<T>(result: Result<T, mixed>): void | T {
|
|
47
|
-
return result.type === 'ok' ? result.value : undefined;
|
|
48
|
-
}
|
|
49
|
-
|
|
50
|
-
export function errValue<E>(result: Result<mixed, E>): void | E {
|
|
51
|
-
return result.type === 'err' ? result.error : undefined;
|
|
52
|
-
}
|
|
53
|
-
|
|
54
46
|
/**
|
|
55
47
|
* Unwrap the value from this Result instance if this is an "Ok" result.
|
|
56
48
|
* Otherwise, will throw the "Err" error via a runtime exception.
|
|
@@ -148,7 +140,7 @@ export function orElse<T, E, E2>(
|
|
|
148
140
|
/**
|
|
149
141
|
* Transform an Ok result. Will not touch Err results.
|
|
150
142
|
*/
|
|
151
|
-
export function
|
|
143
|
+
export function mapOk<T, E, T2>(
|
|
152
144
|
result: Result<T, E>,
|
|
153
145
|
mapper: (value: T) => T2,
|
|
154
146
|
): Result<T2, E> {
|
package/core/tuple.js
CHANGED
|
@@ -16,6 +16,10 @@ var _result = require("../result");
|
|
|
16
16
|
|
|
17
17
|
var _array = require("./array");
|
|
18
18
|
|
|
19
|
+
function okOrErr(result) {
|
|
20
|
+
return result.type === 'ok' ? result.value : result.error;
|
|
21
|
+
}
|
|
22
|
+
|
|
19
23
|
var ntuple = function ntuple(n) {
|
|
20
24
|
return (0, _composition.compose)(_array.poja, (0, _composition.predicate)(function (arr) {
|
|
21
25
|
return arr.length === n;
|
|
@@ -37,7 +41,7 @@ function tuple1(decoder1) {
|
|
|
37
41
|
} catch (e) {
|
|
38
42
|
// If a decoder error has happened while unwrapping all the
|
|
39
43
|
// results, try to construct a good error message
|
|
40
|
-
return (0, _result.err)((0, _annotate.annotate)((
|
|
44
|
+
return (0, _result.err)((0, _annotate.annotate)([okOrErr(result1)]));
|
|
41
45
|
}
|
|
42
46
|
});
|
|
43
47
|
}
|
|
@@ -59,7 +63,7 @@ function tuple2(decoder1, decoder2) {
|
|
|
59
63
|
} catch (e) {
|
|
60
64
|
// If a decoder error has happened while unwrapping all the
|
|
61
65
|
// results, try to construct a good error message
|
|
62
|
-
return (0, _result.err)((0, _annotate.annotate)([(
|
|
66
|
+
return (0, _result.err)((0, _annotate.annotate)([okOrErr(result1), okOrErr(result2)]));
|
|
63
67
|
}
|
|
64
68
|
});
|
|
65
69
|
}
|
|
@@ -83,7 +87,7 @@ function tuple3(decoder1, decoder2, decoder3) {
|
|
|
83
87
|
} catch (e) {
|
|
84
88
|
// If a decoder error has happened while unwrapping all the
|
|
85
89
|
// results, try to construct a good error message
|
|
86
|
-
return (0, _result.err)((0, _annotate.annotate)([(
|
|
90
|
+
return (0, _result.err)((0, _annotate.annotate)([okOrErr(result1), okOrErr(result2), okOrErr(result3)]));
|
|
87
91
|
}
|
|
88
92
|
});
|
|
89
93
|
}
|
|
@@ -109,7 +113,7 @@ function tuple4(decoder1, decoder2, decoder3, decoder4) {
|
|
|
109
113
|
} catch (e) {
|
|
110
114
|
// If a decoder error has happened while unwrapping all the
|
|
111
115
|
// results, try to construct a good error message
|
|
112
|
-
return (0, _result.err)((0, _annotate.annotate)([(
|
|
116
|
+
return (0, _result.err)((0, _annotate.annotate)([okOrErr(result1), okOrErr(result2), okOrErr(result3), okOrErr(result4)]));
|
|
113
117
|
}
|
|
114
118
|
});
|
|
115
119
|
}
|
|
@@ -137,7 +141,7 @@ function tuple5(decoder1, decoder2, decoder3, decoder4, decoder5) {
|
|
|
137
141
|
} catch (e) {
|
|
138
142
|
// If a decoder error has happened while unwrapping all the
|
|
139
143
|
// results, try to construct a good error message
|
|
140
|
-
return (0, _result.err)((0, _annotate.annotate)([(
|
|
144
|
+
return (0, _result.err)((0, _annotate.annotate)([okOrErr(result1), okOrErr(result2), okOrErr(result3), okOrErr(result4), okOrErr(result5)]));
|
|
141
145
|
}
|
|
142
146
|
});
|
|
143
147
|
}
|
|
@@ -167,7 +171,7 @@ function tuple6(decoder1, decoder2, decoder3, decoder4, decoder5, decoder6) {
|
|
|
167
171
|
} catch (e) {
|
|
168
172
|
// If a decoder error has happened while unwrapping all the
|
|
169
173
|
// results, try to construct a good error message
|
|
170
|
-
return (0, _result.err)((0, _annotate.annotate)([(
|
|
174
|
+
return (0, _result.err)((0, _annotate.annotate)([okOrErr(result1), okOrErr(result2), okOrErr(result3), okOrErr(result4), okOrErr(result5), okOrErr(result6)]));
|
|
171
175
|
}
|
|
172
176
|
});
|
|
173
177
|
}
|
package/core/tuple.js.flow
CHANGED
|
@@ -2,9 +2,14 @@
|
|
|
2
2
|
|
|
3
3
|
import { annotate } from '../annotate';
|
|
4
4
|
import { compose, predicate } from './composition';
|
|
5
|
-
import { err,
|
|
5
|
+
import { err, ok, unwrap } from '../result';
|
|
6
6
|
import { poja } from './array';
|
|
7
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
|
+
}
|
|
8
13
|
|
|
9
14
|
const ntuple = (n: number) =>
|
|
10
15
|
compose(
|
|
@@ -26,7 +31,7 @@ export function tuple1<T>(decoder1: Decoder<T>): Decoder<[T]> {
|
|
|
26
31
|
} catch (e) {
|
|
27
32
|
// If a decoder error has happened while unwrapping all the
|
|
28
33
|
// results, try to construct a good error message
|
|
29
|
-
return err(annotate(
|
|
34
|
+
return err(annotate([okOrErr(result1)]));
|
|
30
35
|
}
|
|
31
36
|
});
|
|
32
37
|
}
|
|
@@ -49,12 +54,7 @@ export function tuple2<T1, T2>(
|
|
|
49
54
|
} catch (e) {
|
|
50
55
|
// If a decoder error has happened while unwrapping all the
|
|
51
56
|
// 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
|
-
);
|
|
57
|
+
return err(annotate([okOrErr(result1), okOrErr(result2)]));
|
|
58
58
|
}
|
|
59
59
|
});
|
|
60
60
|
}
|
|
@@ -79,13 +79,7 @@ export function tuple3<T1, T2, T3>(
|
|
|
79
79
|
} catch (e) {
|
|
80
80
|
// If a decoder error has happened while unwrapping all the
|
|
81
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
|
-
);
|
|
82
|
+
return err(annotate([okOrErr(result1), okOrErr(result2), okOrErr(result3)]));
|
|
89
83
|
}
|
|
90
84
|
});
|
|
91
85
|
}
|
|
@@ -119,10 +113,10 @@ export function tuple4<T1, T2, T3, T4>(
|
|
|
119
113
|
// results, try to construct a good error message
|
|
120
114
|
return err(
|
|
121
115
|
annotate([
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
116
|
+
okOrErr(result1),
|
|
117
|
+
okOrErr(result2),
|
|
118
|
+
okOrErr(result3),
|
|
119
|
+
okOrErr(result4),
|
|
126
120
|
]),
|
|
127
121
|
);
|
|
128
122
|
}
|
|
@@ -161,11 +155,11 @@ export function tuple5<T1, T2, T3, T4, T5>(
|
|
|
161
155
|
// results, try to construct a good error message
|
|
162
156
|
return err(
|
|
163
157
|
annotate([
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
158
|
+
okOrErr(result1),
|
|
159
|
+
okOrErr(result2),
|
|
160
|
+
okOrErr(result3),
|
|
161
|
+
okOrErr(result4),
|
|
162
|
+
okOrErr(result5),
|
|
169
163
|
]),
|
|
170
164
|
);
|
|
171
165
|
}
|
|
@@ -207,12 +201,12 @@ export function tuple6<T1, T2, T3, T4, T5, T6>(
|
|
|
207
201
|
// results, try to construct a good error message
|
|
208
202
|
return err(
|
|
209
203
|
annotate([
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
204
|
+
okOrErr(result1),
|
|
205
|
+
okOrErr(result2),
|
|
206
|
+
okOrErr(result3),
|
|
207
|
+
okOrErr(result4),
|
|
208
|
+
okOrErr(result5),
|
|
209
|
+
okOrErr(result6),
|
|
216
210
|
]),
|
|
217
211
|
);
|
|
218
212
|
}
|
package/index.js.flow
CHANGED
|
@@ -18,9 +18,16 @@
|
|
|
18
18
|
* type. In our case, it's fine to fail with a runtime error.
|
|
19
19
|
*
|
|
20
20
|
*/
|
|
21
|
-
export type {
|
|
22
|
-
|
|
23
|
-
|
|
21
|
+
export type {
|
|
22
|
+
Decoder,
|
|
23
|
+
DecodeResult,
|
|
24
|
+
DecoderType,
|
|
25
|
+
Guard,
|
|
26
|
+
GuardType,
|
|
27
|
+
Predicate,
|
|
28
|
+
Scalar,
|
|
29
|
+
} from './_types';
|
|
30
|
+
export type { Result } from './result';
|
|
24
31
|
export type { JSONValue, JSONObject, JSONArray } from './core/json';
|
|
25
32
|
|
|
26
33
|
export { guard } from './_guard';
|
package/package.json
CHANGED
package/result.js
CHANGED
|
@@ -4,17 +4,15 @@ exports.__esModule = true;
|
|
|
4
4
|
exports.andThen = andThen;
|
|
5
5
|
exports.dispatch = dispatch;
|
|
6
6
|
exports.err = err;
|
|
7
|
-
exports.errValue = errValue;
|
|
8
7
|
exports.expect = expect;
|
|
9
8
|
exports.isErr = isErr;
|
|
10
9
|
exports.isOk = isOk;
|
|
11
|
-
exports.map = map;
|
|
12
10
|
exports.mapError = mapError;
|
|
11
|
+
exports.mapOk = mapOk;
|
|
13
12
|
exports.ok = ok;
|
|
14
13
|
exports.orElse = orElse;
|
|
15
14
|
exports.toString = toString;
|
|
16
15
|
exports.unwrap = unwrap;
|
|
17
|
-
exports.value = value;
|
|
18
16
|
exports.withDefault = withDefault;
|
|
19
17
|
|
|
20
18
|
/**
|
|
@@ -29,7 +27,8 @@ exports.withDefault = withDefault;
|
|
|
29
27
|
function ok(value) {
|
|
30
28
|
return {
|
|
31
29
|
type: 'ok',
|
|
32
|
-
value: value
|
|
30
|
+
value: value,
|
|
31
|
+
error: undefined
|
|
33
32
|
};
|
|
34
33
|
}
|
|
35
34
|
/**
|
|
@@ -40,6 +39,7 @@ function ok(value) {
|
|
|
40
39
|
function err(error) {
|
|
41
40
|
return {
|
|
42
41
|
type: 'err',
|
|
42
|
+
value: undefined,
|
|
43
43
|
error: error
|
|
44
44
|
};
|
|
45
45
|
}
|
|
@@ -59,14 +59,6 @@ function isErr(result) {
|
|
|
59
59
|
function withDefault(result, defaultValue) {
|
|
60
60
|
return result.type === 'ok' ? result.value : defaultValue;
|
|
61
61
|
}
|
|
62
|
-
|
|
63
|
-
function value(result) {
|
|
64
|
-
return result.type === 'ok' ? result.value : undefined;
|
|
65
|
-
}
|
|
66
|
-
|
|
67
|
-
function errValue(result) {
|
|
68
|
-
return result.type === 'err' ? result.error : undefined;
|
|
69
|
-
}
|
|
70
62
|
/**
|
|
71
63
|
* Unwrap the value from this Result instance if this is an "Ok" result.
|
|
72
64
|
* Otherwise, will throw the "Err" error via a runtime exception.
|
|
@@ -159,7 +151,7 @@ function orElse(result1, lazyResult2) {
|
|
|
159
151
|
*/
|
|
160
152
|
|
|
161
153
|
|
|
162
|
-
function
|
|
154
|
+
function mapOk(result, mapper) {
|
|
163
155
|
return result.type === 'ok' ? ok(mapper(result.value)) : result;
|
|
164
156
|
}
|
|
165
157
|
/**
|
package/result.js.flow
CHANGED
|
@@ -6,8 +6,8 @@
|
|
|
6
6
|
* | Err <error>
|
|
7
7
|
*/
|
|
8
8
|
|
|
9
|
-
type Ok<+T> = {| +type: 'ok', +value: T |};
|
|
10
|
-
type Err<+E> = {| +type: 'err', +error: E |};
|
|
9
|
+
type Ok<+T> = {| +type: 'ok', +value: T, +error: void |};
|
|
10
|
+
type Err<+E> = {| +type: 'err', +value: void, +error: E |};
|
|
11
11
|
|
|
12
12
|
export type Result<+T, +E> = Ok<T> | Err<E>;
|
|
13
13
|
|
|
@@ -15,14 +15,14 @@ export type Result<+T, +E> = Ok<T> | Err<E>;
|
|
|
15
15
|
* Create a new Result instance representing a successful computation.
|
|
16
16
|
*/
|
|
17
17
|
export function ok<T>(value: T): Ok<T> {
|
|
18
|
-
return { type: 'ok', value };
|
|
18
|
+
return { type: 'ok', value, error: undefined };
|
|
19
19
|
}
|
|
20
20
|
|
|
21
21
|
/**
|
|
22
22
|
* Create a new Result instance representing a failed computation.
|
|
23
23
|
*/
|
|
24
24
|
export function err<E>(error: E): Err<E> {
|
|
25
|
-
return { type: 'err', error };
|
|
25
|
+
return { type: 'err', value: undefined, error };
|
|
26
26
|
}
|
|
27
27
|
|
|
28
28
|
export function toString(result: Result<mixed, mixed>): string {
|
|
@@ -43,14 +43,6 @@ export function withDefault<T>(result: Result<T, mixed>, defaultValue: T): T {
|
|
|
43
43
|
return result.type === 'ok' ? result.value : defaultValue;
|
|
44
44
|
}
|
|
45
45
|
|
|
46
|
-
export function value<T>(result: Result<T, mixed>): void | T {
|
|
47
|
-
return result.type === 'ok' ? result.value : undefined;
|
|
48
|
-
}
|
|
49
|
-
|
|
50
|
-
export function errValue<E>(result: Result<mixed, E>): void | E {
|
|
51
|
-
return result.type === 'err' ? result.error : undefined;
|
|
52
|
-
}
|
|
53
|
-
|
|
54
46
|
/**
|
|
55
47
|
* Unwrap the value from this Result instance if this is an "Ok" result.
|
|
56
48
|
* Otherwise, will throw the "Err" error via a runtime exception.
|
|
@@ -148,7 +140,7 @@ export function orElse<T, E, E2>(
|
|
|
148
140
|
/**
|
|
149
141
|
* Transform an Ok result. Will not touch Err results.
|
|
150
142
|
*/
|
|
151
|
-
export function
|
|
143
|
+
export function mapOk<T, E, T2>(
|
|
152
144
|
result: Result<T, E>,
|
|
153
145
|
mapper: (value: T) => T2,
|
|
154
146
|
): Result<T2, E> {
|