decoders 2.0.0-beta1 → 2.0.0-beta5
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/{es → _esm}/_guard.js +0 -0
- package/{cjs → _esm}/_guard.js.flow +0 -0
- package/{es → _esm}/_types.js +0 -0
- package/{cjs → _esm}/_types.js.flow +0 -0
- package/{es → _esm}/_utils.js +0 -0
- package/{cjs → _esm}/_utils.js.flow +0 -0
- package/{es → _esm}/annotate.js +0 -0
- package/{cjs → _esm}/annotate.js.flow +0 -0
- package/{es/stdlib → _esm/core}/array.js +0 -0
- package/{cjs/stdlib → _esm/core}/array.js.flow +0 -0
- package/{es/stdlib → _esm/core}/boolean.js +0 -0
- package/{cjs/stdlib → _esm/core}/boolean.js.flow +0 -0
- package/{es/stdlib → _esm/core}/composition.js +0 -0
- package/{cjs/stdlib → _esm/core}/composition.js.flow +0 -0
- package/{es/stdlib → _esm/core}/constants.js +0 -0
- package/{cjs/stdlib → _esm/core}/constants.js.flow +0 -0
- package/{es/stdlib → _esm/core}/date.js +0 -0
- package/{cjs/stdlib → _esm/core}/date.js.flow +0 -0
- package/{es/stdlib → _esm/core}/describe.js +0 -0
- package/{cjs/stdlib → _esm/core}/describe.js.flow +0 -0
- package/{es/stdlib → _esm/core}/dispatch.js +0 -0
- package/{cjs/stdlib → _esm/core}/dispatch.js.flow +0 -0
- package/{es/stdlib → _esm/core}/either.js +0 -0
- package/{cjs/stdlib → _esm/core}/either.js.flow +0 -0
- package/{es/stdlib → _esm/core}/fail.js +0 -0
- package/{cjs/stdlib → _esm/core}/fail.js.flow +0 -0
- package/{es/stdlib → _esm/core}/instanceOf.js +0 -0
- package/{cjs/stdlib → _esm/core}/instanceOf.js.flow +0 -0
- package/{es/stdlib → _esm/core}/json.js +0 -0
- package/{cjs/stdlib → _esm/core}/json.js.flow +0 -0
- package/{es/stdlib → _esm/core}/lazy.js +0 -0
- package/{cjs/stdlib → _esm/core}/lazy.js.flow +0 -0
- package/{es/stdlib → _esm/core}/mapping.js +0 -0
- package/{cjs/stdlib → _esm/core}/mapping.js.flow +0 -0
- package/{es/stdlib → _esm/core}/number.js +0 -0
- package/{cjs/stdlib → _esm/core}/number.js.flow +0 -0
- package/{es/stdlib → _esm/core}/object.js +0 -0
- package/{cjs/stdlib → _esm/core}/object.js.flow +0 -0
- package/{es/stdlib → _esm/core}/optional.js +0 -0
- package/{cjs/stdlib → _esm/core}/optional.js.flow +0 -0
- package/{es/stdlib → _esm/core}/string.js +0 -0
- package/{cjs/stdlib → _esm/core}/string.js.flow +0 -0
- package/{es/stdlib → _esm/core}/tuple.js +7 -7
- package/{cjs/stdlib → _esm/core}/tuple.js.flow +21 -26
- package/{es → _esm}/format/index.js +0 -0
- package/{cjs → _esm}/format/index.js.flow +0 -0
- package/{es → _esm}/format/inline.js +0 -0
- package/{cjs → _esm}/format/inline.js.flow +0 -0
- package/{es → _esm}/format/short.js +0 -0
- package/{cjs → _esm}/format/short.js.flow +0 -0
- package/{es → _esm}/index.js +18 -18
- package/{cjs → _esm}/index.js.flow +29 -29
- package/{es → _esm}/result.js +14 -5
- package/{cjs → _esm}/result.js.flow +14 -6
- package/{cjs/_guard.js → _guard.js} +0 -0
- package/_guard.js.flow +20 -0
- package/{cjs/_types.js → _types.js} +0 -0
- package/_types.js.flow +20 -0
- package/{cjs/_utils.js → _utils.js} +0 -0
- package/_utils.js.flow +97 -0
- package/{cjs/annotate.js → annotate.js} +0 -0
- package/annotate.js.flow +218 -0
- package/{cjs/stdlib → core}/array.js +0 -0
- package/core/array.js.flow +103 -0
- package/{cjs/stdlib → core}/boolean.js +0 -0
- package/core/boolean.js.flow +29 -0
- package/{cjs/stdlib → core}/composition.js +0 -0
- package/core/composition.js.flow +43 -0
- package/{cjs/stdlib → core}/constants.js +0 -0
- package/core/constants.js.flow +46 -0
- package/{cjs/stdlib → core}/date.js +0 -0
- package/core/date.js.flow +40 -0
- package/{cjs/stdlib → core}/describe.js +0 -0
- package/core/describe.js.flow +17 -0
- package/{cjs/stdlib → core}/dispatch.js +0 -0
- package/core/dispatch.js.flow +58 -0
- package/{cjs/stdlib → core}/either.js +0 -0
- package/core/either.js.flow +151 -0
- package/{cjs/stdlib → core}/fail.js +0 -0
- package/core/fail.js.flow +12 -0
- package/{cjs/stdlib → core}/instanceOf.js +0 -0
- package/core/instanceOf.js.flow +20 -0
- package/{cjs/stdlib → core}/json.js +0 -0
- package/core/json.js.flow +28 -0
- package/{cjs/stdlib → core}/lazy.js +0 -0
- package/core/lazy.js.flow +15 -0
- package/{cjs/stdlib → core}/mapping.js +0 -0
- package/core/mapping.js.flow +54 -0
- package/{cjs/stdlib → core}/number.js +0 -0
- package/core/number.js.flow +34 -0
- package/{cjs/stdlib → core}/object.js +0 -0
- package/core/object.js.flow +203 -0
- package/{cjs/stdlib → core}/optional.js +0 -0
- package/core/optional.js.flow +41 -0
- package/{cjs/stdlib → core}/string.js +0 -0
- package/core/string.js.flow +82 -0
- package/{cjs/stdlib → core}/tuple.js +6 -6
- package/core/tuple.js.flow +215 -0
- package/{cjs/format → format}/index.js +0 -0
- package/format/index.js.flow +4 -0
- package/{cjs/format → format}/inline.js +0 -0
- package/format/inline.js.flow +122 -0
- package/{cjs/format → format}/short.js +0 -0
- package/format/short.js.flow +8 -0
- package/{cjs/index.js → index.js} +18 -18
- package/index.js.flow +63 -0
- package/package.json +3 -12
- package/{cjs/result.js → result.js} +18 -7
- package/result.js.flow +174 -0
- package/ts/_guard.d.ts +0 -7
- package/ts/_helpers.d.ts +0 -79
- package/ts/_types.d.ts +0 -16
- package/ts/_utils.d.ts +0 -13
- package/ts/annotate.d.ts +0 -58
- 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/index.d.ts +0 -42
- package/ts/inline.d.ts +0 -3
- 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/result.d.ts +0 -39
- package/ts/short.d.ts +0 -3
- package/ts/string.d.ts +0 -7
- package/ts/tuple.d.ts +0 -30
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/{es → _esm}/_guard.js
RENAMED
|
File without changes
|
|
File without changes
|
package/{es → _esm}/_types.js
RENAMED
|
File without changes
|
|
File without changes
|
package/{es → _esm}/_utils.js
RENAMED
|
File without changes
|
|
File without changes
|
package/{es → _esm}/annotate.js
RENAMED
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { annotate } from '../annotate';
|
|
2
2
|
import { compose, predicate } from './composition';
|
|
3
|
-
import { err,
|
|
3
|
+
import { err, ok, okOrErrValue, unwrap } from '../result';
|
|
4
4
|
import { poja } from './array';
|
|
5
5
|
|
|
6
6
|
var ntuple = function ntuple(n) {
|
|
@@ -24,7 +24,7 @@ export function tuple1(decoder1) {
|
|
|
24
24
|
} catch (e) {
|
|
25
25
|
// If a decoder error has happened while unwrapping all the
|
|
26
26
|
// results, try to construct a good error message
|
|
27
|
-
return err(annotate(
|
|
27
|
+
return err(annotate([okOrErrValue(result1)]));
|
|
28
28
|
}
|
|
29
29
|
});
|
|
30
30
|
}
|
|
@@ -45,7 +45,7 @@ export function tuple2(decoder1, decoder2) {
|
|
|
45
45
|
} catch (e) {
|
|
46
46
|
// If a decoder error has happened while unwrapping all the
|
|
47
47
|
// results, try to construct a good error message
|
|
48
|
-
return err(annotate([
|
|
48
|
+
return err(annotate([okOrErrValue(result1), okOrErrValue(result2)]));
|
|
49
49
|
}
|
|
50
50
|
});
|
|
51
51
|
}
|
|
@@ -68,7 +68,7 @@ export function tuple3(decoder1, decoder2, decoder3) {
|
|
|
68
68
|
} catch (e) {
|
|
69
69
|
// If a decoder error has happened while unwrapping all the
|
|
70
70
|
// results, try to construct a good error message
|
|
71
|
-
return err(annotate([
|
|
71
|
+
return err(annotate([okOrErrValue(result1), okOrErrValue(result2), okOrErrValue(result3)]));
|
|
72
72
|
}
|
|
73
73
|
});
|
|
74
74
|
}
|
|
@@ -93,7 +93,7 @@ export function tuple4(decoder1, decoder2, decoder3, decoder4) {
|
|
|
93
93
|
} catch (e) {
|
|
94
94
|
// If a decoder error has happened while unwrapping all the
|
|
95
95
|
// results, try to construct a good error message
|
|
96
|
-
return err(annotate([
|
|
96
|
+
return err(annotate([okOrErrValue(result1), okOrErrValue(result2), okOrErrValue(result3), okOrErrValue(result4)]));
|
|
97
97
|
}
|
|
98
98
|
});
|
|
99
99
|
}
|
|
@@ -120,7 +120,7 @@ export function tuple5(decoder1, decoder2, decoder3, decoder4, decoder5) {
|
|
|
120
120
|
} catch (e) {
|
|
121
121
|
// If a decoder error has happened while unwrapping all the
|
|
122
122
|
// results, try to construct a good error message
|
|
123
|
-
return err(annotate([
|
|
123
|
+
return err(annotate([okOrErrValue(result1), okOrErrValue(result2), okOrErrValue(result3), okOrErrValue(result4), okOrErrValue(result5)]));
|
|
124
124
|
}
|
|
125
125
|
});
|
|
126
126
|
}
|
|
@@ -149,7 +149,7 @@ export function tuple6(decoder1, decoder2, decoder3, decoder4, decoder5, decoder
|
|
|
149
149
|
} catch (e) {
|
|
150
150
|
// If a decoder error has happened while unwrapping all the
|
|
151
151
|
// results, try to construct a good error message
|
|
152
|
-
return err(annotate([
|
|
152
|
+
return err(annotate([okOrErrValue(result1), okOrErrValue(result2), okOrErrValue(result3), okOrErrValue(result4), okOrErrValue(result5), okOrErrValue(result6)]));
|
|
153
153
|
}
|
|
154
154
|
});
|
|
155
155
|
}
|
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
|
|
3
3
|
import { annotate } from '../annotate';
|
|
4
4
|
import { compose, predicate } from './composition';
|
|
5
|
-
import { err,
|
|
5
|
+
import { err, ok, okOrErrValue, unwrap } from '../result';
|
|
6
6
|
import { poja } from './array';
|
|
7
7
|
import type { Decoder } from '../_types';
|
|
8
8
|
|
|
@@ -26,7 +26,7 @@ export function tuple1<T>(decoder1: Decoder<T>): Decoder<[T]> {
|
|
|
26
26
|
} catch (e) {
|
|
27
27
|
// If a decoder error has happened while unwrapping all the
|
|
28
28
|
// results, try to construct a good error message
|
|
29
|
-
return err(annotate(
|
|
29
|
+
return err(annotate([okOrErrValue(result1)]));
|
|
30
30
|
}
|
|
31
31
|
});
|
|
32
32
|
}
|
|
@@ -49,12 +49,7 @@ export function tuple2<T1, T2>(
|
|
|
49
49
|
} catch (e) {
|
|
50
50
|
// If a decoder error has happened while unwrapping all the
|
|
51
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
|
-
);
|
|
52
|
+
return err(annotate([okOrErrValue(result1), okOrErrValue(result2)]));
|
|
58
53
|
}
|
|
59
54
|
});
|
|
60
55
|
}
|
|
@@ -81,9 +76,9 @@ export function tuple3<T1, T2, T3>(
|
|
|
81
76
|
// results, try to construct a good error message
|
|
82
77
|
return err(
|
|
83
78
|
annotate([
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
79
|
+
okOrErrValue(result1),
|
|
80
|
+
okOrErrValue(result2),
|
|
81
|
+
okOrErrValue(result3),
|
|
87
82
|
]),
|
|
88
83
|
);
|
|
89
84
|
}
|
|
@@ -119,10 +114,10 @@ export function tuple4<T1, T2, T3, T4>(
|
|
|
119
114
|
// results, try to construct a good error message
|
|
120
115
|
return err(
|
|
121
116
|
annotate([
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
117
|
+
okOrErrValue(result1),
|
|
118
|
+
okOrErrValue(result2),
|
|
119
|
+
okOrErrValue(result3),
|
|
120
|
+
okOrErrValue(result4),
|
|
126
121
|
]),
|
|
127
122
|
);
|
|
128
123
|
}
|
|
@@ -161,11 +156,11 @@ export function tuple5<T1, T2, T3, T4, T5>(
|
|
|
161
156
|
// results, try to construct a good error message
|
|
162
157
|
return err(
|
|
163
158
|
annotate([
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
159
|
+
okOrErrValue(result1),
|
|
160
|
+
okOrErrValue(result2),
|
|
161
|
+
okOrErrValue(result3),
|
|
162
|
+
okOrErrValue(result4),
|
|
163
|
+
okOrErrValue(result5),
|
|
169
164
|
]),
|
|
170
165
|
);
|
|
171
166
|
}
|
|
@@ -207,12 +202,12 @@ export function tuple6<T1, T2, T3, T4, T5, T6>(
|
|
|
207
202
|
// results, try to construct a good error message
|
|
208
203
|
return err(
|
|
209
204
|
annotate([
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
205
|
+
okOrErrValue(result1),
|
|
206
|
+
okOrErrValue(result2),
|
|
207
|
+
okOrErrValue(result3),
|
|
208
|
+
okOrErrValue(result4),
|
|
209
|
+
okOrErrValue(result5),
|
|
210
|
+
okOrErrValue(result6),
|
|
216
211
|
]),
|
|
217
212
|
);
|
|
218
213
|
}
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
package/{es → _esm}/index.js
RENAMED
|
@@ -17,21 +17,21 @@
|
|
|
17
17
|
*
|
|
18
18
|
*/
|
|
19
19
|
export { guard } from './_guard';
|
|
20
|
-
export { compose, map, predicate } from './
|
|
21
|
-
export { array, nonEmptyArray, poja } from './
|
|
22
|
-
export { boolean, numericBoolean, truthy } from './
|
|
23
|
-
export { constant, hardcoded, mixed, null_, undefined_, unknown } from './
|
|
24
|
-
export { date, iso8601 } from './
|
|
25
|
-
export { describe } from './
|
|
26
|
-
export { dispatch } from './
|
|
27
|
-
export { either, either3, either4, either5, either6, either7, either8, either9, oneOf } from './
|
|
28
|
-
export { fail } from './
|
|
29
|
-
export { instanceOf } from './
|
|
30
|
-
export { json, jsonObject, jsonArray } from './
|
|
31
|
-
export { lazy } from './
|
|
32
|
-
export { mapping, dict } from './
|
|
33
|
-
export { integer, number, positiveInteger, positiveNumber } from './
|
|
34
|
-
export { exact, inexact, object, pojo } from './
|
|
35
|
-
export { maybe, nullable, optional } from './
|
|
36
|
-
export { email, nonEmptyString, regex, string, url } from './
|
|
37
|
-
export { tuple1, tuple2, tuple3, tuple4, tuple5, tuple6 } from './
|
|
20
|
+
export { compose, map, predicate } from './core/composition';
|
|
21
|
+
export { array, nonEmptyArray, poja } from './core/array';
|
|
22
|
+
export { boolean, numericBoolean, truthy } from './core/boolean';
|
|
23
|
+
export { constant, hardcoded, mixed, null_, undefined_, unknown } from './core/constants';
|
|
24
|
+
export { date, iso8601 } from './core/date';
|
|
25
|
+
export { describe } from './core/describe';
|
|
26
|
+
export { dispatch } from './core/dispatch';
|
|
27
|
+
export { either, either3, either4, either5, either6, either7, either8, either9, oneOf } from './core/either';
|
|
28
|
+
export { fail } from './core/fail';
|
|
29
|
+
export { instanceOf } from './core/instanceOf';
|
|
30
|
+
export { json, jsonObject, jsonArray } from './core/json';
|
|
31
|
+
export { lazy } from './core/lazy';
|
|
32
|
+
export { mapping, dict } from './core/mapping';
|
|
33
|
+
export { integer, number, positiveInteger, positiveNumber } from './core/number';
|
|
34
|
+
export { exact, inexact, object, pojo } from './core/object';
|
|
35
|
+
export { maybe, nullable, optional } from './core/optional';
|
|
36
|
+
export { email, nonEmptyString, regex, string, url } from './core/string';
|
|
37
|
+
export { tuple1, tuple2, tuple3, tuple4, tuple5, tuple6 } from './core/tuple';
|
|
@@ -18,28 +18,28 @@
|
|
|
18
18
|
* type. In our case, it's fine to fail with a runtime error.
|
|
19
19
|
*
|
|
20
20
|
*/
|
|
21
|
-
export type {
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
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';
|
|
31
|
+
export type { JSONValue, JSONObject, JSONArray } from './core/json';
|
|
25
32
|
|
|
26
33
|
export { guard } from './_guard';
|
|
27
34
|
|
|
28
|
-
export { compose, map, predicate } from './
|
|
35
|
+
export { compose, map, predicate } from './core/composition';
|
|
29
36
|
|
|
30
|
-
export { array, nonEmptyArray, poja } from './
|
|
31
|
-
export { boolean, numericBoolean, truthy } from './
|
|
32
|
-
export {
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
null_,
|
|
37
|
-
undefined_,
|
|
38
|
-
unknown,
|
|
39
|
-
} from './stdlib/constants';
|
|
40
|
-
export { date, iso8601 } from './stdlib/date';
|
|
41
|
-
export { describe } from './stdlib/describe';
|
|
42
|
-
export { dispatch } from './stdlib/dispatch';
|
|
37
|
+
export { array, nonEmptyArray, poja } from './core/array';
|
|
38
|
+
export { boolean, numericBoolean, truthy } from './core/boolean';
|
|
39
|
+
export { constant, hardcoded, mixed, null_, undefined_, unknown } from './core/constants';
|
|
40
|
+
export { date, iso8601 } from './core/date';
|
|
41
|
+
export { describe } from './core/describe';
|
|
42
|
+
export { dispatch } from './core/dispatch';
|
|
43
43
|
export {
|
|
44
44
|
either,
|
|
45
45
|
either3,
|
|
@@ -50,14 +50,14 @@ export {
|
|
|
50
50
|
either8,
|
|
51
51
|
either9,
|
|
52
52
|
oneOf,
|
|
53
|
-
} from './
|
|
54
|
-
export { fail } from './
|
|
55
|
-
export { instanceOf } from './
|
|
56
|
-
export { json, jsonObject, jsonArray } from './
|
|
57
|
-
export { lazy } from './
|
|
58
|
-
export { mapping, dict } from './
|
|
59
|
-
export { integer, number, positiveInteger, positiveNumber } from './
|
|
60
|
-
export { exact, inexact, object, pojo } from './
|
|
61
|
-
export { maybe, nullable, optional } from './
|
|
62
|
-
export { email, nonEmptyString, regex, string, url } from './
|
|
63
|
-
export { tuple1, tuple2, tuple3, tuple4, tuple5, tuple6 } from './
|
|
53
|
+
} from './core/either';
|
|
54
|
+
export { fail } from './core/fail';
|
|
55
|
+
export { instanceOf } from './core/instanceOf';
|
|
56
|
+
export { json, jsonObject, jsonArray } from './core/json';
|
|
57
|
+
export { lazy } from './core/lazy';
|
|
58
|
+
export { mapping, dict } from './core/mapping';
|
|
59
|
+
export { integer, number, positiveInteger, positiveNumber } from './core/number';
|
|
60
|
+
export { exact, inexact, object, pojo } from './core/object';
|
|
61
|
+
export { maybe, nullable, optional } from './core/optional';
|
|
62
|
+
export { email, nonEmptyString, regex, string, url } from './core/string';
|
|
63
|
+
export { tuple1, tuple2, tuple3, tuple4, tuple5, tuple6 } from './core/tuple';
|
package/{es → _esm}/result.js
RENAMED
|
@@ -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
|
}
|
|
@@ -34,13 +36,20 @@ export function isErr(result) {
|
|
|
34
36
|
}
|
|
35
37
|
export function withDefault(result, defaultValue) {
|
|
36
38
|
return result.type === 'ok' ? result.value : defaultValue;
|
|
37
|
-
}
|
|
38
|
-
|
|
39
|
+
} // TODO: Remove this from the public API? The same can be achieved now with
|
|
40
|
+
// TODO: const { value } = result;
|
|
41
|
+
|
|
42
|
+
export function okValue(result) {
|
|
39
43
|
return result.type === 'ok' ? result.value : undefined;
|
|
40
|
-
}
|
|
44
|
+
} // TODO: Remove this from the public API? The same can be achieved now with
|
|
45
|
+
// TODO: const { error } = result;
|
|
46
|
+
|
|
41
47
|
export function errValue(result) {
|
|
42
48
|
return result.type === 'err' ? result.error : undefined;
|
|
43
49
|
}
|
|
50
|
+
export function okOrErrValue(result) {
|
|
51
|
+
return result.type === 'ok' ? result.value : result.error;
|
|
52
|
+
}
|
|
44
53
|
/**
|
|
45
54
|
* Unwrap the value from this Result instance if this is an "Ok" result.
|
|
46
55
|
* Otherwise, will throw the "Err" error via a runtime exception.
|
|
@@ -127,7 +136,7 @@ export function orElse(result1, lazyResult2) {
|
|
|
127
136
|
* Transform an Ok result. Will not touch Err results.
|
|
128
137
|
*/
|
|
129
138
|
|
|
130
|
-
export function
|
|
139
|
+
export function mapOk(result, mapper) {
|
|
131
140
|
return result.type === 'ok' ? ok(mapper(result.value)) : result;
|
|
132
141
|
}
|
|
133
142
|
/**
|
|
@@ -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,22 @@ 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
|
-
|
|
46
|
+
// TODO: Remove this from the public API? The same can be achieved now with
|
|
47
|
+
// TODO: const { value } = result;
|
|
48
|
+
export function okValue<T>(result: Result<T, mixed>): void | T {
|
|
47
49
|
return result.type === 'ok' ? result.value : undefined;
|
|
48
50
|
}
|
|
49
51
|
|
|
52
|
+
// TODO: Remove this from the public API? The same can be achieved now with
|
|
53
|
+
// TODO: const { error } = result;
|
|
50
54
|
export function errValue<E>(result: Result<mixed, E>): void | E {
|
|
51
55
|
return result.type === 'err' ? result.error : undefined;
|
|
52
56
|
}
|
|
53
57
|
|
|
58
|
+
export function okOrErrValue<T, E>(result: Result<T, E>): T | E {
|
|
59
|
+
return result.type === 'ok' ? result.value : result.error;
|
|
60
|
+
}
|
|
61
|
+
|
|
54
62
|
/**
|
|
55
63
|
* Unwrap the value from this Result instance if this is an "Ok" result.
|
|
56
64
|
* Otherwise, will throw the "Err" error via a runtime exception.
|
|
@@ -148,7 +156,7 @@ export function orElse<T, E, E2>(
|
|
|
148
156
|
/**
|
|
149
157
|
* Transform an Ok result. Will not touch Err results.
|
|
150
158
|
*/
|
|
151
|
-
export function
|
|
159
|
+
export function mapOk<T, E, T2>(
|
|
152
160
|
result: Result<T, E>,
|
|
153
161
|
mapper: (value: T) => T2,
|
|
154
162
|
): Result<T2, E> {
|
|
File without changes
|
package/_guard.js.flow
ADDED
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
// @flow strict
|
|
2
|
+
|
|
3
|
+
import * as Result from './result';
|
|
4
|
+
import { formatInline } from './format';
|
|
5
|
+
import type { Annotation } from './annotate';
|
|
6
|
+
import type { Decoder, Guard } from './_types';
|
|
7
|
+
|
|
8
|
+
export function guard<T>(
|
|
9
|
+
decoder: Decoder<T>,
|
|
10
|
+
formatter: (Annotation) => string = formatInline,
|
|
11
|
+
): Guard<T> {
|
|
12
|
+
return (blob: mixed) =>
|
|
13
|
+
Result.unwrap(
|
|
14
|
+
Result.mapError(decoder(blob), (annotation) => {
|
|
15
|
+
const err = new Error('\n' + formatter(annotation));
|
|
16
|
+
err.name = 'Decoding error';
|
|
17
|
+
return err;
|
|
18
|
+
}),
|
|
19
|
+
);
|
|
20
|
+
}
|
|
File without changes
|
package/_types.js.flow
ADDED
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
// @flow strict
|
|
2
|
+
|
|
3
|
+
import type { Annotation } from './annotate';
|
|
4
|
+
import type { Result } from './result';
|
|
5
|
+
|
|
6
|
+
export type Scalar = string | number | boolean | symbol | void | null;
|
|
7
|
+
|
|
8
|
+
export type Predicate<T> = (T) => boolean;
|
|
9
|
+
export type DecodeResult<T> = Result<T, Annotation>;
|
|
10
|
+
|
|
11
|
+
export type Decoder<T, F = mixed> = (F) => DecodeResult<T>;
|
|
12
|
+
export type Guard<T> = (mixed) => T;
|
|
13
|
+
|
|
14
|
+
/**
|
|
15
|
+
* A "type function" which informs Flow about how a type will be modified at runtime.
|
|
16
|
+
* Read this as "given a Guard of type T, I can produce a value of type T". This
|
|
17
|
+
* definition helps construct $ObjMap types.
|
|
18
|
+
*/
|
|
19
|
+
export type DecoderType = <T>(Decoder<T>) => T;
|
|
20
|
+
export type GuardType = <T>(Guard<T>) => T;
|
|
File without changes
|