decoders 2.0.0-beta3 → 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/CHANGELOG.md +4 -0
- 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/core/tuple.js +10 -6
- package/core/tuple.js.flow +24 -30
- package/{_esm/core/tuple.js → core/tuple.mjs} +14 -10
- 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/index.js.flow +1 -1
- package/{_esm/index.js → index.mjs} +19 -19
- package/package.json +15 -3
- package/result.d.ts +39 -0
- package/result.js +5 -75
- package/result.js.flow +5 -73
- package/{_esm/result.js → result.mjs} +4 -62
- 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 -220
- 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 -166
- 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
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
import
|
|
2
|
-
import {
|
|
3
|
-
import {
|
|
1
|
+
import { annotate } from '../annotate.mjs';
|
|
2
|
+
import { compose, predicate } from './composition.mjs';
|
|
3
|
+
import { err, ok } from '../result.mjs';
|
|
4
4
|
|
|
5
5
|
/** Match groups in this regex:
|
|
6
6
|
* \1 - the scheme
|
|
@@ -17,7 +17,7 @@ var DEFAULT_SCHEMES = ['https'];
|
|
|
17
17
|
*/
|
|
18
18
|
|
|
19
19
|
export var string = function string(blob) {
|
|
20
|
-
return typeof blob === 'string' ?
|
|
20
|
+
return typeof blob === 'string' ? ok(blob) : err(annotate(blob, 'Must be string'));
|
|
21
21
|
};
|
|
22
22
|
/**
|
|
23
23
|
* Decoder that only returns Ok for non-empty string inputs. Err otherwise.
|
|
@@ -62,14 +62,14 @@ export var url = function url(schemes) {
|
|
|
62
62
|
var matches = value.match(url_re);
|
|
63
63
|
|
|
64
64
|
if (!matches) {
|
|
65
|
-
return
|
|
65
|
+
return err(annotate(value, 'Must be URL'));
|
|
66
66
|
} else {
|
|
67
67
|
var scheme = matches[1];
|
|
68
68
|
|
|
69
69
|
if (schemes.length === 0 || schemes.includes(scheme.toLowerCase())) {
|
|
70
|
-
return
|
|
70
|
+
return ok(value);
|
|
71
71
|
} else {
|
|
72
|
-
return
|
|
72
|
+
return err(annotate(value, "URL scheme must be any of: " + schemes.join(', ')));
|
|
73
73
|
}
|
|
74
74
|
}
|
|
75
75
|
});
|
package/core/tuple.d.ts
ADDED
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
import { Decoder } from '../_types';
|
|
2
|
+
|
|
3
|
+
export function tuple1<T1>(d1: Decoder<T1>): Decoder<[T1]>;
|
|
4
|
+
export function tuple2<T1, T2>(d1: Decoder<T1>, d2: Decoder<T2>): Decoder<[T1, T2]>;
|
|
5
|
+
export function tuple3<T1, T2, T3>(
|
|
6
|
+
d1: Decoder<T1>,
|
|
7
|
+
d2: Decoder<T2>,
|
|
8
|
+
d3: Decoder<T3>,
|
|
9
|
+
): Decoder<[T1, T2, T3]>;
|
|
10
|
+
export function tuple4<T1, T2, T3, T4>(
|
|
11
|
+
d1: Decoder<T1>,
|
|
12
|
+
d2: Decoder<T2>,
|
|
13
|
+
d3: Decoder<T3>,
|
|
14
|
+
d4: Decoder<T4>,
|
|
15
|
+
): Decoder<[T1, T2, T3, T4]>;
|
|
16
|
+
export function tuple5<T1, T2, T3, T4, T5>(
|
|
17
|
+
d1: Decoder<T1>,
|
|
18
|
+
d2: Decoder<T2>,
|
|
19
|
+
d3: Decoder<T3>,
|
|
20
|
+
d4: Decoder<T4>,
|
|
21
|
+
d5: Decoder<T5>,
|
|
22
|
+
): Decoder<[T1, T2, T3, T4, T5]>;
|
|
23
|
+
export function tuple6<T1, T2, T3, T4, T5, T6>(
|
|
24
|
+
d1: Decoder<T1>,
|
|
25
|
+
d2: Decoder<T2>,
|
|
26
|
+
d3: Decoder<T3>,
|
|
27
|
+
d4: Decoder<T4>,
|
|
28
|
+
d5: Decoder<T5>,
|
|
29
|
+
d6: Decoder<T6>,
|
|
30
|
+
): Decoder<[T1, T2, T3, T4, T5, T6]>;
|
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
|
}
|
|
@@ -1,7 +1,11 @@
|
|
|
1
|
-
import { annotate } from '../annotate';
|
|
2
|
-
import { compose, predicate } from './composition';
|
|
3
|
-
import { err,
|
|
4
|
-
import { poja } from './array';
|
|
1
|
+
import { annotate } from '../annotate.mjs';
|
|
2
|
+
import { compose, predicate } from './composition.mjs';
|
|
3
|
+
import { err, ok, unwrap } from '../result.mjs';
|
|
4
|
+
import { poja } from './array.mjs';
|
|
5
|
+
|
|
6
|
+
function okOrErr(result) {
|
|
7
|
+
return result.type === 'ok' ? result.value : result.error;
|
|
8
|
+
}
|
|
5
9
|
|
|
6
10
|
var ntuple = function ntuple(n) {
|
|
7
11
|
return compose(poja, predicate(function (arr) {
|
|
@@ -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/format.d.ts
ADDED
|
@@ -2,10 +2,11 @@
|
|
|
2
2
|
|
|
3
3
|
exports.__esModule = true;
|
|
4
4
|
exports.formatInline = formatInline;
|
|
5
|
+
exports.formatShort = formatShort;
|
|
5
6
|
exports.serializeAnnotation = serializeAnnotation;
|
|
6
7
|
exports.serializeValue = serializeValue;
|
|
7
8
|
|
|
8
|
-
var _utils = require("
|
|
9
|
+
var _utils = require("./_utils");
|
|
9
10
|
|
|
10
11
|
function serializeString(s, width) {
|
|
11
12
|
if (width === void 0) {
|
|
@@ -143,4 +144,8 @@ function formatInline(ann) {
|
|
|
143
144
|
} else {
|
|
144
145
|
return serialized;
|
|
145
146
|
}
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
function formatShort(ann) {
|
|
150
|
+
return (0, _utils.summarize)(ann, []).join('\n');
|
|
146
151
|
}
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
// @flow strict
|
|
2
2
|
|
|
3
|
-
import {
|
|
4
|
-
import type { Annotation, ArrayAnnotation, ObjectAnnotation } from '
|
|
3
|
+
import { summarize as _summarize, asDate, INDENT, indent, isMultiline } from './_utils';
|
|
4
|
+
import type { Annotation, ArrayAnnotation, ObjectAnnotation } from './annotate';
|
|
5
5
|
|
|
6
6
|
function serializeString(s: string, width: number = 80): string {
|
|
7
7
|
// Full string
|
|
@@ -120,3 +120,7 @@ export function formatInline(ann: Annotation): string {
|
|
|
120
120
|
return serialized;
|
|
121
121
|
}
|
|
122
122
|
}
|
|
123
|
+
|
|
124
|
+
export function formatShort(ann: Annotation): string {
|
|
125
|
+
return _summarize(ann, []).join('\n');
|
|
126
|
+
}
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { summarize as _summarize, asDate, INDENT, indent, isMultiline } from './_utils.mjs';
|
|
2
2
|
|
|
3
3
|
function serializeString(s, width) {
|
|
4
4
|
if (width === void 0) {
|
|
@@ -134,4 +134,7 @@ export function formatInline(ann) {
|
|
|
134
134
|
} else {
|
|
135
135
|
return serialized;
|
|
136
136
|
}
|
|
137
|
+
}
|
|
138
|
+
export function formatShort(ann) {
|
|
139
|
+
return _summarize(ann, []).join('\n');
|
|
137
140
|
}
|
|
@@ -1,24 +1,4 @@
|
|
|
1
|
-
|
|
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 {
|
|
1
|
+
export {
|
|
22
2
|
Decoder,
|
|
23
3
|
DecodeResult,
|
|
24
4
|
DecoderType,
|
|
@@ -27,8 +7,8 @@ export type {
|
|
|
27
7
|
Predicate,
|
|
28
8
|
Scalar,
|
|
29
9
|
} from './_types';
|
|
30
|
-
|
|
31
|
-
export
|
|
10
|
+
export { Result } from './result';
|
|
11
|
+
export { JSONValue, JSONObject, JSONArray } from './core/json';
|
|
32
12
|
|
|
33
13
|
export { guard } from './_guard';
|
|
34
14
|
|
package/index.js.flow
CHANGED
|
@@ -16,22 +16,22 @@
|
|
|
16
16
|
* type. In our case, it's fine to fail with a runtime error.
|
|
17
17
|
*
|
|
18
18
|
*/
|
|
19
|
-
export { guard } from './_guard';
|
|
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';
|
|
19
|
+
export { guard } from './_guard.mjs';
|
|
20
|
+
export { compose, map, predicate } from './core/composition.mjs';
|
|
21
|
+
export { array, nonEmptyArray, poja } from './core/array.mjs';
|
|
22
|
+
export { boolean, numericBoolean, truthy } from './core/boolean.mjs';
|
|
23
|
+
export { constant, hardcoded, mixed, null_, undefined_, unknown } from './core/constants.mjs';
|
|
24
|
+
export { date, iso8601 } from './core/date.mjs';
|
|
25
|
+
export { describe } from './core/describe.mjs';
|
|
26
|
+
export { dispatch } from './core/dispatch.mjs';
|
|
27
|
+
export { either, either3, either4, either5, either6, either7, either8, either9, oneOf } from './core/either.mjs';
|
|
28
|
+
export { fail } from './core/fail.mjs';
|
|
29
|
+
export { instanceOf } from './core/instanceOf.mjs';
|
|
30
|
+
export { json, jsonObject, jsonArray } from './core/json.mjs';
|
|
31
|
+
export { lazy } from './core/lazy.mjs';
|
|
32
|
+
export { mapping, dict } from './core/mapping.mjs';
|
|
33
|
+
export { integer, number, positiveInteger, positiveNumber } from './core/number.mjs';
|
|
34
|
+
export { exact, inexact, object, pojo } from './core/object.mjs';
|
|
35
|
+
export { maybe, nullable, optional } from './core/optional.mjs';
|
|
36
|
+
export { email, nonEmptyString, regex, string, url } from './core/string.mjs';
|
|
37
|
+
export { tuple1, tuple2, tuple3, tuple4, tuple5, tuple6 } from './core/tuple.mjs';
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "decoders",
|
|
3
|
-
"version": "2.0.0-
|
|
3
|
+
"version": "2.0.0-beta7",
|
|
4
4
|
"description": "Elegant and battle-tested validation library for type-safe input data (for TypeScript and Flow)",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"repository": {
|
|
@@ -13,7 +13,7 @@
|
|
|
13
13
|
"url": "https://github.com/nvie/decoders/issues"
|
|
14
14
|
},
|
|
15
15
|
"main": "./index.js",
|
|
16
|
-
"module": "./
|
|
16
|
+
"module": "./index.mjs",
|
|
17
17
|
"keywords": [
|
|
18
18
|
"decoder",
|
|
19
19
|
"decoders",
|
|
@@ -40,6 +40,18 @@
|
|
|
40
40
|
"map",
|
|
41
41
|
"predicate"
|
|
42
42
|
],
|
|
43
|
+
"*": {
|
|
44
|
+
"*": [
|
|
45
|
+
"NotSupportedTSVersion.d.ts"
|
|
46
|
+
]
|
|
47
|
+
},
|
|
43
48
|
"githubUrl": "https://github.com/nvie/decoders",
|
|
44
|
-
"sideEffects": false
|
|
49
|
+
"sideEffects": false,
|
|
50
|
+
"typesVersions": {
|
|
51
|
+
">=4.1.0": {
|
|
52
|
+
"*": [
|
|
53
|
+
"*"
|
|
54
|
+
]
|
|
55
|
+
}
|
|
56
|
+
}
|
|
45
57
|
}
|
package/result.d.ts
ADDED
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
export interface Ok<T> {
|
|
2
|
+
type: 'ok';
|
|
3
|
+
value: T;
|
|
4
|
+
error: undefined;
|
|
5
|
+
}
|
|
6
|
+
|
|
7
|
+
export interface Err<E> {
|
|
8
|
+
type: 'err';
|
|
9
|
+
value: undefined;
|
|
10
|
+
error: E;
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
export type Result<T, E> = Ok<T> | Err<E>;
|
|
14
|
+
|
|
15
|
+
export function ok<T>(value: T): Ok<T>;
|
|
16
|
+
export function err<E>(error: E): Err<E>;
|
|
17
|
+
export function unwrap<T>(result: Result<T, unknown>): T;
|
|
18
|
+
export function expect<T>(result: Result<T, unknown>, message: string | Error): T;
|
|
19
|
+
export function dispatch<T, E, O>(
|
|
20
|
+
result: Result<T, E>,
|
|
21
|
+
okCallback: (value: T) => O,
|
|
22
|
+
errCallback: (error: E) => O,
|
|
23
|
+
): O;
|
|
24
|
+
export function andThen<T, E, T2>(
|
|
25
|
+
result1: Result<T, E>,
|
|
26
|
+
lazyResult2: (value: T) => Result<T2, E>,
|
|
27
|
+
): Result<T2, E>;
|
|
28
|
+
export function orElse<T, E, E2>(
|
|
29
|
+
result1: Result<T, E>,
|
|
30
|
+
lazyResult2: (errValue: E) => Result<T, E2>,
|
|
31
|
+
): Result<T, E2>;
|
|
32
|
+
export function mapOk<T, E, T2>(
|
|
33
|
+
result: Result<T, E>,
|
|
34
|
+
mapper: (value: T) => T2,
|
|
35
|
+
): Result<T2, E>;
|
|
36
|
+
export function mapError<T, E, E2>(
|
|
37
|
+
result: Result<T, E>,
|
|
38
|
+
mapper: (error: E) => E2,
|
|
39
|
+
): Result<T, E2>;
|
package/result.js
CHANGED
|
@@ -4,18 +4,12 @@ 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
|
-
exports.isErr = isErr;
|
|
10
|
-
exports.isOk = isOk;
|
|
11
|
-
exports.map = map;
|
|
12
8
|
exports.mapError = mapError;
|
|
9
|
+
exports.mapOk = mapOk;
|
|
13
10
|
exports.ok = ok;
|
|
14
11
|
exports.orElse = orElse;
|
|
15
|
-
exports.toString = toString;
|
|
16
12
|
exports.unwrap = unwrap;
|
|
17
|
-
exports.value = value;
|
|
18
|
-
exports.withDefault = withDefault;
|
|
19
13
|
|
|
20
14
|
/**
|
|
21
15
|
* Result <value> <error>
|
|
@@ -29,7 +23,8 @@ exports.withDefault = withDefault;
|
|
|
29
23
|
function ok(value) {
|
|
30
24
|
return {
|
|
31
25
|
type: 'ok',
|
|
32
|
-
value: value
|
|
26
|
+
value: value,
|
|
27
|
+
error: undefined
|
|
33
28
|
};
|
|
34
29
|
}
|
|
35
30
|
/**
|
|
@@ -40,33 +35,10 @@ function ok(value) {
|
|
|
40
35
|
function err(error) {
|
|
41
36
|
return {
|
|
42
37
|
type: 'err',
|
|
38
|
+
value: undefined,
|
|
43
39
|
error: error
|
|
44
40
|
};
|
|
45
41
|
}
|
|
46
|
-
|
|
47
|
-
function toString(result) {
|
|
48
|
-
return result.type === 'ok' ? "Ok(" + String(result.value) + ")" : "Err(" + String(result.error) + ")";
|
|
49
|
-
}
|
|
50
|
-
|
|
51
|
-
function isOk(result) {
|
|
52
|
-
return result.type === 'ok';
|
|
53
|
-
}
|
|
54
|
-
|
|
55
|
-
function isErr(result) {
|
|
56
|
-
return result.type === 'err';
|
|
57
|
-
}
|
|
58
|
-
|
|
59
|
-
function withDefault(result, defaultValue) {
|
|
60
|
-
return result.type === 'ok' ? result.value : defaultValue;
|
|
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
42
|
/**
|
|
71
43
|
* Unwrap the value from this Result instance if this is an "Ok" result.
|
|
72
44
|
* Otherwise, will throw the "Err" error via a runtime exception.
|
|
@@ -92,48 +64,6 @@ function expect(result, message) {
|
|
|
92
64
|
function dispatch(result, okCallback, errCallback) {
|
|
93
65
|
return result.type === 'ok' ? okCallback(result.value) : errCallback(result.error);
|
|
94
66
|
}
|
|
95
|
-
/**
|
|
96
|
-
* If the given result is OK, defers to the other result. Otherwise returns the
|
|
97
|
-
* error result.
|
|
98
|
-
*
|
|
99
|
-
* It's like saying A && B, but on Result.
|
|
100
|
-
*
|
|
101
|
-
* Examples:
|
|
102
|
-
*
|
|
103
|
-
* Result.ok(42) && Result.ok('hi') // => Ok('hi')
|
|
104
|
-
* Result.err('boo') && Result.ok('hi') // => Err('boo')
|
|
105
|
-
* Result.ok(42) && Result.err('boo') // => Err('boo')
|
|
106
|
-
* Result.err('boo') && Result.err('boo') // => Err('boo')
|
|
107
|
-
*
|
|
108
|
-
*/
|
|
109
|
-
// export function and<T, E, T2>(
|
|
110
|
-
// result1: Result<T, E>,
|
|
111
|
-
// result2: Result<T2, E>,
|
|
112
|
-
// ): Result<T2, E> {
|
|
113
|
-
// return result1.type === 'ok' ? result2 : result1;
|
|
114
|
-
// }
|
|
115
|
-
|
|
116
|
-
/**
|
|
117
|
-
* If the given result is OK, return that result. Otherwise, defers to the
|
|
118
|
-
* other result.
|
|
119
|
-
*
|
|
120
|
-
* It's like saying A || B, but on Result.
|
|
121
|
-
*
|
|
122
|
-
* Examples:
|
|
123
|
-
*
|
|
124
|
-
* Result.ok(42) || Result.ok('hi') // => Ok(42)
|
|
125
|
-
* Result.err('boo') || Result.ok('hi') // => Ok('hi')
|
|
126
|
-
* Result.ok(42) || Result.err('boo') // => Ok(42)
|
|
127
|
-
* Result.err('bleh') || Result.err('boo') // => Err('boo')
|
|
128
|
-
*
|
|
129
|
-
*/
|
|
130
|
-
// export function or<T, E, E2>(
|
|
131
|
-
// result1: Result<T, E>,
|
|
132
|
-
// result2: Result<T, E2>,
|
|
133
|
-
// ): Result<T, E2> {
|
|
134
|
-
// return result1.type === 'ok' ? result1 : result2;
|
|
135
|
-
// }
|
|
136
|
-
|
|
137
67
|
/**
|
|
138
68
|
* Like .and(), aka &&, but the second argument gets evaluated lazily only if
|
|
139
69
|
* the first result is an Ok result. If so, it has access to the Ok value from
|
|
@@ -159,7 +89,7 @@ function orElse(result1, lazyResult2) {
|
|
|
159
89
|
*/
|
|
160
90
|
|
|
161
91
|
|
|
162
|
-
function
|
|
92
|
+
function mapOk(result, mapper) {
|
|
163
93
|
return result.type === 'ok' ? ok(mapper(result.value)) : result;
|
|
164
94
|
}
|
|
165
95
|
/**
|