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
package/result.js.flow
CHANGED
|
@@ -6,8 +6,8 @@
|
|
|
6
6
|
* | Err <error>
|
|
7
7
|
*/
|
|
8
8
|
|
|
9
|
-
type Ok<+T> = {| +type: 'ok', +value: T |};
|
|
10
|
-
type Err<+E> = {| +type: 'err', +error: E |};
|
|
9
|
+
type Ok<+T> = {| +type: 'ok', +value: T, +error: void |};
|
|
10
|
+
type Err<+E> = {| +type: 'err', +value: void, +error: E |};
|
|
11
11
|
|
|
12
12
|
export type Result<+T, +E> = Ok<T> | Err<E>;
|
|
13
13
|
|
|
@@ -15,40 +15,14 @@ export type Result<+T, +E> = Ok<T> | Err<E>;
|
|
|
15
15
|
* Create a new Result instance representing a successful computation.
|
|
16
16
|
*/
|
|
17
17
|
export function ok<T>(value: T): Ok<T> {
|
|
18
|
-
return { type: 'ok', value };
|
|
18
|
+
return { type: 'ok', value, error: undefined };
|
|
19
19
|
}
|
|
20
20
|
|
|
21
21
|
/**
|
|
22
22
|
* Create a new Result instance representing a failed computation.
|
|
23
23
|
*/
|
|
24
24
|
export function err<E>(error: E): Err<E> {
|
|
25
|
-
return { type: 'err', error };
|
|
26
|
-
}
|
|
27
|
-
|
|
28
|
-
export function toString(result: Result<mixed, mixed>): string {
|
|
29
|
-
return result.type === 'ok'
|
|
30
|
-
? `Ok(${String(result.value)})`
|
|
31
|
-
: `Err(${String(result.error)})`;
|
|
32
|
-
}
|
|
33
|
-
|
|
34
|
-
export function isOk(result: Result<mixed, mixed>): boolean {
|
|
35
|
-
return result.type === 'ok';
|
|
36
|
-
}
|
|
37
|
-
|
|
38
|
-
export function isErr(result: Result<mixed, mixed>): boolean {
|
|
39
|
-
return result.type === 'err';
|
|
40
|
-
}
|
|
41
|
-
|
|
42
|
-
export function withDefault<T>(result: Result<T, mixed>, defaultValue: T): T {
|
|
43
|
-
return result.type === 'ok' ? result.value : defaultValue;
|
|
44
|
-
}
|
|
45
|
-
|
|
46
|
-
export function value<T>(result: Result<T, mixed>): void | T {
|
|
47
|
-
return result.type === 'ok' ? result.value : undefined;
|
|
48
|
-
}
|
|
49
|
-
|
|
50
|
-
export function errValue<E>(result: Result<mixed, E>): void | E {
|
|
51
|
-
return result.type === 'err' ? result.error : undefined;
|
|
25
|
+
return { type: 'err', value: undefined, error };
|
|
52
26
|
}
|
|
53
27
|
|
|
54
28
|
/**
|
|
@@ -79,48 +53,6 @@ export function dispatch<T, E, O>(
|
|
|
79
53
|
return result.type === 'ok' ? okCallback(result.value) : errCallback(result.error);
|
|
80
54
|
}
|
|
81
55
|
|
|
82
|
-
/**
|
|
83
|
-
* If the given result is OK, defers to the other result. Otherwise returns the
|
|
84
|
-
* error result.
|
|
85
|
-
*
|
|
86
|
-
* It's like saying A && B, but on Result.
|
|
87
|
-
*
|
|
88
|
-
* Examples:
|
|
89
|
-
*
|
|
90
|
-
* Result.ok(42) && Result.ok('hi') // => Ok('hi')
|
|
91
|
-
* Result.err('boo') && Result.ok('hi') // => Err('boo')
|
|
92
|
-
* Result.ok(42) && Result.err('boo') // => Err('boo')
|
|
93
|
-
* Result.err('boo') && Result.err('boo') // => Err('boo')
|
|
94
|
-
*
|
|
95
|
-
*/
|
|
96
|
-
// export function and<T, E, T2>(
|
|
97
|
-
// result1: Result<T, E>,
|
|
98
|
-
// result2: Result<T2, E>,
|
|
99
|
-
// ): Result<T2, E> {
|
|
100
|
-
// return result1.type === 'ok' ? result2 : result1;
|
|
101
|
-
// }
|
|
102
|
-
|
|
103
|
-
/**
|
|
104
|
-
* If the given result is OK, return that result. Otherwise, defers to the
|
|
105
|
-
* other result.
|
|
106
|
-
*
|
|
107
|
-
* It's like saying A || B, but on Result.
|
|
108
|
-
*
|
|
109
|
-
* Examples:
|
|
110
|
-
*
|
|
111
|
-
* Result.ok(42) || Result.ok('hi') // => Ok(42)
|
|
112
|
-
* Result.err('boo') || Result.ok('hi') // => Ok('hi')
|
|
113
|
-
* Result.ok(42) || Result.err('boo') // => Ok(42)
|
|
114
|
-
* Result.err('bleh') || Result.err('boo') // => Err('boo')
|
|
115
|
-
*
|
|
116
|
-
*/
|
|
117
|
-
// export function or<T, E, E2>(
|
|
118
|
-
// result1: Result<T, E>,
|
|
119
|
-
// result2: Result<T, E2>,
|
|
120
|
-
// ): Result<T, E2> {
|
|
121
|
-
// return result1.type === 'ok' ? result1 : result2;
|
|
122
|
-
// }
|
|
123
|
-
|
|
124
56
|
/**
|
|
125
57
|
* Like .and(), aka &&, but the second argument gets evaluated lazily only if
|
|
126
58
|
* the first result is an Ok result. If so, it has access to the Ok value from
|
|
@@ -148,7 +80,7 @@ export function orElse<T, E, E2>(
|
|
|
148
80
|
/**
|
|
149
81
|
* Transform an Ok result. Will not touch Err results.
|
|
150
82
|
*/
|
|
151
|
-
export function
|
|
83
|
+
export function mapOk<T, E, T2>(
|
|
152
84
|
result: Result<T, E>,
|
|
153
85
|
mapper: (value: T) => T2,
|
|
154
86
|
): Result<T2, E> {
|
|
@@ -10,7 +10,8 @@
|
|
|
10
10
|
export function ok(value) {
|
|
11
11
|
return {
|
|
12
12
|
type: 'ok',
|
|
13
|
-
value: value
|
|
13
|
+
value: value,
|
|
14
|
+
error: undefined
|
|
14
15
|
};
|
|
15
16
|
}
|
|
16
17
|
/**
|
|
@@ -20,27 +21,10 @@ export function ok(value) {
|
|
|
20
21
|
export function err(error) {
|
|
21
22
|
return {
|
|
22
23
|
type: 'err',
|
|
24
|
+
value: undefined,
|
|
23
25
|
error: error
|
|
24
26
|
};
|
|
25
27
|
}
|
|
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
28
|
/**
|
|
45
29
|
* Unwrap the value from this Result instance if this is an "Ok" result.
|
|
46
30
|
* Otherwise, will throw the "Err" error via a runtime exception.
|
|
@@ -63,48 +47,6 @@ export function expect(result, message) {
|
|
|
63
47
|
export function dispatch(result, okCallback, errCallback) {
|
|
64
48
|
return result.type === 'ok' ? okCallback(result.value) : errCallback(result.error);
|
|
65
49
|
}
|
|
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
50
|
/**
|
|
109
51
|
* Like .and(), aka &&, but the second argument gets evaluated lazily only if
|
|
110
52
|
* the first result is an Ok result. If so, it has access to the Ok value from
|
|
@@ -127,7 +69,7 @@ export function orElse(result1, lazyResult2) {
|
|
|
127
69
|
* Transform an Ok result. Will not touch Err results.
|
|
128
70
|
*/
|
|
129
71
|
|
|
130
|
-
export function
|
|
72
|
+
export function mapOk(result, mapper) {
|
|
131
73
|
return result.type === 'ok' ? ok(mapper(result.value)) : result;
|
|
132
74
|
}
|
|
133
75
|
/**
|
package/_esm/_guard.js.flow
DELETED
|
@@ -1,20 +0,0 @@
|
|
|
1
|
-
// @flow strict
|
|
2
|
-
|
|
3
|
-
import * as Result from './result';
|
|
4
|
-
import { formatInline } from './format';
|
|
5
|
-
import type { Annotation } from './annotate';
|
|
6
|
-
import type { Decoder, Guard } from './_types';
|
|
7
|
-
|
|
8
|
-
export function guard<T>(
|
|
9
|
-
decoder: Decoder<T>,
|
|
10
|
-
formatter: (Annotation) => string = formatInline,
|
|
11
|
-
): Guard<T> {
|
|
12
|
-
return (blob: mixed) =>
|
|
13
|
-
Result.unwrap(
|
|
14
|
-
Result.mapError(decoder(blob), (annotation) => {
|
|
15
|
-
const err = new Error('\n' + formatter(annotation));
|
|
16
|
-
err.name = 'Decoding error';
|
|
17
|
-
return err;
|
|
18
|
-
}),
|
|
19
|
-
);
|
|
20
|
-
}
|
package/_esm/_types.js.flow
DELETED
|
@@ -1,20 +0,0 @@
|
|
|
1
|
-
// @flow strict
|
|
2
|
-
|
|
3
|
-
import type { Annotation } from './annotate';
|
|
4
|
-
import type { Result } from './result';
|
|
5
|
-
|
|
6
|
-
export type Scalar = string | number | boolean | symbol | void | null;
|
|
7
|
-
|
|
8
|
-
export type Predicate<T> = (T) => boolean;
|
|
9
|
-
export type DecodeResult<T> = Result<T, Annotation>;
|
|
10
|
-
|
|
11
|
-
export type Decoder<T, F = mixed> = (F) => DecodeResult<T>;
|
|
12
|
-
export type Guard<T> = (mixed) => T;
|
|
13
|
-
|
|
14
|
-
/**
|
|
15
|
-
* A "type function" which informs Flow about how a type will be modified at runtime.
|
|
16
|
-
* Read this as "given a Guard of type T, I can produce a value of type T". This
|
|
17
|
-
* definition helps construct $ObjMap types.
|
|
18
|
-
*/
|
|
19
|
-
export type DecoderType = <T>(Decoder<T>) => T;
|
|
20
|
-
export type GuardType = <T>(Guard<T>) => T;
|
package/_esm/_utils.js.flow
DELETED
|
@@ -1,97 +0,0 @@
|
|
|
1
|
-
// @flow strict
|
|
2
|
-
|
|
3
|
-
import type { Annotation } from './annotate';
|
|
4
|
-
|
|
5
|
-
// $FlowFixMe[unclear-type] - deliberate casting
|
|
6
|
-
type cast = any;
|
|
7
|
-
|
|
8
|
-
// Two spaces of indentation
|
|
9
|
-
export const INDENT = ' ';
|
|
10
|
-
|
|
11
|
-
/**
|
|
12
|
-
* `x instanceof Date` checks are unreliable across stack frames (that information
|
|
13
|
-
* might get lost by the JS runtime), so we'll have to reside to more runtime
|
|
14
|
-
* inspection checks.
|
|
15
|
-
*
|
|
16
|
-
* Taken from https://stackoverflow.com/a/44198641
|
|
17
|
-
*/
|
|
18
|
-
export function isDate(value: mixed): boolean {
|
|
19
|
-
return (
|
|
20
|
-
!!value &&
|
|
21
|
-
// $FlowFixMe[method-unbinding]
|
|
22
|
-
Object.prototype.toString.call(value) === '[object Date]' &&
|
|
23
|
-
!isNaN(value)
|
|
24
|
-
);
|
|
25
|
-
}
|
|
26
|
-
|
|
27
|
-
/**
|
|
28
|
-
* Is value is a valid Date instance, then return that. If not, then return
|
|
29
|
-
* null.
|
|
30
|
-
*/
|
|
31
|
-
export function asDate(value: mixed): Date | null {
|
|
32
|
-
return isDate(value) ? ((value: cast): Date) : null;
|
|
33
|
-
}
|
|
34
|
-
|
|
35
|
-
export function isMultiline(s: string): boolean {
|
|
36
|
-
return s.indexOf('\n') >= 0;
|
|
37
|
-
}
|
|
38
|
-
|
|
39
|
-
export function indent(s: string, prefix: string = INDENT): string {
|
|
40
|
-
if (isMultiline(s)) {
|
|
41
|
-
return s
|
|
42
|
-
.split('\n')
|
|
43
|
-
.map((line) => prefix + line)
|
|
44
|
-
.join('\n');
|
|
45
|
-
} else {
|
|
46
|
-
return prefix + s;
|
|
47
|
-
}
|
|
48
|
-
}
|
|
49
|
-
|
|
50
|
-
/**
|
|
51
|
-
* Walks the annotation tree and emits the annotation's key path within the
|
|
52
|
-
* object tree, and the message as a series of messages (array of strings).
|
|
53
|
-
*/
|
|
54
|
-
export function summarize(
|
|
55
|
-
ann: Annotation,
|
|
56
|
-
keypath: $ReadOnlyArray<number | string> = [],
|
|
57
|
-
): Array<string> {
|
|
58
|
-
const result: Array<string> = [];
|
|
59
|
-
|
|
60
|
-
if (ann.type === 'array') {
|
|
61
|
-
const items = ann.items;
|
|
62
|
-
let index = 0;
|
|
63
|
-
items.forEach((ann) => {
|
|
64
|
-
summarize(ann, [...keypath, index++]).forEach((item) =>
|
|
65
|
-
// Collect to results
|
|
66
|
-
result.push(item),
|
|
67
|
-
);
|
|
68
|
-
});
|
|
69
|
-
} else if (ann.type === 'object') {
|
|
70
|
-
const fields = ann.fields;
|
|
71
|
-
Object.keys(fields).forEach((key) => {
|
|
72
|
-
const value = fields[key];
|
|
73
|
-
summarize(value, [...keypath, key]).forEach((item) =>
|
|
74
|
-
// Collect to results
|
|
75
|
-
result.push(item),
|
|
76
|
-
);
|
|
77
|
-
});
|
|
78
|
-
}
|
|
79
|
-
|
|
80
|
-
const text = ann.text;
|
|
81
|
-
if (!text) {
|
|
82
|
-
return result;
|
|
83
|
-
}
|
|
84
|
-
|
|
85
|
-
let prefix: string;
|
|
86
|
-
if (keypath.length === 0) {
|
|
87
|
-
prefix = '';
|
|
88
|
-
} else if (keypath.length === 1) {
|
|
89
|
-
prefix =
|
|
90
|
-
typeof keypath[0] === 'number'
|
|
91
|
-
? `Value at index ${keypath[0]}: `
|
|
92
|
-
: `Value at key ${JSON.stringify(keypath[0])}: `;
|
|
93
|
-
} else {
|
|
94
|
-
prefix = `Value at keypath ${keypath.map((x) => x.toString()).join('.')}: `;
|
|
95
|
-
}
|
|
96
|
-
return [...result, prefix + text];
|
|
97
|
-
}
|
package/_esm/annotate.js.flow
DELETED
|
@@ -1,218 +0,0 @@
|
|
|
1
|
-
// @flow strict
|
|
2
|
-
|
|
3
|
-
type cast = $FlowFixMe;
|
|
4
|
-
|
|
5
|
-
const _register: WeakSet<{ ... }> = new WeakSet();
|
|
6
|
-
|
|
7
|
-
export type ObjectAnnotation = {|
|
|
8
|
-
+type: 'object',
|
|
9
|
-
+fields: { +[key: string]: Annotation },
|
|
10
|
-
+text?: string,
|
|
11
|
-
|};
|
|
12
|
-
|
|
13
|
-
export type ArrayAnnotation = {|
|
|
14
|
-
+type: 'array',
|
|
15
|
-
+items: $ReadOnlyArray<Annotation>,
|
|
16
|
-
+text?: string,
|
|
17
|
-
|};
|
|
18
|
-
|
|
19
|
-
export type ScalarAnnotation = {|
|
|
20
|
-
+type: 'scalar',
|
|
21
|
-
+value: mixed,
|
|
22
|
-
+text?: string,
|
|
23
|
-
|};
|
|
24
|
-
|
|
25
|
-
export type FunctionAnnotation = {|
|
|
26
|
-
+type: 'function',
|
|
27
|
-
+text?: string,
|
|
28
|
-
|};
|
|
29
|
-
|
|
30
|
-
export type CircularRefAnnotation = {|
|
|
31
|
-
+type: 'circular-ref',
|
|
32
|
-
+text?: string,
|
|
33
|
-
|};
|
|
34
|
-
|
|
35
|
-
export type UnknownAnnotation = {|
|
|
36
|
-
+type: 'unknown',
|
|
37
|
-
+value: mixed,
|
|
38
|
-
+text?: string,
|
|
39
|
-
|};
|
|
40
|
-
|
|
41
|
-
export type Annotation =
|
|
42
|
-
| ObjectAnnotation
|
|
43
|
-
| ArrayAnnotation
|
|
44
|
-
| ScalarAnnotation
|
|
45
|
-
| FunctionAnnotation
|
|
46
|
-
| CircularRefAnnotation
|
|
47
|
-
| UnknownAnnotation;
|
|
48
|
-
|
|
49
|
-
function brand<A: Annotation>(ann: A): A {
|
|
50
|
-
_register.add(ann);
|
|
51
|
-
return ann;
|
|
52
|
-
}
|
|
53
|
-
|
|
54
|
-
export function object(
|
|
55
|
-
fields: { +[key: string]: Annotation },
|
|
56
|
-
text?: string,
|
|
57
|
-
): ObjectAnnotation {
|
|
58
|
-
return brand({ type: 'object', fields, text });
|
|
59
|
-
}
|
|
60
|
-
|
|
61
|
-
export function array(items: $ReadOnlyArray<Annotation>, text?: string): ArrayAnnotation {
|
|
62
|
-
return brand({
|
|
63
|
-
type: 'array',
|
|
64
|
-
items,
|
|
65
|
-
text,
|
|
66
|
-
});
|
|
67
|
-
}
|
|
68
|
-
|
|
69
|
-
export function func(text?: string): FunctionAnnotation {
|
|
70
|
-
return brand({
|
|
71
|
-
type: 'function',
|
|
72
|
-
text,
|
|
73
|
-
});
|
|
74
|
-
}
|
|
75
|
-
|
|
76
|
-
export function unknown(value: mixed, text?: string): UnknownAnnotation {
|
|
77
|
-
return brand({
|
|
78
|
-
type: 'unknown',
|
|
79
|
-
value,
|
|
80
|
-
text,
|
|
81
|
-
});
|
|
82
|
-
}
|
|
83
|
-
|
|
84
|
-
export function scalar(value: mixed, text?: string): ScalarAnnotation {
|
|
85
|
-
return brand({
|
|
86
|
-
type: 'scalar',
|
|
87
|
-
value,
|
|
88
|
-
text,
|
|
89
|
-
});
|
|
90
|
-
}
|
|
91
|
-
|
|
92
|
-
export function circularRef(text?: string): CircularRefAnnotation {
|
|
93
|
-
return brand({
|
|
94
|
-
type: 'circular-ref',
|
|
95
|
-
text,
|
|
96
|
-
});
|
|
97
|
-
}
|
|
98
|
-
|
|
99
|
-
/**
|
|
100
|
-
* Given an existing Annotation, set the annotation's text to a new value.
|
|
101
|
-
*/
|
|
102
|
-
export function updateText<A: Annotation>(annotation: A, text?: string): A {
|
|
103
|
-
if (text !== undefined) {
|
|
104
|
-
return brand({ ...annotation, text });
|
|
105
|
-
} else {
|
|
106
|
-
return annotation;
|
|
107
|
-
}
|
|
108
|
-
}
|
|
109
|
-
|
|
110
|
-
/**
|
|
111
|
-
* Given an existing ObjectAnnotation, merges new Annotations in there.
|
|
112
|
-
*/
|
|
113
|
-
export function merge(
|
|
114
|
-
objAnnotation: ObjectAnnotation,
|
|
115
|
-
fields: { +[key: string]: Annotation },
|
|
116
|
-
): ObjectAnnotation {
|
|
117
|
-
const newFields = { ...objAnnotation.fields, ...fields };
|
|
118
|
-
return object(newFields, objAnnotation.text);
|
|
119
|
-
}
|
|
120
|
-
|
|
121
|
-
export function asAnnotation(thing: mixed): Annotation | void {
|
|
122
|
-
return typeof thing === 'object' && thing !== null && _register.has(thing)
|
|
123
|
-
? ((thing: cast): Annotation)
|
|
124
|
-
: undefined;
|
|
125
|
-
}
|
|
126
|
-
|
|
127
|
-
type RefSet = WeakSet<{ ... } | $ReadOnlyArray<mixed>>;
|
|
128
|
-
|
|
129
|
-
function annotateArray(
|
|
130
|
-
value: $ReadOnlyArray<mixed>,
|
|
131
|
-
text?: string,
|
|
132
|
-
seen: RefSet,
|
|
133
|
-
): ArrayAnnotation | CircularRefAnnotation {
|
|
134
|
-
seen.add(value);
|
|
135
|
-
|
|
136
|
-
const items = value.map((v) => annotate(v, undefined, seen));
|
|
137
|
-
return array(items, text);
|
|
138
|
-
}
|
|
139
|
-
|
|
140
|
-
function annotateObject(
|
|
141
|
-
obj: { +[string]: mixed },
|
|
142
|
-
text?: string,
|
|
143
|
-
seen: RefSet,
|
|
144
|
-
): ObjectAnnotation {
|
|
145
|
-
seen.add(obj);
|
|
146
|
-
|
|
147
|
-
const fields = {};
|
|
148
|
-
Object.keys(obj).forEach((key) => {
|
|
149
|
-
const value = obj[key];
|
|
150
|
-
fields[key] = annotate(value, undefined, seen);
|
|
151
|
-
});
|
|
152
|
-
return object(fields, text);
|
|
153
|
-
}
|
|
154
|
-
|
|
155
|
-
function annotate(value: mixed, text?: string, seen: RefSet): Annotation {
|
|
156
|
-
if (
|
|
157
|
-
value === null ||
|
|
158
|
-
value === undefined ||
|
|
159
|
-
typeof value === 'string' ||
|
|
160
|
-
typeof value === 'number' ||
|
|
161
|
-
typeof value === 'boolean' ||
|
|
162
|
-
typeof value === 'symbol' ||
|
|
163
|
-
typeof value.getMonth === 'function'
|
|
164
|
-
) {
|
|
165
|
-
return scalar(value, text);
|
|
166
|
-
}
|
|
167
|
-
|
|
168
|
-
const ann = asAnnotation(value);
|
|
169
|
-
if (ann) {
|
|
170
|
-
return updateText(ann, text);
|
|
171
|
-
}
|
|
172
|
-
|
|
173
|
-
if (Array.isArray(value)) {
|
|
174
|
-
// "Circular references" can only exist in objects or arrays
|
|
175
|
-
if (seen.has(value)) {
|
|
176
|
-
return circularRef(text);
|
|
177
|
-
} else {
|
|
178
|
-
return annotateArray(value, text, seen);
|
|
179
|
-
}
|
|
180
|
-
}
|
|
181
|
-
|
|
182
|
-
if (typeof value === 'object') {
|
|
183
|
-
// "Circular references" can only exist in objects or arrays
|
|
184
|
-
if (seen.has(value)) {
|
|
185
|
-
return circularRef(text);
|
|
186
|
-
} else {
|
|
187
|
-
return annotateObject(value, text, seen);
|
|
188
|
-
}
|
|
189
|
-
}
|
|
190
|
-
|
|
191
|
-
if (typeof value === 'function') {
|
|
192
|
-
return func(text);
|
|
193
|
-
}
|
|
194
|
-
|
|
195
|
-
return unknown(value, text);
|
|
196
|
-
}
|
|
197
|
-
|
|
198
|
-
function public_annotate(value: mixed, text?: string): Annotation {
|
|
199
|
-
return annotate(value, text, new WeakSet());
|
|
200
|
-
}
|
|
201
|
-
|
|
202
|
-
function public_annotateObject(
|
|
203
|
-
obj: { +[string]: mixed },
|
|
204
|
-
text?: string,
|
|
205
|
-
): ObjectAnnotation {
|
|
206
|
-
return annotateObject(obj, text, new WeakSet());
|
|
207
|
-
}
|
|
208
|
-
|
|
209
|
-
export {
|
|
210
|
-
// This construct just ensures the "seen" weakmap (used for circular
|
|
211
|
-
// reference detection) isn't made part of the public API.
|
|
212
|
-
public_annotate as annotate,
|
|
213
|
-
public_annotateObject as annotateObject,
|
|
214
|
-
//
|
|
215
|
-
// NOTE: Don't acces theses private APIs directly. They are only exported here
|
|
216
|
-
// to better enable unit testing.
|
|
217
|
-
annotate as __private_annotate,
|
|
218
|
-
};
|
package/_esm/core/array.js.flow
DELETED
|
@@ -1,103 +0,0 @@
|
|
|
1
|
-
// @flow strict
|
|
2
|
-
|
|
3
|
-
import * as Result from '../result';
|
|
4
|
-
import { annotate } from '../annotate';
|
|
5
|
-
import { compose, predicate } from './composition';
|
|
6
|
-
import type { Decoder, DecodeResult } from '../_types';
|
|
7
|
-
|
|
8
|
-
/**
|
|
9
|
-
* Like a "Plain Old JavaScript Object", but for arrays: "Plain Old JavaScript
|
|
10
|
-
* Array" ^_^
|
|
11
|
-
*/
|
|
12
|
-
export const poja: Decoder<Array<mixed>> = (blob: mixed) => {
|
|
13
|
-
if (!Array.isArray(blob)) {
|
|
14
|
-
return Result.err(annotate(blob, 'Must be an array'));
|
|
15
|
-
}
|
|
16
|
-
return Result.ok(
|
|
17
|
-
// NOTE: Since Flow 0.98, Array.isArray() returns $ReadOnlyArray<mixed>
|
|
18
|
-
// instead of Array<mixed>. For rationale, see
|
|
19
|
-
// https://github.com/facebook/flow/issues/7684. In this case, we
|
|
20
|
-
// don't want to output read-only types because it's up to the user of
|
|
21
|
-
// decoders to determine what they want to do with the decoded output.
|
|
22
|
-
// If they want to write items into the array, that's fine!
|
|
23
|
-
// The fastest way to turn a read-only array into a normal array in
|
|
24
|
-
// Javascript is to use .slice() on it, see this benchmark:
|
|
25
|
-
// http://jsben.ch/lO6C5
|
|
26
|
-
blob.slice(),
|
|
27
|
-
);
|
|
28
|
-
};
|
|
29
|
-
|
|
30
|
-
/**
|
|
31
|
-
* Given an array of Result instances, loop over them all and return:
|
|
32
|
-
* - An [index, err] tuple, indicating the (index of the) first Err instance
|
|
33
|
-
* encountered; or
|
|
34
|
-
* - a new Ok with an array of all unwrapped Ok'ed values
|
|
35
|
-
*/
|
|
36
|
-
function all<T>(
|
|
37
|
-
items: $ReadOnlyArray<DecodeResult<T>>,
|
|
38
|
-
blobs: $ReadOnlyArray<mixed>,
|
|
39
|
-
): DecodeResult<Array<T>> {
|
|
40
|
-
const results: Array<T> = [];
|
|
41
|
-
for (let index = 0; index < items.length; ++index) {
|
|
42
|
-
const result = items[index];
|
|
43
|
-
if (result.type === 'ok') {
|
|
44
|
-
results.push(result.value);
|
|
45
|
-
} else {
|
|
46
|
-
const ann = result.error;
|
|
47
|
-
|
|
48
|
-
// Rewrite the annotation to include the index information, and inject it into the original blob
|
|
49
|
-
const clone = [...blobs];
|
|
50
|
-
clone.splice(
|
|
51
|
-
index,
|
|
52
|
-
1,
|
|
53
|
-
annotate(
|
|
54
|
-
ann,
|
|
55
|
-
ann.text ? `${ann.text} (at index ${index})` : `index ${index}`,
|
|
56
|
-
),
|
|
57
|
-
);
|
|
58
|
-
|
|
59
|
-
// const errValue = [];
|
|
60
|
-
// if (index > 0) {
|
|
61
|
-
// errValue.push('...'); // TODO: make special mark, not string!
|
|
62
|
-
// }
|
|
63
|
-
// errValue.push(
|
|
64
|
-
// );
|
|
65
|
-
// if (index < iterable.length - 1) {
|
|
66
|
-
// errValue.push('...'); // TODO: make special mark, not string!
|
|
67
|
-
// }
|
|
68
|
-
return Result.err(annotate(clone));
|
|
69
|
-
}
|
|
70
|
-
}
|
|
71
|
-
return Result.ok(results);
|
|
72
|
-
}
|
|
73
|
-
|
|
74
|
-
/**
|
|
75
|
-
* Given a T, builds a decoder that assumes an array input and returns an
|
|
76
|
-
* Array<T>.
|
|
77
|
-
*/
|
|
78
|
-
function members<T>(decoder: Decoder<T>): Decoder<Array<T>, Array<mixed>> {
|
|
79
|
-
return (blobs: $ReadOnlyArray<mixed>) => {
|
|
80
|
-
const results = blobs.map(decoder);
|
|
81
|
-
const result = all(results, blobs);
|
|
82
|
-
return result;
|
|
83
|
-
};
|
|
84
|
-
}
|
|
85
|
-
|
|
86
|
-
/**
|
|
87
|
-
* Builds a Decoder that returns Ok for values of `Array<T>`, given a Decoder
|
|
88
|
-
* for `T`. Err otherwise.
|
|
89
|
-
*/
|
|
90
|
-
export function array<T>(decoder: Decoder<T>): Decoder<Array<T>> {
|
|
91
|
-
return compose(poja, members(decoder));
|
|
92
|
-
}
|
|
93
|
-
|
|
94
|
-
/**
|
|
95
|
-
* Builds a Decoder that returns Ok for values of `Array<T>`, but will reject
|
|
96
|
-
* empty arrays.
|
|
97
|
-
*/
|
|
98
|
-
export function nonEmptyArray<T>(decoder: Decoder<T>): Decoder<Array<T>> {
|
|
99
|
-
return compose(
|
|
100
|
-
array(decoder),
|
|
101
|
-
predicate((arr) => arr.length > 0, 'Must be non-empty array'),
|
|
102
|
-
);
|
|
103
|
-
}
|
|
@@ -1,29 +0,0 @@
|
|
|
1
|
-
// @flow strict
|
|
2
|
-
|
|
3
|
-
import * as Result from '../result';
|
|
4
|
-
import { annotate } from '../annotate';
|
|
5
|
-
import { map } from './composition';
|
|
6
|
-
import { number } from './number';
|
|
7
|
-
import type { Decoder } from '../_types';
|
|
8
|
-
|
|
9
|
-
/**
|
|
10
|
-
* Decoder that only returns Ok for boolean inputs. Err otherwise.
|
|
11
|
-
*/
|
|
12
|
-
export const boolean: Decoder<boolean> = (blob: mixed) => {
|
|
13
|
-
return typeof blob === 'boolean'
|
|
14
|
-
? Result.ok(blob)
|
|
15
|
-
: Result.err(annotate(blob, 'Must be boolean'));
|
|
16
|
-
};
|
|
17
|
-
|
|
18
|
-
/**
|
|
19
|
-
* Decoder that returns true for all truthy values, and false otherwise. Never fails.
|
|
20
|
-
*/
|
|
21
|
-
export const truthy: Decoder<boolean> = (blob: mixed) => {
|
|
22
|
-
return Result.ok(!!blob);
|
|
23
|
-
};
|
|
24
|
-
|
|
25
|
-
/**
|
|
26
|
-
* Decoder that only returns Ok for numeric input values representing booleans.
|
|
27
|
-
* Returns their boolean representation. Err otherwise.
|
|
28
|
-
*/
|
|
29
|
-
export const numericBoolean: Decoder<boolean> = map(number, (n) => !!n);
|