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
|
@@ -1,21 +1,22 @@
|
|
|
1
1
|
// @flow strict
|
|
2
2
|
|
|
3
|
-
import
|
|
4
|
-
import {
|
|
5
|
-
|
|
6
|
-
import type { Decoder, Scalar } from './types';
|
|
3
|
+
import * as Result from '../result';
|
|
4
|
+
import { annotate } from '../annotate';
|
|
5
|
+
import type { Decoder, Scalar } from '../_types';
|
|
7
6
|
|
|
8
7
|
/**
|
|
9
8
|
* Decoder that only returns Ok for `null` inputs. Err otherwise.
|
|
10
9
|
*/
|
|
11
10
|
export const null_: Decoder<null> = (blob: mixed) =>
|
|
12
|
-
blob === null ?
|
|
11
|
+
blob === null ? Result.ok(blob) : Result.err(annotate(blob, 'Must be null'));
|
|
13
12
|
|
|
14
13
|
/**
|
|
15
14
|
* Decoder that only returns Ok for `undefined` inputs. Err otherwise.
|
|
16
15
|
*/
|
|
17
16
|
export const undefined_: Decoder<void> = (blob: mixed) =>
|
|
18
|
-
blob === undefined
|
|
17
|
+
blob === undefined
|
|
18
|
+
? Result.ok(blob)
|
|
19
|
+
: Result.err(annotate(blob, 'Must be undefined'));
|
|
19
20
|
|
|
20
21
|
/**
|
|
21
22
|
* Decoder that only returns Ok for the given value constant. Err otherwise.
|
|
@@ -23,23 +24,23 @@ export const undefined_: Decoder<void> = (blob: mixed) =>
|
|
|
23
24
|
export function constant<T: Scalar>(value: T): Decoder<T> {
|
|
24
25
|
return (blob: mixed) =>
|
|
25
26
|
blob === value
|
|
26
|
-
?
|
|
27
|
-
:
|
|
27
|
+
? Result.ok(value)
|
|
28
|
+
: Result.err(annotate(blob, `Must be constant ${String(value)}`));
|
|
28
29
|
}
|
|
29
30
|
|
|
30
31
|
/**
|
|
31
32
|
* Decoder that always returns Ok for the given hardcoded value, no matter what the input.
|
|
32
33
|
*/
|
|
33
34
|
export function hardcoded<T>(value: T): Decoder<T> {
|
|
34
|
-
return (
|
|
35
|
+
return () => Result.ok(value);
|
|
35
36
|
}
|
|
36
37
|
|
|
37
38
|
/**
|
|
38
39
|
* Decoder that always returns Ok for the given hardcoded value, no matter what the input.
|
|
39
40
|
*/
|
|
40
|
-
export const
|
|
41
|
+
export const unknown: Decoder<mixed> = (blob: mixed) => Result.ok(blob);
|
|
41
42
|
|
|
42
43
|
/**
|
|
43
|
-
* Alias of
|
|
44
|
+
* Alias of unknown.
|
|
44
45
|
*/
|
|
45
|
-
export const
|
|
46
|
+
export const mixed: Decoder<mixed> = unknown;
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
exports.__esModule = true;
|
|
4
|
+
exports.iso8601 = exports.date = void 0;
|
|
5
|
+
|
|
6
|
+
var Result = _interopRequireWildcard(require("../result"));
|
|
7
|
+
|
|
8
|
+
var _annotate = require("../annotate");
|
|
9
|
+
|
|
10
|
+
var _utils = require("../_utils");
|
|
11
|
+
|
|
12
|
+
var _composition = require("./composition");
|
|
13
|
+
|
|
14
|
+
var _string = require("./string");
|
|
15
|
+
|
|
16
|
+
function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function _getRequireWildcardCache(nodeInterop) { return nodeInterop ? cacheNodeInterop : cacheBabelInterop; })(nodeInterop); }
|
|
17
|
+
|
|
18
|
+
function _interopRequireWildcard(obj, nodeInterop) { if (!nodeInterop && obj && obj.__esModule) { return obj; } if (obj === null || typeof obj !== "object" && typeof obj !== "function") { return { "default": obj }; } var cache = _getRequireWildcardCache(nodeInterop); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj["default"] = obj; if (cache) { cache.set(obj, newObj); } return newObj; }
|
|
19
|
+
|
|
20
|
+
// Only matches the shape. This "over-matches" some values that still aren't
|
|
21
|
+
// valid dates (like 9999-99-99), but those will be caught by JS Date's
|
|
22
|
+
// internal validations
|
|
23
|
+
var iso8601_re = /^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}(?:[.]\d+)?(?:Z|[+-]\d{2}:?\d{2})$/;
|
|
24
|
+
|
|
25
|
+
var date = function date(value) {
|
|
26
|
+
return (0, _utils.isDate)(value) ? Result.ok(value) : Result.err((0, _annotate.annotate)(value, 'Must be a Date'));
|
|
27
|
+
};
|
|
28
|
+
/**
|
|
29
|
+
* Decoder that only returns Ok for strings that are valid ISO8601 date
|
|
30
|
+
* strings. Err otherwise.
|
|
31
|
+
*/
|
|
32
|
+
|
|
33
|
+
|
|
34
|
+
exports.date = date;
|
|
35
|
+
var iso8601 = (0, _composition.map)( // Input itself needs to match the ISO8601 regex...
|
|
36
|
+
(0, _string.regex)(iso8601_re, 'Must be ISO8601 format'), // Make sure it is a _valid_ date
|
|
37
|
+
function (value) {
|
|
38
|
+
var date = new Date(value);
|
|
39
|
+
|
|
40
|
+
if (isNaN(date.getTime())) {
|
|
41
|
+
throw new Error('Must be valid date/time value');
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
return date;
|
|
45
|
+
});
|
|
46
|
+
exports.iso8601 = iso8601;
|
|
@@ -1,11 +1,11 @@
|
|
|
1
1
|
// @flow strict
|
|
2
2
|
|
|
3
|
-
import
|
|
4
|
-
import {
|
|
5
|
-
|
|
3
|
+
import * as Result from '../result';
|
|
4
|
+
import { annotate } from '../annotate';
|
|
5
|
+
import { isDate } from '../_utils';
|
|
6
|
+
import { map } from './composition';
|
|
6
7
|
import { regex } from './string';
|
|
7
|
-
import type { Decoder } from '
|
|
8
|
-
import { isDate, map } from './utils';
|
|
8
|
+
import type { Decoder } from '../_types';
|
|
9
9
|
|
|
10
10
|
// $FlowFixMe[unclear-type] (not really an issue) - deliberate casting
|
|
11
11
|
type cast = any;
|
|
@@ -17,7 +17,9 @@ const iso8601_re =
|
|
|
17
17
|
/^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}(?:[.]\d+)?(?:Z|[+-]\d{2}:?\d{2})$/;
|
|
18
18
|
|
|
19
19
|
export const date: Decoder<Date> = (value: mixed) =>
|
|
20
|
-
isDate(value)
|
|
20
|
+
isDate(value)
|
|
21
|
+
? Result.ok(((value: cast): Date))
|
|
22
|
+
: Result.err(annotate(value, 'Must be a Date'));
|
|
21
23
|
|
|
22
24
|
/**
|
|
23
25
|
* Decoder that only returns Ok for strings that are valid ISO8601 date
|
|
@@ -34,5 +36,5 @@ export const iso8601: Decoder<Date> = map(
|
|
|
34
36
|
throw new Error('Must be valid date/time value');
|
|
35
37
|
}
|
|
36
38
|
return date;
|
|
37
|
-
}
|
|
39
|
+
},
|
|
38
40
|
);
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
exports.__esModule = true;
|
|
4
|
+
exports.describe = describe;
|
|
5
|
+
|
|
6
|
+
var Result = _interopRequireWildcard(require("../result"));
|
|
7
|
+
|
|
8
|
+
var _annotate = require("../annotate");
|
|
9
|
+
|
|
10
|
+
function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function _getRequireWildcardCache(nodeInterop) { return nodeInterop ? cacheNodeInterop : cacheBabelInterop; })(nodeInterop); }
|
|
11
|
+
|
|
12
|
+
function _interopRequireWildcard(obj, nodeInterop) { if (!nodeInterop && obj && obj.__esModule) { return obj; } if (obj === null || typeof obj !== "object" && typeof obj !== "function") { return { "default": obj }; } var cache = _getRequireWildcardCache(nodeInterop); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj["default"] = obj; if (cache) { cache.set(obj, newObj); } return newObj; }
|
|
13
|
+
|
|
14
|
+
/**
|
|
15
|
+
* Wrap another decoder, and override the error message in case it fails. This
|
|
16
|
+
* is useful to "simplify" otherwise potentially complex error messages, or to
|
|
17
|
+
* use language in those error messages that can be relayed to end users (for
|
|
18
|
+
* example to show in form errors).
|
|
19
|
+
*/
|
|
20
|
+
function describe(decoder, message) {
|
|
21
|
+
return function (blob) {
|
|
22
|
+
return Result.mapError(decoder(blob), function (err) {
|
|
23
|
+
return (0, _annotate.annotate)(err, message);
|
|
24
|
+
});
|
|
25
|
+
};
|
|
26
|
+
}
|
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
// @flow strict
|
|
2
2
|
|
|
3
|
-
import
|
|
4
|
-
|
|
5
|
-
import type { Decoder } from '
|
|
3
|
+
import * as Result from '../result';
|
|
4
|
+
import { annotate } from '../annotate';
|
|
5
|
+
import type { Decoder } from '../_types';
|
|
6
6
|
|
|
7
7
|
/**
|
|
8
8
|
* Wrap another decoder, and override the error message in case it fails. This
|
|
@@ -12,6 +12,6 @@ import type { Decoder } from './types';
|
|
|
12
12
|
*/
|
|
13
13
|
export function describe<T>(decoder: Decoder<T>, message: string): Decoder<T> {
|
|
14
14
|
return (blob: mixed) => {
|
|
15
|
-
return decoder(blob)
|
|
15
|
+
return Result.mapError(decoder(blob), (err) => annotate(err, message));
|
|
16
16
|
};
|
|
17
17
|
}
|
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
exports.__esModule = true;
|
|
4
|
+
exports.dispatch = dispatch;
|
|
5
|
+
|
|
6
|
+
var Result = _interopRequireWildcard(require("../result"));
|
|
7
|
+
|
|
8
|
+
var _object2 = require("./object");
|
|
9
|
+
|
|
10
|
+
var _either = require("./either");
|
|
11
|
+
|
|
12
|
+
function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function _getRequireWildcardCache(nodeInterop) { return nodeInterop ? cacheNodeInterop : cacheBabelInterop; })(nodeInterop); }
|
|
13
|
+
|
|
14
|
+
function _interopRequireWildcard(obj, nodeInterop) { if (!nodeInterop && obj && obj.__esModule) { return obj; } if (obj === null || typeof obj !== "object" && typeof obj !== "function") { return { "default": obj }; } var cache = _getRequireWildcardCache(nodeInterop); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj["default"] = obj; if (cache) { cache.set(obj, newObj); } return newObj; }
|
|
15
|
+
|
|
16
|
+
/**
|
|
17
|
+
* Dispatches to one of several given decoders, based on the value found at
|
|
18
|
+
* runtime in the given field. For example, suppose you have these decoders:
|
|
19
|
+
*
|
|
20
|
+
* const rectangle = object({
|
|
21
|
+
* type: constant('rect'),
|
|
22
|
+
* x: number,
|
|
23
|
+
* y: number,
|
|
24
|
+
* width: number,
|
|
25
|
+
* height: number,
|
|
26
|
+
* });
|
|
27
|
+
*
|
|
28
|
+
* const circle = object({
|
|
29
|
+
* type: constant('circle'),
|
|
30
|
+
* cx: number,
|
|
31
|
+
* cy: number,
|
|
32
|
+
* r: number,
|
|
33
|
+
* });
|
|
34
|
+
*
|
|
35
|
+
* Then these two decoders are equivalent:
|
|
36
|
+
*
|
|
37
|
+
* const shape = either(rectangle, circle)
|
|
38
|
+
* const shape = dispatch('type', { rectangle, circle })
|
|
39
|
+
*
|
|
40
|
+
* Will be of type Decoder<Rectangle | Circle>.
|
|
41
|
+
*
|
|
42
|
+
* But the dispatch version will typically be more runtime-efficient. The
|
|
43
|
+
* reason is that it will first do minimal work to "look ahead" into the `type`
|
|
44
|
+
* field here, and based on that value, pick the decoder to invoke.
|
|
45
|
+
*
|
|
46
|
+
* The `either` version will simply try to invoke each decoder, until it finds
|
|
47
|
+
* one that matches.
|
|
48
|
+
*
|
|
49
|
+
* Also, the error messages will be less ambiguous using `dispatch()`.
|
|
50
|
+
*/
|
|
51
|
+
function dispatch(field, mapping) {
|
|
52
|
+
var _object;
|
|
53
|
+
|
|
54
|
+
var base = (0, _object2.object)((_object = {}, _object[field] = (0, _either.oneOf)(Object.keys(mapping)), _object));
|
|
55
|
+
return function (blob) {
|
|
56
|
+
return Result.andThen(base(blob), function (baseObj) {
|
|
57
|
+
var decoderName = baseObj[field];
|
|
58
|
+
var decoder = mapping[decoderName];
|
|
59
|
+
return decoder(blob);
|
|
60
|
+
});
|
|
61
|
+
};
|
|
62
|
+
}
|
|
@@ -1,8 +1,9 @@
|
|
|
1
1
|
// @flow strict
|
|
2
2
|
|
|
3
|
-
import
|
|
3
|
+
import * as Result from '../result';
|
|
4
4
|
import { object } from './object';
|
|
5
|
-
import
|
|
5
|
+
import { oneOf } from './either';
|
|
6
|
+
import type { Decoder, DecoderType } from '../_types';
|
|
6
7
|
|
|
7
8
|
// $FlowFixMe[unclear-type] (not really an issue) - deliberate use of `any` - not sure how we should get rid of this
|
|
8
9
|
type anything = any;
|
|
@@ -44,11 +45,11 @@ type anything = any;
|
|
|
44
45
|
*/
|
|
45
46
|
export function dispatch<O: { +[field: string]: Decoder<anything>, ... }>(
|
|
46
47
|
field: string,
|
|
47
|
-
mapping: O
|
|
48
|
-
): Decoder<$Values<$ObjMap<O,
|
|
48
|
+
mapping: O,
|
|
49
|
+
): Decoder<$Values<$ObjMap<O, DecoderType>>> {
|
|
49
50
|
const base = object({ [field]: oneOf(Object.keys(mapping)) });
|
|
50
51
|
return (blob: mixed) => {
|
|
51
|
-
return base(blob)
|
|
52
|
+
return Result.andThen(base(blob), (baseObj) => {
|
|
52
53
|
const decoderName = baseObj[field];
|
|
53
54
|
const decoder = mapping[decoderName];
|
|
54
55
|
return decoder(blob);
|
|
@@ -0,0 +1,117 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
exports.__esModule = true;
|
|
4
|
+
exports.either = either;
|
|
5
|
+
exports.either3 = either3;
|
|
6
|
+
exports.either4 = either4;
|
|
7
|
+
exports.either5 = either5;
|
|
8
|
+
exports.either6 = either6;
|
|
9
|
+
exports.either7 = either7;
|
|
10
|
+
exports.either8 = either8;
|
|
11
|
+
exports.either9 = either9;
|
|
12
|
+
exports.oneOf = oneOf;
|
|
13
|
+
|
|
14
|
+
var Result = _interopRequireWildcard(require("../result"));
|
|
15
|
+
|
|
16
|
+
var _annotate = require("../annotate");
|
|
17
|
+
|
|
18
|
+
var _utils = require("../_utils");
|
|
19
|
+
|
|
20
|
+
function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function _getRequireWildcardCache(nodeInterop) { return nodeInterop ? cacheNodeInterop : cacheBabelInterop; })(nodeInterop); }
|
|
21
|
+
|
|
22
|
+
function _interopRequireWildcard(obj, nodeInterop) { if (!nodeInterop && obj && obj.__esModule) { return obj; } if (obj === null || typeof obj !== "object" && typeof obj !== "function") { return { "default": obj }; } var cache = _getRequireWildcardCache(nodeInterop); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj["default"] = obj; if (cache) { cache.set(obj, newObj); } return newObj; }
|
|
23
|
+
|
|
24
|
+
/**
|
|
25
|
+
* Indents and adds a dash in front of this (potentially multiline) string.
|
|
26
|
+
*/
|
|
27
|
+
function itemize(s) {
|
|
28
|
+
return '-' + (0, _utils.indent)(s).substring(1);
|
|
29
|
+
}
|
|
30
|
+
/**
|
|
31
|
+
* Nests another error as an item under a new-to-be-created "Either error". If
|
|
32
|
+
* the given subitem already is an "Either error" of itself, don't indent, but
|
|
33
|
+
* just "inject" its items at the same error level, for nicely flattened either
|
|
34
|
+
* expressions.
|
|
35
|
+
*
|
|
36
|
+
* Avoids:
|
|
37
|
+
*
|
|
38
|
+
* Either:
|
|
39
|
+
* - Either:
|
|
40
|
+
* - Must be P
|
|
41
|
+
* - Either:
|
|
42
|
+
* - Must be Q
|
|
43
|
+
* - Must be R
|
|
44
|
+
* - Must be S
|
|
45
|
+
*
|
|
46
|
+
* And "flattens" these to:
|
|
47
|
+
*
|
|
48
|
+
* Either:
|
|
49
|
+
* - Must be P
|
|
50
|
+
* - Must be Q
|
|
51
|
+
* - Must be R
|
|
52
|
+
* - Must be S
|
|
53
|
+
*
|
|
54
|
+
*/
|
|
55
|
+
|
|
56
|
+
|
|
57
|
+
function nest(errText) {
|
|
58
|
+
var EITHER_PREFIX = 'Either:\n';
|
|
59
|
+
return errText.startsWith(EITHER_PREFIX) ? errText.substr(EITHER_PREFIX.length) : itemize(errText);
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
function either(d1, d2) {
|
|
63
|
+
return function (blob) {
|
|
64
|
+
return Result.orElse(d1(blob), function (err1) {
|
|
65
|
+
return Result.orElse(d2(blob), function (err2) {
|
|
66
|
+
var serr1 = (0, _utils.summarize)(err1).join('\n');
|
|
67
|
+
var serr2 = (0, _utils.summarize)(err2).join('\n');
|
|
68
|
+
var text = ['Either:', nest(serr1), nest(serr2)].join('\n');
|
|
69
|
+
return Result.err((0, _annotate.annotate)(blob, text));
|
|
70
|
+
});
|
|
71
|
+
});
|
|
72
|
+
};
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
function either3(d1, d2, d3) {
|
|
76
|
+
return either(d1, either(d2, d3));
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
function either4(d1, d2, d3, d4) {
|
|
80
|
+
return either(d1, either3(d2, d3, d4));
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
function either5(d1, d2, d3, d4, d5) {
|
|
84
|
+
return either(d1, either4(d2, d3, d4, d5));
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
function either6(d1, d2, d3, d4, d5, d6) {
|
|
88
|
+
return either(d1, either5(d2, d3, d4, d5, d6));
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
function either7(d1, d2, d3, d4, d5, d6, d7) {
|
|
92
|
+
return either(d1, either6(d2, d3, d4, d5, d6, d7));
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
function either8(d1, d2, d3, d4, d5, d6, d7, d8) {
|
|
96
|
+
return either(d1, either7(d2, d3, d4, d5, d6, d7, d8));
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
function either9(d1, d2, d3, d4, d5, d6, d7, d8, d9) {
|
|
100
|
+
return either(d1, either8(d2, d3, d4, d5, d6, d7, d8, d9));
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
function oneOf(constants) {
|
|
104
|
+
return function (blob) {
|
|
105
|
+
var winner = constants.find(function (c) {
|
|
106
|
+
return c === blob;
|
|
107
|
+
});
|
|
108
|
+
|
|
109
|
+
if (winner !== undefined) {
|
|
110
|
+
return Result.ok(winner);
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
return Result.err((0, _annotate.annotate)(blob, "Must be one of " + constants.map(function (value) {
|
|
114
|
+
return JSON.stringify(value);
|
|
115
|
+
}).join(', ')));
|
|
116
|
+
};
|
|
117
|
+
}
|
|
@@ -1,45 +1,65 @@
|
|
|
1
1
|
// @flow strict
|
|
2
2
|
|
|
3
|
-
import
|
|
4
|
-
import {
|
|
5
|
-
import {
|
|
6
|
-
|
|
7
|
-
import type { Decoder, Scalar } from './types';
|
|
3
|
+
import * as Result from '../result';
|
|
4
|
+
import { annotate } from '../annotate';
|
|
5
|
+
import { indent, summarize } from '../_utils';
|
|
6
|
+
import type { Decoder, Scalar } from '../_types';
|
|
8
7
|
|
|
9
8
|
/**
|
|
10
9
|
* Indents and adds a dash in front of this (potentially multiline) string.
|
|
11
10
|
*/
|
|
12
|
-
|
|
13
|
-
function itemize(s: string = ''): string {
|
|
11
|
+
function itemize(s: string): string {
|
|
14
12
|
return '-' + indent(s).substring(1);
|
|
15
13
|
}
|
|
16
14
|
|
|
15
|
+
/**
|
|
16
|
+
* Nests another error as an item under a new-to-be-created "Either error". If
|
|
17
|
+
* the given subitem already is an "Either error" of itself, don't indent, but
|
|
18
|
+
* just "inject" its items at the same error level, for nicely flattened either
|
|
19
|
+
* expressions.
|
|
20
|
+
*
|
|
21
|
+
* Avoids:
|
|
22
|
+
*
|
|
23
|
+
* Either:
|
|
24
|
+
* - Either:
|
|
25
|
+
* - Must be P
|
|
26
|
+
* - Either:
|
|
27
|
+
* - Must be Q
|
|
28
|
+
* - Must be R
|
|
29
|
+
* - Must be S
|
|
30
|
+
*
|
|
31
|
+
* And "flattens" these to:
|
|
32
|
+
*
|
|
33
|
+
* Either:
|
|
34
|
+
* - Must be P
|
|
35
|
+
* - Must be Q
|
|
36
|
+
* - Must be R
|
|
37
|
+
* - Must be S
|
|
38
|
+
*
|
|
39
|
+
*/
|
|
40
|
+
function nest(errText: string): string {
|
|
41
|
+
const EITHER_PREFIX = 'Either:\n';
|
|
42
|
+
return errText.startsWith(EITHER_PREFIX)
|
|
43
|
+
? errText.substr(EITHER_PREFIX.length)
|
|
44
|
+
: itemize(errText);
|
|
45
|
+
}
|
|
46
|
+
|
|
17
47
|
export function either<T1, T2>(d1: Decoder<T1>, d2: Decoder<T2>): Decoder<T1 | T2> {
|
|
18
48
|
return (blob: mixed) =>
|
|
19
|
-
d1(blob)
|
|
20
|
-
(
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
annotate(
|
|
27
|
-
blob,
|
|
28
|
-
[
|
|
29
|
-
'Either:',
|
|
30
|
-
itemize(summarize(err1).join('\n')),
|
|
31
|
-
itemize(summarize(err2).join('\n')),
|
|
32
|
-
].join('\n')
|
|
33
|
-
)
|
|
34
|
-
)
|
|
35
|
-
)
|
|
49
|
+
Result.orElse(d1(blob), (err1) =>
|
|
50
|
+
Result.orElse(d2(blob), (err2) => {
|
|
51
|
+
const serr1 = summarize(err1).join('\n');
|
|
52
|
+
const serr2 = summarize(err2).join('\n');
|
|
53
|
+
const text = ['Either:', nest(serr1), nest(serr2)].join('\n');
|
|
54
|
+
return Result.err(annotate(blob, text));
|
|
55
|
+
}),
|
|
36
56
|
);
|
|
37
57
|
}
|
|
38
58
|
|
|
39
59
|
export function either3<T1, T2, T3>(
|
|
40
60
|
d1: Decoder<T1>,
|
|
41
61
|
d2: Decoder<T2>,
|
|
42
|
-
d3: Decoder<T3
|
|
62
|
+
d3: Decoder<T3>,
|
|
43
63
|
): Decoder<T1 | T2 | T3> {
|
|
44
64
|
return either(d1, either(d2, d3));
|
|
45
65
|
}
|
|
@@ -48,7 +68,7 @@ export function either4<T1, T2, T3, T4>(
|
|
|
48
68
|
d1: Decoder<T1>,
|
|
49
69
|
d2: Decoder<T2>,
|
|
50
70
|
d3: Decoder<T3>,
|
|
51
|
-
d4: Decoder<T4
|
|
71
|
+
d4: Decoder<T4>,
|
|
52
72
|
): Decoder<T1 | T2 | T3 | T4> {
|
|
53
73
|
return either(d1, either3(d2, d3, d4));
|
|
54
74
|
}
|
|
@@ -58,7 +78,7 @@ export function either5<T1, T2, T3, T4, T5>(
|
|
|
58
78
|
d2: Decoder<T2>,
|
|
59
79
|
d3: Decoder<T3>,
|
|
60
80
|
d4: Decoder<T4>,
|
|
61
|
-
d5: Decoder<T5
|
|
81
|
+
d5: Decoder<T5>,
|
|
62
82
|
): Decoder<T1 | T2 | T3 | T4 | T5> {
|
|
63
83
|
return either(d1, either4(d2, d3, d4, d5));
|
|
64
84
|
}
|
|
@@ -69,7 +89,7 @@ export function either6<T1, T2, T3, T4, T5, T6>(
|
|
|
69
89
|
d3: Decoder<T3>,
|
|
70
90
|
d4: Decoder<T4>,
|
|
71
91
|
d5: Decoder<T5>,
|
|
72
|
-
d6: Decoder<T6
|
|
92
|
+
d6: Decoder<T6>,
|
|
73
93
|
): Decoder<T1 | T2 | T3 | T4 | T5 | T6> {
|
|
74
94
|
return either(d1, either5(d2, d3, d4, d5, d6));
|
|
75
95
|
}
|
|
@@ -81,7 +101,7 @@ export function either7<T1, T2, T3, T4, T5, T6, T7>(
|
|
|
81
101
|
d4: Decoder<T4>,
|
|
82
102
|
d5: Decoder<T5>,
|
|
83
103
|
d6: Decoder<T6>,
|
|
84
|
-
d7: Decoder<T7
|
|
104
|
+
d7: Decoder<T7>,
|
|
85
105
|
): Decoder<T1 | T2 | T3 | T4 | T5 | T6 | T7> {
|
|
86
106
|
return either(d1, either6(d2, d3, d4, d5, d6, d7));
|
|
87
107
|
}
|
|
@@ -94,7 +114,7 @@ export function either8<T1, T2, T3, T4, T5, T6, T7, T8>(
|
|
|
94
114
|
d5: Decoder<T5>,
|
|
95
115
|
d6: Decoder<T6>,
|
|
96
116
|
d7: Decoder<T7>,
|
|
97
|
-
d8: Decoder<T8
|
|
117
|
+
d8: Decoder<T8>,
|
|
98
118
|
): Decoder<T1 | T2 | T3 | T4 | T5 | T6 | T7 | T8> {
|
|
99
119
|
return either(d1, either7(d2, d3, d4, d5, d6, d7, d8));
|
|
100
120
|
}
|
|
@@ -108,7 +128,7 @@ export function either9<T1, T2, T3, T4, T5, T6, T7, T8, T9>(
|
|
|
108
128
|
d6: Decoder<T6>,
|
|
109
129
|
d7: Decoder<T7>,
|
|
110
130
|
d8: Decoder<T8>,
|
|
111
|
-
d9: Decoder<T9
|
|
131
|
+
d9: Decoder<T9>,
|
|
112
132
|
): Decoder<T1 | T2 | T3 | T4 | T5 | T6 | T7 | T8 | T9> {
|
|
113
133
|
return either(d1, either8(d2, d3, d4, d5, d6, d7, d8, d9));
|
|
114
134
|
}
|
|
@@ -117,15 +137,15 @@ export function oneOf<T: Scalar>(constants: $ReadOnlyArray<T>): Decoder<T> {
|
|
|
117
137
|
return (blob: mixed) => {
|
|
118
138
|
const winner = constants.find((c) => c === blob);
|
|
119
139
|
if (winner !== undefined) {
|
|
120
|
-
return
|
|
140
|
+
return Result.ok(winner);
|
|
121
141
|
}
|
|
122
|
-
return
|
|
142
|
+
return Result.err(
|
|
123
143
|
annotate(
|
|
124
144
|
blob,
|
|
125
145
|
`Must be one of ${constants
|
|
126
146
|
.map((value) => JSON.stringify(value))
|
|
127
|
-
.join(', ')}
|
|
128
|
-
)
|
|
147
|
+
.join(', ')}`,
|
|
148
|
+
),
|
|
129
149
|
);
|
|
130
150
|
};
|
|
131
151
|
}
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
exports.__esModule = true;
|
|
4
|
+
exports.fail = fail;
|
|
5
|
+
|
|
6
|
+
var Result = _interopRequireWildcard(require("../result"));
|
|
7
|
+
|
|
8
|
+
var _annotate = require("../annotate");
|
|
9
|
+
|
|
10
|
+
function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function _getRequireWildcardCache(nodeInterop) { return nodeInterop ? cacheNodeInterop : cacheBabelInterop; })(nodeInterop); }
|
|
11
|
+
|
|
12
|
+
function _interopRequireWildcard(obj, nodeInterop) { if (!nodeInterop && obj && obj.__esModule) { return obj; } if (obj === null || typeof obj !== "object" && typeof obj !== "function") { return { "default": obj }; } var cache = _getRequireWildcardCache(nodeInterop); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj["default"] = obj; if (cache) { cache.set(obj, newObj); } return newObj; }
|
|
13
|
+
|
|
14
|
+
/**
|
|
15
|
+
* Decoder that always fails with the given error message, no matter what the input.
|
|
16
|
+
*/
|
|
17
|
+
function fail(msg) {
|
|
18
|
+
return function (blob) {
|
|
19
|
+
return Result.err((0, _annotate.annotate)(blob, msg));
|
|
20
|
+
};
|
|
21
|
+
}
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
// @flow strict
|
|
2
|
+
|
|
3
|
+
import * as Result from '../result';
|
|
4
|
+
import { annotate } from '../annotate';
|
|
5
|
+
import type { Decoder } from '../_types';
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* Decoder that always fails with the given error message, no matter what the input.
|
|
9
|
+
*/
|
|
10
|
+
export function fail(msg: string): Decoder<empty> {
|
|
11
|
+
return (blob: mixed) => Result.err(annotate(blob, msg));
|
|
12
|
+
}
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
exports.__esModule = true;
|
|
4
|
+
exports.instanceOf = instanceOf;
|
|
5
|
+
|
|
6
|
+
var Result = _interopRequireWildcard(require("../result"));
|
|
7
|
+
|
|
8
|
+
var _annotate = require("../annotate");
|
|
9
|
+
|
|
10
|
+
function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function _getRequireWildcardCache(nodeInterop) { return nodeInterop ? cacheNodeInterop : cacheBabelInterop; })(nodeInterop); }
|
|
11
|
+
|
|
12
|
+
function _interopRequireWildcard(obj, nodeInterop) { if (!nodeInterop && obj && obj.__esModule) { return obj; } if (obj === null || typeof obj !== "object" && typeof obj !== "function") { return { "default": obj }; } var cache = _getRequireWildcardCache(nodeInterop); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj["default"] = obj; if (cache) { cache.set(obj, newObj); } return newObj; }
|
|
13
|
+
|
|
14
|
+
function instanceOf(klass) {
|
|
15
|
+
return function (blob) {
|
|
16
|
+
return blob instanceof klass ? Result.ok(blob) : Result.err((0, _annotate.annotate)(blob, "Must be " + // $FlowFixMe[incompatible-use] - klass.name is fine?
|
|
17
|
+
klass.name + " instance"));
|
|
18
|
+
};
|
|
19
|
+
}
|
|
@@ -1,21 +1,20 @@
|
|
|
1
1
|
// @flow strict
|
|
2
2
|
|
|
3
|
-
import
|
|
4
|
-
import {
|
|
5
|
-
|
|
6
|
-
import type { Decoder } from './types';
|
|
3
|
+
import * as Result from '../result';
|
|
4
|
+
import { annotate } from '../annotate';
|
|
5
|
+
import type { Decoder } from '../_types';
|
|
7
6
|
|
|
8
7
|
export function instanceOf<T>(klass: Class<T>): Decoder<T> {
|
|
9
8
|
return (blob: mixed) =>
|
|
10
9
|
blob instanceof klass
|
|
11
|
-
?
|
|
12
|
-
:
|
|
10
|
+
? Result.ok(blob)
|
|
11
|
+
: Result.err(
|
|
13
12
|
annotate(
|
|
14
13
|
blob,
|
|
15
14
|
`Must be ${
|
|
16
15
|
// $FlowFixMe[incompatible-use] - klass.name is fine?
|
|
17
16
|
klass.name
|
|
18
|
-
} instance
|
|
19
|
-
)
|
|
17
|
+
} instance`,
|
|
18
|
+
),
|
|
20
19
|
);
|
|
21
20
|
}
|