decoders 2.0.0-beta5 → 2.0.0-beta9
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 +19 -0
- package/NotSupportedTSVersion.d.ts +1 -0
- package/README.md +934 -387
- 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/_utils.js +1 -1
- package/_utils.js.flow +3 -3
- package/{_esm/_utils.js → _utils.mjs} +1 -1
- 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 +8 -0
- package/core/array.js +19 -12
- package/core/array.js.flow +15 -11
- package/{_esm/core/array.js → core/array.mjs} +19 -10
- package/core/boolean.d.ts +5 -0
- package/core/boolean.js +5 -9
- package/core/boolean.js.flow +5 -7
- package/{_esm/core/boolean.js → core/boolean.mjs} +7 -7
- package/core/composition.d.ts +18 -0
- package/core/composition.js +41 -15
- package/core/composition.js.flow +41 -10
- package/core/composition.mjs +70 -0
- 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 +5 -9
- package/core/date.js.flow +4 -6
- package/{_esm/core/date.js → core/date.mjs} +7 -7
- 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 +11 -13
- package/core/dispatch.js.flow +13 -12
- package/{_esm/core/dispatch.js → core/dispatch.mjs} +12 -11
- package/core/either.d.ts +66 -0
- package/core/either.js +34 -50
- package/core/either.js.flow +40 -86
- package/core/either.mjs +90 -0
- 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.js +3 -3
- package/core/json.js.flow +3 -3
- 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/number.d.ts +6 -0
- package/core/number.js +9 -13
- package/core/number.js.flow +18 -12
- package/core/number.mjs +25 -0
- package/core/object.d.ts +38 -0
- package/core/object.js +66 -13
- package/core/object.js.flow +84 -28
- package/{_esm/core/object.js → core/object.mjs} +64 -11
- 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 +13 -0
- package/core/string.js +31 -49
- package/core/string.js.flow +29 -39
- package/core/string.mjs +58 -0
- package/core/tuple.d.ts +30 -0
- package/core/tuple.js +30 -149
- package/core/tuple.js.flow +33 -197
- package/core/tuple.mjs +45 -0
- 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/index.d.ts +42 -0
- package/index.js +33 -42
- package/index.js.flow +17 -18
- package/{_esm/index.js → index.mjs} +18 -19
- package/package.json +15 -3
- package/result.d.ts +39 -0
- package/result.js +9 -90
- package/result.js.flow +11 -87
- package/result.mjs +81 -0
- 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 +0 -42
- 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 +0 -90
- 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 +0 -54
- package/_esm/core/mapping.js.flow +0 -54
- package/_esm/core/number.js +0 -25
- 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 +0 -76
- package/_esm/core/string.js.flow +0 -82
- package/_esm/core/tuple.js +0 -155
- package/_esm/core/tuple.js.flow +0 -215
- 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/index.js.flow +0 -63
- package/_esm/result.js +0 -148
- package/_esm/result.js.flow +0 -174
- package/core/mapping.js +0 -67
- package/core/mapping.js.flow +0 -54
- 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,215 +0,0 @@
|
|
|
1
|
-
// @flow strict
|
|
2
|
-
|
|
3
|
-
import { annotate } from '../annotate';
|
|
4
|
-
import { compose, predicate } from './composition';
|
|
5
|
-
import { err, ok, okOrErrValue, unwrap } 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([okOrErrValue(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(annotate([okOrErrValue(result1), okOrErrValue(result2)]));
|
|
53
|
-
}
|
|
54
|
-
});
|
|
55
|
-
}
|
|
56
|
-
|
|
57
|
-
/**
|
|
58
|
-
* Builds a Decoder that returns Ok for 3-tuples of [T1, T2, T3], given
|
|
59
|
-
* Decoders for T1, T2, and T3. Err otherwise.
|
|
60
|
-
*/
|
|
61
|
-
export function tuple3<T1, T2, T3>(
|
|
62
|
-
decoder1: Decoder<T1>,
|
|
63
|
-
decoder2: Decoder<T2>,
|
|
64
|
-
decoder3: Decoder<T3>,
|
|
65
|
-
): Decoder<[T1, T2, T3]> {
|
|
66
|
-
return compose(ntuple(3), (blobs: $ReadOnlyArray<mixed>) => {
|
|
67
|
-
const [blob1, blob2, blob3] = blobs;
|
|
68
|
-
|
|
69
|
-
const result1 = decoder1(blob1);
|
|
70
|
-
const result2 = decoder2(blob2);
|
|
71
|
-
const result3 = decoder3(blob3);
|
|
72
|
-
try {
|
|
73
|
-
return ok([unwrap(result1), unwrap(result2), unwrap(result3)]);
|
|
74
|
-
} catch (e) {
|
|
75
|
-
// If a decoder error has happened while unwrapping all the
|
|
76
|
-
// results, try to construct a good error message
|
|
77
|
-
return err(
|
|
78
|
-
annotate([
|
|
79
|
-
okOrErrValue(result1),
|
|
80
|
-
okOrErrValue(result2),
|
|
81
|
-
okOrErrValue(result3),
|
|
82
|
-
]),
|
|
83
|
-
);
|
|
84
|
-
}
|
|
85
|
-
});
|
|
86
|
-
}
|
|
87
|
-
|
|
88
|
-
/**
|
|
89
|
-
* Builds a Decoder that returns Ok for 4-tuples of [T1, T2, T3, T4], given
|
|
90
|
-
* Decoders for T1, T2, T3, and T4. Err otherwise.
|
|
91
|
-
*/
|
|
92
|
-
export function tuple4<T1, T2, T3, T4>(
|
|
93
|
-
decoder1: Decoder<T1>,
|
|
94
|
-
decoder2: Decoder<T2>,
|
|
95
|
-
decoder3: Decoder<T3>,
|
|
96
|
-
decoder4: Decoder<T4>,
|
|
97
|
-
): Decoder<[T1, T2, T3, T4]> {
|
|
98
|
-
return compose(ntuple(4), (blobs: $ReadOnlyArray<mixed>) => {
|
|
99
|
-
const [blob1, blob2, blob3, blob4] = blobs;
|
|
100
|
-
|
|
101
|
-
const result1 = decoder1(blob1);
|
|
102
|
-
const result2 = decoder2(blob2);
|
|
103
|
-
const result3 = decoder3(blob3);
|
|
104
|
-
const result4 = decoder4(blob4);
|
|
105
|
-
try {
|
|
106
|
-
return ok([
|
|
107
|
-
unwrap(result1),
|
|
108
|
-
unwrap(result2),
|
|
109
|
-
unwrap(result3),
|
|
110
|
-
unwrap(result4),
|
|
111
|
-
]);
|
|
112
|
-
} catch (e) {
|
|
113
|
-
// If a decoder error has happened while unwrapping all the
|
|
114
|
-
// results, try to construct a good error message
|
|
115
|
-
return err(
|
|
116
|
-
annotate([
|
|
117
|
-
okOrErrValue(result1),
|
|
118
|
-
okOrErrValue(result2),
|
|
119
|
-
okOrErrValue(result3),
|
|
120
|
-
okOrErrValue(result4),
|
|
121
|
-
]),
|
|
122
|
-
);
|
|
123
|
-
}
|
|
124
|
-
});
|
|
125
|
-
}
|
|
126
|
-
|
|
127
|
-
/**
|
|
128
|
-
* Builds a Decoder that returns Ok for 5-tuples of [T1, T2, T3, T4, T5], given
|
|
129
|
-
* Decoders for T1, T2, T3, T4, and T5. Err otherwise.
|
|
130
|
-
*/
|
|
131
|
-
export function tuple5<T1, T2, T3, T4, T5>(
|
|
132
|
-
decoder1: Decoder<T1>,
|
|
133
|
-
decoder2: Decoder<T2>,
|
|
134
|
-
decoder3: Decoder<T3>,
|
|
135
|
-
decoder4: Decoder<T4>,
|
|
136
|
-
decoder5: Decoder<T5>,
|
|
137
|
-
): Decoder<[T1, T2, T3, T4, T5]> {
|
|
138
|
-
return compose(ntuple(5), (blobs: $ReadOnlyArray<mixed>) => {
|
|
139
|
-
const [blob1, blob2, blob3, blob4, blob5] = blobs;
|
|
140
|
-
|
|
141
|
-
const result1 = decoder1(blob1);
|
|
142
|
-
const result2 = decoder2(blob2);
|
|
143
|
-
const result3 = decoder3(blob3);
|
|
144
|
-
const result4 = decoder4(blob4);
|
|
145
|
-
const result5 = decoder5(blob5);
|
|
146
|
-
try {
|
|
147
|
-
return ok([
|
|
148
|
-
unwrap(result1),
|
|
149
|
-
unwrap(result2),
|
|
150
|
-
unwrap(result3),
|
|
151
|
-
unwrap(result4),
|
|
152
|
-
unwrap(result5),
|
|
153
|
-
]);
|
|
154
|
-
} catch (e) {
|
|
155
|
-
// If a decoder error has happened while unwrapping all the
|
|
156
|
-
// results, try to construct a good error message
|
|
157
|
-
return err(
|
|
158
|
-
annotate([
|
|
159
|
-
okOrErrValue(result1),
|
|
160
|
-
okOrErrValue(result2),
|
|
161
|
-
okOrErrValue(result3),
|
|
162
|
-
okOrErrValue(result4),
|
|
163
|
-
okOrErrValue(result5),
|
|
164
|
-
]),
|
|
165
|
-
);
|
|
166
|
-
}
|
|
167
|
-
});
|
|
168
|
-
}
|
|
169
|
-
|
|
170
|
-
/**
|
|
171
|
-
* Builds a Decoder that returns Ok for 5-tuples of [T1, T2, T3, T4, T5], given
|
|
172
|
-
* Decoders for T1, T2, T3, T4, T5, and T6. Err otherwise.
|
|
173
|
-
*/
|
|
174
|
-
export function tuple6<T1, T2, T3, T4, T5, T6>(
|
|
175
|
-
decoder1: Decoder<T1>,
|
|
176
|
-
decoder2: Decoder<T2>,
|
|
177
|
-
decoder3: Decoder<T3>,
|
|
178
|
-
decoder4: Decoder<T4>,
|
|
179
|
-
decoder5: Decoder<T5>,
|
|
180
|
-
decoder6: Decoder<T6>,
|
|
181
|
-
): Decoder<[T1, T2, T3, T4, T5, T6]> {
|
|
182
|
-
return compose(ntuple(6), (blobs: $ReadOnlyArray<mixed>) => {
|
|
183
|
-
const [blob1, blob2, blob3, blob4, blob5, blob6] = blobs;
|
|
184
|
-
|
|
185
|
-
const result1 = decoder1(blob1);
|
|
186
|
-
const result2 = decoder2(blob2);
|
|
187
|
-
const result3 = decoder3(blob3);
|
|
188
|
-
const result4 = decoder4(blob4);
|
|
189
|
-
const result5 = decoder5(blob5);
|
|
190
|
-
const result6 = decoder6(blob6);
|
|
191
|
-
try {
|
|
192
|
-
return ok([
|
|
193
|
-
unwrap(result1),
|
|
194
|
-
unwrap(result2),
|
|
195
|
-
unwrap(result3),
|
|
196
|
-
unwrap(result4),
|
|
197
|
-
unwrap(result5),
|
|
198
|
-
unwrap(result6),
|
|
199
|
-
]);
|
|
200
|
-
} catch (e) {
|
|
201
|
-
// If a decoder error has happened while unwrapping all the
|
|
202
|
-
// results, try to construct a good error message
|
|
203
|
-
return err(
|
|
204
|
-
annotate([
|
|
205
|
-
okOrErrValue(result1),
|
|
206
|
-
okOrErrValue(result2),
|
|
207
|
-
okOrErrValue(result3),
|
|
208
|
-
okOrErrValue(result4),
|
|
209
|
-
okOrErrValue(result5),
|
|
210
|
-
okOrErrValue(result6),
|
|
211
|
-
]),
|
|
212
|
-
);
|
|
213
|
-
}
|
|
214
|
-
});
|
|
215
|
-
}
|
package/_esm/format/index.js
DELETED
package/_esm/format/short.js
DELETED
package/_esm/index.js.flow
DELETED
|
@@ -1,63 +0,0 @@
|
|
|
1
|
-
// @flow strict
|
|
2
|
-
|
|
3
|
-
/**
|
|
4
|
-
* Elm-like JSON decoders, for use with Flow.
|
|
5
|
-
* See http://elmplayground.com/decoding-json-in-elm-1 for an introduction.
|
|
6
|
-
*
|
|
7
|
-
* Why? All JSON responses coming from our API endpoints are just that: free-form
|
|
8
|
-
* JSON data. To Flow, the only type classification possilbe is "any" -- effectively
|
|
9
|
-
* turning off all type checks for anything related to JSON. To the receiving end
|
|
10
|
-
* (our frontend), the structure of that data is completely opaque to any type
|
|
11
|
-
* checkers since JSON values can be anything: an object, an array, null, a string,
|
|
12
|
-
* a bool, etc. Our type system is not a runtime type system, so we need a way of
|
|
13
|
-
* "converting" an any-type JSON value into a type that we want to work with in our
|
|
14
|
-
* frontend code base.
|
|
15
|
-
*
|
|
16
|
-
* Elm's solution to this problem is to define composable decoders: functions that
|
|
17
|
-
* take anything and either fail with an error, or guarantee to return the expected
|
|
18
|
-
* type. In our case, it's fine to fail with a runtime error.
|
|
19
|
-
*
|
|
20
|
-
*/
|
|
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';
|
|
32
|
-
|
|
33
|
-
export { guard } from './_guard';
|
|
34
|
-
|
|
35
|
-
export { compose, map, predicate } from './core/composition';
|
|
36
|
-
|
|
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
|
-
export {
|
|
44
|
-
either,
|
|
45
|
-
either3,
|
|
46
|
-
either4,
|
|
47
|
-
either5,
|
|
48
|
-
either6,
|
|
49
|
-
either7,
|
|
50
|
-
either8,
|
|
51
|
-
either9,
|
|
52
|
-
oneOf,
|
|
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/_esm/result.js
DELETED
|
@@ -1,148 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Result <value> <error>
|
|
3
|
-
* = Ok <value>
|
|
4
|
-
* | Err <error>
|
|
5
|
-
*/
|
|
6
|
-
|
|
7
|
-
/**
|
|
8
|
-
* Create a new Result instance representing a successful computation.
|
|
9
|
-
*/
|
|
10
|
-
export function ok(value) {
|
|
11
|
-
return {
|
|
12
|
-
type: 'ok',
|
|
13
|
-
value: value,
|
|
14
|
-
error: undefined
|
|
15
|
-
};
|
|
16
|
-
}
|
|
17
|
-
/**
|
|
18
|
-
* Create a new Result instance representing a failed computation.
|
|
19
|
-
*/
|
|
20
|
-
|
|
21
|
-
export function err(error) {
|
|
22
|
-
return {
|
|
23
|
-
type: 'err',
|
|
24
|
-
value: undefined,
|
|
25
|
-
error: error
|
|
26
|
-
};
|
|
27
|
-
}
|
|
28
|
-
export function toString(result) {
|
|
29
|
-
return result.type === 'ok' ? "Ok(" + String(result.value) + ")" : "Err(" + String(result.error) + ")";
|
|
30
|
-
}
|
|
31
|
-
export function isOk(result) {
|
|
32
|
-
return result.type === 'ok';
|
|
33
|
-
}
|
|
34
|
-
export function isErr(result) {
|
|
35
|
-
return result.type === 'err';
|
|
36
|
-
}
|
|
37
|
-
export function withDefault(result, defaultValue) {
|
|
38
|
-
return result.type === 'ok' ? result.value : defaultValue;
|
|
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) {
|
|
43
|
-
return result.type === 'ok' ? result.value : undefined;
|
|
44
|
-
} // TODO: Remove this from the public API? The same can be achieved now with
|
|
45
|
-
// TODO: const { error } = result;
|
|
46
|
-
|
|
47
|
-
export function errValue(result) {
|
|
48
|
-
return result.type === 'err' ? result.error : undefined;
|
|
49
|
-
}
|
|
50
|
-
export function okOrErrValue(result) {
|
|
51
|
-
return result.type === 'ok' ? result.value : result.error;
|
|
52
|
-
}
|
|
53
|
-
/**
|
|
54
|
-
* Unwrap the value from this Result instance if this is an "Ok" result.
|
|
55
|
-
* Otherwise, will throw the "Err" error via a runtime exception.
|
|
56
|
-
*/
|
|
57
|
-
|
|
58
|
-
export function unwrap(result) {
|
|
59
|
-
if (result.type === 'ok') {
|
|
60
|
-
return result.value;
|
|
61
|
-
} else {
|
|
62
|
-
throw result.error;
|
|
63
|
-
}
|
|
64
|
-
}
|
|
65
|
-
export function expect(result, message) {
|
|
66
|
-
if (result.type === 'ok') {
|
|
67
|
-
return result.value;
|
|
68
|
-
} else {
|
|
69
|
-
throw message instanceof Error ? message : new Error(message);
|
|
70
|
-
}
|
|
71
|
-
}
|
|
72
|
-
export function dispatch(result, okCallback, errCallback) {
|
|
73
|
-
return result.type === 'ok' ? okCallback(result.value) : errCallback(result.error);
|
|
74
|
-
}
|
|
75
|
-
/**
|
|
76
|
-
* If the given result is OK, defers to the other result. Otherwise returns the
|
|
77
|
-
* error result.
|
|
78
|
-
*
|
|
79
|
-
* It's like saying A && B, but on Result.
|
|
80
|
-
*
|
|
81
|
-
* Examples:
|
|
82
|
-
*
|
|
83
|
-
* Result.ok(42) && Result.ok('hi') // => Ok('hi')
|
|
84
|
-
* Result.err('boo') && Result.ok('hi') // => Err('boo')
|
|
85
|
-
* Result.ok(42) && Result.err('boo') // => Err('boo')
|
|
86
|
-
* Result.err('boo') && Result.err('boo') // => Err('boo')
|
|
87
|
-
*
|
|
88
|
-
*/
|
|
89
|
-
// export function and<T, E, T2>(
|
|
90
|
-
// result1: Result<T, E>,
|
|
91
|
-
// result2: Result<T2, E>,
|
|
92
|
-
// ): Result<T2, E> {
|
|
93
|
-
// return result1.type === 'ok' ? result2 : result1;
|
|
94
|
-
// }
|
|
95
|
-
|
|
96
|
-
/**
|
|
97
|
-
* If the given result is OK, return that result. Otherwise, defers to the
|
|
98
|
-
* other result.
|
|
99
|
-
*
|
|
100
|
-
* It's like saying A || B, but on Result.
|
|
101
|
-
*
|
|
102
|
-
* Examples:
|
|
103
|
-
*
|
|
104
|
-
* Result.ok(42) || Result.ok('hi') // => Ok(42)
|
|
105
|
-
* Result.err('boo') || Result.ok('hi') // => Ok('hi')
|
|
106
|
-
* Result.ok(42) || Result.err('boo') // => Ok(42)
|
|
107
|
-
* Result.err('bleh') || Result.err('boo') // => Err('boo')
|
|
108
|
-
*
|
|
109
|
-
*/
|
|
110
|
-
// export function or<T, E, E2>(
|
|
111
|
-
// result1: Result<T, E>,
|
|
112
|
-
// result2: Result<T, E2>,
|
|
113
|
-
// ): Result<T, E2> {
|
|
114
|
-
// return result1.type === 'ok' ? result1 : result2;
|
|
115
|
-
// }
|
|
116
|
-
|
|
117
|
-
/**
|
|
118
|
-
* Like .and(), aka &&, but the second argument gets evaluated lazily only if
|
|
119
|
-
* the first result is an Ok result. If so, it has access to the Ok value from
|
|
120
|
-
* the first argument.
|
|
121
|
-
*/
|
|
122
|
-
|
|
123
|
-
export function andThen(result1, lazyResult2) {
|
|
124
|
-
return result1.type === 'ok' ? lazyResult2(result1.value) : result1;
|
|
125
|
-
}
|
|
126
|
-
/**
|
|
127
|
-
* Like .or(), aka ||, but the second argument gets evaluated lazily only if
|
|
128
|
-
* the first result is an Err result. If so, it has access to the Err value
|
|
129
|
-
* from the first argument.
|
|
130
|
-
*/
|
|
131
|
-
|
|
132
|
-
export function orElse(result1, lazyResult2) {
|
|
133
|
-
return result1.type === 'ok' ? result1 : lazyResult2(result1.error);
|
|
134
|
-
}
|
|
135
|
-
/**
|
|
136
|
-
* Transform an Ok result. Will not touch Err results.
|
|
137
|
-
*/
|
|
138
|
-
|
|
139
|
-
export function mapOk(result, mapper) {
|
|
140
|
-
return result.type === 'ok' ? ok(mapper(result.value)) : result;
|
|
141
|
-
}
|
|
142
|
-
/**
|
|
143
|
-
* Transform an Err value. Will not touch Ok results.
|
|
144
|
-
*/
|
|
145
|
-
|
|
146
|
-
export function mapError(result, mapper) {
|
|
147
|
-
return result.type === 'ok' ? result : err(mapper(result.error));
|
|
148
|
-
}
|
package/_esm/result.js.flow
DELETED
|
@@ -1,174 +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
|
-
// 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 {
|
|
49
|
-
return result.type === 'ok' ? result.value : undefined;
|
|
50
|
-
}
|
|
51
|
-
|
|
52
|
-
// TODO: Remove this from the public API? The same can be achieved now with
|
|
53
|
-
// TODO: const { error } = result;
|
|
54
|
-
export function errValue<E>(result: Result<mixed, E>): void | E {
|
|
55
|
-
return result.type === 'err' ? result.error : undefined;
|
|
56
|
-
}
|
|
57
|
-
|
|
58
|
-
export function okOrErrValue<T, E>(result: Result<T, E>): T | E {
|
|
59
|
-
return result.type === 'ok' ? result.value : result.error;
|
|
60
|
-
}
|
|
61
|
-
|
|
62
|
-
/**
|
|
63
|
-
* Unwrap the value from this Result instance if this is an "Ok" result.
|
|
64
|
-
* Otherwise, will throw the "Err" error via a runtime exception.
|
|
65
|
-
*/
|
|
66
|
-
export function unwrap<T>(result: Result<T, mixed>): T {
|
|
67
|
-
if (result.type === 'ok') {
|
|
68
|
-
return result.value;
|
|
69
|
-
} else {
|
|
70
|
-
throw result.error;
|
|
71
|
-
}
|
|
72
|
-
}
|
|
73
|
-
|
|
74
|
-
export function expect<T>(result: Result<T, mixed>, message: string | Error): T {
|
|
75
|
-
if (result.type === 'ok') {
|
|
76
|
-
return result.value;
|
|
77
|
-
} else {
|
|
78
|
-
throw message instanceof Error ? message : new Error(message);
|
|
79
|
-
}
|
|
80
|
-
}
|
|
81
|
-
|
|
82
|
-
export function dispatch<T, E, O>(
|
|
83
|
-
result: Result<T, E>,
|
|
84
|
-
okCallback: (value: T) => O,
|
|
85
|
-
errCallback: (error: E) => O,
|
|
86
|
-
): O {
|
|
87
|
-
return result.type === 'ok' ? okCallback(result.value) : errCallback(result.error);
|
|
88
|
-
}
|
|
89
|
-
|
|
90
|
-
/**
|
|
91
|
-
* If the given result is OK, defers to the other result. Otherwise returns the
|
|
92
|
-
* error result.
|
|
93
|
-
*
|
|
94
|
-
* It's like saying A && B, but on Result.
|
|
95
|
-
*
|
|
96
|
-
* Examples:
|
|
97
|
-
*
|
|
98
|
-
* Result.ok(42) && Result.ok('hi') // => Ok('hi')
|
|
99
|
-
* Result.err('boo') && Result.ok('hi') // => Err('boo')
|
|
100
|
-
* Result.ok(42) && Result.err('boo') // => Err('boo')
|
|
101
|
-
* Result.err('boo') && Result.err('boo') // => Err('boo')
|
|
102
|
-
*
|
|
103
|
-
*/
|
|
104
|
-
// export function and<T, E, T2>(
|
|
105
|
-
// result1: Result<T, E>,
|
|
106
|
-
// result2: Result<T2, E>,
|
|
107
|
-
// ): Result<T2, E> {
|
|
108
|
-
// return result1.type === 'ok' ? result2 : result1;
|
|
109
|
-
// }
|
|
110
|
-
|
|
111
|
-
/**
|
|
112
|
-
* If the given result is OK, return that result. Otherwise, defers to the
|
|
113
|
-
* other result.
|
|
114
|
-
*
|
|
115
|
-
* It's like saying A || B, but on Result.
|
|
116
|
-
*
|
|
117
|
-
* Examples:
|
|
118
|
-
*
|
|
119
|
-
* Result.ok(42) || Result.ok('hi') // => Ok(42)
|
|
120
|
-
* Result.err('boo') || Result.ok('hi') // => Ok('hi')
|
|
121
|
-
* Result.ok(42) || Result.err('boo') // => Ok(42)
|
|
122
|
-
* Result.err('bleh') || Result.err('boo') // => Err('boo')
|
|
123
|
-
*
|
|
124
|
-
*/
|
|
125
|
-
// export function or<T, E, E2>(
|
|
126
|
-
// result1: Result<T, E>,
|
|
127
|
-
// result2: Result<T, E2>,
|
|
128
|
-
// ): Result<T, E2> {
|
|
129
|
-
// return result1.type === 'ok' ? result1 : result2;
|
|
130
|
-
// }
|
|
131
|
-
|
|
132
|
-
/**
|
|
133
|
-
* Like .and(), aka &&, but the second argument gets evaluated lazily only if
|
|
134
|
-
* the first result is an Ok result. If so, it has access to the Ok value from
|
|
135
|
-
* the first argument.
|
|
136
|
-
*/
|
|
137
|
-
export function andThen<T, E, T2>(
|
|
138
|
-
result1: Result<T, E>,
|
|
139
|
-
lazyResult2: (value: T) => Result<T2, E>,
|
|
140
|
-
): Result<T2, E> {
|
|
141
|
-
return result1.type === 'ok' ? lazyResult2(result1.value) : result1;
|
|
142
|
-
}
|
|
143
|
-
|
|
144
|
-
/**
|
|
145
|
-
* Like .or(), aka ||, but the second argument gets evaluated lazily only if
|
|
146
|
-
* the first result is an Err result. If so, it has access to the Err value
|
|
147
|
-
* from the first argument.
|
|
148
|
-
*/
|
|
149
|
-
export function orElse<T, E, E2>(
|
|
150
|
-
result1: Result<T, E>,
|
|
151
|
-
lazyResult2: (errValue: E) => Result<T, E2>,
|
|
152
|
-
): Result<T, E2> {
|
|
153
|
-
return result1.type === 'ok' ? result1 : lazyResult2(result1.error);
|
|
154
|
-
}
|
|
155
|
-
|
|
156
|
-
/**
|
|
157
|
-
* Transform an Ok result. Will not touch Err results.
|
|
158
|
-
*/
|
|
159
|
-
export function mapOk<T, E, T2>(
|
|
160
|
-
result: Result<T, E>,
|
|
161
|
-
mapper: (value: T) => T2,
|
|
162
|
-
): Result<T2, E> {
|
|
163
|
-
return result.type === 'ok' ? ok(mapper(result.value)) : result;
|
|
164
|
-
}
|
|
165
|
-
|
|
166
|
-
/**
|
|
167
|
-
* Transform an Err value. Will not touch Ok results.
|
|
168
|
-
*/
|
|
169
|
-
export function mapError<T, E, E2>(
|
|
170
|
-
result: Result<T, E>,
|
|
171
|
-
mapper: (error: E) => E2,
|
|
172
|
-
): Result<T, E2> {
|
|
173
|
-
return result.type === 'ok' ? result : err(mapper(result.error));
|
|
174
|
-
}
|
package/core/mapping.js
DELETED
|
@@ -1,67 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
|
|
3
|
-
exports.__esModule = true;
|
|
4
|
-
exports.dict = dict;
|
|
5
|
-
exports.mapping = mapping;
|
|
6
|
-
|
|
7
|
-
var Result = _interopRequireWildcard(require("../result"));
|
|
8
|
-
|
|
9
|
-
var _annotate = require("../annotate");
|
|
10
|
-
|
|
11
|
-
var _composition = require("./composition");
|
|
12
|
-
|
|
13
|
-
var _object = require("./object");
|
|
14
|
-
|
|
15
|
-
function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function _getRequireWildcardCache(nodeInterop) { return nodeInterop ? cacheNodeInterop : cacheBabelInterop; })(nodeInterop); }
|
|
16
|
-
|
|
17
|
-
function _interopRequireWildcard(obj, nodeInterop) { if (!nodeInterop && obj && obj.__esModule) { return obj; } if (obj === null || typeof obj !== "object" && typeof obj !== "function") { return { "default": obj }; } var cache = _getRequireWildcardCache(nodeInterop); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj["default"] = obj; if (cache) { cache.set(obj, newObj); } return newObj; }
|
|
18
|
-
|
|
19
|
-
/**
|
|
20
|
-
* Given an object, will decode a Map of string keys to whatever values.
|
|
21
|
-
*
|
|
22
|
-
* For example, given a decoder for a Person, we can verify a Person lookup
|
|
23
|
-
* table structure (of type Map<string, Person>) like so:
|
|
24
|
-
*
|
|
25
|
-
* mapping(person)
|
|
26
|
-
*
|
|
27
|
-
*/
|
|
28
|
-
function mapping(decoder) {
|
|
29
|
-
return (0, _composition.compose)(_object.pojo, function (blob) {
|
|
30
|
-
var tuples = [];
|
|
31
|
-
var errors = null;
|
|
32
|
-
Object.keys(blob).forEach(function (key) {
|
|
33
|
-
var value = blob[key];
|
|
34
|
-
var result = decoder(value);
|
|
35
|
-
|
|
36
|
-
if (result.type === 'ok') {
|
|
37
|
-
if (errors === null) {
|
|
38
|
-
tuples.push([key, result.value]);
|
|
39
|
-
}
|
|
40
|
-
} else {
|
|
41
|
-
tuples.length = 0; // Clear the tuples array
|
|
42
|
-
|
|
43
|
-
if (errors === null) {
|
|
44
|
-
errors = {};
|
|
45
|
-
}
|
|
46
|
-
|
|
47
|
-
errors[key] = result.error;
|
|
48
|
-
}
|
|
49
|
-
});
|
|
50
|
-
|
|
51
|
-
if (errors !== null) {
|
|
52
|
-
return Result.err((0, _annotate.merge)((0, _annotate.annotateObject)(blob), errors));
|
|
53
|
-
} else {
|
|
54
|
-
return Result.ok(new Map(tuples));
|
|
55
|
-
}
|
|
56
|
-
});
|
|
57
|
-
}
|
|
58
|
-
/**
|
|
59
|
-
* Like mapping(), but returns an object rather than a Map instance.
|
|
60
|
-
*/
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
function dict(decoder) {
|
|
64
|
-
return (0, _composition.map)(mapping(decoder), function (m) {
|
|
65
|
-
return Object.fromEntries(m);
|
|
66
|
-
});
|
|
67
|
-
}
|