decoders 1.25.5 → 2.0.0-beta2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +23 -3
- package/_esm/_guard.js +15 -0
- package/_esm/_guard.js.flow +20 -0
- package/_esm/_types.js +0 -0
- package/_esm/_types.js.flow +20 -0
- package/_esm/_utils.js +93 -0
- package/_esm/_utils.js.flow +97 -0
- package/_esm/annotate.js +144 -0
- package/_esm/annotate.js.flow +218 -0
- package/_esm/core/array.js +91 -0
- package/{array.js.flow → _esm/core/array.js.flow} +22 -25
- package/_esm/core/boolean.js +28 -0
- package/{boolean.js.flow → _esm/core/boolean.js.flow} +8 -7
- package/_esm/core/composition.js +42 -0
- package/{utils.js.flow → _esm/core/composition.js.flow} +7 -22
- package/_esm/core/constants.js +46 -0
- package/{constants.js.flow → _esm/core/constants.js.flow} +13 -12
- package/_esm/core/date.js +28 -0
- package/{date.js.flow → _esm/core/date.js.flow} +9 -7
- package/{describe.js → _esm/core/describe.js} +5 -11
- package/{describe.js.flow → _esm/core/describe.js.flow} +4 -4
- package/{dispatch.js → _esm/core/dispatch.js} +8 -15
- package/{dispatch.js.flow → _esm/core/dispatch.js.flow} +6 -5
- package/_esm/core/either.js +90 -0
- package/{either.js.flow → _esm/core/either.js.flow} +55 -35
- package/_esm/core/fail.js +11 -0
- package/_esm/core/fail.js.flow +12 -0
- package/_esm/core/instanceOf.js +8 -0
- package/{instanceOf.js.flow → _esm/core/instanceOf.js.flow} +7 -8
- package/_esm/core/json.js +15 -0
- package/{json.js.flow → _esm/core/json.js.flow} +4 -4
- package/_esm/core/lazy.js +11 -0
- package/{lazy.js.flow → _esm/core/lazy.js.flow} +1 -1
- package/_esm/core/mapping.js +54 -0
- package/_esm/core/mapping.js.flow +54 -0
- package/_esm/core/number.js +25 -0
- package/{number.js.flow → _esm/core/number.js.flow} +10 -11
- package/_esm/core/object.js +175 -0
- package/{object.js.flow → _esm/core/object.js.flow} +44 -52
- package/_esm/core/optional.js +38 -0
- package/{optional.js.flow → _esm/core/optional.js.flow} +6 -7
- package/{string.js → _esm/core/string.js} +18 -35
- package/{string.js.flow → _esm/core/string.js.flow} +13 -12
- package/_esm/core/tuple.js +155 -0
- package/{tuple.js.flow → _esm/core/tuple.js.flow} +62 -63
- package/_esm/format/index.js +2 -0
- package/_esm/format/index.js.flow +4 -0
- package/_esm/format/inline.js +137 -0
- package/_esm/format/inline.js.flow +122 -0
- package/_esm/format/short.js +4 -0
- package/_esm/format/short.js.flow +8 -0
- package/_esm/index.js +37 -0
- package/_esm/index.js.flow +56 -0
- package/_esm/result.js +139 -0
- package/_esm/result.js.flow +166 -0
- package/_guard.js +26 -0
- package/_guard.js.flow +20 -0
- package/_types.js +1 -0
- package/_types.js.flow +20 -0
- package/_utils.js +108 -0
- package/_utils.js.flow +97 -0
- package/annotate.js +161 -0
- package/annotate.js.flow +218 -0
- package/core/array.js +108 -0
- package/core/array.js.flow +103 -0
- package/core/boolean.js +44 -0
- package/core/boolean.js.flow +29 -0
- package/core/composition.js +56 -0
- package/core/composition.js.flow +43 -0
- package/core/constants.js +69 -0
- package/core/constants.js.flow +46 -0
- package/core/date.js +46 -0
- package/core/date.js.flow +40 -0
- package/core/describe.js +26 -0
- package/core/describe.js.flow +17 -0
- package/core/dispatch.js +62 -0
- package/core/dispatch.js.flow +58 -0
- package/core/either.js +117 -0
- package/core/either.js.flow +151 -0
- package/core/fail.js +21 -0
- package/core/fail.js.flow +12 -0
- package/core/instanceOf.js +19 -0
- package/core/instanceOf.js.flow +20 -0
- package/{json.js → core/json.js} +3 -5
- package/core/json.js.flow +28 -0
- package/{lazy.js → core/lazy.js} +1 -3
- package/core/lazy.js.flow +15 -0
- package/core/mapping.js +67 -0
- package/core/mapping.js.flow +54 -0
- package/core/number.js +40 -0
- package/core/number.js.flow +34 -0
- package/core/object.js +194 -0
- package/core/object.js.flow +203 -0
- package/core/optional.js +54 -0
- package/core/optional.js.flow +41 -0
- package/core/string.js +98 -0
- package/core/string.js.flow +82 -0
- package/core/tuple.js +173 -0
- package/core/tuple.js.flow +220 -0
- package/format/index.js +12 -0
- package/format/index.js.flow +4 -0
- package/format/inline.js +146 -0
- package/format/inline.js.flow +122 -0
- package/format/short.js +10 -0
- package/format/short.js.flow +8 -0
- package/index.js +118 -395
- package/index.js.flow +24 -24
- package/package.json +2 -6
- package/result.js +172 -0
- package/result.js.flow +166 -0
- package/array.d.ts +0 -5
- package/array.js +0 -133
- package/boolean.d.ts +0 -5
- package/boolean.js +0 -42
- package/constants.d.ts +0 -11
- package/constants.js +0 -67
- package/date.d.ts +0 -4
- package/date.js +0 -42
- package/describe.d.ts +0 -3
- package/dispatch.d.ts +0 -8
- package/either.d.ts +0 -61
- package/either.js +0 -85
- package/fail.d.ts +0 -3
- package/fail.js +0 -19
- package/fail.js.flow +0 -13
- package/guard.d.ts +0 -7
- package/guard.js +0 -30
- package/guard.js.flow +0 -36
- package/helpers.d.ts +0 -79
- package/index.d.ts +0 -38
- package/instanceOf.d.ts +0 -3
- package/instanceOf.js +0 -17
- package/json.d.ts +0 -11
- package/lazy.d.ts +0 -3
- package/mapping.d.ts +0 -4
- package/mapping.js +0 -113
- package/mapping.js.flow +0 -71
- package/number.d.ts +0 -6
- package/number.js +0 -38
- package/object.d.ts +0 -33
- package/object.js +0 -254
- package/optional.d.ts +0 -5
- package/optional.js +0 -52
- package/string.d.ts +0 -7
- package/tuple.d.ts +0 -30
- package/tuple.js +0 -199
- package/types.d.ts +0 -18
- package/types.js +0 -5
- package/types.js.flow +0 -26
- package/utils.d.ts +0 -13
- package/utils.js +0 -70
package/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/either.js
DELETED
|
@@ -1,85 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
|
|
3
|
-
Object.defineProperty(exports, "__esModule", {
|
|
4
|
-
value: true
|
|
5
|
-
});
|
|
6
|
-
exports.either = either;
|
|
7
|
-
exports.either3 = either3;
|
|
8
|
-
exports.either4 = either4;
|
|
9
|
-
exports.either5 = either5;
|
|
10
|
-
exports.either6 = either6;
|
|
11
|
-
exports.either7 = either7;
|
|
12
|
-
exports.either8 = either8;
|
|
13
|
-
exports.either9 = either9;
|
|
14
|
-
exports.oneOf = oneOf;
|
|
15
|
-
|
|
16
|
-
var _debrief = require("debrief");
|
|
17
|
-
|
|
18
|
-
var _Result = require("lemons/Result");
|
|
19
|
-
|
|
20
|
-
/**
|
|
21
|
-
* Indents and adds a dash in front of this (potentially multiline) string.
|
|
22
|
-
*/
|
|
23
|
-
// istanbul ignore next
|
|
24
|
-
function itemize() {
|
|
25
|
-
var s = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : '';
|
|
26
|
-
return '-' + (0, _debrief.indent)(s).substring(1);
|
|
27
|
-
}
|
|
28
|
-
|
|
29
|
-
function either(d1, d2) {
|
|
30
|
-
return function (blob) {
|
|
31
|
-
return d1(blob).dispatch(function (value1) {
|
|
32
|
-
return (0, _Result.Ok)(value1);
|
|
33
|
-
}, function (err1) {
|
|
34
|
-
return d2(blob).dispatch(function (value2) {
|
|
35
|
-
return (0, _Result.Ok)(value2);
|
|
36
|
-
}, function (err2) {
|
|
37
|
-
return (0, _Result.Err)((0, _debrief.annotate)(blob, ['Either:', itemize((0, _debrief.summarize)(err1).join('\n')), itemize((0, _debrief.summarize)(err2).join('\n'))].join('\n')));
|
|
38
|
-
});
|
|
39
|
-
});
|
|
40
|
-
};
|
|
41
|
-
}
|
|
42
|
-
|
|
43
|
-
function either3(d1, d2, d3) {
|
|
44
|
-
return either(d1, either(d2, d3));
|
|
45
|
-
}
|
|
46
|
-
|
|
47
|
-
function either4(d1, d2, d3, d4) {
|
|
48
|
-
return either(d1, either3(d2, d3, d4));
|
|
49
|
-
}
|
|
50
|
-
|
|
51
|
-
function either5(d1, d2, d3, d4, d5) {
|
|
52
|
-
return either(d1, either4(d2, d3, d4, d5));
|
|
53
|
-
}
|
|
54
|
-
|
|
55
|
-
function either6(d1, d2, d3, d4, d5, d6) {
|
|
56
|
-
return either(d1, either5(d2, d3, d4, d5, d6));
|
|
57
|
-
}
|
|
58
|
-
|
|
59
|
-
function either7(d1, d2, d3, d4, d5, d6, d7) {
|
|
60
|
-
return either(d1, either6(d2, d3, d4, d5, d6, d7));
|
|
61
|
-
}
|
|
62
|
-
|
|
63
|
-
function either8(d1, d2, d3, d4, d5, d6, d7, d8) {
|
|
64
|
-
return either(d1, either7(d2, d3, d4, d5, d6, d7, d8));
|
|
65
|
-
}
|
|
66
|
-
|
|
67
|
-
function either9(d1, d2, d3, d4, d5, d6, d7, d8, d9) {
|
|
68
|
-
return either(d1, either8(d2, d3, d4, d5, d6, d7, d8, d9));
|
|
69
|
-
}
|
|
70
|
-
|
|
71
|
-
function oneOf(constants) {
|
|
72
|
-
return function (blob) {
|
|
73
|
-
var winner = constants.find(function (c) {
|
|
74
|
-
return c === blob;
|
|
75
|
-
});
|
|
76
|
-
|
|
77
|
-
if (winner !== undefined) {
|
|
78
|
-
return (0, _Result.Ok)(winner);
|
|
79
|
-
}
|
|
80
|
-
|
|
81
|
-
return (0, _Result.Err)((0, _debrief.annotate)(blob, "Must be one of ".concat(constants.map(function (value) {
|
|
82
|
-
return JSON.stringify(value);
|
|
83
|
-
}).join(', '))));
|
|
84
|
-
};
|
|
85
|
-
}
|
package/fail.d.ts
DELETED
package/fail.js
DELETED
|
@@ -1,19 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
|
|
3
|
-
Object.defineProperty(exports, "__esModule", {
|
|
4
|
-
value: true
|
|
5
|
-
});
|
|
6
|
-
exports.fail = fail;
|
|
7
|
-
|
|
8
|
-
var _debrief = require("debrief");
|
|
9
|
-
|
|
10
|
-
var _Result = require("lemons/Result");
|
|
11
|
-
|
|
12
|
-
/**
|
|
13
|
-
* Decoder that always fails with the given error message, no matter what the input.
|
|
14
|
-
*/
|
|
15
|
-
function fail(msg) {
|
|
16
|
-
return function (blob) {
|
|
17
|
-
return (0, _Result.Err)((0, _debrief.annotate)(blob, msg));
|
|
18
|
-
};
|
|
19
|
-
}
|
package/fail.js.flow
DELETED
|
@@ -1,13 +0,0 @@
|
|
|
1
|
-
// @flow strict
|
|
2
|
-
|
|
3
|
-
import { annotate } from 'debrief';
|
|
4
|
-
import { Err } from 'lemons/Result';
|
|
5
|
-
|
|
6
|
-
import type { Decoder } from './types';
|
|
7
|
-
|
|
8
|
-
/**
|
|
9
|
-
* Decoder that always fails with the given error message, no matter what the input.
|
|
10
|
-
*/
|
|
11
|
-
export function fail(msg: string): Decoder<empty> {
|
|
12
|
-
return (blob: mixed) => Err(annotate(blob, msg));
|
|
13
|
-
}
|
package/guard.d.ts
DELETED
package/guard.js
DELETED
|
@@ -1,30 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
|
|
3
|
-
Object.defineProperty(exports, "__esModule", {
|
|
4
|
-
value: true
|
|
5
|
-
});
|
|
6
|
-
exports.guard = guard;
|
|
7
|
-
|
|
8
|
-
var _debrief = require("debrief");
|
|
9
|
-
|
|
10
|
-
/**
|
|
11
|
-
* Alternative for serialize() that does not echo back the input value.
|
|
12
|
-
*/
|
|
13
|
-
function serializeSimple(annotation) {
|
|
14
|
-
return (0, _debrief.summarize)(annotation).join('\n');
|
|
15
|
-
}
|
|
16
|
-
|
|
17
|
-
function guard(decoder, options) {
|
|
18
|
-
var o = options || {};
|
|
19
|
-
var style = o.style || 'inline';
|
|
20
|
-
var serializer = style === 'inline' ? _debrief.serialize // Normal serializer, which echoes back inputted value and inlines errors
|
|
21
|
-
: serializeSimple; // Only returns error messages, without echoing back input
|
|
22
|
-
|
|
23
|
-
return function (blob) {
|
|
24
|
-
return decoder(blob).mapError(function (annotation) {
|
|
25
|
-
var err = new Error('\n' + serializer(annotation));
|
|
26
|
-
err.name = 'Decoding error';
|
|
27
|
-
return err;
|
|
28
|
-
}).unwrap();
|
|
29
|
-
};
|
|
30
|
-
}
|
package/guard.js.flow
DELETED
|
@@ -1,36 +0,0 @@
|
|
|
1
|
-
// @flow strict
|
|
2
|
-
|
|
3
|
-
import { serialize as serializeInline, summarize } from 'debrief';
|
|
4
|
-
import type { Annotation } from 'debrief';
|
|
5
|
-
|
|
6
|
-
import type { Decoder, Guard } from './types';
|
|
7
|
-
|
|
8
|
-
type Options = {|
|
|
9
|
-
style?: 'inline' | 'simple', // `inline` by default
|
|
10
|
-
|};
|
|
11
|
-
|
|
12
|
-
/**
|
|
13
|
-
* Alternative for serialize() that does not echo back the input value.
|
|
14
|
-
*/
|
|
15
|
-
function serializeSimple(annotation: Annotation) {
|
|
16
|
-
return summarize(annotation).join('\n');
|
|
17
|
-
}
|
|
18
|
-
|
|
19
|
-
export function guard<T>(decoder: Decoder<T>, options?: Options): Guard<T> {
|
|
20
|
-
const o = options || {};
|
|
21
|
-
const style = o.style || 'inline';
|
|
22
|
-
|
|
23
|
-
const serializer =
|
|
24
|
-
style === 'inline'
|
|
25
|
-
? serializeInline // Normal serializer, which echoes back inputted value and inlines errors
|
|
26
|
-
: serializeSimple; // Only returns error messages, without echoing back input
|
|
27
|
-
|
|
28
|
-
return (blob: mixed) =>
|
|
29
|
-
decoder(blob)
|
|
30
|
-
.mapError((annotation) => {
|
|
31
|
-
const err = new Error('\n' + serializer(annotation));
|
|
32
|
-
err.name = 'Decoding error';
|
|
33
|
-
return err;
|
|
34
|
-
})
|
|
35
|
-
.unwrap();
|
|
36
|
-
}
|
package/helpers.d.ts
DELETED
|
@@ -1,79 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Given a type like:
|
|
3
|
-
*
|
|
4
|
-
* {
|
|
5
|
-
* a: string;
|
|
6
|
-
* b: number | undefined;
|
|
7
|
-
* c: null | undefined;
|
|
8
|
-
* d: null;
|
|
9
|
-
* e: undefined;
|
|
10
|
-
* }
|
|
11
|
-
*
|
|
12
|
-
* Will drop all the "undefined" types. In this case, only "e":
|
|
13
|
-
*
|
|
14
|
-
* {
|
|
15
|
-
* a: string;
|
|
16
|
-
* b: number | undefined;
|
|
17
|
-
* c: null | undefined;
|
|
18
|
-
* d: null;
|
|
19
|
-
* }
|
|
20
|
-
*
|
|
21
|
-
*/
|
|
22
|
-
type Compact<T> = { [K in IsDefined<T, keyof T>]: T[K] };
|
|
23
|
-
|
|
24
|
-
type IsDefined<T, K extends keyof T> = K extends any
|
|
25
|
-
? T[K] extends undefined
|
|
26
|
-
? never
|
|
27
|
-
: K
|
|
28
|
-
: never;
|
|
29
|
-
|
|
30
|
-
//
|
|
31
|
-
// HACK:
|
|
32
|
-
// These weird conditionals test whether TypeScript is configured with the
|
|
33
|
-
// `strictNullChecks` compiler option. We use these definitions to influence
|
|
34
|
-
// what's considered a "required" vs an "optional" key for the AllowImplicit
|
|
35
|
-
// type.
|
|
36
|
-
//
|
|
37
|
-
// If strictNullChecks is false, then we should not be emitting any `?` fields
|
|
38
|
-
// and consider all fields "required" because everything is optional by default
|
|
39
|
-
// in that mode anyway.
|
|
40
|
-
//
|
|
41
|
-
type NoStrictNullChecks = undefined extends string ? 1 : undefined;
|
|
42
|
-
// ^^^^^^^^^^^^^^^^^^^^^^^^
|
|
43
|
-
type StrictNullChecks = undefined extends string ? undefined : 1;
|
|
44
|
-
// ^^^^^^^^^^^^^^^^^^^^^^^^
|
|
45
|
-
|
|
46
|
-
export type RequiredKeys<T> = keyof Compact<{
|
|
47
|
-
[K in keyof T]: undefined extends T[K] ? NoStrictNullChecks : 1;
|
|
48
|
-
}>;
|
|
49
|
-
|
|
50
|
-
export type OptionalKeys<T> = keyof Compact<{
|
|
51
|
-
[K in keyof T]: undefined extends T[K] ? 1 : StrictNullChecks;
|
|
52
|
-
}>;
|
|
53
|
-
|
|
54
|
-
/**
|
|
55
|
-
* Transforms an object type, by marking all fields that contain "undefined"
|
|
56
|
-
* with a question mark, i.e. allowing implicit-undefineds when
|
|
57
|
-
* explicit-undefined are also allowed.
|
|
58
|
-
*
|
|
59
|
-
* For example, if:
|
|
60
|
-
*
|
|
61
|
-
* type User = {
|
|
62
|
-
* name: string;
|
|
63
|
-
* age: number | null | undefined;
|
|
64
|
-
* }
|
|
65
|
-
*
|
|
66
|
-
* Then AllowImplicit<User> will become equivalent to:
|
|
67
|
-
*
|
|
68
|
-
* {
|
|
69
|
-
* name: string;
|
|
70
|
-
* age?: number | null;
|
|
71
|
-
* ^
|
|
72
|
-
* Note the question mark
|
|
73
|
-
* }
|
|
74
|
-
*/
|
|
75
|
-
type AllowImplicit<T> = { [K in RequiredKeys<T>]-?: T[K] } & {
|
|
76
|
-
[K in OptionalKeys<T>]+?: Exclude<T[K], undefined>;
|
|
77
|
-
};
|
|
78
|
-
|
|
79
|
-
export { AllowImplicit };
|
package/index.d.ts
DELETED
|
@@ -1,38 +0,0 @@
|
|
|
1
|
-
import { Decoder, DecoderType, Guard, GuardType } from './types';
|
|
2
|
-
|
|
3
|
-
export { guard } from './guard';
|
|
4
|
-
export { compose, map, predicate } from './utils';
|
|
5
|
-
|
|
6
|
-
export { array, nonEmptyArray, poja } from './array';
|
|
7
|
-
export { boolean, numericBoolean, truthy } from './boolean';
|
|
8
|
-
export { constant, hardcoded, mixed, null_, undefined_, unknown } from './constants';
|
|
9
|
-
export { date, iso8601 } from './date';
|
|
10
|
-
export { describe } from './describe';
|
|
11
|
-
export { dispatch } from './dispatch';
|
|
12
|
-
export {
|
|
13
|
-
either,
|
|
14
|
-
either3,
|
|
15
|
-
either4,
|
|
16
|
-
either5,
|
|
17
|
-
either6,
|
|
18
|
-
either7,
|
|
19
|
-
either8,
|
|
20
|
-
either9,
|
|
21
|
-
oneOf,
|
|
22
|
-
} from './either';
|
|
23
|
-
export { fail } from './fail';
|
|
24
|
-
export { instanceOf } from './instanceOf';
|
|
25
|
-
export { json, jsonArray, jsonObject } from './json';
|
|
26
|
-
export { lazy } from './lazy';
|
|
27
|
-
export { mapping, dict } from './mapping';
|
|
28
|
-
export { integer, number, positiveInteger, positiveNumber } from './number';
|
|
29
|
-
export { exact, inexact, object, pojo } from './object';
|
|
30
|
-
export { maybe, nullable, optional } from './optional';
|
|
31
|
-
export { email, nonEmptyString, regex, string, url } from './string';
|
|
32
|
-
export { tuple1, tuple2, tuple3, tuple4, tuple5, tuple6 } from './tuple';
|
|
33
|
-
|
|
34
|
-
export { Decoder, Guard };
|
|
35
|
-
export { DecoderType, GuardType };
|
|
36
|
-
export { JSONArray, JSONObject, JSONValue } from './json';
|
|
37
|
-
|
|
38
|
-
export type $DecoderType<T> = DecoderType<T>; // Alias for backward compatibility
|
package/instanceOf.d.ts
DELETED
package/instanceOf.js
DELETED
|
@@ -1,17 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
|
|
3
|
-
Object.defineProperty(exports, "__esModule", {
|
|
4
|
-
value: true
|
|
5
|
-
});
|
|
6
|
-
exports.instanceOf = instanceOf;
|
|
7
|
-
|
|
8
|
-
var _debrief = require("debrief");
|
|
9
|
-
|
|
10
|
-
var _Result = require("lemons/Result");
|
|
11
|
-
|
|
12
|
-
function instanceOf(klass) {
|
|
13
|
-
return function (blob) {
|
|
14
|
-
return blob instanceof klass ? (0, _Result.Ok)(blob) : (0, _Result.Err)((0, _debrief.annotate)(blob, "Must be ".concat( // $FlowFixMe[incompatible-use] - klass.name is fine?
|
|
15
|
-
klass.name, " instance")));
|
|
16
|
-
};
|
|
17
|
-
}
|
package/json.d.ts
DELETED
|
@@ -1,11 +0,0 @@
|
|
|
1
|
-
import { Decoder } from './types';
|
|
2
|
-
|
|
3
|
-
export type JSONValue = null | string | number | boolean | JSONObject | JSONArray;
|
|
4
|
-
export interface JSONObject {
|
|
5
|
-
[key: string]: JSONValue;
|
|
6
|
-
}
|
|
7
|
-
export type JSONArray = JSONValue[];
|
|
8
|
-
|
|
9
|
-
export const json: Decoder<JSONValue>;
|
|
10
|
-
export const jsonArray: Decoder<JSONArray>;
|
|
11
|
-
export const jsonObject: Decoder<JSONObject>;
|
package/lazy.d.ts
DELETED
package/mapping.d.ts
DELETED
package/mapping.js
DELETED
|
@@ -1,113 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
|
|
3
|
-
Object.defineProperty(exports, "__esModule", {
|
|
4
|
-
value: true
|
|
5
|
-
});
|
|
6
|
-
exports.dict = dict;
|
|
7
|
-
exports.mapping = mapping;
|
|
8
|
-
|
|
9
|
-
var _debrief = require("debrief");
|
|
10
|
-
|
|
11
|
-
var _Result = require("lemons/Result");
|
|
12
|
-
|
|
13
|
-
var _object = require("./object");
|
|
14
|
-
|
|
15
|
-
var _utils = require("./utils");
|
|
16
|
-
|
|
17
|
-
function _slicedToArray(arr, i) { return _arrayWithHoles(arr) || _iterableToArrayLimit(arr, i) || _unsupportedIterableToArray(arr, i) || _nonIterableRest(); }
|
|
18
|
-
|
|
19
|
-
function _nonIterableRest() { throw new TypeError("Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); }
|
|
20
|
-
|
|
21
|
-
function _iterableToArrayLimit(arr, i) { var _i = arr == null ? null : typeof Symbol !== "undefined" && arr[Symbol.iterator] || arr["@@iterator"]; if (_i == null) return; var _arr = []; var _n = true; var _d = false; var _s, _e; try { for (_i = _i.call(arr); !(_n = (_s = _i.next()).done); _n = true) { _arr.push(_s.value); if (i && _arr.length === i) break; } } catch (err) { _d = true; _e = err; } finally { try { if (!_n && _i["return"] != null) _i["return"](); } finally { if (_d) throw _e; } } return _arr; }
|
|
22
|
-
|
|
23
|
-
function _arrayWithHoles(arr) { if (Array.isArray(arr)) return arr; }
|
|
24
|
-
|
|
25
|
-
function _createForOfIteratorHelper(o, allowArrayLike) { var it = typeof Symbol !== "undefined" && o[Symbol.iterator] || o["@@iterator"]; if (!it) { if (Array.isArray(o) || (it = _unsupportedIterableToArray(o)) || allowArrayLike && o && typeof o.length === "number") { if (it) o = it; var i = 0; var F = function F() {}; return { s: F, n: function n() { if (i >= o.length) return { done: true }; return { done: false, value: o[i++] }; }, e: function e(_e2) { throw _e2; }, f: F }; } throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); } var normalCompletion = true, didErr = false, err; return { s: function s() { it = it.call(o); }, n: function n() { var step = it.next(); normalCompletion = step.done; return step; }, e: function e(_e3) { didErr = true; err = _e3; }, f: function f() { try { if (!normalCompletion && it["return"] != null) it["return"](); } finally { if (didErr) throw err; } } }; }
|
|
26
|
-
|
|
27
|
-
function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); }
|
|
28
|
-
|
|
29
|
-
function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) { arr2[i] = arr[i]; } return arr2; }
|
|
30
|
-
|
|
31
|
-
function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); if (enumerableOnly) { symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; }); } keys.push.apply(keys, symbols); } return keys; }
|
|
32
|
-
|
|
33
|
-
function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i] != null ? arguments[i] : {}; if (i % 2) { ownKeys(Object(source), true).forEach(function (key) { _defineProperty(target, key, source[key]); }); } else if (Object.getOwnPropertyDescriptors) { Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)); } else { ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } } return target; }
|
|
34
|
-
|
|
35
|
-
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; }
|
|
36
|
-
|
|
37
|
-
/**
|
|
38
|
-
* Given an object, will decode a Map of string keys to whatever values.
|
|
39
|
-
*
|
|
40
|
-
* For example, given a decoder for a Person, we can verify a Person lookup
|
|
41
|
-
* table structure (of type Map<string, Person>) like so:
|
|
42
|
-
*
|
|
43
|
-
* mapping(person)
|
|
44
|
-
*
|
|
45
|
-
*/
|
|
46
|
-
function mapping(decoder) {
|
|
47
|
-
return (0, _utils.compose)(_object.pojo, // $FlowFixMe[unclear-type] (not really an issue) - deliberate use of Object here
|
|
48
|
-
function (blob) {
|
|
49
|
-
var tuples = [];
|
|
50
|
-
var errors = [];
|
|
51
|
-
Object.keys(blob).forEach(function (key) {
|
|
52
|
-
var value = blob[key];
|
|
53
|
-
var result = decoder(value);
|
|
54
|
-
|
|
55
|
-
try {
|
|
56
|
-
var okValue = result.unwrap();
|
|
57
|
-
|
|
58
|
-
if (errors.length === 0) {
|
|
59
|
-
tuples.push([key, okValue]);
|
|
60
|
-
}
|
|
61
|
-
} catch (e) {
|
|
62
|
-
/* istanbul ignore else */
|
|
63
|
-
if ((0, _debrief.isAnnotation)(e)) {
|
|
64
|
-
tuples.length = 0; // Clear the tuples array
|
|
65
|
-
|
|
66
|
-
errors.push([key, e]);
|
|
67
|
-
} else {
|
|
68
|
-
// Otherwise, simply rethrow it
|
|
69
|
-
|
|
70
|
-
/* istanbul ignore next */
|
|
71
|
-
throw e;
|
|
72
|
-
}
|
|
73
|
-
}
|
|
74
|
-
});
|
|
75
|
-
|
|
76
|
-
if (errors.length > 0) {
|
|
77
|
-
return (0, _Result.Err)((0, _debrief.annotateFields)(blob, errors));
|
|
78
|
-
} else {
|
|
79
|
-
return (0, _Result.Ok)(new Map(tuples));
|
|
80
|
-
}
|
|
81
|
-
});
|
|
82
|
-
}
|
|
83
|
-
|
|
84
|
-
function mapToObject(mapping) {
|
|
85
|
-
var result = _objectSpread({}, null);
|
|
86
|
-
|
|
87
|
-
var _iterator = _createForOfIteratorHelper(mapping.entries()),
|
|
88
|
-
_step;
|
|
89
|
-
|
|
90
|
-
try {
|
|
91
|
-
for (_iterator.s(); !(_step = _iterator.n()).done;) {
|
|
92
|
-
var _step$value = _slicedToArray(_step.value, 2),
|
|
93
|
-
k = _step$value[0],
|
|
94
|
-
v = _step$value[1];
|
|
95
|
-
|
|
96
|
-
result[k] = v;
|
|
97
|
-
}
|
|
98
|
-
} catch (err) {
|
|
99
|
-
_iterator.e(err);
|
|
100
|
-
} finally {
|
|
101
|
-
_iterator.f();
|
|
102
|
-
}
|
|
103
|
-
|
|
104
|
-
return result;
|
|
105
|
-
}
|
|
106
|
-
/**
|
|
107
|
-
* Like mapping(), but returns an object rather than a Map instance.
|
|
108
|
-
*/
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
function dict(decoder) {
|
|
112
|
-
return (0, _utils.map)(mapping(decoder), mapToObject);
|
|
113
|
-
}
|
package/mapping.js.flow
DELETED
|
@@ -1,71 +0,0 @@
|
|
|
1
|
-
// @flow strict
|
|
2
|
-
|
|
3
|
-
import type { Annotation } from 'debrief';
|
|
4
|
-
import { annotateFields, isAnnotation } from 'debrief';
|
|
5
|
-
import { Err, Ok } from 'lemons/Result';
|
|
6
|
-
|
|
7
|
-
import { pojo } from './object';
|
|
8
|
-
import type { Decoder } from './types';
|
|
9
|
-
import { compose, map } from './utils';
|
|
10
|
-
|
|
11
|
-
/**
|
|
12
|
-
* Given an object, will decode a Map of string keys to whatever values.
|
|
13
|
-
*
|
|
14
|
-
* For example, given a decoder for a Person, we can verify a Person lookup
|
|
15
|
-
* table structure (of type Map<string, Person>) like so:
|
|
16
|
-
*
|
|
17
|
-
* mapping(person)
|
|
18
|
-
*
|
|
19
|
-
*/
|
|
20
|
-
export function mapping<T>(decoder: Decoder<T>): Decoder<Map<string, T>> {
|
|
21
|
-
return compose(
|
|
22
|
-
pojo,
|
|
23
|
-
// $FlowFixMe[unclear-type] (not really an issue) - deliberate use of Object here
|
|
24
|
-
(blob: Object) => {
|
|
25
|
-
let tuples: Array<[string, T]> = [];
|
|
26
|
-
let errors: Array<[string, string | Annotation]> = [];
|
|
27
|
-
|
|
28
|
-
Object.keys(blob).forEach((key: string) => {
|
|
29
|
-
const value: T = blob[key];
|
|
30
|
-
const result = decoder(value);
|
|
31
|
-
try {
|
|
32
|
-
const okValue = result.unwrap();
|
|
33
|
-
if (errors.length === 0) {
|
|
34
|
-
tuples.push([key, okValue]);
|
|
35
|
-
}
|
|
36
|
-
} catch (e) {
|
|
37
|
-
/* istanbul ignore else */
|
|
38
|
-
if (isAnnotation(e)) {
|
|
39
|
-
tuples.length = 0; // Clear the tuples array
|
|
40
|
-
errors.push([key, e]);
|
|
41
|
-
} else {
|
|
42
|
-
// Otherwise, simply rethrow it
|
|
43
|
-
/* istanbul ignore next */
|
|
44
|
-
throw e;
|
|
45
|
-
}
|
|
46
|
-
}
|
|
47
|
-
});
|
|
48
|
-
|
|
49
|
-
if (errors.length > 0) {
|
|
50
|
-
return Err(annotateFields(blob, errors));
|
|
51
|
-
} else {
|
|
52
|
-
return Ok(new Map(tuples));
|
|
53
|
-
}
|
|
54
|
-
}
|
|
55
|
-
);
|
|
56
|
-
}
|
|
57
|
-
|
|
58
|
-
function mapToObject<T>(mapping: Map<string, T>): { [string]: T } {
|
|
59
|
-
const result: { [string]: T } = { ...null };
|
|
60
|
-
for (const [k, v] of mapping.entries()) {
|
|
61
|
-
result[k] = v;
|
|
62
|
-
}
|
|
63
|
-
return result;
|
|
64
|
-
}
|
|
65
|
-
|
|
66
|
-
/**
|
|
67
|
-
* Like mapping(), but returns an object rather than a Map instance.
|
|
68
|
-
*/
|
|
69
|
-
export function dict<T>(decoder: Decoder<T>): Decoder<{ [string]: T }> {
|
|
70
|
-
return map(mapping(decoder), mapToObject);
|
|
71
|
-
}
|
package/number.d.ts
DELETED
package/number.js
DELETED
|
@@ -1,38 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
|
|
3
|
-
Object.defineProperty(exports, "__esModule", {
|
|
4
|
-
value: true
|
|
5
|
-
});
|
|
6
|
-
exports.positiveNumber = exports.positiveInteger = exports.number = exports.integer = void 0;
|
|
7
|
-
|
|
8
|
-
var _debrief = require("debrief");
|
|
9
|
-
|
|
10
|
-
var _Result = require("lemons/Result");
|
|
11
|
-
|
|
12
|
-
var _utils = require("./utils");
|
|
13
|
-
|
|
14
|
-
var anyNumber = function anyNumber(blob) {
|
|
15
|
-
return typeof blob === 'number' && !Number.isNaN(blob) ? (0, _Result.Ok)(blob) : (0, _Result.Err)((0, _debrief.annotate)(blob, 'Must be number'));
|
|
16
|
-
};
|
|
17
|
-
|
|
18
|
-
var isInteger = function isInteger(n) {
|
|
19
|
-
return Number.isInteger(n);
|
|
20
|
-
};
|
|
21
|
-
|
|
22
|
-
var isFinite = function isFinite(n) {
|
|
23
|
-
return Number.isFinite(n);
|
|
24
|
-
};
|
|
25
|
-
|
|
26
|
-
var number = (0, _utils.compose)(anyNumber, (0, _utils.predicate)(isFinite, 'Number must be finite'));
|
|
27
|
-
exports.number = number;
|
|
28
|
-
var positiveNumber = (0, _utils.compose)(number, (0, _utils.predicate)(function (n) {
|
|
29
|
-
return n >= 0;
|
|
30
|
-
}, 'Number must be positive')); // Integers
|
|
31
|
-
|
|
32
|
-
exports.positiveNumber = positiveNumber;
|
|
33
|
-
var integer = (0, _utils.compose)(number, (0, _utils.predicate)(isInteger, 'Number must be an integer'));
|
|
34
|
-
exports.integer = integer;
|
|
35
|
-
var positiveInteger = (0, _utils.compose)(integer, (0, _utils.predicate)(function (n) {
|
|
36
|
-
return n >= 0;
|
|
37
|
-
}, 'Number must be positive'));
|
|
38
|
-
exports.positiveInteger = positiveInteger;
|