decoders 1.25.4 → 2.0.0-beta1
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 +30 -3
- package/cjs/_guard.js +26 -0
- package/cjs/_guard.js.flow +20 -0
- package/cjs/_types.js +1 -0
- package/cjs/_types.js.flow +20 -0
- package/cjs/_utils.js +108 -0
- package/cjs/_utils.js.flow +97 -0
- package/cjs/annotate.js +161 -0
- package/cjs/annotate.js.flow +218 -0
- package/cjs/format/index.js +12 -0
- package/cjs/format/index.js.flow +4 -0
- package/cjs/format/inline.js +146 -0
- package/cjs/format/inline.js.flow +122 -0
- package/cjs/format/short.js +10 -0
- package/cjs/format/short.js.flow +8 -0
- package/cjs/index.js +120 -0
- package/{index.js.flow → cjs/index.js.flow} +31 -24
- package/cjs/result.js +172 -0
- package/cjs/result.js.flow +166 -0
- package/cjs/stdlib/array.js +108 -0
- package/{array.js.flow → cjs/stdlib/array.js.flow} +22 -25
- package/cjs/stdlib/boolean.js +44 -0
- package/{boolean.js.flow → cjs/stdlib/boolean.js.flow} +8 -7
- package/cjs/stdlib/composition.js +56 -0
- package/{utils.js.flow → cjs/stdlib/composition.js.flow} +7 -22
- package/cjs/stdlib/constants.js +69 -0
- package/{constants.js.flow → cjs/stdlib/constants.js.flow} +13 -12
- package/cjs/stdlib/date.js +46 -0
- package/{date.js.flow → cjs/stdlib/date.js.flow} +9 -7
- package/cjs/stdlib/describe.js +26 -0
- package/{describe.js.flow → cjs/stdlib/describe.js.flow} +4 -4
- package/cjs/stdlib/dispatch.js +62 -0
- package/{dispatch.js.flow → cjs/stdlib/dispatch.js.flow} +6 -5
- package/cjs/stdlib/either.js +117 -0
- package/{either.js.flow → cjs/stdlib/either.js.flow} +55 -35
- package/cjs/stdlib/fail.js +21 -0
- package/cjs/stdlib/fail.js.flow +12 -0
- package/cjs/stdlib/instanceOf.js +19 -0
- package/{instanceOf.js.flow → cjs/stdlib/instanceOf.js.flow} +7 -8
- package/{json.js → cjs/stdlib/json.js} +3 -5
- package/{json.js.flow → cjs/stdlib/json.js.flow} +4 -4
- package/{lazy.js → cjs/stdlib/lazy.js} +1 -3
- package/{lazy.js.flow → cjs/stdlib/lazy.js.flow} +1 -1
- package/cjs/stdlib/mapping.js +67 -0
- package/cjs/stdlib/mapping.js.flow +54 -0
- package/cjs/stdlib/number.js +40 -0
- package/{number.js.flow → cjs/stdlib/number.js.flow} +10 -11
- package/cjs/stdlib/object.js +194 -0
- package/{object.js.flow → cjs/stdlib/object.js.flow} +44 -52
- package/cjs/stdlib/optional.js +54 -0
- package/{optional.js.flow → cjs/stdlib/optional.js.flow} +6 -7
- package/cjs/stdlib/string.js +98 -0
- package/{string.js.flow → cjs/stdlib/string.js.flow} +13 -12
- package/cjs/stdlib/tuple.js +173 -0
- package/{tuple.js.flow → cjs/stdlib/tuple.js.flow} +62 -63
- package/es/_guard.js +15 -0
- package/es/_types.js +0 -0
- package/es/_utils.js +93 -0
- package/es/annotate.js +144 -0
- package/es/format/index.js +2 -0
- package/es/format/inline.js +137 -0
- package/es/format/short.js +4 -0
- package/es/index.js +37 -0
- package/es/result.js +139 -0
- package/es/stdlib/array.js +91 -0
- package/es/stdlib/boolean.js +28 -0
- package/es/stdlib/composition.js +42 -0
- package/es/stdlib/constants.js +46 -0
- package/es/stdlib/date.js +28 -0
- package/{describe.js → es/stdlib/describe.js} +5 -11
- package/{dispatch.js → es/stdlib/dispatch.js} +8 -15
- package/es/stdlib/either.js +90 -0
- package/es/stdlib/fail.js +11 -0
- package/es/stdlib/instanceOf.js +8 -0
- package/es/stdlib/json.js +15 -0
- package/es/stdlib/lazy.js +11 -0
- package/es/stdlib/mapping.js +54 -0
- package/es/stdlib/number.js +25 -0
- package/es/stdlib/object.js +175 -0
- package/es/stdlib/optional.js +38 -0
- package/{string.js → es/stdlib/string.js} +18 -35
- package/es/stdlib/tuple.js +155 -0
- package/package.json +12 -7
- package/ts/_guard.d.ts +7 -0
- package/ts/_helpers.d.ts +79 -0
- package/{types.d.ts → ts/_types.d.ts} +3 -5
- package/{utils.d.ts → ts/_utils.d.ts} +3 -3
- package/ts/annotate.d.ts +58 -0
- package/{array.d.ts → ts/array.d.ts} +1 -1
- package/{boolean.d.ts → ts/boolean.d.ts} +1 -1
- package/{constants.d.ts → ts/constants.d.ts} +1 -1
- package/{date.d.ts → ts/date.d.ts} +1 -1
- package/{describe.d.ts → ts/describe.d.ts} +1 -1
- package/{dispatch.d.ts → ts/dispatch.d.ts} +3 -3
- package/{either.d.ts → ts/either.d.ts} +8 -8
- package/{fail.d.ts → ts/fail.d.ts} +1 -1
- package/ts/index.d.ts +42 -0
- package/ts/inline.d.ts +3 -0
- package/{instanceOf.d.ts → ts/instanceOf.d.ts} +1 -1
- package/{json.d.ts → ts/json.d.ts} +1 -1
- package/{lazy.d.ts → ts/lazy.d.ts} +1 -1
- package/{mapping.d.ts → ts/mapping.d.ts} +1 -1
- package/{number.d.ts → ts/number.d.ts} +1 -1
- package/{object.d.ts → ts/object.d.ts} +8 -8
- package/{optional.d.ts → ts/optional.d.ts} +1 -1
- package/ts/result.d.ts +39 -0
- package/ts/short.d.ts +3 -0
- package/{string.d.ts → ts/string.d.ts} +1 -1
- package/{tuple.d.ts → ts/tuple.d.ts} +5 -5
- package/array.js +0 -133
- package/boolean.js +0 -42
- package/constants.js +0 -67
- package/date.js +0 -42
- package/either.js +0 -85
- 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 -62
- package/index.d.ts +0 -38
- package/index.js +0 -397
- package/instanceOf.js +0 -17
- package/mapping.js +0 -113
- package/mapping.js.flow +0 -71
- package/number.js +0 -38
- package/object.js +0 -254
- package/optional.js +0 -52
- package/tuple.js +0 -199
- package/types.js +0 -5
- package/types.js.flow +0 -26
- package/utils.js +0 -70
package/es/result.js
ADDED
|
@@ -0,0 +1,139 @@
|
|
|
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
|
+
}
|
|
@@ -0,0 +1,91 @@
|
|
|
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
|
+
}
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
import * as Result from '../result';
|
|
2
|
+
import { annotate } from '../annotate';
|
|
3
|
+
import { map } from './composition';
|
|
4
|
+
import { number } from './number';
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* Decoder that only returns Ok for boolean inputs. Err otherwise.
|
|
8
|
+
*/
|
|
9
|
+
var _boolean = function _boolean(blob) {
|
|
10
|
+
return typeof blob === 'boolean' ? Result.ok(blob) : Result.err(annotate(blob, 'Must be boolean'));
|
|
11
|
+
};
|
|
12
|
+
/**
|
|
13
|
+
* Decoder that returns true for all truthy values, and false otherwise. Never fails.
|
|
14
|
+
*/
|
|
15
|
+
|
|
16
|
+
|
|
17
|
+
export { _boolean as boolean };
|
|
18
|
+
export var truthy = function truthy(blob) {
|
|
19
|
+
return Result.ok(!!blob);
|
|
20
|
+
};
|
|
21
|
+
/**
|
|
22
|
+
* Decoder that only returns Ok for numeric input values representing booleans.
|
|
23
|
+
* Returns their boolean representation. Err otherwise.
|
|
24
|
+
*/
|
|
25
|
+
|
|
26
|
+
export var numericBoolean = map(number, function (n) {
|
|
27
|
+
return !!n;
|
|
28
|
+
});
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
import * as Result from '../result';
|
|
2
|
+
import { annotate } from '../annotate';
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* Given a decoder T and a mapping function from T's to V's, returns a decoder
|
|
6
|
+
* for V's. This is useful to change the original input data.
|
|
7
|
+
*/
|
|
8
|
+
export function map(decoder, mapper) {
|
|
9
|
+
return compose(decoder, function (x) {
|
|
10
|
+
try {
|
|
11
|
+
return Result.ok(mapper(x));
|
|
12
|
+
} catch (e) {
|
|
13
|
+
return Result.err(annotate(x, e instanceof Error ? e.message : String(e)));
|
|
14
|
+
}
|
|
15
|
+
});
|
|
16
|
+
}
|
|
17
|
+
/**
|
|
18
|
+
* Compose two decoders by passing the result of the first into the second.
|
|
19
|
+
* The second decoder may assume as its input type the output type of the first
|
|
20
|
+
* decoder (so it's not necessary to accept the typical "mixed"). This is
|
|
21
|
+
* useful for "narrowing down" the checks. For example, if you want to write
|
|
22
|
+
* a decoder for positive numbers, you can compose it from an existing decoder
|
|
23
|
+
* for any number, and a decoder that, assuming a number, checks if it's
|
|
24
|
+
* positive. Very often combined with the predicate() helper as the second
|
|
25
|
+
* argument.
|
|
26
|
+
*/
|
|
27
|
+
|
|
28
|
+
export function compose(decoder, next) {
|
|
29
|
+
return function (blob) {
|
|
30
|
+
return Result.andThen(decoder(blob), next);
|
|
31
|
+
};
|
|
32
|
+
}
|
|
33
|
+
/**
|
|
34
|
+
* Factory function returning a Decoder<T>, given a predicate function that
|
|
35
|
+
* accepts/rejects the input of type T.
|
|
36
|
+
*/
|
|
37
|
+
|
|
38
|
+
export function predicate(predicate, msg) {
|
|
39
|
+
return function (value) {
|
|
40
|
+
return predicate(value) ? Result.ok(value) : Result.err(annotate(value, msg));
|
|
41
|
+
};
|
|
42
|
+
}
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
import * as Result from '../result';
|
|
2
|
+
import { annotate } from '../annotate';
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* Decoder that only returns Ok for `null` inputs. Err otherwise.
|
|
6
|
+
*/
|
|
7
|
+
export var null_ = function null_(blob) {
|
|
8
|
+
return blob === null ? Result.ok(blob) : Result.err(annotate(blob, 'Must be null'));
|
|
9
|
+
};
|
|
10
|
+
/**
|
|
11
|
+
* Decoder that only returns Ok for `undefined` inputs. Err otherwise.
|
|
12
|
+
*/
|
|
13
|
+
|
|
14
|
+
export var undefined_ = function undefined_(blob) {
|
|
15
|
+
return blob === undefined ? Result.ok(blob) : Result.err(annotate(blob, 'Must be undefined'));
|
|
16
|
+
};
|
|
17
|
+
/**
|
|
18
|
+
* Decoder that only returns Ok for the given value constant. Err otherwise.
|
|
19
|
+
*/
|
|
20
|
+
|
|
21
|
+
export function constant(value) {
|
|
22
|
+
return function (blob) {
|
|
23
|
+
return blob === value ? Result.ok(value) : Result.err(annotate(blob, "Must be constant " + String(value)));
|
|
24
|
+
};
|
|
25
|
+
}
|
|
26
|
+
/**
|
|
27
|
+
* Decoder that always returns Ok for the given hardcoded value, no matter what the input.
|
|
28
|
+
*/
|
|
29
|
+
|
|
30
|
+
export function hardcoded(value) {
|
|
31
|
+
return function () {
|
|
32
|
+
return Result.ok(value);
|
|
33
|
+
};
|
|
34
|
+
}
|
|
35
|
+
/**
|
|
36
|
+
* Decoder that always returns Ok for the given hardcoded value, no matter what the input.
|
|
37
|
+
*/
|
|
38
|
+
|
|
39
|
+
export var unknown = function unknown(blob) {
|
|
40
|
+
return Result.ok(blob);
|
|
41
|
+
};
|
|
42
|
+
/**
|
|
43
|
+
* Alias of unknown.
|
|
44
|
+
*/
|
|
45
|
+
|
|
46
|
+
export var mixed = unknown;
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
import * as Result from '../result';
|
|
2
|
+
import { annotate } from '../annotate';
|
|
3
|
+
import { isDate } from '../_utils';
|
|
4
|
+
import { map } from './composition';
|
|
5
|
+
import { regex } from './string';
|
|
6
|
+
// Only matches the shape. This "over-matches" some values that still aren't
|
|
7
|
+
// valid dates (like 9999-99-99), but those will be caught by JS Date's
|
|
8
|
+
// internal validations
|
|
9
|
+
var iso8601_re = /^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}(?:[.]\d+)?(?:Z|[+-]\d{2}:?\d{2})$/;
|
|
10
|
+
export var date = function date(value) {
|
|
11
|
+
return isDate(value) ? Result.ok(value) : Result.err(annotate(value, 'Must be a Date'));
|
|
12
|
+
};
|
|
13
|
+
/**
|
|
14
|
+
* Decoder that only returns Ok for strings that are valid ISO8601 date
|
|
15
|
+
* strings. Err otherwise.
|
|
16
|
+
*/
|
|
17
|
+
|
|
18
|
+
export var iso8601 = map( // Input itself needs to match the ISO8601 regex...
|
|
19
|
+
regex(iso8601_re, 'Must be ISO8601 format'), // Make sure it is a _valid_ date
|
|
20
|
+
function (value) {
|
|
21
|
+
var date = new Date(value);
|
|
22
|
+
|
|
23
|
+
if (isNaN(date.getTime())) {
|
|
24
|
+
throw new Error('Must be valid date/time value');
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
return date;
|
|
28
|
+
});
|
|
@@ -1,11 +1,5 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
Object.defineProperty(exports, "__esModule", {
|
|
4
|
-
value: true
|
|
5
|
-
});
|
|
6
|
-
exports.describe = describe;
|
|
7
|
-
|
|
8
|
-
var _debrief = require("debrief");
|
|
1
|
+
import * as Result from '../result';
|
|
2
|
+
import { annotate } from '../annotate';
|
|
9
3
|
|
|
10
4
|
/**
|
|
11
5
|
* Wrap another decoder, and override the error message in case it fails. This
|
|
@@ -13,10 +7,10 @@ var _debrief = require("debrief");
|
|
|
13
7
|
* use language in those error messages that can be relayed to end users (for
|
|
14
8
|
* example to show in form errors).
|
|
15
9
|
*/
|
|
16
|
-
function describe(decoder, message) {
|
|
10
|
+
export function describe(decoder, message) {
|
|
17
11
|
return function (blob) {
|
|
18
|
-
return decoder(blob)
|
|
19
|
-
return
|
|
12
|
+
return Result.mapError(decoder(blob), function (err) {
|
|
13
|
+
return annotate(err, message);
|
|
20
14
|
});
|
|
21
15
|
};
|
|
22
16
|
}
|
|
@@ -1,15 +1,6 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
value: true
|
|
5
|
-
});
|
|
6
|
-
exports.dispatch = dispatch;
|
|
7
|
-
|
|
8
|
-
var _either = require("./either");
|
|
9
|
-
|
|
10
|
-
var _object2 = require("./object");
|
|
11
|
-
|
|
12
|
-
function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
|
|
1
|
+
import * as Result from '../result';
|
|
2
|
+
import { object } from './object';
|
|
3
|
+
import { oneOf } from './either';
|
|
13
4
|
|
|
14
5
|
/**
|
|
15
6
|
* Dispatches to one of several given decoders, based on the value found at
|
|
@@ -46,10 +37,12 @@ function _defineProperty(obj, key, value) { if (key in obj) { Object.definePrope
|
|
|
46
37
|
*
|
|
47
38
|
* Also, the error messages will be less ambiguous using `dispatch()`.
|
|
48
39
|
*/
|
|
49
|
-
function dispatch(field, mapping) {
|
|
50
|
-
var
|
|
40
|
+
export function dispatch(field, mapping) {
|
|
41
|
+
var _object;
|
|
42
|
+
|
|
43
|
+
var base = object((_object = {}, _object[field] = oneOf(Object.keys(mapping)), _object));
|
|
51
44
|
return function (blob) {
|
|
52
|
-
return base(blob)
|
|
45
|
+
return Result.andThen(base(blob), function (baseObj) {
|
|
53
46
|
var decoderName = baseObj[field];
|
|
54
47
|
var decoder = mapping[decoderName];
|
|
55
48
|
return decoder(blob);
|
|
@@ -0,0 +1,90 @@
|
|
|
1
|
+
import * as Result from '../result';
|
|
2
|
+
import { annotate } from '../annotate';
|
|
3
|
+
import { indent, summarize } from '../_utils';
|
|
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 Result.orElse(d1(blob), function (err1) {
|
|
46
|
+
return Result.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 Result.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 Result.ok(winner);
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
return Result.err(annotate(blob, "Must be one of " + constants.map(function (value) {
|
|
87
|
+
return JSON.stringify(value);
|
|
88
|
+
}).join(', ')));
|
|
89
|
+
};
|
|
90
|
+
}
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import * as Result from '../result';
|
|
2
|
+
import { annotate } from '../annotate';
|
|
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 Result.err(annotate(blob, msg));
|
|
10
|
+
};
|
|
11
|
+
}
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import * as Result from '../result';
|
|
2
|
+
import { annotate } from '../annotate';
|
|
3
|
+
export function instanceOf(klass) {
|
|
4
|
+
return function (blob) {
|
|
5
|
+
return blob instanceof klass ? Result.ok(blob) : Result.err(annotate(blob, "Must be " + // $FlowFixMe[incompatible-use] - klass.name is fine?
|
|
6
|
+
klass.name + " instance"));
|
|
7
|
+
};
|
|
8
|
+
}
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import { array } from './array';
|
|
2
|
+
import { boolean as _boolean } from './boolean';
|
|
3
|
+
import { dict } from './mapping';
|
|
4
|
+
import { either6 } from './either';
|
|
5
|
+
import { lazy } from './lazy';
|
|
6
|
+
import { null_ } from './constants';
|
|
7
|
+
import { number } from './number';
|
|
8
|
+
import { string } from './string';
|
|
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);
|
|
@@ -0,0 +1,11 @@
|
|
|
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
|
+
}
|
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
import * as Result from '../result';
|
|
2
|
+
import { annotateObject } from '../annotate';
|
|
3
|
+
import { compose, map } from './composition';
|
|
4
|
+
import { merge } from '../annotate';
|
|
5
|
+
import { pojo } from './object';
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* Given an object, will decode a Map of string keys to whatever values.
|
|
9
|
+
*
|
|
10
|
+
* For example, given a decoder for a Person, we can verify a Person lookup
|
|
11
|
+
* table structure (of type Map<string, Person>) like so:
|
|
12
|
+
*
|
|
13
|
+
* mapping(person)
|
|
14
|
+
*
|
|
15
|
+
*/
|
|
16
|
+
export function mapping(decoder) {
|
|
17
|
+
return compose(pojo, function (blob) {
|
|
18
|
+
var tuples = [];
|
|
19
|
+
var errors = null;
|
|
20
|
+
Object.keys(blob).forEach(function (key) {
|
|
21
|
+
var value = blob[key];
|
|
22
|
+
var result = decoder(value);
|
|
23
|
+
|
|
24
|
+
if (result.type === 'ok') {
|
|
25
|
+
if (errors === null) {
|
|
26
|
+
tuples.push([key, result.value]);
|
|
27
|
+
}
|
|
28
|
+
} else {
|
|
29
|
+
tuples.length = 0; // Clear the tuples array
|
|
30
|
+
|
|
31
|
+
if (errors === null) {
|
|
32
|
+
errors = {};
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
errors[key] = result.error;
|
|
36
|
+
}
|
|
37
|
+
});
|
|
38
|
+
|
|
39
|
+
if (errors !== null) {
|
|
40
|
+
return Result.err(merge(annotateObject(blob), errors));
|
|
41
|
+
} else {
|
|
42
|
+
return Result.ok(new Map(tuples));
|
|
43
|
+
}
|
|
44
|
+
});
|
|
45
|
+
}
|
|
46
|
+
/**
|
|
47
|
+
* Like mapping(), but returns an object rather than a Map instance.
|
|
48
|
+
*/
|
|
49
|
+
|
|
50
|
+
export function dict(decoder) {
|
|
51
|
+
return map(mapping(decoder), function (m) {
|
|
52
|
+
return Object.fromEntries(m);
|
|
53
|
+
});
|
|
54
|
+
}
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
import * as Result from '../result';
|
|
2
|
+
import { annotate } from '../annotate';
|
|
3
|
+
import { compose, predicate } from './composition';
|
|
4
|
+
|
|
5
|
+
var anyNumber = function anyNumber(blob) {
|
|
6
|
+
return typeof blob === 'number' && !Number.isNaN(blob) ? Result.ok(blob) : Result.err(annotate(blob, 'Must be number'));
|
|
7
|
+
};
|
|
8
|
+
|
|
9
|
+
var isInteger = function isInteger(n) {
|
|
10
|
+
return Number.isInteger(n);
|
|
11
|
+
};
|
|
12
|
+
|
|
13
|
+
var isFinite = function isFinite(n) {
|
|
14
|
+
return Number.isFinite(n);
|
|
15
|
+
};
|
|
16
|
+
|
|
17
|
+
export var number = compose(anyNumber, predicate(isFinite, 'Number must be finite'));
|
|
18
|
+
export var positiveNumber = compose(number, predicate(function (n) {
|
|
19
|
+
return n >= 0;
|
|
20
|
+
}, 'Number must be positive')); // Integers
|
|
21
|
+
|
|
22
|
+
export var integer = compose(number, predicate(isInteger, 'Number must be an integer'));
|
|
23
|
+
export var positiveInteger = compose(integer, predicate(function (n) {
|
|
24
|
+
return n >= 0;
|
|
25
|
+
}, 'Number must be positive'));
|