decoders 2.0.0-beta8 → 2.0.1
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 +67 -20
- package/Decoder.d.ts +94 -0
- package/Decoder.js +115 -0
- package/Decoder.js.flow +286 -0
- package/Decoder.mjs +108 -0
- package/README.md +122 -1345
- package/_utils.d.ts +0 -1
- package/_utils.js +51 -68
- package/_utils.js.flow +30 -20
- package/_utils.mjs +39 -54
- package/annotate.js +79 -72
- package/annotate.mjs +62 -58
- package/format.d.ts +4 -2
- package/format.js +64 -74
- package/format.js.flow +14 -12
- package/format.mjs +58 -68
- package/index.d.ts +38 -41
- package/index.js +66 -98
- package/index.js.flow +39 -58
- package/index.mjs +11 -37
- package/{core → lib}/_helpers.d.ts +0 -0
- package/lib/arrays.d.ts +59 -0
- package/lib/arrays.js +97 -0
- package/lib/arrays.js.flow +138 -0
- package/lib/arrays.mjs +84 -0
- package/lib/basics.d.ts +93 -0
- package/lib/basics.js +94 -0
- package/lib/basics.js.flow +124 -0
- package/lib/basics.mjs +75 -0
- package/lib/booleans.d.ts +16 -0
- package/lib/booleans.js +25 -0
- package/lib/booleans.js.flow +22 -0
- package/lib/booleans.mjs +15 -0
- package/lib/dates.d.ts +15 -0
- package/lib/dates.js +28 -0
- package/lib/dates.js.flow +40 -0
- package/lib/dates.mjs +19 -0
- package/lib/json.d.ts +35 -0
- package/lib/json.js +33 -0
- package/lib/json.js.flow +50 -0
- package/lib/json.mjs +18 -0
- package/lib/numbers.d.ts +31 -0
- package/lib/numbers.js +31 -0
- package/lib/numbers.js.flow +48 -0
- package/lib/numbers.mjs +21 -0
- package/lib/objects.d.ts +76 -0
- package/lib/objects.js +161 -0
- package/lib/objects.js.flow +238 -0
- package/lib/objects.mjs +147 -0
- package/lib/strings.d.ts +56 -0
- package/lib/strings.js +58 -0
- package/lib/strings.js.flow +90 -0
- package/lib/strings.mjs +40 -0
- package/lib/unions.d.ts +55 -0
- package/lib/unions.js +96 -0
- package/lib/unions.js.flow +155 -0
- package/lib/unions.mjs +83 -0
- package/lib/utilities.d.ts +40 -0
- package/lib/utilities.js +48 -0
- package/lib/utilities.js.flow +65 -0
- package/lib/utilities.mjs +37 -0
- package/package.json +64 -17
- package/result.d.ts +2 -25
- package/result.js +9 -90
- package/result.js.flow +4 -76
- package/result.mjs +5 -71
- package/_guard.d.ts +0 -7
- package/_guard.js +0 -22
- package/_guard.js.flow +0 -20
- package/_guard.mjs +0 -15
- package/_types.d.ts +0 -13
- package/_types.js +0 -1
- package/_types.js.flow +0 -20
- package/_types.mjs +0 -0
- package/core/array.d.ts +0 -5
- package/core/array.js +0 -104
- package/core/array.js.flow +0 -100
- package/core/array.mjs +0 -91
- package/core/boolean.d.ts +0 -5
- package/core/boolean.js +0 -40
- package/core/boolean.js.flow +0 -27
- package/core/boolean.mjs +0 -28
- package/core/composition.d.ts +0 -14
- package/core/composition.js +0 -54
- package/core/composition.js.flow +0 -48
- package/core/composition.mjs +0 -44
- package/core/constants.d.ts +0 -11
- package/core/constants.js +0 -65
- package/core/constants.js.flow +0 -44
- package/core/constants.mjs +0 -46
- package/core/date.d.ts +0 -4
- package/core/date.js +0 -42
- package/core/date.js.flow +0 -38
- package/core/date.mjs +0 -28
- package/core/describe.d.ts +0 -3
- package/core/describe.js +0 -22
- package/core/describe.js.flow +0 -17
- package/core/describe.mjs +0 -16
- package/core/dispatch.d.ts +0 -8
- package/core/dispatch.js +0 -58
- package/core/dispatch.js.flow +0 -58
- package/core/dispatch.mjs +0 -51
- package/core/either.d.ts +0 -61
- package/core/either.js +0 -113
- package/core/either.js.flow +0 -151
- package/core/either.mjs +0 -90
- package/core/fail.d.ts +0 -3
- package/core/fail.js +0 -17
- package/core/fail.js.flow +0 -12
- package/core/fail.mjs +0 -11
- package/core/instanceOf.d.ts +0 -3
- package/core/instanceOf.js +0 -15
- package/core/instanceOf.js.flow +0 -20
- package/core/instanceOf.mjs +0 -8
- package/core/json.d.ts +0 -11
- package/core/json.js +0 -31
- package/core/json.js.flow +0 -28
- package/core/json.mjs +0 -15
- package/core/lazy.d.ts +0 -3
- package/core/lazy.js +0 -16
- package/core/lazy.js.flow +0 -15
- package/core/lazy.mjs +0 -11
- package/core/mapping.d.ts +0 -6
- package/core/mapping.js +0 -67
- package/core/mapping.js.flow +0 -62
- package/core/mapping.mjs +0 -58
- package/core/number.d.ts +0 -6
- package/core/number.js +0 -36
- package/core/number.js.flow +0 -40
- package/core/number.mjs +0 -25
- package/core/object.d.ts +0 -33
- package/core/object.js +0 -190
- package/core/object.js.flow +0 -203
- package/core/object.mjs +0 -175
- package/core/optional.d.ts +0 -5
- package/core/optional.js +0 -50
- package/core/optional.js.flow +0 -41
- package/core/optional.mjs +0 -38
- package/core/string.d.ts +0 -10
- package/core/string.js +0 -68
- package/core/string.js.flow +0 -59
- package/core/string.mjs +0 -49
- package/core/tuple.d.ts +0 -30
- package/core/tuple.js +0 -177
- package/core/tuple.js.flow +0 -211
- package/core/tuple.mjs +0 -159
package/core/either.js.flow
DELETED
|
@@ -1,151 +0,0 @@
|
|
|
1
|
-
// @flow strict
|
|
2
|
-
|
|
3
|
-
import { annotate } from '../annotate';
|
|
4
|
-
import { err, ok, orElse } from '../result';
|
|
5
|
-
import { indent, summarize } from '../_utils';
|
|
6
|
-
import type { Decoder, Scalar } from '../_types';
|
|
7
|
-
|
|
8
|
-
/**
|
|
9
|
-
* Indents and adds a dash in front of this (potentially multiline) string.
|
|
10
|
-
*/
|
|
11
|
-
function itemize(s: string): string {
|
|
12
|
-
return '-' + indent(s).substring(1);
|
|
13
|
-
}
|
|
14
|
-
|
|
15
|
-
/**
|
|
16
|
-
* Nests another error as an item under a new-to-be-created "Either error". If
|
|
17
|
-
* the given subitem already is an "Either error" of itself, don't indent, but
|
|
18
|
-
* just "inject" its items at the same error level, for nicely flattened either
|
|
19
|
-
* expressions.
|
|
20
|
-
*
|
|
21
|
-
* Avoids:
|
|
22
|
-
*
|
|
23
|
-
* Either:
|
|
24
|
-
* - Either:
|
|
25
|
-
* - Must be P
|
|
26
|
-
* - Either:
|
|
27
|
-
* - Must be Q
|
|
28
|
-
* - Must be R
|
|
29
|
-
* - Must be S
|
|
30
|
-
*
|
|
31
|
-
* And "flattens" these to:
|
|
32
|
-
*
|
|
33
|
-
* Either:
|
|
34
|
-
* - Must be P
|
|
35
|
-
* - Must be Q
|
|
36
|
-
* - Must be R
|
|
37
|
-
* - Must be S
|
|
38
|
-
*
|
|
39
|
-
*/
|
|
40
|
-
function nest(errText: string): string {
|
|
41
|
-
const EITHER_PREFIX = 'Either:\n';
|
|
42
|
-
return errText.startsWith(EITHER_PREFIX)
|
|
43
|
-
? errText.substr(EITHER_PREFIX.length)
|
|
44
|
-
: itemize(errText);
|
|
45
|
-
}
|
|
46
|
-
|
|
47
|
-
export function either<T1, T2>(d1: Decoder<T1>, d2: Decoder<T2>): Decoder<T1 | T2> {
|
|
48
|
-
return (blob: mixed) =>
|
|
49
|
-
orElse(d1(blob), (err1) =>
|
|
50
|
-
orElse(d2(blob), (err2) => {
|
|
51
|
-
const serr1 = summarize(err1).join('\n');
|
|
52
|
-
const serr2 = summarize(err2).join('\n');
|
|
53
|
-
const text = ['Either:', nest(serr1), nest(serr2)].join('\n');
|
|
54
|
-
return err(annotate(blob, text));
|
|
55
|
-
}),
|
|
56
|
-
);
|
|
57
|
-
}
|
|
58
|
-
|
|
59
|
-
export function either3<T1, T2, T3>(
|
|
60
|
-
d1: Decoder<T1>,
|
|
61
|
-
d2: Decoder<T2>,
|
|
62
|
-
d3: Decoder<T3>,
|
|
63
|
-
): Decoder<T1 | T2 | T3> {
|
|
64
|
-
return either(d1, either(d2, d3));
|
|
65
|
-
}
|
|
66
|
-
|
|
67
|
-
export function either4<T1, T2, T3, T4>(
|
|
68
|
-
d1: Decoder<T1>,
|
|
69
|
-
d2: Decoder<T2>,
|
|
70
|
-
d3: Decoder<T3>,
|
|
71
|
-
d4: Decoder<T4>,
|
|
72
|
-
): Decoder<T1 | T2 | T3 | T4> {
|
|
73
|
-
return either(d1, either3(d2, d3, d4));
|
|
74
|
-
}
|
|
75
|
-
|
|
76
|
-
export function either5<T1, T2, T3, T4, T5>(
|
|
77
|
-
d1: Decoder<T1>,
|
|
78
|
-
d2: Decoder<T2>,
|
|
79
|
-
d3: Decoder<T3>,
|
|
80
|
-
d4: Decoder<T4>,
|
|
81
|
-
d5: Decoder<T5>,
|
|
82
|
-
): Decoder<T1 | T2 | T3 | T4 | T5> {
|
|
83
|
-
return either(d1, either4(d2, d3, d4, d5));
|
|
84
|
-
}
|
|
85
|
-
|
|
86
|
-
export function either6<T1, T2, T3, T4, T5, T6>(
|
|
87
|
-
d1: Decoder<T1>,
|
|
88
|
-
d2: Decoder<T2>,
|
|
89
|
-
d3: Decoder<T3>,
|
|
90
|
-
d4: Decoder<T4>,
|
|
91
|
-
d5: Decoder<T5>,
|
|
92
|
-
d6: Decoder<T6>,
|
|
93
|
-
): Decoder<T1 | T2 | T3 | T4 | T5 | T6> {
|
|
94
|
-
return either(d1, either5(d2, d3, d4, d5, d6));
|
|
95
|
-
}
|
|
96
|
-
|
|
97
|
-
export function either7<T1, T2, T3, T4, T5, T6, T7>(
|
|
98
|
-
d1: Decoder<T1>,
|
|
99
|
-
d2: Decoder<T2>,
|
|
100
|
-
d3: Decoder<T3>,
|
|
101
|
-
d4: Decoder<T4>,
|
|
102
|
-
d5: Decoder<T5>,
|
|
103
|
-
d6: Decoder<T6>,
|
|
104
|
-
d7: Decoder<T7>,
|
|
105
|
-
): Decoder<T1 | T2 | T3 | T4 | T5 | T6 | T7> {
|
|
106
|
-
return either(d1, either6(d2, d3, d4, d5, d6, d7));
|
|
107
|
-
}
|
|
108
|
-
|
|
109
|
-
export function either8<T1, T2, T3, T4, T5, T6, T7, T8>(
|
|
110
|
-
d1: Decoder<T1>,
|
|
111
|
-
d2: Decoder<T2>,
|
|
112
|
-
d3: Decoder<T3>,
|
|
113
|
-
d4: Decoder<T4>,
|
|
114
|
-
d5: Decoder<T5>,
|
|
115
|
-
d6: Decoder<T6>,
|
|
116
|
-
d7: Decoder<T7>,
|
|
117
|
-
d8: Decoder<T8>,
|
|
118
|
-
): Decoder<T1 | T2 | T3 | T4 | T5 | T6 | T7 | T8> {
|
|
119
|
-
return either(d1, either7(d2, d3, d4, d5, d6, d7, d8));
|
|
120
|
-
}
|
|
121
|
-
|
|
122
|
-
export function either9<T1, T2, T3, T4, T5, T6, T7, T8, T9>(
|
|
123
|
-
d1: Decoder<T1>,
|
|
124
|
-
d2: Decoder<T2>,
|
|
125
|
-
d3: Decoder<T3>,
|
|
126
|
-
d4: Decoder<T4>,
|
|
127
|
-
d5: Decoder<T5>,
|
|
128
|
-
d6: Decoder<T6>,
|
|
129
|
-
d7: Decoder<T7>,
|
|
130
|
-
d8: Decoder<T8>,
|
|
131
|
-
d9: Decoder<T9>,
|
|
132
|
-
): Decoder<T1 | T2 | T3 | T4 | T5 | T6 | T7 | T8 | T9> {
|
|
133
|
-
return either(d1, either8(d2, d3, d4, d5, d6, d7, d8, d9));
|
|
134
|
-
}
|
|
135
|
-
|
|
136
|
-
export function oneOf<T: Scalar>(constants: $ReadOnlyArray<T>): Decoder<T> {
|
|
137
|
-
return (blob: mixed) => {
|
|
138
|
-
const winner = constants.find((c) => c === blob);
|
|
139
|
-
if (winner !== undefined) {
|
|
140
|
-
return ok(winner);
|
|
141
|
-
}
|
|
142
|
-
return err(
|
|
143
|
-
annotate(
|
|
144
|
-
blob,
|
|
145
|
-
`Must be one of ${constants
|
|
146
|
-
.map((value) => JSON.stringify(value))
|
|
147
|
-
.join(', ')}`,
|
|
148
|
-
),
|
|
149
|
-
);
|
|
150
|
-
};
|
|
151
|
-
}
|
package/core/either.mjs
DELETED
|
@@ -1,90 +0,0 @@
|
|
|
1
|
-
import { annotate } from '../annotate.mjs';
|
|
2
|
-
import { err, ok, orElse } from '../result.mjs';
|
|
3
|
-
import { indent, summarize } from '../_utils.mjs';
|
|
4
|
-
|
|
5
|
-
/**
|
|
6
|
-
* Indents and adds a dash in front of this (potentially multiline) string.
|
|
7
|
-
*/
|
|
8
|
-
function itemize(s) {
|
|
9
|
-
return '-' + indent(s).substring(1);
|
|
10
|
-
}
|
|
11
|
-
/**
|
|
12
|
-
* Nests another error as an item under a new-to-be-created "Either error". If
|
|
13
|
-
* the given subitem already is an "Either error" of itself, don't indent, but
|
|
14
|
-
* just "inject" its items at the same error level, for nicely flattened either
|
|
15
|
-
* expressions.
|
|
16
|
-
*
|
|
17
|
-
* Avoids:
|
|
18
|
-
*
|
|
19
|
-
* Either:
|
|
20
|
-
* - Either:
|
|
21
|
-
* - Must be P
|
|
22
|
-
* - Either:
|
|
23
|
-
* - Must be Q
|
|
24
|
-
* - Must be R
|
|
25
|
-
* - Must be S
|
|
26
|
-
*
|
|
27
|
-
* And "flattens" these to:
|
|
28
|
-
*
|
|
29
|
-
* Either:
|
|
30
|
-
* - Must be P
|
|
31
|
-
* - Must be Q
|
|
32
|
-
* - Must be R
|
|
33
|
-
* - Must be S
|
|
34
|
-
*
|
|
35
|
-
*/
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
function nest(errText) {
|
|
39
|
-
var EITHER_PREFIX = 'Either:\n';
|
|
40
|
-
return errText.startsWith(EITHER_PREFIX) ? errText.substr(EITHER_PREFIX.length) : itemize(errText);
|
|
41
|
-
}
|
|
42
|
-
|
|
43
|
-
export function either(d1, d2) {
|
|
44
|
-
return function (blob) {
|
|
45
|
-
return orElse(d1(blob), function (err1) {
|
|
46
|
-
return orElse(d2(blob), function (err2) {
|
|
47
|
-
var serr1 = summarize(err1).join('\n');
|
|
48
|
-
var serr2 = summarize(err2).join('\n');
|
|
49
|
-
var text = ['Either:', nest(serr1), nest(serr2)].join('\n');
|
|
50
|
-
return err(annotate(blob, text));
|
|
51
|
-
});
|
|
52
|
-
});
|
|
53
|
-
};
|
|
54
|
-
}
|
|
55
|
-
export function either3(d1, d2, d3) {
|
|
56
|
-
return either(d1, either(d2, d3));
|
|
57
|
-
}
|
|
58
|
-
export function either4(d1, d2, d3, d4) {
|
|
59
|
-
return either(d1, either3(d2, d3, d4));
|
|
60
|
-
}
|
|
61
|
-
export function either5(d1, d2, d3, d4, d5) {
|
|
62
|
-
return either(d1, either4(d2, d3, d4, d5));
|
|
63
|
-
}
|
|
64
|
-
export function either6(d1, d2, d3, d4, d5, d6) {
|
|
65
|
-
return either(d1, either5(d2, d3, d4, d5, d6));
|
|
66
|
-
}
|
|
67
|
-
export function either7(d1, d2, d3, d4, d5, d6, d7) {
|
|
68
|
-
return either(d1, either6(d2, d3, d4, d5, d6, d7));
|
|
69
|
-
}
|
|
70
|
-
export function either8(d1, d2, d3, d4, d5, d6, d7, d8) {
|
|
71
|
-
return either(d1, either7(d2, d3, d4, d5, d6, d7, d8));
|
|
72
|
-
}
|
|
73
|
-
export function either9(d1, d2, d3, d4, d5, d6, d7, d8, d9) {
|
|
74
|
-
return either(d1, either8(d2, d3, d4, d5, d6, d7, d8, d9));
|
|
75
|
-
}
|
|
76
|
-
export function oneOf(constants) {
|
|
77
|
-
return function (blob) {
|
|
78
|
-
var winner = constants.find(function (c) {
|
|
79
|
-
return c === blob;
|
|
80
|
-
});
|
|
81
|
-
|
|
82
|
-
if (winner !== undefined) {
|
|
83
|
-
return ok(winner);
|
|
84
|
-
}
|
|
85
|
-
|
|
86
|
-
return err(annotate(blob, "Must be one of " + constants.map(function (value) {
|
|
87
|
-
return JSON.stringify(value);
|
|
88
|
-
}).join(', ')));
|
|
89
|
-
};
|
|
90
|
-
}
|
package/core/fail.d.ts
DELETED
package/core/fail.js
DELETED
|
@@ -1,17 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
|
|
3
|
-
exports.__esModule = true;
|
|
4
|
-
exports.fail = fail;
|
|
5
|
-
|
|
6
|
-
var _annotate = require("../annotate");
|
|
7
|
-
|
|
8
|
-
var _result = require("../result");
|
|
9
|
-
|
|
10
|
-
/**
|
|
11
|
-
* Decoder that always fails with the given error message, no matter what the input.
|
|
12
|
-
*/
|
|
13
|
-
function fail(msg) {
|
|
14
|
-
return function (blob) {
|
|
15
|
-
return (0, _result.err)((0, _annotate.annotate)(blob, msg));
|
|
16
|
-
};
|
|
17
|
-
}
|
package/core/fail.js.flow
DELETED
|
@@ -1,12 +0,0 @@
|
|
|
1
|
-
// @flow strict
|
|
2
|
-
|
|
3
|
-
import { annotate } from '../annotate';
|
|
4
|
-
import { err } from '../result';
|
|
5
|
-
import type { Decoder } from '../_types';
|
|
6
|
-
|
|
7
|
-
/**
|
|
8
|
-
* Decoder that always fails with the given error message, no matter what the input.
|
|
9
|
-
*/
|
|
10
|
-
export function fail(msg: string): Decoder<empty> {
|
|
11
|
-
return (blob: mixed) => err(annotate(blob, msg));
|
|
12
|
-
}
|
package/core/fail.mjs
DELETED
|
@@ -1,11 +0,0 @@
|
|
|
1
|
-
import { annotate } from '../annotate.mjs';
|
|
2
|
-
import { err } from '../result.mjs';
|
|
3
|
-
|
|
4
|
-
/**
|
|
5
|
-
* Decoder that always fails with the given error message, no matter what the input.
|
|
6
|
-
*/
|
|
7
|
-
export function fail(msg) {
|
|
8
|
-
return function (blob) {
|
|
9
|
-
return err(annotate(blob, msg));
|
|
10
|
-
};
|
|
11
|
-
}
|
package/core/instanceOf.d.ts
DELETED
package/core/instanceOf.js
DELETED
|
@@ -1,15 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
|
|
3
|
-
exports.__esModule = true;
|
|
4
|
-
exports.instanceOf = instanceOf;
|
|
5
|
-
|
|
6
|
-
var _annotate = require("../annotate");
|
|
7
|
-
|
|
8
|
-
var _result = require("../result");
|
|
9
|
-
|
|
10
|
-
function instanceOf(klass) {
|
|
11
|
-
return function (blob) {
|
|
12
|
-
return blob instanceof klass ? (0, _result.ok)(blob) : (0, _result.err)((0, _annotate.annotate)(blob, "Must be " + // $FlowFixMe[incompatible-use] - klass.name is fine?
|
|
13
|
-
klass.name + " instance"));
|
|
14
|
-
};
|
|
15
|
-
}
|
package/core/instanceOf.js.flow
DELETED
|
@@ -1,20 +0,0 @@
|
|
|
1
|
-
// @flow strict
|
|
2
|
-
|
|
3
|
-
import { annotate } from '../annotate';
|
|
4
|
-
import { err, ok } from '../result';
|
|
5
|
-
import type { Decoder } from '../_types';
|
|
6
|
-
|
|
7
|
-
export function instanceOf<T>(klass: Class<T>): Decoder<T> {
|
|
8
|
-
return (blob: mixed) =>
|
|
9
|
-
blob instanceof klass
|
|
10
|
-
? ok(blob)
|
|
11
|
-
: err(
|
|
12
|
-
annotate(
|
|
13
|
-
blob,
|
|
14
|
-
`Must be ${
|
|
15
|
-
// $FlowFixMe[incompatible-use] - klass.name is fine?
|
|
16
|
-
klass.name
|
|
17
|
-
} instance`,
|
|
18
|
-
),
|
|
19
|
-
);
|
|
20
|
-
}
|
package/core/instanceOf.mjs
DELETED
|
@@ -1,8 +0,0 @@
|
|
|
1
|
-
import { annotate } from '../annotate.mjs';
|
|
2
|
-
import { err, ok } from '../result.mjs';
|
|
3
|
-
export function instanceOf(klass) {
|
|
4
|
-
return function (blob) {
|
|
5
|
-
return blob instanceof klass ? ok(blob) : err(annotate(blob, "Must be " + // $FlowFixMe[incompatible-use] - klass.name is fine?
|
|
6
|
-
klass.name + " instance"));
|
|
7
|
-
};
|
|
8
|
-
}
|
package/core/json.d.ts
DELETED
|
@@ -1,11 +0,0 @@
|
|
|
1
|
-
import { Decoder } from '../_types';
|
|
2
|
-
|
|
3
|
-
export type JSONValue = null | string | number | boolean | JSONObject | JSONArray;
|
|
4
|
-
export interface JSONObject {
|
|
5
|
-
[key: string]: JSONValue;
|
|
6
|
-
}
|
|
7
|
-
export type JSONArray = JSONValue[];
|
|
8
|
-
|
|
9
|
-
export const json: Decoder<JSONValue>;
|
|
10
|
-
export const jsonArray: Decoder<JSONArray>;
|
|
11
|
-
export const jsonObject: Decoder<JSONObject>;
|
package/core/json.js
DELETED
|
@@ -1,31 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
|
|
3
|
-
exports.__esModule = true;
|
|
4
|
-
exports.jsonObject = exports.jsonArray = exports.json = void 0;
|
|
5
|
-
|
|
6
|
-
var _array = require("./array");
|
|
7
|
-
|
|
8
|
-
var _boolean2 = require("./boolean");
|
|
9
|
-
|
|
10
|
-
var _mapping = require("./mapping");
|
|
11
|
-
|
|
12
|
-
var _either = require("./either");
|
|
13
|
-
|
|
14
|
-
var _lazy = require("./lazy");
|
|
15
|
-
|
|
16
|
-
var _constants = require("./constants");
|
|
17
|
-
|
|
18
|
-
var _number = require("./number");
|
|
19
|
-
|
|
20
|
-
var _string = require("./string");
|
|
21
|
-
|
|
22
|
-
var jsonObject = (0, _lazy.lazy)(function () {
|
|
23
|
-
return (0, _mapping.dict)(json);
|
|
24
|
-
});
|
|
25
|
-
exports.jsonObject = jsonObject;
|
|
26
|
-
var jsonArray = (0, _lazy.lazy)(function () {
|
|
27
|
-
return (0, _array.array)(json);
|
|
28
|
-
});
|
|
29
|
-
exports.jsonArray = jsonArray;
|
|
30
|
-
var json = (0, _either.either6)(_constants.null_, _string.string, _number.number, _boolean2["boolean"], jsonObject, jsonArray);
|
|
31
|
-
exports.json = json;
|
package/core/json.js.flow
DELETED
|
@@ -1,28 +0,0 @@
|
|
|
1
|
-
// @flow strict
|
|
2
|
-
|
|
3
|
-
import { array } from './array';
|
|
4
|
-
import { boolean } from './boolean';
|
|
5
|
-
import { dict } from './mapping';
|
|
6
|
-
import { either6 } from './either';
|
|
7
|
-
import { lazy } from './lazy';
|
|
8
|
-
import { null_ } from './constants';
|
|
9
|
-
import { number } from './number';
|
|
10
|
-
import { string } from './string';
|
|
11
|
-
import type { Decoder } from '../_types';
|
|
12
|
-
|
|
13
|
-
export type JSONValue = null | string | number | boolean | JSONObject | JSONArray;
|
|
14
|
-
export type JSONObject = { [string]: JSONValue };
|
|
15
|
-
export type JSONArray = Array<JSONValue>;
|
|
16
|
-
|
|
17
|
-
export const jsonObject: Decoder<JSONObject> = lazy(() => dict(json));
|
|
18
|
-
|
|
19
|
-
export const jsonArray: Decoder<JSONArray> = lazy(() => array(json));
|
|
20
|
-
|
|
21
|
-
export const json: Decoder<JSONValue> = either6(
|
|
22
|
-
null_,
|
|
23
|
-
string,
|
|
24
|
-
number,
|
|
25
|
-
boolean,
|
|
26
|
-
jsonObject,
|
|
27
|
-
jsonArray,
|
|
28
|
-
);
|
package/core/json.mjs
DELETED
|
@@ -1,15 +0,0 @@
|
|
|
1
|
-
import { array } from './array.mjs';
|
|
2
|
-
import { boolean as _boolean } from './boolean.mjs';
|
|
3
|
-
import { dict } from './mapping.mjs';
|
|
4
|
-
import { either6 } from './either.mjs';
|
|
5
|
-
import { lazy } from './lazy.mjs';
|
|
6
|
-
import { null_ } from './constants.mjs';
|
|
7
|
-
import { number } from './number.mjs';
|
|
8
|
-
import { string } from './string.mjs';
|
|
9
|
-
export var jsonObject = lazy(function () {
|
|
10
|
-
return dict(json);
|
|
11
|
-
});
|
|
12
|
-
export var jsonArray = lazy(function () {
|
|
13
|
-
return array(json);
|
|
14
|
-
});
|
|
15
|
-
export var json = either6(null_, string, number, _boolean, jsonObject, jsonArray);
|
package/core/lazy.d.ts
DELETED
package/core/lazy.js
DELETED
|
@@ -1,16 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
|
|
3
|
-
exports.__esModule = true;
|
|
4
|
-
exports.lazy = lazy;
|
|
5
|
-
|
|
6
|
-
/**
|
|
7
|
-
* Given an function returning a Decoder, will use that decoder to decode the
|
|
8
|
-
* value. This is typically used to build decoders for recursive or
|
|
9
|
-
* self-referential types.
|
|
10
|
-
*/
|
|
11
|
-
function lazy(decoderFn) {
|
|
12
|
-
return function (blob) {
|
|
13
|
-
var decoder = decoderFn();
|
|
14
|
-
return decoder(blob);
|
|
15
|
-
};
|
|
16
|
-
}
|
package/core/lazy.js.flow
DELETED
|
@@ -1,15 +0,0 @@
|
|
|
1
|
-
// @flow strict
|
|
2
|
-
|
|
3
|
-
import type { Decoder } from '../_types';
|
|
4
|
-
|
|
5
|
-
/**
|
|
6
|
-
* Given an function returning a Decoder, will use that decoder to decode the
|
|
7
|
-
* value. This is typically used to build decoders for recursive or
|
|
8
|
-
* self-referential types.
|
|
9
|
-
*/
|
|
10
|
-
export function lazy<T>(decoderFn: () => Decoder<T>): Decoder<T> {
|
|
11
|
-
return (blob: mixed) => {
|
|
12
|
-
const decoder = decoderFn();
|
|
13
|
-
return decoder(blob);
|
|
14
|
-
};
|
|
15
|
-
}
|
package/core/lazy.mjs
DELETED
|
@@ -1,11 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Given an function returning a Decoder, will use that decoder to decode the
|
|
3
|
-
* value. This is typically used to build decoders for recursive or
|
|
4
|
-
* self-referential types.
|
|
5
|
-
*/
|
|
6
|
-
export function lazy(decoderFn) {
|
|
7
|
-
return function (blob) {
|
|
8
|
-
var decoder = decoderFn();
|
|
9
|
-
return decoder(blob);
|
|
10
|
-
};
|
|
11
|
-
}
|
package/core/mapping.d.ts
DELETED
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 _annotate = require("../annotate");
|
|
8
|
-
|
|
9
|
-
var _composition = require("./composition");
|
|
10
|
-
|
|
11
|
-
var _result = require("../result");
|
|
12
|
-
|
|
13
|
-
var _object = require("./object");
|
|
14
|
-
|
|
15
|
-
/**
|
|
16
|
-
* Like mapping(), but returns an object rather than a Map instance.
|
|
17
|
-
*/
|
|
18
|
-
function dict(decoder) {
|
|
19
|
-
return (0, _composition.compose)(_object.pojo, function (blob) {
|
|
20
|
-
var rv = {};
|
|
21
|
-
var errors = null;
|
|
22
|
-
Object.keys(blob).forEach(function (key) {
|
|
23
|
-
var value = blob[key];
|
|
24
|
-
var result = decoder(value);
|
|
25
|
-
|
|
26
|
-
if (result.type === 'ok') {
|
|
27
|
-
if (errors === null) {
|
|
28
|
-
rv[key] = result.value;
|
|
29
|
-
}
|
|
30
|
-
} else {
|
|
31
|
-
rv = {}; // Clear the success value so it can get garbage collected early
|
|
32
|
-
|
|
33
|
-
if (errors === null) {
|
|
34
|
-
errors = {};
|
|
35
|
-
}
|
|
36
|
-
|
|
37
|
-
errors[key] = result.error;
|
|
38
|
-
}
|
|
39
|
-
});
|
|
40
|
-
|
|
41
|
-
if (errors !== null) {
|
|
42
|
-
return (0, _result.err)((0, _annotate.merge)((0, _annotate.annotateObject)(blob), errors));
|
|
43
|
-
} else {
|
|
44
|
-
return (0, _result.ok)(rv);
|
|
45
|
-
}
|
|
46
|
-
});
|
|
47
|
-
}
|
|
48
|
-
/**
|
|
49
|
-
* Given an object, will decode a Map of string keys to whatever values.
|
|
50
|
-
*
|
|
51
|
-
* For example, given a decoder for a Person, we can verify a Person lookup
|
|
52
|
-
* table structure (of type Map<string, Person>) like so:
|
|
53
|
-
*
|
|
54
|
-
* mapping(person)
|
|
55
|
-
*
|
|
56
|
-
*/
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
function mapping(decoder) {
|
|
60
|
-
return (0, _composition.map)(dict(decoder), function (obj) {
|
|
61
|
-
return new Map( // This is effectively Object.entries(obj), but in a way that Flow
|
|
62
|
-
// will know the types are okay
|
|
63
|
-
Object.keys(obj).map(function (key) {
|
|
64
|
-
return [key, obj[key]];
|
|
65
|
-
}));
|
|
66
|
-
});
|
|
67
|
-
}
|
package/core/mapping.js.flow
DELETED
|
@@ -1,62 +0,0 @@
|
|
|
1
|
-
// @flow strict
|
|
2
|
-
|
|
3
|
-
import { annotateObject } from '../annotate';
|
|
4
|
-
import { compose, map } from './composition';
|
|
5
|
-
import { err, ok } from '../result';
|
|
6
|
-
import { merge } from '../annotate';
|
|
7
|
-
import { pojo } from './object';
|
|
8
|
-
import type { Annotation } from '../annotate';
|
|
9
|
-
import type { Decoder } from '../_types';
|
|
10
|
-
|
|
11
|
-
/**
|
|
12
|
-
* Like mapping(), but returns an object rather than a Map instance.
|
|
13
|
-
*/
|
|
14
|
-
export function dict<T>(decoder: Decoder<T>): Decoder<{ [string]: T }> {
|
|
15
|
-
return compose(pojo, (blob: { +[key: string]: mixed }) => {
|
|
16
|
-
let rv: { [key: string]: T } = {};
|
|
17
|
-
let errors: { [key: string]: Annotation } | null = null;
|
|
18
|
-
|
|
19
|
-
Object.keys(blob).forEach((key: string) => {
|
|
20
|
-
const value = blob[key];
|
|
21
|
-
const result = decoder(value);
|
|
22
|
-
if (result.type === 'ok') {
|
|
23
|
-
if (errors === null) {
|
|
24
|
-
rv[key] = result.value;
|
|
25
|
-
}
|
|
26
|
-
} else {
|
|
27
|
-
rv = {}; // Clear the success value so it can get garbage collected early
|
|
28
|
-
if (errors === null) {
|
|
29
|
-
errors = {};
|
|
30
|
-
}
|
|
31
|
-
errors[key] = result.error;
|
|
32
|
-
}
|
|
33
|
-
});
|
|
34
|
-
|
|
35
|
-
if (errors !== null) {
|
|
36
|
-
return err(merge(annotateObject(blob), errors));
|
|
37
|
-
} else {
|
|
38
|
-
return ok(rv);
|
|
39
|
-
}
|
|
40
|
-
});
|
|
41
|
-
}
|
|
42
|
-
|
|
43
|
-
/**
|
|
44
|
-
* Given an object, will decode a Map of string keys to whatever values.
|
|
45
|
-
*
|
|
46
|
-
* For example, given a decoder for a Person, we can verify a Person lookup
|
|
47
|
-
* table structure (of type Map<string, Person>) like so:
|
|
48
|
-
*
|
|
49
|
-
* mapping(person)
|
|
50
|
-
*
|
|
51
|
-
*/
|
|
52
|
-
export function mapping<T>(decoder: Decoder<T>): Decoder<Map<string, T>> {
|
|
53
|
-
return map(
|
|
54
|
-
dict(decoder),
|
|
55
|
-
(obj) =>
|
|
56
|
-
new Map(
|
|
57
|
-
// This is effectively Object.entries(obj), but in a way that Flow
|
|
58
|
-
// will know the types are okay
|
|
59
|
-
Object.keys(obj).map((key) => [key, obj[key]]),
|
|
60
|
-
),
|
|
61
|
-
);
|
|
62
|
-
}
|
package/core/mapping.mjs
DELETED
|
@@ -1,58 +0,0 @@
|
|
|
1
|
-
import { annotateObject } from '../annotate.mjs';
|
|
2
|
-
import { compose, map } from './composition.mjs';
|
|
3
|
-
import { err, ok } from '../result.mjs';
|
|
4
|
-
import { merge } from '../annotate.mjs';
|
|
5
|
-
import { pojo } from './object.mjs';
|
|
6
|
-
|
|
7
|
-
/**
|
|
8
|
-
* Like mapping(), but returns an object rather than a Map instance.
|
|
9
|
-
*/
|
|
10
|
-
export function dict(decoder) {
|
|
11
|
-
return compose(pojo, function (blob) {
|
|
12
|
-
var rv = {};
|
|
13
|
-
var errors = null;
|
|
14
|
-
Object.keys(blob).forEach(function (key) {
|
|
15
|
-
var value = blob[key];
|
|
16
|
-
var result = decoder(value);
|
|
17
|
-
|
|
18
|
-
if (result.type === 'ok') {
|
|
19
|
-
if (errors === null) {
|
|
20
|
-
rv[key] = result.value;
|
|
21
|
-
}
|
|
22
|
-
} else {
|
|
23
|
-
rv = {}; // Clear the success value so it can get garbage collected early
|
|
24
|
-
|
|
25
|
-
if (errors === null) {
|
|
26
|
-
errors = {};
|
|
27
|
-
}
|
|
28
|
-
|
|
29
|
-
errors[key] = result.error;
|
|
30
|
-
}
|
|
31
|
-
});
|
|
32
|
-
|
|
33
|
-
if (errors !== null) {
|
|
34
|
-
return err(merge(annotateObject(blob), errors));
|
|
35
|
-
} else {
|
|
36
|
-
return ok(rv);
|
|
37
|
-
}
|
|
38
|
-
});
|
|
39
|
-
}
|
|
40
|
-
/**
|
|
41
|
-
* Given an object, will decode a Map of string keys to whatever values.
|
|
42
|
-
*
|
|
43
|
-
* For example, given a decoder for a Person, we can verify a Person lookup
|
|
44
|
-
* table structure (of type Map<string, Person>) like so:
|
|
45
|
-
*
|
|
46
|
-
* mapping(person)
|
|
47
|
-
*
|
|
48
|
-
*/
|
|
49
|
-
|
|
50
|
-
export function mapping(decoder) {
|
|
51
|
-
return map(dict(decoder), function (obj) {
|
|
52
|
-
return new Map( // This is effectively Object.entries(obj), but in a way that Flow
|
|
53
|
-
// will know the types are okay
|
|
54
|
-
Object.keys(obj).map(function (key) {
|
|
55
|
-
return [key, obj[key]];
|
|
56
|
-
}));
|
|
57
|
-
});
|
|
58
|
-
}
|