decoders 2.0.0-beta1 → 2.0.0-beta13
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 +53 -5
- package/Decoder.d.ts +94 -0
- package/Decoder.js +222 -0
- package/Decoder.js.flow +286 -0
- package/Decoder.mjs +215 -0
- package/NotSupportedTSVersion.d.ts +1 -0
- package/README.md +124 -961
- package/_utils.d.ts +9 -0
- package/{cjs/_utils.js → _utils.js} +12 -18
- package/{cjs/_utils.js.flow → _utils.js.flow} +15 -19
- package/{es/_utils.js → _utils.mjs} +11 -15
- package/{ts/annotate.d.ts → annotate.d.ts} +25 -21
- package/{cjs/annotate.js → annotate.js} +0 -0
- package/{cjs/annotate.js.flow → annotate.js.flow} +0 -0
- package/{es/annotate.js → annotate.mjs} +0 -0
- package/format.d.ts +6 -0
- package/{cjs/format/inline.js → format.js} +7 -2
- package/{cjs/format/inline.js.flow → format.js.flow} +9 -3
- package/{es/format/inline.js → format.mjs} +5 -2
- package/index.d.ts +40 -0
- package/index.js +89 -0
- package/index.js.flow +44 -0
- package/index.mjs +11 -0
- package/{ts → lib}/_helpers.d.ts +0 -0
- package/lib/arrays.d.ts +59 -0
- package/lib/arrays.js +139 -0
- package/lib/arrays.js.flow +138 -0
- package/lib/arrays.mjs +124 -0
- package/lib/basics.d.ts +93 -0
- package/lib/basics.js +144 -0
- package/lib/basics.js.flow +124 -0
- package/lib/basics.mjs +120 -0
- package/lib/booleans.d.ts +16 -0
- package/lib/booleans.js +35 -0
- package/lib/booleans.js.flow +22 -0
- package/lib/booleans.mjs +25 -0
- package/lib/dates.d.ts +15 -0
- package/lib/dates.js +44 -0
- package/lib/dates.js.flow +40 -0
- package/lib/dates.mjs +34 -0
- package/lib/json.d.ts +35 -0
- package/lib/json.js +55 -0
- package/lib/json.js.flow +50 -0
- package/lib/json.mjs +40 -0
- package/lib/numbers.d.ts +31 -0
- package/lib/numbers.js +51 -0
- package/lib/numbers.js.flow +48 -0
- package/lib/numbers.mjs +41 -0
- package/lib/objects.d.ts +75 -0
- package/lib/objects.js +240 -0
- package/lib/objects.js.flow +246 -0
- package/lib/objects.mjs +223 -0
- package/lib/strings.d.ts +56 -0
- package/lib/strings.js +101 -0
- package/lib/strings.js.flow +90 -0
- package/lib/strings.mjs +82 -0
- package/lib/unions.d.ts +55 -0
- package/lib/unions.js +160 -0
- package/lib/unions.js.flow +155 -0
- package/lib/unions.mjs +146 -0
- package/lib/utilities.d.ts +34 -0
- package/lib/utilities.js +75 -0
- package/lib/utilities.js.flow +65 -0
- package/lib/utilities.mjs +60 -0
- package/package.json +79 -29
- package/result.d.ts +16 -0
- package/result.js +34 -0
- package/result.js.flow +26 -0
- package/result.mjs +27 -0
- package/cjs/_guard.js +0 -26
- package/cjs/_guard.js.flow +0 -20
- package/cjs/_types.js +0 -1
- package/cjs/_types.js.flow +0 -20
- package/cjs/format/index.js +0 -12
- package/cjs/format/index.js.flow +0 -4
- package/cjs/format/short.js +0 -10
- package/cjs/format/short.js.flow +0 -8
- package/cjs/index.js +0 -120
- package/cjs/index.js.flow +0 -63
- package/cjs/result.js +0 -172
- package/cjs/result.js.flow +0 -166
- package/cjs/stdlib/array.js +0 -108
- package/cjs/stdlib/array.js.flow +0 -103
- package/cjs/stdlib/boolean.js +0 -44
- package/cjs/stdlib/boolean.js.flow +0 -29
- package/cjs/stdlib/composition.js +0 -56
- package/cjs/stdlib/composition.js.flow +0 -43
- package/cjs/stdlib/constants.js +0 -69
- package/cjs/stdlib/constants.js.flow +0 -46
- package/cjs/stdlib/date.js +0 -46
- package/cjs/stdlib/date.js.flow +0 -40
- package/cjs/stdlib/describe.js +0 -26
- package/cjs/stdlib/describe.js.flow +0 -17
- package/cjs/stdlib/dispatch.js +0 -62
- package/cjs/stdlib/dispatch.js.flow +0 -58
- package/cjs/stdlib/either.js +0 -117
- package/cjs/stdlib/either.js.flow +0 -151
- package/cjs/stdlib/fail.js +0 -21
- package/cjs/stdlib/fail.js.flow +0 -12
- package/cjs/stdlib/instanceOf.js +0 -19
- package/cjs/stdlib/instanceOf.js.flow +0 -20
- package/cjs/stdlib/json.js +0 -31
- package/cjs/stdlib/json.js.flow +0 -28
- package/cjs/stdlib/lazy.js +0 -16
- package/cjs/stdlib/lazy.js.flow +0 -15
- package/cjs/stdlib/mapping.js +0 -67
- package/cjs/stdlib/mapping.js.flow +0 -54
- package/cjs/stdlib/number.js +0 -40
- package/cjs/stdlib/number.js.flow +0 -34
- package/cjs/stdlib/object.js +0 -194
- package/cjs/stdlib/object.js.flow +0 -203
- package/cjs/stdlib/optional.js +0 -54
- package/cjs/stdlib/optional.js.flow +0 -41
- package/cjs/stdlib/string.js +0 -98
- package/cjs/stdlib/string.js.flow +0 -82
- package/cjs/stdlib/tuple.js +0 -173
- package/cjs/stdlib/tuple.js.flow +0 -220
- package/es/_guard.js +0 -15
- package/es/_types.js +0 -0
- package/es/format/index.js +0 -2
- package/es/format/short.js +0 -4
- package/es/index.js +0 -37
- package/es/result.js +0 -139
- package/es/stdlib/array.js +0 -91
- package/es/stdlib/boolean.js +0 -28
- package/es/stdlib/composition.js +0 -42
- package/es/stdlib/constants.js +0 -46
- package/es/stdlib/date.js +0 -28
- package/es/stdlib/describe.js +0 -16
- package/es/stdlib/dispatch.js +0 -51
- package/es/stdlib/either.js +0 -90
- package/es/stdlib/fail.js +0 -11
- package/es/stdlib/instanceOf.js +0 -8
- package/es/stdlib/json.js +0 -15
- package/es/stdlib/lazy.js +0 -11
- package/es/stdlib/mapping.js +0 -54
- package/es/stdlib/number.js +0 -25
- package/es/stdlib/object.js +0 -175
- package/es/stdlib/optional.js +0 -38
- package/es/stdlib/string.js +0 -76
- package/es/stdlib/tuple.js +0 -155
- package/ts/_guard.d.ts +0 -7
- package/ts/_types.d.ts +0 -16
- package/ts/_utils.d.ts +0 -13
- 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/cjs/stdlib/tuple.js
DELETED
|
@@ -1,173 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
|
|
3
|
-
exports.__esModule = true;
|
|
4
|
-
exports.tuple1 = tuple1;
|
|
5
|
-
exports.tuple2 = tuple2;
|
|
6
|
-
exports.tuple3 = tuple3;
|
|
7
|
-
exports.tuple4 = tuple4;
|
|
8
|
-
exports.tuple5 = tuple5;
|
|
9
|
-
exports.tuple6 = tuple6;
|
|
10
|
-
|
|
11
|
-
var _annotate = require("../annotate");
|
|
12
|
-
|
|
13
|
-
var _composition = require("./composition");
|
|
14
|
-
|
|
15
|
-
var _result = require("../result");
|
|
16
|
-
|
|
17
|
-
var _array = require("./array");
|
|
18
|
-
|
|
19
|
-
var ntuple = function ntuple(n) {
|
|
20
|
-
return (0, _composition.compose)(_array.poja, (0, _composition.predicate)(function (arr) {
|
|
21
|
-
return arr.length === n;
|
|
22
|
-
}, "Must be a " + n + "-tuple"));
|
|
23
|
-
};
|
|
24
|
-
/**
|
|
25
|
-
* Builds a Decoder that returns Ok for 1-tuple of [T], given a Decoder for T.
|
|
26
|
-
* Err otherwise.
|
|
27
|
-
*/
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
function tuple1(decoder1) {
|
|
31
|
-
return (0, _composition.compose)(ntuple(1), function (blobs) {
|
|
32
|
-
var blob1 = blobs[0];
|
|
33
|
-
var result1 = decoder1(blob1);
|
|
34
|
-
|
|
35
|
-
try {
|
|
36
|
-
return (0, _result.ok)([(0, _result.unwrap)(result1)]);
|
|
37
|
-
} catch (e) {
|
|
38
|
-
// If a decoder error has happened while unwrapping all the
|
|
39
|
-
// results, try to construct a good error message
|
|
40
|
-
return (0, _result.err)((0, _annotate.annotate)((0, _result.errValue)(result1)));
|
|
41
|
-
}
|
|
42
|
-
});
|
|
43
|
-
}
|
|
44
|
-
/**
|
|
45
|
-
* Builds a Decoder that returns Ok for 2-tuples of [T1, T2], given Decoders
|
|
46
|
-
* for T1 and T2. Err otherwise.
|
|
47
|
-
*/
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
function tuple2(decoder1, decoder2) {
|
|
51
|
-
return (0, _composition.compose)(ntuple(2), function (blobs) {
|
|
52
|
-
var blob1 = blobs[0],
|
|
53
|
-
blob2 = blobs[1];
|
|
54
|
-
var result1 = decoder1(blob1);
|
|
55
|
-
var result2 = decoder2(blob2);
|
|
56
|
-
|
|
57
|
-
try {
|
|
58
|
-
return (0, _result.ok)([(0, _result.unwrap)(result1), (0, _result.unwrap)(result2)]);
|
|
59
|
-
} catch (e) {
|
|
60
|
-
// If a decoder error has happened while unwrapping all the
|
|
61
|
-
// results, try to construct a good error message
|
|
62
|
-
return (0, _result.err)((0, _annotate.annotate)([(0, _result.isErr)(result1) ? (0, _result.errValue)(result1) : (0, _result.value)(result1), (0, _result.isErr)(result2) ? (0, _result.errValue)(result2) : (0, _result.value)(result2)]));
|
|
63
|
-
}
|
|
64
|
-
});
|
|
65
|
-
}
|
|
66
|
-
/**
|
|
67
|
-
* Builds a Decoder that returns Ok for 3-tuples of [T1, T2, T3], given
|
|
68
|
-
* Decoders for T1, T2, and T3. Err otherwise.
|
|
69
|
-
*/
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
function tuple3(decoder1, decoder2, decoder3) {
|
|
73
|
-
return (0, _composition.compose)(ntuple(3), function (blobs) {
|
|
74
|
-
var blob1 = blobs[0],
|
|
75
|
-
blob2 = blobs[1],
|
|
76
|
-
blob3 = blobs[2];
|
|
77
|
-
var result1 = decoder1(blob1);
|
|
78
|
-
var result2 = decoder2(blob2);
|
|
79
|
-
var result3 = decoder3(blob3);
|
|
80
|
-
|
|
81
|
-
try {
|
|
82
|
-
return (0, _result.ok)([(0, _result.unwrap)(result1), (0, _result.unwrap)(result2), (0, _result.unwrap)(result3)]);
|
|
83
|
-
} catch (e) {
|
|
84
|
-
// If a decoder error has happened while unwrapping all the
|
|
85
|
-
// results, try to construct a good error message
|
|
86
|
-
return (0, _result.err)((0, _annotate.annotate)([(0, _result.isErr)(result1) ? (0, _result.errValue)(result1) : (0, _result.value)(result1), (0, _result.isErr)(result2) ? (0, _result.errValue)(result2) : (0, _result.value)(result2), (0, _result.isErr)(result3) ? (0, _result.errValue)(result3) : (0, _result.value)(result3)]));
|
|
87
|
-
}
|
|
88
|
-
});
|
|
89
|
-
}
|
|
90
|
-
/**
|
|
91
|
-
* Builds a Decoder that returns Ok for 4-tuples of [T1, T2, T3, T4], given
|
|
92
|
-
* Decoders for T1, T2, T3, and T4. Err otherwise.
|
|
93
|
-
*/
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
function tuple4(decoder1, decoder2, decoder3, decoder4) {
|
|
97
|
-
return (0, _composition.compose)(ntuple(4), function (blobs) {
|
|
98
|
-
var blob1 = blobs[0],
|
|
99
|
-
blob2 = blobs[1],
|
|
100
|
-
blob3 = blobs[2],
|
|
101
|
-
blob4 = blobs[3];
|
|
102
|
-
var result1 = decoder1(blob1);
|
|
103
|
-
var result2 = decoder2(blob2);
|
|
104
|
-
var result3 = decoder3(blob3);
|
|
105
|
-
var result4 = decoder4(blob4);
|
|
106
|
-
|
|
107
|
-
try {
|
|
108
|
-
return (0, _result.ok)([(0, _result.unwrap)(result1), (0, _result.unwrap)(result2), (0, _result.unwrap)(result3), (0, _result.unwrap)(result4)]);
|
|
109
|
-
} catch (e) {
|
|
110
|
-
// If a decoder error has happened while unwrapping all the
|
|
111
|
-
// results, try to construct a good error message
|
|
112
|
-
return (0, _result.err)((0, _annotate.annotate)([(0, _result.isErr)(result1) ? (0, _result.errValue)(result1) : (0, _result.value)(result1), (0, _result.isErr)(result2) ? (0, _result.errValue)(result2) : (0, _result.value)(result2), (0, _result.isErr)(result3) ? (0, _result.errValue)(result3) : (0, _result.value)(result3), (0, _result.isErr)(result4) ? (0, _result.errValue)(result4) : (0, _result.value)(result4)]));
|
|
113
|
-
}
|
|
114
|
-
});
|
|
115
|
-
}
|
|
116
|
-
/**
|
|
117
|
-
* Builds a Decoder that returns Ok for 5-tuples of [T1, T2, T3, T4, T5], given
|
|
118
|
-
* Decoders for T1, T2, T3, T4, and T5. Err otherwise.
|
|
119
|
-
*/
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
function tuple5(decoder1, decoder2, decoder3, decoder4, decoder5) {
|
|
123
|
-
return (0, _composition.compose)(ntuple(5), function (blobs) {
|
|
124
|
-
var blob1 = blobs[0],
|
|
125
|
-
blob2 = blobs[1],
|
|
126
|
-
blob3 = blobs[2],
|
|
127
|
-
blob4 = blobs[3],
|
|
128
|
-
blob5 = blobs[4];
|
|
129
|
-
var result1 = decoder1(blob1);
|
|
130
|
-
var result2 = decoder2(blob2);
|
|
131
|
-
var result3 = decoder3(blob3);
|
|
132
|
-
var result4 = decoder4(blob4);
|
|
133
|
-
var result5 = decoder5(blob5);
|
|
134
|
-
|
|
135
|
-
try {
|
|
136
|
-
return (0, _result.ok)([(0, _result.unwrap)(result1), (0, _result.unwrap)(result2), (0, _result.unwrap)(result3), (0, _result.unwrap)(result4), (0, _result.unwrap)(result5)]);
|
|
137
|
-
} catch (e) {
|
|
138
|
-
// If a decoder error has happened while unwrapping all the
|
|
139
|
-
// results, try to construct a good error message
|
|
140
|
-
return (0, _result.err)((0, _annotate.annotate)([(0, _result.isErr)(result1) ? (0, _result.errValue)(result1) : (0, _result.value)(result1), (0, _result.isErr)(result2) ? (0, _result.errValue)(result2) : (0, _result.value)(result2), (0, _result.isErr)(result3) ? (0, _result.errValue)(result3) : (0, _result.value)(result3), (0, _result.isErr)(result4) ? (0, _result.errValue)(result4) : (0, _result.value)(result4), (0, _result.isErr)(result5) ? (0, _result.errValue)(result5) : (0, _result.value)(result5)]));
|
|
141
|
-
}
|
|
142
|
-
});
|
|
143
|
-
}
|
|
144
|
-
/**
|
|
145
|
-
* Builds a Decoder that returns Ok for 5-tuples of [T1, T2, T3, T4, T5], given
|
|
146
|
-
* Decoders for T1, T2, T3, T4, T5, and T6. Err otherwise.
|
|
147
|
-
*/
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
function tuple6(decoder1, decoder2, decoder3, decoder4, decoder5, decoder6) {
|
|
151
|
-
return (0, _composition.compose)(ntuple(6), function (blobs) {
|
|
152
|
-
var blob1 = blobs[0],
|
|
153
|
-
blob2 = blobs[1],
|
|
154
|
-
blob3 = blobs[2],
|
|
155
|
-
blob4 = blobs[3],
|
|
156
|
-
blob5 = blobs[4],
|
|
157
|
-
blob6 = blobs[5];
|
|
158
|
-
var result1 = decoder1(blob1);
|
|
159
|
-
var result2 = decoder2(blob2);
|
|
160
|
-
var result3 = decoder3(blob3);
|
|
161
|
-
var result4 = decoder4(blob4);
|
|
162
|
-
var result5 = decoder5(blob5);
|
|
163
|
-
var result6 = decoder6(blob6);
|
|
164
|
-
|
|
165
|
-
try {
|
|
166
|
-
return (0, _result.ok)([(0, _result.unwrap)(result1), (0, _result.unwrap)(result2), (0, _result.unwrap)(result3), (0, _result.unwrap)(result4), (0, _result.unwrap)(result5), (0, _result.unwrap)(result6)]);
|
|
167
|
-
} catch (e) {
|
|
168
|
-
// If a decoder error has happened while unwrapping all the
|
|
169
|
-
// results, try to construct a good error message
|
|
170
|
-
return (0, _result.err)((0, _annotate.annotate)([(0, _result.isErr)(result1) ? (0, _result.errValue)(result1) : (0, _result.value)(result1), (0, _result.isErr)(result2) ? (0, _result.errValue)(result2) : (0, _result.value)(result2), (0, _result.isErr)(result3) ? (0, _result.errValue)(result3) : (0, _result.value)(result3), (0, _result.isErr)(result4) ? (0, _result.errValue)(result4) : (0, _result.value)(result4), (0, _result.isErr)(result5) ? (0, _result.errValue)(result5) : (0, _result.value)(result5), (0, _result.isErr)(result6) ? (0, _result.errValue)(result6) : (0, _result.value)(result6)]));
|
|
171
|
-
}
|
|
172
|
-
});
|
|
173
|
-
}
|
package/cjs/stdlib/tuple.js.flow
DELETED
|
@@ -1,220 +0,0 @@
|
|
|
1
|
-
// @flow strict
|
|
2
|
-
|
|
3
|
-
import { annotate } from '../annotate';
|
|
4
|
-
import { compose, predicate } from './composition';
|
|
5
|
-
import { err, errValue, isErr, ok, unwrap, value } 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(errValue(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(
|
|
53
|
-
annotate([
|
|
54
|
-
isErr(result1) ? errValue(result1) : value(result1),
|
|
55
|
-
isErr(result2) ? errValue(result2) : value(result2),
|
|
56
|
-
]),
|
|
57
|
-
);
|
|
58
|
-
}
|
|
59
|
-
});
|
|
60
|
-
}
|
|
61
|
-
|
|
62
|
-
/**
|
|
63
|
-
* Builds a Decoder that returns Ok for 3-tuples of [T1, T2, T3], given
|
|
64
|
-
* Decoders for T1, T2, and T3. Err otherwise.
|
|
65
|
-
*/
|
|
66
|
-
export function tuple3<T1, T2, T3>(
|
|
67
|
-
decoder1: Decoder<T1>,
|
|
68
|
-
decoder2: Decoder<T2>,
|
|
69
|
-
decoder3: Decoder<T3>,
|
|
70
|
-
): Decoder<[T1, T2, T3]> {
|
|
71
|
-
return compose(ntuple(3), (blobs: $ReadOnlyArray<mixed>) => {
|
|
72
|
-
const [blob1, blob2, blob3] = blobs;
|
|
73
|
-
|
|
74
|
-
const result1 = decoder1(blob1);
|
|
75
|
-
const result2 = decoder2(blob2);
|
|
76
|
-
const result3 = decoder3(blob3);
|
|
77
|
-
try {
|
|
78
|
-
return ok([unwrap(result1), unwrap(result2), unwrap(result3)]);
|
|
79
|
-
} catch (e) {
|
|
80
|
-
// If a decoder error has happened while unwrapping all the
|
|
81
|
-
// results, try to construct a good error message
|
|
82
|
-
return err(
|
|
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
|
-
);
|
|
89
|
-
}
|
|
90
|
-
});
|
|
91
|
-
}
|
|
92
|
-
|
|
93
|
-
/**
|
|
94
|
-
* Builds a Decoder that returns Ok for 4-tuples of [T1, T2, T3, T4], given
|
|
95
|
-
* Decoders for T1, T2, T3, and T4. Err otherwise.
|
|
96
|
-
*/
|
|
97
|
-
export function tuple4<T1, T2, T3, T4>(
|
|
98
|
-
decoder1: Decoder<T1>,
|
|
99
|
-
decoder2: Decoder<T2>,
|
|
100
|
-
decoder3: Decoder<T3>,
|
|
101
|
-
decoder4: Decoder<T4>,
|
|
102
|
-
): Decoder<[T1, T2, T3, T4]> {
|
|
103
|
-
return compose(ntuple(4), (blobs: $ReadOnlyArray<mixed>) => {
|
|
104
|
-
const [blob1, blob2, blob3, blob4] = blobs;
|
|
105
|
-
|
|
106
|
-
const result1 = decoder1(blob1);
|
|
107
|
-
const result2 = decoder2(blob2);
|
|
108
|
-
const result3 = decoder3(blob3);
|
|
109
|
-
const result4 = decoder4(blob4);
|
|
110
|
-
try {
|
|
111
|
-
return ok([
|
|
112
|
-
unwrap(result1),
|
|
113
|
-
unwrap(result2),
|
|
114
|
-
unwrap(result3),
|
|
115
|
-
unwrap(result4),
|
|
116
|
-
]);
|
|
117
|
-
} catch (e) {
|
|
118
|
-
// If a decoder error has happened while unwrapping all the
|
|
119
|
-
// results, try to construct a good error message
|
|
120
|
-
return err(
|
|
121
|
-
annotate([
|
|
122
|
-
isErr(result1) ? errValue(result1) : value(result1),
|
|
123
|
-
isErr(result2) ? errValue(result2) : value(result2),
|
|
124
|
-
isErr(result3) ? errValue(result3) : value(result3),
|
|
125
|
-
isErr(result4) ? errValue(result4) : value(result4),
|
|
126
|
-
]),
|
|
127
|
-
);
|
|
128
|
-
}
|
|
129
|
-
});
|
|
130
|
-
}
|
|
131
|
-
|
|
132
|
-
/**
|
|
133
|
-
* Builds a Decoder that returns Ok for 5-tuples of [T1, T2, T3, T4, T5], given
|
|
134
|
-
* Decoders for T1, T2, T3, T4, and T5. Err otherwise.
|
|
135
|
-
*/
|
|
136
|
-
export function tuple5<T1, T2, T3, T4, T5>(
|
|
137
|
-
decoder1: Decoder<T1>,
|
|
138
|
-
decoder2: Decoder<T2>,
|
|
139
|
-
decoder3: Decoder<T3>,
|
|
140
|
-
decoder4: Decoder<T4>,
|
|
141
|
-
decoder5: Decoder<T5>,
|
|
142
|
-
): Decoder<[T1, T2, T3, T4, T5]> {
|
|
143
|
-
return compose(ntuple(5), (blobs: $ReadOnlyArray<mixed>) => {
|
|
144
|
-
const [blob1, blob2, blob3, blob4, blob5] = blobs;
|
|
145
|
-
|
|
146
|
-
const result1 = decoder1(blob1);
|
|
147
|
-
const result2 = decoder2(blob2);
|
|
148
|
-
const result3 = decoder3(blob3);
|
|
149
|
-
const result4 = decoder4(blob4);
|
|
150
|
-
const result5 = decoder5(blob5);
|
|
151
|
-
try {
|
|
152
|
-
return ok([
|
|
153
|
-
unwrap(result1),
|
|
154
|
-
unwrap(result2),
|
|
155
|
-
unwrap(result3),
|
|
156
|
-
unwrap(result4),
|
|
157
|
-
unwrap(result5),
|
|
158
|
-
]);
|
|
159
|
-
} catch (e) {
|
|
160
|
-
// If a decoder error has happened while unwrapping all the
|
|
161
|
-
// results, try to construct a good error message
|
|
162
|
-
return err(
|
|
163
|
-
annotate([
|
|
164
|
-
isErr(result1) ? errValue(result1) : value(result1),
|
|
165
|
-
isErr(result2) ? errValue(result2) : value(result2),
|
|
166
|
-
isErr(result3) ? errValue(result3) : value(result3),
|
|
167
|
-
isErr(result4) ? errValue(result4) : value(result4),
|
|
168
|
-
isErr(result5) ? errValue(result5) : value(result5),
|
|
169
|
-
]),
|
|
170
|
-
);
|
|
171
|
-
}
|
|
172
|
-
});
|
|
173
|
-
}
|
|
174
|
-
|
|
175
|
-
/**
|
|
176
|
-
* Builds a Decoder that returns Ok for 5-tuples of [T1, T2, T3, T4, T5], given
|
|
177
|
-
* Decoders for T1, T2, T3, T4, T5, and T6. Err otherwise.
|
|
178
|
-
*/
|
|
179
|
-
export function tuple6<T1, T2, T3, T4, T5, T6>(
|
|
180
|
-
decoder1: Decoder<T1>,
|
|
181
|
-
decoder2: Decoder<T2>,
|
|
182
|
-
decoder3: Decoder<T3>,
|
|
183
|
-
decoder4: Decoder<T4>,
|
|
184
|
-
decoder5: Decoder<T5>,
|
|
185
|
-
decoder6: Decoder<T6>,
|
|
186
|
-
): Decoder<[T1, T2, T3, T4, T5, T6]> {
|
|
187
|
-
return compose(ntuple(6), (blobs: $ReadOnlyArray<mixed>) => {
|
|
188
|
-
const [blob1, blob2, blob3, blob4, blob5, blob6] = blobs;
|
|
189
|
-
|
|
190
|
-
const result1 = decoder1(blob1);
|
|
191
|
-
const result2 = decoder2(blob2);
|
|
192
|
-
const result3 = decoder3(blob3);
|
|
193
|
-
const result4 = decoder4(blob4);
|
|
194
|
-
const result5 = decoder5(blob5);
|
|
195
|
-
const result6 = decoder6(blob6);
|
|
196
|
-
try {
|
|
197
|
-
return ok([
|
|
198
|
-
unwrap(result1),
|
|
199
|
-
unwrap(result2),
|
|
200
|
-
unwrap(result3),
|
|
201
|
-
unwrap(result4),
|
|
202
|
-
unwrap(result5),
|
|
203
|
-
unwrap(result6),
|
|
204
|
-
]);
|
|
205
|
-
} catch (e) {
|
|
206
|
-
// If a decoder error has happened while unwrapping all the
|
|
207
|
-
// results, try to construct a good error message
|
|
208
|
-
return err(
|
|
209
|
-
annotate([
|
|
210
|
-
isErr(result1) ? errValue(result1) : value(result1),
|
|
211
|
-
isErr(result2) ? errValue(result2) : value(result2),
|
|
212
|
-
isErr(result3) ? errValue(result3) : value(result3),
|
|
213
|
-
isErr(result4) ? errValue(result4) : value(result4),
|
|
214
|
-
isErr(result5) ? errValue(result5) : value(result5),
|
|
215
|
-
isErr(result6) ? errValue(result6) : value(result6),
|
|
216
|
-
]),
|
|
217
|
-
);
|
|
218
|
-
}
|
|
219
|
-
});
|
|
220
|
-
}
|
package/es/_guard.js
DELETED
|
@@ -1,15 +0,0 @@
|
|
|
1
|
-
import * as Result from './result';
|
|
2
|
-
import { formatInline } from './format';
|
|
3
|
-
export function guard(decoder, formatter) {
|
|
4
|
-
if (formatter === void 0) {
|
|
5
|
-
formatter = formatInline;
|
|
6
|
-
}
|
|
7
|
-
|
|
8
|
-
return function (blob) {
|
|
9
|
-
return Result.unwrap(Result.mapError(decoder(blob), function (annotation) {
|
|
10
|
-
var err = new Error('\n' + formatter(annotation));
|
|
11
|
-
err.name = 'Decoding error';
|
|
12
|
-
return err;
|
|
13
|
-
}));
|
|
14
|
-
};
|
|
15
|
-
}
|
package/es/_types.js
DELETED
|
File without changes
|
package/es/format/index.js
DELETED
package/es/format/short.js
DELETED
package/es/index.js
DELETED
|
@@ -1,37 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Elm-like JSON decoders, for use with Flow.
|
|
3
|
-
* See http://elmplayground.com/decoding-json-in-elm-1 for an introduction.
|
|
4
|
-
*
|
|
5
|
-
* Why? All JSON responses coming from our API endpoints are just that: free-form
|
|
6
|
-
* JSON data. To Flow, the only type classification possilbe is "any" -- effectively
|
|
7
|
-
* turning off all type checks for anything related to JSON. To the receiving end
|
|
8
|
-
* (our frontend), the structure of that data is completely opaque to any type
|
|
9
|
-
* checkers since JSON values can be anything: an object, an array, null, a string,
|
|
10
|
-
* a bool, etc. Our type system is not a runtime type system, so we need a way of
|
|
11
|
-
* "converting" an any-type JSON value into a type that we want to work with in our
|
|
12
|
-
* frontend code base.
|
|
13
|
-
*
|
|
14
|
-
* Elm's solution to this problem is to define composable decoders: functions that
|
|
15
|
-
* take anything and either fail with an error, or guarantee to return the expected
|
|
16
|
-
* type. In our case, it's fine to fail with a runtime error.
|
|
17
|
-
*
|
|
18
|
-
*/
|
|
19
|
-
export { guard } from './_guard';
|
|
20
|
-
export { compose, map, predicate } from './stdlib/composition';
|
|
21
|
-
export { array, nonEmptyArray, poja } from './stdlib/array';
|
|
22
|
-
export { boolean, numericBoolean, truthy } from './stdlib/boolean';
|
|
23
|
-
export { constant, hardcoded, mixed, null_, undefined_, unknown } from './stdlib/constants';
|
|
24
|
-
export { date, iso8601 } from './stdlib/date';
|
|
25
|
-
export { describe } from './stdlib/describe';
|
|
26
|
-
export { dispatch } from './stdlib/dispatch';
|
|
27
|
-
export { either, either3, either4, either5, either6, either7, either8, either9, oneOf } from './stdlib/either';
|
|
28
|
-
export { fail } from './stdlib/fail';
|
|
29
|
-
export { instanceOf } from './stdlib/instanceOf';
|
|
30
|
-
export { json, jsonObject, jsonArray } from './stdlib/json';
|
|
31
|
-
export { lazy } from './stdlib/lazy';
|
|
32
|
-
export { mapping, dict } from './stdlib/mapping';
|
|
33
|
-
export { integer, number, positiveInteger, positiveNumber } from './stdlib/number';
|
|
34
|
-
export { exact, inexact, object, pojo } from './stdlib/object';
|
|
35
|
-
export { maybe, nullable, optional } from './stdlib/optional';
|
|
36
|
-
export { email, nonEmptyString, regex, string, url } from './stdlib/string';
|
|
37
|
-
export { tuple1, tuple2, tuple3, tuple4, tuple5, tuple6 } from './stdlib/tuple';
|
package/es/result.js
DELETED
|
@@ -1,139 +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
|
-
};
|
|
15
|
-
}
|
|
16
|
-
/**
|
|
17
|
-
* Create a new Result instance representing a failed computation.
|
|
18
|
-
*/
|
|
19
|
-
|
|
20
|
-
export function err(error) {
|
|
21
|
-
return {
|
|
22
|
-
type: 'err',
|
|
23
|
-
error: error
|
|
24
|
-
};
|
|
25
|
-
}
|
|
26
|
-
export function toString(result) {
|
|
27
|
-
return result.type === 'ok' ? "Ok(" + String(result.value) + ")" : "Err(" + String(result.error) + ")";
|
|
28
|
-
}
|
|
29
|
-
export function isOk(result) {
|
|
30
|
-
return result.type === 'ok';
|
|
31
|
-
}
|
|
32
|
-
export function isErr(result) {
|
|
33
|
-
return result.type === 'err';
|
|
34
|
-
}
|
|
35
|
-
export function withDefault(result, defaultValue) {
|
|
36
|
-
return result.type === 'ok' ? result.value : defaultValue;
|
|
37
|
-
}
|
|
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
|
-
/**
|
|
45
|
-
* Unwrap the value from this Result instance if this is an "Ok" result.
|
|
46
|
-
* Otherwise, will throw the "Err" error via a runtime exception.
|
|
47
|
-
*/
|
|
48
|
-
|
|
49
|
-
export function unwrap(result) {
|
|
50
|
-
if (result.type === 'ok') {
|
|
51
|
-
return result.value;
|
|
52
|
-
} else {
|
|
53
|
-
throw result.error;
|
|
54
|
-
}
|
|
55
|
-
}
|
|
56
|
-
export function expect(result, message) {
|
|
57
|
-
if (result.type === 'ok') {
|
|
58
|
-
return result.value;
|
|
59
|
-
} else {
|
|
60
|
-
throw message instanceof Error ? message : new Error(message);
|
|
61
|
-
}
|
|
62
|
-
}
|
|
63
|
-
export function dispatch(result, okCallback, errCallback) {
|
|
64
|
-
return result.type === 'ok' ? okCallback(result.value) : errCallback(result.error);
|
|
65
|
-
}
|
|
66
|
-
/**
|
|
67
|
-
* If the given result is OK, defers to the other result. Otherwise returns the
|
|
68
|
-
* error result.
|
|
69
|
-
*
|
|
70
|
-
* It's like saying A && B, but on Result.
|
|
71
|
-
*
|
|
72
|
-
* Examples:
|
|
73
|
-
*
|
|
74
|
-
* Result.ok(42) && Result.ok('hi') // => Ok('hi')
|
|
75
|
-
* Result.err('boo') && Result.ok('hi') // => Err('boo')
|
|
76
|
-
* Result.ok(42) && Result.err('boo') // => Err('boo')
|
|
77
|
-
* Result.err('boo') && Result.err('boo') // => Err('boo')
|
|
78
|
-
*
|
|
79
|
-
*/
|
|
80
|
-
// export function and<T, E, T2>(
|
|
81
|
-
// result1: Result<T, E>,
|
|
82
|
-
// result2: Result<T2, E>,
|
|
83
|
-
// ): Result<T2, E> {
|
|
84
|
-
// return result1.type === 'ok' ? result2 : result1;
|
|
85
|
-
// }
|
|
86
|
-
|
|
87
|
-
/**
|
|
88
|
-
* If the given result is OK, return that result. Otherwise, defers to the
|
|
89
|
-
* other result.
|
|
90
|
-
*
|
|
91
|
-
* It's like saying A || B, but on Result.
|
|
92
|
-
*
|
|
93
|
-
* Examples:
|
|
94
|
-
*
|
|
95
|
-
* Result.ok(42) || Result.ok('hi') // => Ok(42)
|
|
96
|
-
* Result.err('boo') || Result.ok('hi') // => Ok('hi')
|
|
97
|
-
* Result.ok(42) || Result.err('boo') // => Ok(42)
|
|
98
|
-
* Result.err('bleh') || Result.err('boo') // => Err('boo')
|
|
99
|
-
*
|
|
100
|
-
*/
|
|
101
|
-
// export function or<T, E, E2>(
|
|
102
|
-
// result1: Result<T, E>,
|
|
103
|
-
// result2: Result<T, E2>,
|
|
104
|
-
// ): Result<T, E2> {
|
|
105
|
-
// return result1.type === 'ok' ? result1 : result2;
|
|
106
|
-
// }
|
|
107
|
-
|
|
108
|
-
/**
|
|
109
|
-
* Like .and(), aka &&, but the second argument gets evaluated lazily only if
|
|
110
|
-
* the first result is an Ok result. If so, it has access to the Ok value from
|
|
111
|
-
* the first argument.
|
|
112
|
-
*/
|
|
113
|
-
|
|
114
|
-
export function andThen(result1, lazyResult2) {
|
|
115
|
-
return result1.type === 'ok' ? lazyResult2(result1.value) : result1;
|
|
116
|
-
}
|
|
117
|
-
/**
|
|
118
|
-
* Like .or(), aka ||, but the second argument gets evaluated lazily only if
|
|
119
|
-
* the first result is an Err result. If so, it has access to the Err value
|
|
120
|
-
* from the first argument.
|
|
121
|
-
*/
|
|
122
|
-
|
|
123
|
-
export function orElse(result1, lazyResult2) {
|
|
124
|
-
return result1.type === 'ok' ? result1 : lazyResult2(result1.error);
|
|
125
|
-
}
|
|
126
|
-
/**
|
|
127
|
-
* Transform an Ok result. Will not touch Err results.
|
|
128
|
-
*/
|
|
129
|
-
|
|
130
|
-
export function map(result, mapper) {
|
|
131
|
-
return result.type === 'ok' ? ok(mapper(result.value)) : result;
|
|
132
|
-
}
|
|
133
|
-
/**
|
|
134
|
-
* Transform an Err value. Will not touch Ok results.
|
|
135
|
-
*/
|
|
136
|
-
|
|
137
|
-
export function mapError(result, mapper) {
|
|
138
|
-
return result.type === 'ok' ? result : err(mapper(result.error));
|
|
139
|
-
}
|
package/es/stdlib/array.js
DELETED
|
@@ -1,91 +0,0 @@
|
|
|
1
|
-
import * as Result from '../result';
|
|
2
|
-
import { annotate } from '../annotate';
|
|
3
|
-
import { compose, predicate } from './composition';
|
|
4
|
-
|
|
5
|
-
/**
|
|
6
|
-
* Like a "Plain Old JavaScript Object", but for arrays: "Plain Old JavaScript
|
|
7
|
-
* Array" ^_^
|
|
8
|
-
*/
|
|
9
|
-
export var poja = function poja(blob) {
|
|
10
|
-
if (!Array.isArray(blob)) {
|
|
11
|
-
return Result.err(annotate(blob, 'Must be an array'));
|
|
12
|
-
}
|
|
13
|
-
|
|
14
|
-
return Result.ok( // NOTE: Since Flow 0.98, Array.isArray() returns $ReadOnlyArray<mixed>
|
|
15
|
-
// instead of Array<mixed>. For rationale, see
|
|
16
|
-
// https://github.com/facebook/flow/issues/7684. In this case, we
|
|
17
|
-
// don't want to output read-only types because it's up to the user of
|
|
18
|
-
// decoders to determine what they want to do with the decoded output.
|
|
19
|
-
// If they want to write items into the array, that's fine!
|
|
20
|
-
// The fastest way to turn a read-only array into a normal array in
|
|
21
|
-
// Javascript is to use .slice() on it, see this benchmark:
|
|
22
|
-
// http://jsben.ch/lO6C5
|
|
23
|
-
blob.slice());
|
|
24
|
-
};
|
|
25
|
-
/**
|
|
26
|
-
* Given an array of Result instances, loop over them all and return:
|
|
27
|
-
* - An [index, err] tuple, indicating the (index of the) first Err instance
|
|
28
|
-
* encountered; or
|
|
29
|
-
* - a new Ok with an array of all unwrapped Ok'ed values
|
|
30
|
-
*/
|
|
31
|
-
|
|
32
|
-
function all(items, blobs) {
|
|
33
|
-
var results = [];
|
|
34
|
-
|
|
35
|
-
for (var index = 0; index < items.length; ++index) {
|
|
36
|
-
var result = items[index];
|
|
37
|
-
|
|
38
|
-
if (result.type === 'ok') {
|
|
39
|
-
results.push(result.value);
|
|
40
|
-
} else {
|
|
41
|
-
var ann = result.error; // Rewrite the annotation to include the index information, and inject it into the original blob
|
|
42
|
-
|
|
43
|
-
var clone = [].concat(blobs);
|
|
44
|
-
clone.splice(index, 1, annotate(ann, ann.text ? ann.text + " (at index " + index + ")" : "index " + index)); // const errValue = [];
|
|
45
|
-
// if (index > 0) {
|
|
46
|
-
// errValue.push('...'); // TODO: make special mark, not string!
|
|
47
|
-
// }
|
|
48
|
-
// errValue.push(
|
|
49
|
-
// );
|
|
50
|
-
// if (index < iterable.length - 1) {
|
|
51
|
-
// errValue.push('...'); // TODO: make special mark, not string!
|
|
52
|
-
// }
|
|
53
|
-
|
|
54
|
-
return Result.err(annotate(clone));
|
|
55
|
-
}
|
|
56
|
-
}
|
|
57
|
-
|
|
58
|
-
return Result.ok(results);
|
|
59
|
-
}
|
|
60
|
-
/**
|
|
61
|
-
* Given a T, builds a decoder that assumes an array input and returns an
|
|
62
|
-
* Array<T>.
|
|
63
|
-
*/
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
function members(decoder) {
|
|
67
|
-
return function (blobs) {
|
|
68
|
-
var results = blobs.map(decoder);
|
|
69
|
-
var result = all(results, blobs);
|
|
70
|
-
return result;
|
|
71
|
-
};
|
|
72
|
-
}
|
|
73
|
-
/**
|
|
74
|
-
* Builds a Decoder that returns Ok for values of `Array<T>`, given a Decoder
|
|
75
|
-
* for `T`. Err otherwise.
|
|
76
|
-
*/
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
export function array(decoder) {
|
|
80
|
-
return compose(poja, members(decoder));
|
|
81
|
-
}
|
|
82
|
-
/**
|
|
83
|
-
* Builds a Decoder that returns Ok for values of `Array<T>`, but will reject
|
|
84
|
-
* empty arrays.
|
|
85
|
-
*/
|
|
86
|
-
|
|
87
|
-
export function nonEmptyArray(decoder) {
|
|
88
|
-
return compose(array(decoder), predicate(function (arr) {
|
|
89
|
-
return arr.length > 0;
|
|
90
|
-
}, 'Must be non-empty array'));
|
|
91
|
-
}
|