decoders 1.25.5 → 2.0.0-beta2
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 +23 -3
- package/_esm/_guard.js +15 -0
- package/_esm/_guard.js.flow +20 -0
- package/_esm/_types.js +0 -0
- package/_esm/_types.js.flow +20 -0
- package/_esm/_utils.js +93 -0
- package/_esm/_utils.js.flow +97 -0
- package/_esm/annotate.js +144 -0
- package/_esm/annotate.js.flow +218 -0
- package/_esm/core/array.js +91 -0
- package/{array.js.flow → _esm/core/array.js.flow} +22 -25
- package/_esm/core/boolean.js +28 -0
- package/{boolean.js.flow → _esm/core/boolean.js.flow} +8 -7
- package/_esm/core/composition.js +42 -0
- package/{utils.js.flow → _esm/core/composition.js.flow} +7 -22
- package/_esm/core/constants.js +46 -0
- package/{constants.js.flow → _esm/core/constants.js.flow} +13 -12
- package/_esm/core/date.js +28 -0
- package/{date.js.flow → _esm/core/date.js.flow} +9 -7
- package/{describe.js → _esm/core/describe.js} +5 -11
- package/{describe.js.flow → _esm/core/describe.js.flow} +4 -4
- package/{dispatch.js → _esm/core/dispatch.js} +8 -15
- package/{dispatch.js.flow → _esm/core/dispatch.js.flow} +6 -5
- package/_esm/core/either.js +90 -0
- package/{either.js.flow → _esm/core/either.js.flow} +55 -35
- package/_esm/core/fail.js +11 -0
- package/_esm/core/fail.js.flow +12 -0
- package/_esm/core/instanceOf.js +8 -0
- package/{instanceOf.js.flow → _esm/core/instanceOf.js.flow} +7 -8
- package/_esm/core/json.js +15 -0
- package/{json.js.flow → _esm/core/json.js.flow} +4 -4
- package/_esm/core/lazy.js +11 -0
- package/{lazy.js.flow → _esm/core/lazy.js.flow} +1 -1
- package/_esm/core/mapping.js +54 -0
- package/_esm/core/mapping.js.flow +54 -0
- package/_esm/core/number.js +25 -0
- package/{number.js.flow → _esm/core/number.js.flow} +10 -11
- package/_esm/core/object.js +175 -0
- package/{object.js.flow → _esm/core/object.js.flow} +44 -52
- package/_esm/core/optional.js +38 -0
- package/{optional.js.flow → _esm/core/optional.js.flow} +6 -7
- package/{string.js → _esm/core/string.js} +18 -35
- package/{string.js.flow → _esm/core/string.js.flow} +13 -12
- package/_esm/core/tuple.js +155 -0
- package/{tuple.js.flow → _esm/core/tuple.js.flow} +62 -63
- package/_esm/format/index.js +2 -0
- package/_esm/format/index.js.flow +4 -0
- package/_esm/format/inline.js +137 -0
- package/_esm/format/inline.js.flow +122 -0
- package/_esm/format/short.js +4 -0
- package/_esm/format/short.js.flow +8 -0
- package/_esm/index.js +37 -0
- package/_esm/index.js.flow +56 -0
- package/_esm/result.js +139 -0
- package/_esm/result.js.flow +166 -0
- package/_guard.js +26 -0
- package/_guard.js.flow +20 -0
- package/_types.js +1 -0
- package/_types.js.flow +20 -0
- package/_utils.js +108 -0
- package/_utils.js.flow +97 -0
- package/annotate.js +161 -0
- package/annotate.js.flow +218 -0
- package/core/array.js +108 -0
- package/core/array.js.flow +103 -0
- package/core/boolean.js +44 -0
- package/core/boolean.js.flow +29 -0
- package/core/composition.js +56 -0
- package/core/composition.js.flow +43 -0
- package/core/constants.js +69 -0
- package/core/constants.js.flow +46 -0
- package/core/date.js +46 -0
- package/core/date.js.flow +40 -0
- package/core/describe.js +26 -0
- package/core/describe.js.flow +17 -0
- package/core/dispatch.js +62 -0
- package/core/dispatch.js.flow +58 -0
- package/core/either.js +117 -0
- package/core/either.js.flow +151 -0
- package/core/fail.js +21 -0
- package/core/fail.js.flow +12 -0
- package/core/instanceOf.js +19 -0
- package/core/instanceOf.js.flow +20 -0
- package/{json.js → core/json.js} +3 -5
- package/core/json.js.flow +28 -0
- package/{lazy.js → core/lazy.js} +1 -3
- package/core/lazy.js.flow +15 -0
- package/core/mapping.js +67 -0
- package/core/mapping.js.flow +54 -0
- package/core/number.js +40 -0
- package/core/number.js.flow +34 -0
- package/core/object.js +194 -0
- package/core/object.js.flow +203 -0
- package/core/optional.js +54 -0
- package/core/optional.js.flow +41 -0
- package/core/string.js +98 -0
- package/core/string.js.flow +82 -0
- package/core/tuple.js +173 -0
- package/core/tuple.js.flow +220 -0
- package/format/index.js +12 -0
- package/format/index.js.flow +4 -0
- package/format/inline.js +146 -0
- package/format/inline.js.flow +122 -0
- package/format/short.js +10 -0
- package/format/short.js.flow +8 -0
- package/index.js +118 -395
- package/index.js.flow +24 -24
- package/package.json +2 -6
- package/result.js +172 -0
- package/result.js.flow +166 -0
- package/array.d.ts +0 -5
- package/array.js +0 -133
- package/boolean.d.ts +0 -5
- package/boolean.js +0 -42
- package/constants.d.ts +0 -11
- package/constants.js +0 -67
- package/date.d.ts +0 -4
- package/date.js +0 -42
- package/describe.d.ts +0 -3
- package/dispatch.d.ts +0 -8
- package/either.d.ts +0 -61
- package/either.js +0 -85
- package/fail.d.ts +0 -3
- package/fail.js +0 -19
- package/fail.js.flow +0 -13
- package/guard.d.ts +0 -7
- package/guard.js +0 -30
- package/guard.js.flow +0 -36
- package/helpers.d.ts +0 -79
- package/index.d.ts +0 -38
- package/instanceOf.d.ts +0 -3
- package/instanceOf.js +0 -17
- package/json.d.ts +0 -11
- package/lazy.d.ts +0 -3
- package/mapping.d.ts +0 -4
- package/mapping.js +0 -113
- package/mapping.js.flow +0 -71
- package/number.d.ts +0 -6
- package/number.js +0 -38
- package/object.d.ts +0 -33
- package/object.js +0 -254
- package/optional.d.ts +0 -5
- package/optional.js +0 -52
- package/string.d.ts +0 -7
- package/tuple.d.ts +0 -30
- package/tuple.js +0 -199
- package/types.d.ts +0 -18
- package/types.js +0 -5
- package/types.js.flow +0 -26
- package/utils.d.ts +0 -13
- package/utils.js +0 -70
package/result.js
ADDED
|
@@ -0,0 +1,172 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
exports.__esModule = true;
|
|
4
|
+
exports.andThen = andThen;
|
|
5
|
+
exports.dispatch = dispatch;
|
|
6
|
+
exports.err = err;
|
|
7
|
+
exports.errValue = errValue;
|
|
8
|
+
exports.expect = expect;
|
|
9
|
+
exports.isErr = isErr;
|
|
10
|
+
exports.isOk = isOk;
|
|
11
|
+
exports.map = map;
|
|
12
|
+
exports.mapError = mapError;
|
|
13
|
+
exports.ok = ok;
|
|
14
|
+
exports.orElse = orElse;
|
|
15
|
+
exports.toString = toString;
|
|
16
|
+
exports.unwrap = unwrap;
|
|
17
|
+
exports.value = value;
|
|
18
|
+
exports.withDefault = withDefault;
|
|
19
|
+
|
|
20
|
+
/**
|
|
21
|
+
* Result <value> <error>
|
|
22
|
+
* = Ok <value>
|
|
23
|
+
* | Err <error>
|
|
24
|
+
*/
|
|
25
|
+
|
|
26
|
+
/**
|
|
27
|
+
* Create a new Result instance representing a successful computation.
|
|
28
|
+
*/
|
|
29
|
+
function ok(value) {
|
|
30
|
+
return {
|
|
31
|
+
type: 'ok',
|
|
32
|
+
value: value
|
|
33
|
+
};
|
|
34
|
+
}
|
|
35
|
+
/**
|
|
36
|
+
* Create a new Result instance representing a failed computation.
|
|
37
|
+
*/
|
|
38
|
+
|
|
39
|
+
|
|
40
|
+
function err(error) {
|
|
41
|
+
return {
|
|
42
|
+
type: 'err',
|
|
43
|
+
error: error
|
|
44
|
+
};
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
function toString(result) {
|
|
48
|
+
return result.type === 'ok' ? "Ok(" + String(result.value) + ")" : "Err(" + String(result.error) + ")";
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
function isOk(result) {
|
|
52
|
+
return result.type === 'ok';
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
function isErr(result) {
|
|
56
|
+
return result.type === 'err';
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
function withDefault(result, defaultValue) {
|
|
60
|
+
return result.type === 'ok' ? result.value : defaultValue;
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
function value(result) {
|
|
64
|
+
return result.type === 'ok' ? result.value : undefined;
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
function errValue(result) {
|
|
68
|
+
return result.type === 'err' ? result.error : undefined;
|
|
69
|
+
}
|
|
70
|
+
/**
|
|
71
|
+
* Unwrap the value from this Result instance if this is an "Ok" result.
|
|
72
|
+
* Otherwise, will throw the "Err" error via a runtime exception.
|
|
73
|
+
*/
|
|
74
|
+
|
|
75
|
+
|
|
76
|
+
function unwrap(result) {
|
|
77
|
+
if (result.type === 'ok') {
|
|
78
|
+
return result.value;
|
|
79
|
+
} else {
|
|
80
|
+
throw result.error;
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
function expect(result, message) {
|
|
85
|
+
if (result.type === 'ok') {
|
|
86
|
+
return result.value;
|
|
87
|
+
} else {
|
|
88
|
+
throw message instanceof Error ? message : new Error(message);
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
function dispatch(result, okCallback, errCallback) {
|
|
93
|
+
return result.type === 'ok' ? okCallback(result.value) : errCallback(result.error);
|
|
94
|
+
}
|
|
95
|
+
/**
|
|
96
|
+
* If the given result is OK, defers to the other result. Otherwise returns the
|
|
97
|
+
* error result.
|
|
98
|
+
*
|
|
99
|
+
* It's like saying A && B, but on Result.
|
|
100
|
+
*
|
|
101
|
+
* Examples:
|
|
102
|
+
*
|
|
103
|
+
* Result.ok(42) && Result.ok('hi') // => Ok('hi')
|
|
104
|
+
* Result.err('boo') && Result.ok('hi') // => Err('boo')
|
|
105
|
+
* Result.ok(42) && Result.err('boo') // => Err('boo')
|
|
106
|
+
* Result.err('boo') && Result.err('boo') // => Err('boo')
|
|
107
|
+
*
|
|
108
|
+
*/
|
|
109
|
+
// export function and<T, E, T2>(
|
|
110
|
+
// result1: Result<T, E>,
|
|
111
|
+
// result2: Result<T2, E>,
|
|
112
|
+
// ): Result<T2, E> {
|
|
113
|
+
// return result1.type === 'ok' ? result2 : result1;
|
|
114
|
+
// }
|
|
115
|
+
|
|
116
|
+
/**
|
|
117
|
+
* If the given result is OK, return that result. Otherwise, defers to the
|
|
118
|
+
* other result.
|
|
119
|
+
*
|
|
120
|
+
* It's like saying A || B, but on Result.
|
|
121
|
+
*
|
|
122
|
+
* Examples:
|
|
123
|
+
*
|
|
124
|
+
* Result.ok(42) || Result.ok('hi') // => Ok(42)
|
|
125
|
+
* Result.err('boo') || Result.ok('hi') // => Ok('hi')
|
|
126
|
+
* Result.ok(42) || Result.err('boo') // => Ok(42)
|
|
127
|
+
* Result.err('bleh') || Result.err('boo') // => Err('boo')
|
|
128
|
+
*
|
|
129
|
+
*/
|
|
130
|
+
// export function or<T, E, E2>(
|
|
131
|
+
// result1: Result<T, E>,
|
|
132
|
+
// result2: Result<T, E2>,
|
|
133
|
+
// ): Result<T, E2> {
|
|
134
|
+
// return result1.type === 'ok' ? result1 : result2;
|
|
135
|
+
// }
|
|
136
|
+
|
|
137
|
+
/**
|
|
138
|
+
* Like .and(), aka &&, but the second argument gets evaluated lazily only if
|
|
139
|
+
* the first result is an Ok result. If so, it has access to the Ok value from
|
|
140
|
+
* the first argument.
|
|
141
|
+
*/
|
|
142
|
+
|
|
143
|
+
|
|
144
|
+
function andThen(result1, lazyResult2) {
|
|
145
|
+
return result1.type === 'ok' ? lazyResult2(result1.value) : result1;
|
|
146
|
+
}
|
|
147
|
+
/**
|
|
148
|
+
* Like .or(), aka ||, but the second argument gets evaluated lazily only if
|
|
149
|
+
* the first result is an Err result. If so, it has access to the Err value
|
|
150
|
+
* from the first argument.
|
|
151
|
+
*/
|
|
152
|
+
|
|
153
|
+
|
|
154
|
+
function orElse(result1, lazyResult2) {
|
|
155
|
+
return result1.type === 'ok' ? result1 : lazyResult2(result1.error);
|
|
156
|
+
}
|
|
157
|
+
/**
|
|
158
|
+
* Transform an Ok result. Will not touch Err results.
|
|
159
|
+
*/
|
|
160
|
+
|
|
161
|
+
|
|
162
|
+
function map(result, mapper) {
|
|
163
|
+
return result.type === 'ok' ? ok(mapper(result.value)) : result;
|
|
164
|
+
}
|
|
165
|
+
/**
|
|
166
|
+
* Transform an Err value. Will not touch Ok results.
|
|
167
|
+
*/
|
|
168
|
+
|
|
169
|
+
|
|
170
|
+
function mapError(result, mapper) {
|
|
171
|
+
return result.type === 'ok' ? result : err(mapper(result.error));
|
|
172
|
+
}
|
package/result.js.flow
ADDED
|
@@ -0,0 +1,166 @@
|
|
|
1
|
+
// @flow strict
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Result <value> <error>
|
|
5
|
+
* = Ok <value>
|
|
6
|
+
* | Err <error>
|
|
7
|
+
*/
|
|
8
|
+
|
|
9
|
+
type Ok<+T> = {| +type: 'ok', +value: T |};
|
|
10
|
+
type Err<+E> = {| +type: 'err', +error: E |};
|
|
11
|
+
|
|
12
|
+
export type Result<+T, +E> = Ok<T> | Err<E>;
|
|
13
|
+
|
|
14
|
+
/**
|
|
15
|
+
* Create a new Result instance representing a successful computation.
|
|
16
|
+
*/
|
|
17
|
+
export function ok<T>(value: T): Ok<T> {
|
|
18
|
+
return { type: 'ok', value };
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
/**
|
|
22
|
+
* Create a new Result instance representing a failed computation.
|
|
23
|
+
*/
|
|
24
|
+
export function err<E>(error: E): Err<E> {
|
|
25
|
+
return { type: 'err', 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;
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
/**
|
|
55
|
+
* Unwrap the value from this Result instance if this is an "Ok" result.
|
|
56
|
+
* Otherwise, will throw the "Err" error via a runtime exception.
|
|
57
|
+
*/
|
|
58
|
+
export function unwrap<T>(result: Result<T, mixed>): T {
|
|
59
|
+
if (result.type === 'ok') {
|
|
60
|
+
return result.value;
|
|
61
|
+
} else {
|
|
62
|
+
throw result.error;
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
export function expect<T>(result: Result<T, mixed>, message: string | Error): T {
|
|
67
|
+
if (result.type === 'ok') {
|
|
68
|
+
return result.value;
|
|
69
|
+
} else {
|
|
70
|
+
throw message instanceof Error ? message : new Error(message);
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
export function dispatch<T, E, O>(
|
|
75
|
+
result: Result<T, E>,
|
|
76
|
+
okCallback: (value: T) => O,
|
|
77
|
+
errCallback: (error: E) => O,
|
|
78
|
+
): O {
|
|
79
|
+
return result.type === 'ok' ? okCallback(result.value) : errCallback(result.error);
|
|
80
|
+
}
|
|
81
|
+
|
|
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
|
+
/**
|
|
125
|
+
* Like .and(), aka &&, but the second argument gets evaluated lazily only if
|
|
126
|
+
* the first result is an Ok result. If so, it has access to the Ok value from
|
|
127
|
+
* the first argument.
|
|
128
|
+
*/
|
|
129
|
+
export function andThen<T, E, T2>(
|
|
130
|
+
result1: Result<T, E>,
|
|
131
|
+
lazyResult2: (value: T) => Result<T2, E>,
|
|
132
|
+
): Result<T2, E> {
|
|
133
|
+
return result1.type === 'ok' ? lazyResult2(result1.value) : result1;
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
/**
|
|
137
|
+
* Like .or(), aka ||, but the second argument gets evaluated lazily only if
|
|
138
|
+
* the first result is an Err result. If so, it has access to the Err value
|
|
139
|
+
* from the first argument.
|
|
140
|
+
*/
|
|
141
|
+
export function orElse<T, E, E2>(
|
|
142
|
+
result1: Result<T, E>,
|
|
143
|
+
lazyResult2: (errValue: E) => Result<T, E2>,
|
|
144
|
+
): Result<T, E2> {
|
|
145
|
+
return result1.type === 'ok' ? result1 : lazyResult2(result1.error);
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
/**
|
|
149
|
+
* Transform an Ok result. Will not touch Err results.
|
|
150
|
+
*/
|
|
151
|
+
export function map<T, E, T2>(
|
|
152
|
+
result: Result<T, E>,
|
|
153
|
+
mapper: (value: T) => T2,
|
|
154
|
+
): Result<T2, E> {
|
|
155
|
+
return result.type === 'ok' ? ok(mapper(result.value)) : result;
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
/**
|
|
159
|
+
* Transform an Err value. Will not touch Ok results.
|
|
160
|
+
*/
|
|
161
|
+
export function mapError<T, E, E2>(
|
|
162
|
+
result: Result<T, E>,
|
|
163
|
+
mapper: (error: E) => E2,
|
|
164
|
+
): Result<T, E2> {
|
|
165
|
+
return result.type === 'ok' ? result : err(mapper(result.error));
|
|
166
|
+
}
|
package/array.d.ts
DELETED
package/array.js
DELETED
|
@@ -1,133 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
|
|
3
|
-
Object.defineProperty(exports, "__esModule", {
|
|
4
|
-
value: true
|
|
5
|
-
});
|
|
6
|
-
exports.array = array;
|
|
7
|
-
exports.nonEmptyArray = nonEmptyArray;
|
|
8
|
-
exports.poja = void 0;
|
|
9
|
-
|
|
10
|
-
var _debrief = require("debrief");
|
|
11
|
-
|
|
12
|
-
var _Result = require("lemons/Result");
|
|
13
|
-
|
|
14
|
-
var _utils = require("./utils");
|
|
15
|
-
|
|
16
|
-
function _toConsumableArray(arr) { return _arrayWithoutHoles(arr) || _iterableToArray(arr) || _unsupportedIterableToArray(arr) || _nonIterableSpread(); }
|
|
17
|
-
|
|
18
|
-
function _nonIterableSpread() { throw new TypeError("Invalid attempt to spread non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); }
|
|
19
|
-
|
|
20
|
-
function _iterableToArray(iter) { if (typeof Symbol !== "undefined" && iter[Symbol.iterator] != null || iter["@@iterator"] != null) return Array.from(iter); }
|
|
21
|
-
|
|
22
|
-
function _arrayWithoutHoles(arr) { if (Array.isArray(arr)) return _arrayLikeToArray(arr); }
|
|
23
|
-
|
|
24
|
-
function _createForOfIteratorHelper(o, allowArrayLike) { var it = typeof Symbol !== "undefined" && o[Symbol.iterator] || o["@@iterator"]; if (!it) { if (Array.isArray(o) || (it = _unsupportedIterableToArray(o)) || allowArrayLike && o && typeof o.length === "number") { if (it) o = it; var i = 0; var F = function F() {}; return { s: F, n: function n() { if (i >= o.length) return { done: true }; return { done: false, value: o[i++] }; }, e: function e(_e) { throw _e; }, f: F }; } throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); } var normalCompletion = true, didErr = false, err; return { s: function s() { it = it.call(o); }, n: function n() { var step = it.next(); normalCompletion = step.done; return step; }, e: function e(_e2) { didErr = true; err = _e2; }, f: function f() { try { if (!normalCompletion && it["return"] != null) it["return"](); } finally { if (didErr) throw err; } } }; }
|
|
25
|
-
|
|
26
|
-
function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); }
|
|
27
|
-
|
|
28
|
-
function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) { arr2[i] = arr[i]; } return arr2; }
|
|
29
|
-
|
|
30
|
-
/**
|
|
31
|
-
* Like a "Plain Old JavaScript Object", but for arrays: "Plain Old JavaScript
|
|
32
|
-
* Array" ^_^
|
|
33
|
-
*/
|
|
34
|
-
var poja = function poja(blob) {
|
|
35
|
-
if (!Array.isArray(blob)) {
|
|
36
|
-
return (0, _Result.Err)((0, _debrief.annotate)(blob, 'Must be an array'));
|
|
37
|
-
}
|
|
38
|
-
|
|
39
|
-
return (0, _Result.Ok)( // NOTE: Since Flow 0.98, Array.isArray() returns $ReadOnlyArray<mixed>
|
|
40
|
-
// instead of Array<mixed>. For rationale, see
|
|
41
|
-
// https://github.com/facebook/flow/issues/7684. In this case, we
|
|
42
|
-
// don't want to output read-only types because it's up to the user of
|
|
43
|
-
// decoders to determine what they want to do with the decoded output.
|
|
44
|
-
// If they want to write items into the array, that's fine!
|
|
45
|
-
// The fastest way to turn a read-only array into a normal array in
|
|
46
|
-
// Javascript is to use .slice() on it, see this benchmark:
|
|
47
|
-
// http://jsben.ch/lO6C5
|
|
48
|
-
blob.slice());
|
|
49
|
-
};
|
|
50
|
-
/**
|
|
51
|
-
* Given an iterable of Result instances, exhaust them all and return:
|
|
52
|
-
* - An [index, err] tuple, indicating the (index of the) first Err instance
|
|
53
|
-
* encountered; or
|
|
54
|
-
* - a new Ok with an array of all unwrapped Ok'ed values
|
|
55
|
-
*/
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
exports.poja = poja;
|
|
59
|
-
|
|
60
|
-
function all(iterable, blobs) {
|
|
61
|
-
var results = [];
|
|
62
|
-
var index = 0;
|
|
63
|
-
|
|
64
|
-
var _iterator = _createForOfIteratorHelper(iterable),
|
|
65
|
-
_step;
|
|
66
|
-
|
|
67
|
-
try {
|
|
68
|
-
for (_iterator.s(); !(_step = _iterator.n()).done;) {
|
|
69
|
-
var result = _step.value;
|
|
70
|
-
|
|
71
|
-
try {
|
|
72
|
-
var value = result.unwrap();
|
|
73
|
-
results.push(value);
|
|
74
|
-
} catch (ann) {
|
|
75
|
-
// Rewrite the annotation to include the index information, and inject it into the original blob
|
|
76
|
-
var clone = _toConsumableArray(blobs);
|
|
77
|
-
|
|
78
|
-
clone.splice(index, 1, (0, _debrief.annotate)(ann, ann.annotation !== undefined ? "".concat(ann.annotation, " (at index ").concat(index, ")") : "index ".concat(index))); // const errValue = [];
|
|
79
|
-
// if (index > 0) {
|
|
80
|
-
// errValue.push('...'); // TODO: make special mark, not string!
|
|
81
|
-
// }
|
|
82
|
-
// errValue.push(
|
|
83
|
-
// );
|
|
84
|
-
// if (index < iterable.length - 1) {
|
|
85
|
-
// errValue.push('...'); // TODO: make special mark, not string!
|
|
86
|
-
// }
|
|
87
|
-
|
|
88
|
-
return (0, _Result.Err)((0, _debrief.annotate)(clone));
|
|
89
|
-
}
|
|
90
|
-
|
|
91
|
-
index++;
|
|
92
|
-
}
|
|
93
|
-
} catch (err) {
|
|
94
|
-
_iterator.e(err);
|
|
95
|
-
} finally {
|
|
96
|
-
_iterator.f();
|
|
97
|
-
}
|
|
98
|
-
|
|
99
|
-
return (0, _Result.Ok)(results);
|
|
100
|
-
}
|
|
101
|
-
/**
|
|
102
|
-
* Given a T, builds a decoder that assumes an array input and returns an
|
|
103
|
-
* Array<T>.
|
|
104
|
-
*/
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
function members(decoder) {
|
|
108
|
-
return function (blobs) {
|
|
109
|
-
var results = blobs.map(decoder);
|
|
110
|
-
var result = all(results, blobs);
|
|
111
|
-
return result;
|
|
112
|
-
};
|
|
113
|
-
}
|
|
114
|
-
/**
|
|
115
|
-
* Builds a Decoder that returns Ok for values of `Array<T>`, given a Decoder
|
|
116
|
-
* for `T`. Err otherwise.
|
|
117
|
-
*/
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
function array(decoder) {
|
|
121
|
-
return (0, _utils.compose)(poja, members(decoder));
|
|
122
|
-
}
|
|
123
|
-
/**
|
|
124
|
-
* Builds a Decoder that returns Ok for values of `Array<T>`, but will reject
|
|
125
|
-
* empty arrays.
|
|
126
|
-
*/
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
function nonEmptyArray(decoder) {
|
|
130
|
-
return (0, _utils.compose)(array(decoder), (0, _utils.predicate)(function (arr) {
|
|
131
|
-
return arr.length > 0;
|
|
132
|
-
}, 'Must be non-empty array'));
|
|
133
|
-
}
|
package/boolean.d.ts
DELETED
package/boolean.js
DELETED
|
@@ -1,42 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
|
|
3
|
-
Object.defineProperty(exports, "__esModule", {
|
|
4
|
-
value: true
|
|
5
|
-
});
|
|
6
|
-
exports.truthy = exports.numericBoolean = exports["boolean"] = void 0;
|
|
7
|
-
|
|
8
|
-
var _debrief = require("debrief");
|
|
9
|
-
|
|
10
|
-
var _Result = require("lemons/Result");
|
|
11
|
-
|
|
12
|
-
var _number = require("./number");
|
|
13
|
-
|
|
14
|
-
var _utils = require("./utils");
|
|
15
|
-
|
|
16
|
-
/**
|
|
17
|
-
* Decoder that only returns Ok for boolean inputs. Err otherwise.
|
|
18
|
-
*/
|
|
19
|
-
var _boolean = function _boolean(blob) {
|
|
20
|
-
return typeof blob === 'boolean' ? (0, _Result.Ok)(blob) : (0, _Result.Err)((0, _debrief.annotate)(blob, 'Must be boolean'));
|
|
21
|
-
};
|
|
22
|
-
/**
|
|
23
|
-
* Decoder that returns true for all truthy values, and false otherwise. Never fails.
|
|
24
|
-
*/
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
exports["boolean"] = _boolean;
|
|
28
|
-
|
|
29
|
-
var truthy = function truthy(blob) {
|
|
30
|
-
return (0, _Result.Ok)(!!blob);
|
|
31
|
-
};
|
|
32
|
-
/**
|
|
33
|
-
* Decoder that only returns Ok for numeric input values representing booleans.
|
|
34
|
-
* Returns their boolean representation. Err otherwise.
|
|
35
|
-
*/
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
exports.truthy = truthy;
|
|
39
|
-
var numericBoolean = (0, _utils.map)(_number.number, function (n) {
|
|
40
|
-
return !!n;
|
|
41
|
-
});
|
|
42
|
-
exports.numericBoolean = numericBoolean;
|
package/constants.d.ts
DELETED
|
@@ -1,11 +0,0 @@
|
|
|
1
|
-
import { Decoder, Scalar } from './types';
|
|
2
|
-
|
|
3
|
-
// Constants
|
|
4
|
-
|
|
5
|
-
export const null_: Decoder<null>;
|
|
6
|
-
export const undefined_: Decoder<undefined>;
|
|
7
|
-
export function constant<T extends Scalar>(value: T): Decoder<T>;
|
|
8
|
-
export function hardcoded<T extends Scalar>(value: T): Decoder<T>;
|
|
9
|
-
export function hardcoded<T>(value: T): Decoder<T>;
|
|
10
|
-
export const mixed: Decoder<unknown>;
|
|
11
|
-
export const unknown: Decoder<unknown>;
|
package/constants.js
DELETED
|
@@ -1,67 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
|
|
3
|
-
Object.defineProperty(exports, "__esModule", {
|
|
4
|
-
value: true
|
|
5
|
-
});
|
|
6
|
-
exports.constant = constant;
|
|
7
|
-
exports.hardcoded = hardcoded;
|
|
8
|
-
exports.unknown = exports.undefined_ = exports.null_ = exports.mixed = void 0;
|
|
9
|
-
|
|
10
|
-
var _debrief = require("debrief");
|
|
11
|
-
|
|
12
|
-
var _Result = require("lemons/Result");
|
|
13
|
-
|
|
14
|
-
/**
|
|
15
|
-
* Decoder that only returns Ok for `null` inputs. Err otherwise.
|
|
16
|
-
*/
|
|
17
|
-
var null_ = function null_(blob) {
|
|
18
|
-
return blob === null ? (0, _Result.Ok)(blob) : (0, _Result.Err)((0, _debrief.annotate)(blob, 'Must be null'));
|
|
19
|
-
};
|
|
20
|
-
/**
|
|
21
|
-
* Decoder that only returns Ok for `undefined` inputs. Err otherwise.
|
|
22
|
-
*/
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
exports.null_ = null_;
|
|
26
|
-
|
|
27
|
-
var undefined_ = function undefined_(blob) {
|
|
28
|
-
return blob === undefined ? (0, _Result.Ok)(blob) : (0, _Result.Err)((0, _debrief.annotate)(blob, 'Must be undefined'));
|
|
29
|
-
};
|
|
30
|
-
/**
|
|
31
|
-
* Decoder that only returns Ok for the given value constant. Err otherwise.
|
|
32
|
-
*/
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
exports.undefined_ = undefined_;
|
|
36
|
-
|
|
37
|
-
function constant(value) {
|
|
38
|
-
return function (blob) {
|
|
39
|
-
return blob === value ? (0, _Result.Ok)(value) : (0, _Result.Err)((0, _debrief.annotate)(blob, "Must be constant ".concat(String(value))));
|
|
40
|
-
};
|
|
41
|
-
}
|
|
42
|
-
/**
|
|
43
|
-
* Decoder that always returns Ok for the given hardcoded value, no matter what the input.
|
|
44
|
-
*/
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
function hardcoded(value) {
|
|
48
|
-
return function (_) {
|
|
49
|
-
return (0, _Result.Ok)(value);
|
|
50
|
-
};
|
|
51
|
-
}
|
|
52
|
-
/**
|
|
53
|
-
* Decoder that always returns Ok for the given hardcoded value, no matter what the input.
|
|
54
|
-
*/
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
var mixed = function mixed(blob) {
|
|
58
|
-
return (0, _Result.Ok)(blob);
|
|
59
|
-
};
|
|
60
|
-
/**
|
|
61
|
-
* Alias of mixed.
|
|
62
|
-
*/
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
exports.mixed = mixed;
|
|
66
|
-
var unknown = mixed;
|
|
67
|
-
exports.unknown = unknown;
|
package/date.d.ts
DELETED
package/date.js
DELETED
|
@@ -1,42 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
|
|
3
|
-
Object.defineProperty(exports, "__esModule", {
|
|
4
|
-
value: true
|
|
5
|
-
});
|
|
6
|
-
exports.iso8601 = exports.date = void 0;
|
|
7
|
-
|
|
8
|
-
var _debrief = require("debrief");
|
|
9
|
-
|
|
10
|
-
var _Result = require("lemons/Result");
|
|
11
|
-
|
|
12
|
-
var _string = require("./string");
|
|
13
|
-
|
|
14
|
-
var _utils = require("./utils");
|
|
15
|
-
|
|
16
|
-
// Only matches the shape. This "over-matches" some values that still aren't
|
|
17
|
-
// valid dates (like 9999-99-99), but those will be caught by JS Date's
|
|
18
|
-
// internal validations
|
|
19
|
-
var iso8601_re = /^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}(?:[.]\d+)?(?:Z|[+-]\d{2}:?\d{2})$/;
|
|
20
|
-
|
|
21
|
-
var date = function date(value) {
|
|
22
|
-
return (0, _utils.isDate)(value) ? (0, _Result.Ok)(value) : (0, _Result.Err)((0, _debrief.annotate)(value, 'Must be a Date'));
|
|
23
|
-
};
|
|
24
|
-
/**
|
|
25
|
-
* Decoder that only returns Ok for strings that are valid ISO8601 date
|
|
26
|
-
* strings. Err otherwise.
|
|
27
|
-
*/
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
exports.date = date;
|
|
31
|
-
var iso8601 = (0, _utils.map)( // Input itself needs to match the ISO8601 regex...
|
|
32
|
-
(0, _string.regex)(iso8601_re, 'Must be ISO8601 format'), // Make sure it is a _valid_ date
|
|
33
|
-
function (value) {
|
|
34
|
-
var date = new Date(value);
|
|
35
|
-
|
|
36
|
-
if (isNaN(date.getTime())) {
|
|
37
|
-
throw new Error('Must be valid date/time value');
|
|
38
|
-
}
|
|
39
|
-
|
|
40
|
-
return date;
|
|
41
|
-
});
|
|
42
|
-
exports.iso8601 = iso8601;
|
package/describe.d.ts
DELETED
package/dispatch.d.ts
DELETED
|
@@ -1,8 +0,0 @@
|
|
|
1
|
-
import { Decoder, $DecoderType } from './types';
|
|
2
|
-
|
|
3
|
-
export type $Values<T extends object> = T[keyof T];
|
|
4
|
-
|
|
5
|
-
export function dispatch<O extends { [key: string]: Decoder<any> }>(
|
|
6
|
-
field: string,
|
|
7
|
-
mapping: O
|
|
8
|
-
): Decoder<$Values<{ [key in keyof O]: $DecoderType<O[key]> }>>;
|