decoders 2.0.0-beta8 → 2.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +67 -20
- package/Decoder.d.ts +94 -0
- package/Decoder.js +115 -0
- package/Decoder.js.flow +286 -0
- package/Decoder.mjs +108 -0
- package/README.md +122 -1345
- package/_utils.d.ts +0 -1
- package/_utils.js +51 -68
- package/_utils.js.flow +30 -20
- package/_utils.mjs +39 -54
- package/annotate.js +79 -72
- package/annotate.mjs +62 -58
- package/format.d.ts +4 -2
- package/format.js +64 -74
- package/format.js.flow +14 -12
- package/format.mjs +58 -68
- package/index.d.ts +38 -41
- package/index.js +66 -98
- package/index.js.flow +39 -58
- package/index.mjs +11 -37
- package/{core → lib}/_helpers.d.ts +0 -0
- package/lib/arrays.d.ts +59 -0
- package/lib/arrays.js +97 -0
- package/lib/arrays.js.flow +138 -0
- package/lib/arrays.mjs +84 -0
- package/lib/basics.d.ts +93 -0
- package/lib/basics.js +94 -0
- package/lib/basics.js.flow +124 -0
- package/lib/basics.mjs +75 -0
- package/lib/booleans.d.ts +16 -0
- package/lib/booleans.js +25 -0
- package/lib/booleans.js.flow +22 -0
- package/lib/booleans.mjs +15 -0
- package/lib/dates.d.ts +15 -0
- package/lib/dates.js +28 -0
- package/lib/dates.js.flow +40 -0
- package/lib/dates.mjs +19 -0
- package/lib/json.d.ts +35 -0
- package/lib/json.js +33 -0
- package/lib/json.js.flow +50 -0
- package/lib/json.mjs +18 -0
- package/lib/numbers.d.ts +31 -0
- package/lib/numbers.js +31 -0
- package/lib/numbers.js.flow +48 -0
- package/lib/numbers.mjs +21 -0
- package/lib/objects.d.ts +76 -0
- package/lib/objects.js +161 -0
- package/lib/objects.js.flow +238 -0
- package/lib/objects.mjs +147 -0
- package/lib/strings.d.ts +56 -0
- package/lib/strings.js +58 -0
- package/lib/strings.js.flow +90 -0
- package/lib/strings.mjs +40 -0
- package/lib/unions.d.ts +55 -0
- package/lib/unions.js +96 -0
- package/lib/unions.js.flow +155 -0
- package/lib/unions.mjs +83 -0
- package/lib/utilities.d.ts +40 -0
- package/lib/utilities.js +48 -0
- package/lib/utilities.js.flow +65 -0
- package/lib/utilities.mjs +37 -0
- package/package.json +64 -17
- package/result.d.ts +2 -25
- package/result.js +9 -90
- package/result.js.flow +4 -76
- package/result.mjs +5 -71
- package/_guard.d.ts +0 -7
- package/_guard.js +0 -22
- package/_guard.js.flow +0 -20
- package/_guard.mjs +0 -15
- package/_types.d.ts +0 -13
- package/_types.js +0 -1
- package/_types.js.flow +0 -20
- package/_types.mjs +0 -0
- package/core/array.d.ts +0 -5
- package/core/array.js +0 -104
- package/core/array.js.flow +0 -100
- package/core/array.mjs +0 -91
- package/core/boolean.d.ts +0 -5
- package/core/boolean.js +0 -40
- package/core/boolean.js.flow +0 -27
- package/core/boolean.mjs +0 -28
- package/core/composition.d.ts +0 -14
- package/core/composition.js +0 -54
- package/core/composition.js.flow +0 -48
- package/core/composition.mjs +0 -44
- package/core/constants.d.ts +0 -11
- package/core/constants.js +0 -65
- package/core/constants.js.flow +0 -44
- package/core/constants.mjs +0 -46
- package/core/date.d.ts +0 -4
- package/core/date.js +0 -42
- package/core/date.js.flow +0 -38
- package/core/date.mjs +0 -28
- package/core/describe.d.ts +0 -3
- package/core/describe.js +0 -22
- package/core/describe.js.flow +0 -17
- package/core/describe.mjs +0 -16
- package/core/dispatch.d.ts +0 -8
- package/core/dispatch.js +0 -58
- package/core/dispatch.js.flow +0 -58
- package/core/dispatch.mjs +0 -51
- package/core/either.d.ts +0 -61
- package/core/either.js +0 -113
- package/core/either.js.flow +0 -151
- package/core/either.mjs +0 -90
- package/core/fail.d.ts +0 -3
- package/core/fail.js +0 -17
- package/core/fail.js.flow +0 -12
- package/core/fail.mjs +0 -11
- package/core/instanceOf.d.ts +0 -3
- package/core/instanceOf.js +0 -15
- package/core/instanceOf.js.flow +0 -20
- package/core/instanceOf.mjs +0 -8
- package/core/json.d.ts +0 -11
- package/core/json.js +0 -31
- package/core/json.js.flow +0 -28
- package/core/json.mjs +0 -15
- package/core/lazy.d.ts +0 -3
- package/core/lazy.js +0 -16
- package/core/lazy.js.flow +0 -15
- package/core/lazy.mjs +0 -11
- package/core/mapping.d.ts +0 -6
- package/core/mapping.js +0 -67
- package/core/mapping.js.flow +0 -62
- package/core/mapping.mjs +0 -58
- package/core/number.d.ts +0 -6
- package/core/number.js +0 -36
- package/core/number.js.flow +0 -40
- package/core/number.mjs +0 -25
- package/core/object.d.ts +0 -33
- package/core/object.js +0 -190
- package/core/object.js.flow +0 -203
- package/core/object.mjs +0 -175
- package/core/optional.d.ts +0 -5
- package/core/optional.js +0 -50
- package/core/optional.js.flow +0 -41
- package/core/optional.mjs +0 -38
- package/core/string.d.ts +0 -10
- package/core/string.js +0 -68
- package/core/string.js.flow +0 -59
- package/core/string.mjs +0 -49
- package/core/tuple.d.ts +0 -30
- package/core/tuple.js +0 -177
- package/core/tuple.js.flow +0 -211
- package/core/tuple.mjs +0 -159
package/core/constants.mjs
DELETED
|
@@ -1,46 +0,0 @@
|
|
|
1
|
-
import { annotate } from '../annotate.mjs';
|
|
2
|
-
import { err, ok } from '../result.mjs';
|
|
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 ? ok(blob) : 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 ? ok(blob) : 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 ? ok(value) : 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 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 ok(blob);
|
|
41
|
-
};
|
|
42
|
-
/**
|
|
43
|
-
* Alias of unknown.
|
|
44
|
-
*/
|
|
45
|
-
|
|
46
|
-
export var mixed = unknown;
|
package/core/date.d.ts
DELETED
package/core/date.js
DELETED
|
@@ -1,42 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
|
|
3
|
-
exports.__esModule = true;
|
|
4
|
-
exports.iso8601 = exports.date = void 0;
|
|
5
|
-
|
|
6
|
-
var _annotate = require("../annotate");
|
|
7
|
-
|
|
8
|
-
var _result = require("../result");
|
|
9
|
-
|
|
10
|
-
var _utils = require("../_utils");
|
|
11
|
-
|
|
12
|
-
var _composition = require("./composition");
|
|
13
|
-
|
|
14
|
-
var _string = require("./string");
|
|
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, _annotate.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, _composition.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/core/date.js.flow
DELETED
|
@@ -1,38 +0,0 @@
|
|
|
1
|
-
// @flow strict
|
|
2
|
-
|
|
3
|
-
import { annotate } from '../annotate';
|
|
4
|
-
import { err, ok } from '../result';
|
|
5
|
-
import { isDate } from '../_utils';
|
|
6
|
-
import { map } from './composition';
|
|
7
|
-
import { regex } from './string';
|
|
8
|
-
import type { Decoder } from '../_types';
|
|
9
|
-
|
|
10
|
-
// $FlowFixMe[unclear-type] (not really an issue) - deliberate casting
|
|
11
|
-
type cast = any;
|
|
12
|
-
|
|
13
|
-
// Only matches the shape. This "over-matches" some values that still aren't
|
|
14
|
-
// valid dates (like 9999-99-99), but those will be caught by JS Date's
|
|
15
|
-
// internal validations
|
|
16
|
-
const iso8601_re =
|
|
17
|
-
/^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}(?:[.]\d+)?(?:Z|[+-]\d{2}:?\d{2})$/;
|
|
18
|
-
|
|
19
|
-
export const date: Decoder<Date> = (value: mixed) =>
|
|
20
|
-
isDate(value) ? ok(((value: cast): Date)) : err(annotate(value, 'Must be a Date'));
|
|
21
|
-
|
|
22
|
-
/**
|
|
23
|
-
* Decoder that only returns Ok for strings that are valid ISO8601 date
|
|
24
|
-
* strings. Err otherwise.
|
|
25
|
-
*/
|
|
26
|
-
export const iso8601: Decoder<Date> = map(
|
|
27
|
-
// Input itself needs to match the ISO8601 regex...
|
|
28
|
-
regex(iso8601_re, 'Must be ISO8601 format'),
|
|
29
|
-
|
|
30
|
-
// Make sure it is a _valid_ date
|
|
31
|
-
(value: string) => {
|
|
32
|
-
const date = new Date(value);
|
|
33
|
-
if (isNaN(date.getTime())) {
|
|
34
|
-
throw new Error('Must be valid date/time value');
|
|
35
|
-
}
|
|
36
|
-
return date;
|
|
37
|
-
},
|
|
38
|
-
);
|
package/core/date.mjs
DELETED
|
@@ -1,28 +0,0 @@
|
|
|
1
|
-
import { annotate } from '../annotate.mjs';
|
|
2
|
-
import { err, ok } from '../result.mjs';
|
|
3
|
-
import { isDate } from '../_utils.mjs';
|
|
4
|
-
import { map } from './composition.mjs';
|
|
5
|
-
import { regex } from './string.mjs';
|
|
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) ? ok(value) : 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
|
-
});
|
package/core/describe.d.ts
DELETED
package/core/describe.js
DELETED
|
@@ -1,22 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
|
|
3
|
-
exports.__esModule = true;
|
|
4
|
-
exports.describe = describe;
|
|
5
|
-
|
|
6
|
-
var _annotate = require("../annotate");
|
|
7
|
-
|
|
8
|
-
var _result = require("../result");
|
|
9
|
-
|
|
10
|
-
/**
|
|
11
|
-
* Wrap another decoder, and override the error message in case it fails. This
|
|
12
|
-
* is useful to "simplify" otherwise potentially complex error messages, or to
|
|
13
|
-
* use language in those error messages that can be relayed to end users (for
|
|
14
|
-
* example to show in form errors).
|
|
15
|
-
*/
|
|
16
|
-
function describe(decoder, message) {
|
|
17
|
-
return function (blob) {
|
|
18
|
-
return (0, _result.mapError)(decoder(blob), function (err) {
|
|
19
|
-
return (0, _annotate.annotate)(err, message);
|
|
20
|
-
});
|
|
21
|
-
};
|
|
22
|
-
}
|
package/core/describe.js.flow
DELETED
|
@@ -1,17 +0,0 @@
|
|
|
1
|
-
// @flow strict
|
|
2
|
-
|
|
3
|
-
import { annotate } from '../annotate';
|
|
4
|
-
import { mapError } from '../result';
|
|
5
|
-
import type { Decoder } from '../_types';
|
|
6
|
-
|
|
7
|
-
/**
|
|
8
|
-
* Wrap another decoder, and override the error message in case it fails. This
|
|
9
|
-
* is useful to "simplify" otherwise potentially complex error messages, or to
|
|
10
|
-
* use language in those error messages that can be relayed to end users (for
|
|
11
|
-
* example to show in form errors).
|
|
12
|
-
*/
|
|
13
|
-
export function describe<T>(decoder: Decoder<T>, message: string): Decoder<T> {
|
|
14
|
-
return (blob: mixed) => {
|
|
15
|
-
return mapError(decoder(blob), (err) => annotate(err, message));
|
|
16
|
-
};
|
|
17
|
-
}
|
package/core/describe.mjs
DELETED
|
@@ -1,16 +0,0 @@
|
|
|
1
|
-
import { annotate } from '../annotate.mjs';
|
|
2
|
-
import { mapError } from '../result.mjs';
|
|
3
|
-
|
|
4
|
-
/**
|
|
5
|
-
* Wrap another decoder, and override the error message in case it fails. This
|
|
6
|
-
* is useful to "simplify" otherwise potentially complex error messages, or to
|
|
7
|
-
* use language in those error messages that can be relayed to end users (for
|
|
8
|
-
* example to show in form errors).
|
|
9
|
-
*/
|
|
10
|
-
export function describe(decoder, message) {
|
|
11
|
-
return function (blob) {
|
|
12
|
-
return mapError(decoder(blob), function (err) {
|
|
13
|
-
return annotate(err, message);
|
|
14
|
-
});
|
|
15
|
-
};
|
|
16
|
-
}
|
package/core/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 disjointUnion<O extends { [key: string]: Decoder<any> }>(
|
|
6
|
-
field: string,
|
|
7
|
-
mapping: O,
|
|
8
|
-
): Decoder<Values<{ [key in keyof O]: DecoderType<O[key]> }>>;
|
package/core/dispatch.js
DELETED
|
@@ -1,58 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
|
|
3
|
-
exports.__esModule = true;
|
|
4
|
-
exports.disjointUnion = disjointUnion;
|
|
5
|
-
|
|
6
|
-
var _result = require("../result");
|
|
7
|
-
|
|
8
|
-
var _object2 = require("./object");
|
|
9
|
-
|
|
10
|
-
var _either = require("./either");
|
|
11
|
-
|
|
12
|
-
/**
|
|
13
|
-
* Dispatches to one of several given decoders, based on the value found at
|
|
14
|
-
* runtime in the given field. For example, suppose you have these decoders:
|
|
15
|
-
*
|
|
16
|
-
* const rectangle = object({
|
|
17
|
-
* type: constant('rect'),
|
|
18
|
-
* x: number,
|
|
19
|
-
* y: number,
|
|
20
|
-
* width: number,
|
|
21
|
-
* height: number,
|
|
22
|
-
* });
|
|
23
|
-
*
|
|
24
|
-
* const circle = object({
|
|
25
|
-
* type: constant('circle'),
|
|
26
|
-
* cx: number,
|
|
27
|
-
* cy: number,
|
|
28
|
-
* r: number,
|
|
29
|
-
* });
|
|
30
|
-
*
|
|
31
|
-
* Then these two decoders are equivalent:
|
|
32
|
-
*
|
|
33
|
-
* const shape = either(rectangle, circle)
|
|
34
|
-
* const shape = disjointUnion('type', { rectangle, circle })
|
|
35
|
-
*
|
|
36
|
-
* Will be of type Decoder<Rectangle | Circle>.
|
|
37
|
-
*
|
|
38
|
-
* But `disjointUnion` will typically be more runtime-efficient. The reason is
|
|
39
|
-
* that it will first do minimal work to "look ahead" into the `type` field
|
|
40
|
-
* here, and based on that value, pick the decoder to invoke.
|
|
41
|
-
*
|
|
42
|
-
* The `either` version will simply try to invoke each decoder, until it finds
|
|
43
|
-
* one that matches.
|
|
44
|
-
*
|
|
45
|
-
* Also, the error messages will be less ambiguous using `disjointUnion()`.
|
|
46
|
-
*/
|
|
47
|
-
function disjointUnion(field, mapping) {
|
|
48
|
-
var _object;
|
|
49
|
-
|
|
50
|
-
var base = (0, _object2.object)((_object = {}, _object[field] = (0, _either.oneOf)(Object.keys(mapping)), _object));
|
|
51
|
-
return function (blob) {
|
|
52
|
-
return (0, _result.andThen)(base(blob), function (baseObj) {
|
|
53
|
-
var decoderName = baseObj[field];
|
|
54
|
-
var decoder = mapping[decoderName];
|
|
55
|
-
return decoder(blob);
|
|
56
|
-
});
|
|
57
|
-
};
|
|
58
|
-
}
|
package/core/dispatch.js.flow
DELETED
|
@@ -1,58 +0,0 @@
|
|
|
1
|
-
// @flow strict
|
|
2
|
-
|
|
3
|
-
import { andThen } from '../result';
|
|
4
|
-
import { object } from './object';
|
|
5
|
-
import { oneOf } from './either';
|
|
6
|
-
import type { Decoder, DecoderType } from '../_types';
|
|
7
|
-
|
|
8
|
-
// $FlowFixMe[unclear-type] (not really an issue) - deliberate use of `any` - not sure how we should get rid of this
|
|
9
|
-
type anything = any;
|
|
10
|
-
|
|
11
|
-
/**
|
|
12
|
-
* Dispatches to one of several given decoders, based on the value found at
|
|
13
|
-
* runtime in the given field. For example, suppose you have these decoders:
|
|
14
|
-
*
|
|
15
|
-
* const rectangle = object({
|
|
16
|
-
* type: constant('rect'),
|
|
17
|
-
* x: number,
|
|
18
|
-
* y: number,
|
|
19
|
-
* width: number,
|
|
20
|
-
* height: number,
|
|
21
|
-
* });
|
|
22
|
-
*
|
|
23
|
-
* const circle = object({
|
|
24
|
-
* type: constant('circle'),
|
|
25
|
-
* cx: number,
|
|
26
|
-
* cy: number,
|
|
27
|
-
* r: number,
|
|
28
|
-
* });
|
|
29
|
-
*
|
|
30
|
-
* Then these two decoders are equivalent:
|
|
31
|
-
*
|
|
32
|
-
* const shape = either(rectangle, circle)
|
|
33
|
-
* const shape = disjointUnion('type', { rectangle, circle })
|
|
34
|
-
*
|
|
35
|
-
* Will be of type Decoder<Rectangle | Circle>.
|
|
36
|
-
*
|
|
37
|
-
* But `disjointUnion` will typically be more runtime-efficient. The reason is
|
|
38
|
-
* that it will first do minimal work to "look ahead" into the `type` field
|
|
39
|
-
* here, and based on that value, pick the decoder to invoke.
|
|
40
|
-
*
|
|
41
|
-
* The `either` version will simply try to invoke each decoder, until it finds
|
|
42
|
-
* one that matches.
|
|
43
|
-
*
|
|
44
|
-
* Also, the error messages will be less ambiguous using `disjointUnion()`.
|
|
45
|
-
*/
|
|
46
|
-
export function disjointUnion<O: { +[field: string]: Decoder<anything>, ... }>(
|
|
47
|
-
field: string,
|
|
48
|
-
mapping: O,
|
|
49
|
-
): Decoder<$Values<$ObjMap<O, DecoderType>>> {
|
|
50
|
-
const base = object({ [field]: oneOf(Object.keys(mapping)) });
|
|
51
|
-
return (blob: mixed) => {
|
|
52
|
-
return andThen(base(blob), (baseObj) => {
|
|
53
|
-
const decoderName = baseObj[field];
|
|
54
|
-
const decoder = mapping[decoderName];
|
|
55
|
-
return decoder(blob);
|
|
56
|
-
});
|
|
57
|
-
};
|
|
58
|
-
}
|
package/core/dispatch.mjs
DELETED
|
@@ -1,51 +0,0 @@
|
|
|
1
|
-
import { andThen } from '../result.mjs';
|
|
2
|
-
import { object } from './object.mjs';
|
|
3
|
-
import { oneOf } from './either.mjs';
|
|
4
|
-
|
|
5
|
-
/**
|
|
6
|
-
* Dispatches to one of several given decoders, based on the value found at
|
|
7
|
-
* runtime in the given field. For example, suppose you have these decoders:
|
|
8
|
-
*
|
|
9
|
-
* const rectangle = object({
|
|
10
|
-
* type: constant('rect'),
|
|
11
|
-
* x: number,
|
|
12
|
-
* y: number,
|
|
13
|
-
* width: number,
|
|
14
|
-
* height: number,
|
|
15
|
-
* });
|
|
16
|
-
*
|
|
17
|
-
* const circle = object({
|
|
18
|
-
* type: constant('circle'),
|
|
19
|
-
* cx: number,
|
|
20
|
-
* cy: number,
|
|
21
|
-
* r: number,
|
|
22
|
-
* });
|
|
23
|
-
*
|
|
24
|
-
* Then these two decoders are equivalent:
|
|
25
|
-
*
|
|
26
|
-
* const shape = either(rectangle, circle)
|
|
27
|
-
* const shape = disjointUnion('type', { rectangle, circle })
|
|
28
|
-
*
|
|
29
|
-
* Will be of type Decoder<Rectangle | Circle>.
|
|
30
|
-
*
|
|
31
|
-
* But `disjointUnion` will typically be more runtime-efficient. The reason is
|
|
32
|
-
* that it will first do minimal work to "look ahead" into the `type` field
|
|
33
|
-
* here, and based on that value, pick the decoder to invoke.
|
|
34
|
-
*
|
|
35
|
-
* The `either` version will simply try to invoke each decoder, until it finds
|
|
36
|
-
* one that matches.
|
|
37
|
-
*
|
|
38
|
-
* Also, the error messages will be less ambiguous using `disjointUnion()`.
|
|
39
|
-
*/
|
|
40
|
-
export function disjointUnion(field, mapping) {
|
|
41
|
-
var _object;
|
|
42
|
-
|
|
43
|
-
var base = object((_object = {}, _object[field] = oneOf(Object.keys(mapping)), _object));
|
|
44
|
-
return function (blob) {
|
|
45
|
-
return andThen(base(blob), function (baseObj) {
|
|
46
|
-
var decoderName = baseObj[field];
|
|
47
|
-
var decoder = mapping[decoderName];
|
|
48
|
-
return decoder(blob);
|
|
49
|
-
});
|
|
50
|
-
};
|
|
51
|
-
}
|
package/core/either.d.ts
DELETED
|
@@ -1,61 +0,0 @@
|
|
|
1
|
-
import { Decoder, Scalar } from '../_types';
|
|
2
|
-
|
|
3
|
-
export function either<T1, T2>(d1: Decoder<T1>, d2: Decoder<T2>): Decoder<T1 | T2>;
|
|
4
|
-
export function either2<T1, T2>(d1: Decoder<T1>, d2: Decoder<T2>): Decoder<T1 | T2>;
|
|
5
|
-
export function either3<T1, T2, T3>(
|
|
6
|
-
d1: Decoder<T1>,
|
|
7
|
-
d2: Decoder<T2>,
|
|
8
|
-
d3: Decoder<T3>,
|
|
9
|
-
): Decoder<T1 | T2 | T3>;
|
|
10
|
-
export function either4<T1, T2, T3, T4>(
|
|
11
|
-
d1: Decoder<T1>,
|
|
12
|
-
d2: Decoder<T2>,
|
|
13
|
-
d3: Decoder<T3>,
|
|
14
|
-
d4: Decoder<T4>,
|
|
15
|
-
): Decoder<T1 | T2 | T3 | T4>;
|
|
16
|
-
export function either5<T1, T2, T3, T4, T5>(
|
|
17
|
-
d1: Decoder<T1>,
|
|
18
|
-
d2: Decoder<T2>,
|
|
19
|
-
d3: Decoder<T3>,
|
|
20
|
-
d4: Decoder<T4>,
|
|
21
|
-
d5: Decoder<T5>,
|
|
22
|
-
): Decoder<T1 | T2 | T3 | T4 | T5>;
|
|
23
|
-
export function either6<T1, T2, T3, T4, T5, T6>(
|
|
24
|
-
d1: Decoder<T1>,
|
|
25
|
-
d2: Decoder<T2>,
|
|
26
|
-
d3: Decoder<T3>,
|
|
27
|
-
d4: Decoder<T4>,
|
|
28
|
-
d5: Decoder<T5>,
|
|
29
|
-
d6: Decoder<T6>,
|
|
30
|
-
): Decoder<T1 | T2 | T3 | T4 | T5 | T6>;
|
|
31
|
-
export function either7<T1, T2, T3, T4, T5, T6, T7>(
|
|
32
|
-
d1: Decoder<T1>,
|
|
33
|
-
d2: Decoder<T2>,
|
|
34
|
-
d3: Decoder<T3>,
|
|
35
|
-
d4: Decoder<T4>,
|
|
36
|
-
d5: Decoder<T5>,
|
|
37
|
-
d6: Decoder<T6>,
|
|
38
|
-
d7: Decoder<T7>,
|
|
39
|
-
): Decoder<T1 | T2 | T3 | T4 | T5 | T6 | T7>;
|
|
40
|
-
export function either8<T1, T2, T3, T4, T5, T6, T7, T8>(
|
|
41
|
-
d1: Decoder<T1>,
|
|
42
|
-
d2: Decoder<T2>,
|
|
43
|
-
d3: Decoder<T3>,
|
|
44
|
-
d4: Decoder<T4>,
|
|
45
|
-
d5: Decoder<T5>,
|
|
46
|
-
d6: Decoder<T6>,
|
|
47
|
-
d7: Decoder<T7>,
|
|
48
|
-
d8: Decoder<T8>,
|
|
49
|
-
): Decoder<T1 | T2 | T3 | T4 | T5 | T6 | T7 | T8>;
|
|
50
|
-
export function either9<T1, T2, T3, T4, T5, T6, T7, T8, T9>(
|
|
51
|
-
d1: Decoder<T1>,
|
|
52
|
-
d2: Decoder<T2>,
|
|
53
|
-
d3: Decoder<T3>,
|
|
54
|
-
d4: Decoder<T4>,
|
|
55
|
-
d5: Decoder<T5>,
|
|
56
|
-
d6: Decoder<T6>,
|
|
57
|
-
d7: Decoder<T7>,
|
|
58
|
-
d8: Decoder<T8>,
|
|
59
|
-
d9: Decoder<T9>,
|
|
60
|
-
): Decoder<T1 | T2 | T3 | T4 | T5 | T6 | T7 | T8 | T9>;
|
|
61
|
-
export function oneOf<T extends Scalar>(constants: readonly T[]): Decoder<T>;
|
package/core/either.js
DELETED
|
@@ -1,113 +0,0 @@
|
|
|
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 _annotate = require("../annotate");
|
|
15
|
-
|
|
16
|
-
var _result = require("../result");
|
|
17
|
-
|
|
18
|
-
var _utils = require("../_utils");
|
|
19
|
-
|
|
20
|
-
/**
|
|
21
|
-
* Indents and adds a dash in front of this (potentially multiline) string.
|
|
22
|
-
*/
|
|
23
|
-
function itemize(s) {
|
|
24
|
-
return '-' + (0, _utils.indent)(s).substring(1);
|
|
25
|
-
}
|
|
26
|
-
/**
|
|
27
|
-
* Nests another error as an item under a new-to-be-created "Either error". If
|
|
28
|
-
* the given subitem already is an "Either error" of itself, don't indent, but
|
|
29
|
-
* just "inject" its items at the same error level, for nicely flattened either
|
|
30
|
-
* expressions.
|
|
31
|
-
*
|
|
32
|
-
* Avoids:
|
|
33
|
-
*
|
|
34
|
-
* Either:
|
|
35
|
-
* - Either:
|
|
36
|
-
* - Must be P
|
|
37
|
-
* - Either:
|
|
38
|
-
* - Must be Q
|
|
39
|
-
* - Must be R
|
|
40
|
-
* - Must be S
|
|
41
|
-
*
|
|
42
|
-
* And "flattens" these to:
|
|
43
|
-
*
|
|
44
|
-
* Either:
|
|
45
|
-
* - Must be P
|
|
46
|
-
* - Must be Q
|
|
47
|
-
* - Must be R
|
|
48
|
-
* - Must be S
|
|
49
|
-
*
|
|
50
|
-
*/
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
function nest(errText) {
|
|
54
|
-
var EITHER_PREFIX = 'Either:\n';
|
|
55
|
-
return errText.startsWith(EITHER_PREFIX) ? errText.substr(EITHER_PREFIX.length) : itemize(errText);
|
|
56
|
-
}
|
|
57
|
-
|
|
58
|
-
function either(d1, d2) {
|
|
59
|
-
return function (blob) {
|
|
60
|
-
return (0, _result.orElse)(d1(blob), function (err1) {
|
|
61
|
-
return (0, _result.orElse)(d2(blob), function (err2) {
|
|
62
|
-
var serr1 = (0, _utils.summarize)(err1).join('\n');
|
|
63
|
-
var serr2 = (0, _utils.summarize)(err2).join('\n');
|
|
64
|
-
var text = ['Either:', nest(serr1), nest(serr2)].join('\n');
|
|
65
|
-
return (0, _result.err)((0, _annotate.annotate)(blob, text));
|
|
66
|
-
});
|
|
67
|
-
});
|
|
68
|
-
};
|
|
69
|
-
}
|
|
70
|
-
|
|
71
|
-
function either3(d1, d2, d3) {
|
|
72
|
-
return either(d1, either(d2, d3));
|
|
73
|
-
}
|
|
74
|
-
|
|
75
|
-
function either4(d1, d2, d3, d4) {
|
|
76
|
-
return either(d1, either3(d2, d3, d4));
|
|
77
|
-
}
|
|
78
|
-
|
|
79
|
-
function either5(d1, d2, d3, d4, d5) {
|
|
80
|
-
return either(d1, either4(d2, d3, d4, d5));
|
|
81
|
-
}
|
|
82
|
-
|
|
83
|
-
function either6(d1, d2, d3, d4, d5, d6) {
|
|
84
|
-
return either(d1, either5(d2, d3, d4, d5, d6));
|
|
85
|
-
}
|
|
86
|
-
|
|
87
|
-
function either7(d1, d2, d3, d4, d5, d6, d7) {
|
|
88
|
-
return either(d1, either6(d2, d3, d4, d5, d6, d7));
|
|
89
|
-
}
|
|
90
|
-
|
|
91
|
-
function either8(d1, d2, d3, d4, d5, d6, d7, d8) {
|
|
92
|
-
return either(d1, either7(d2, d3, d4, d5, d6, d7, d8));
|
|
93
|
-
}
|
|
94
|
-
|
|
95
|
-
function either9(d1, d2, d3, d4, d5, d6, d7, d8, d9) {
|
|
96
|
-
return either(d1, either8(d2, d3, d4, d5, d6, d7, d8, d9));
|
|
97
|
-
}
|
|
98
|
-
|
|
99
|
-
function oneOf(constants) {
|
|
100
|
-
return function (blob) {
|
|
101
|
-
var winner = constants.find(function (c) {
|
|
102
|
-
return c === blob;
|
|
103
|
-
});
|
|
104
|
-
|
|
105
|
-
if (winner !== undefined) {
|
|
106
|
-
return (0, _result.ok)(winner);
|
|
107
|
-
}
|
|
108
|
-
|
|
109
|
-
return (0, _result.err)((0, _annotate.annotate)(blob, "Must be one of " + constants.map(function (value) {
|
|
110
|
-
return JSON.stringify(value);
|
|
111
|
-
}).join(', ')));
|
|
112
|
-
};
|
|
113
|
-
}
|