decoders 2.0.0-beta1 → 2.0.0-beta13
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 +53 -5
- package/Decoder.d.ts +94 -0
- package/Decoder.js +222 -0
- package/Decoder.js.flow +286 -0
- package/Decoder.mjs +215 -0
- package/NotSupportedTSVersion.d.ts +1 -0
- package/README.md +124 -961
- package/_utils.d.ts +9 -0
- package/{cjs/_utils.js → _utils.js} +12 -18
- package/{cjs/_utils.js.flow → _utils.js.flow} +15 -19
- package/{es/_utils.js → _utils.mjs} +11 -15
- package/{ts/annotate.d.ts → annotate.d.ts} +25 -21
- package/{cjs/annotate.js → annotate.js} +0 -0
- package/{cjs/annotate.js.flow → annotate.js.flow} +0 -0
- package/{es/annotate.js → annotate.mjs} +0 -0
- package/format.d.ts +6 -0
- package/{cjs/format/inline.js → format.js} +7 -2
- package/{cjs/format/inline.js.flow → format.js.flow} +9 -3
- package/{es/format/inline.js → format.mjs} +5 -2
- package/index.d.ts +40 -0
- package/index.js +89 -0
- package/index.js.flow +44 -0
- package/index.mjs +11 -0
- package/{ts → lib}/_helpers.d.ts +0 -0
- package/lib/arrays.d.ts +59 -0
- package/lib/arrays.js +139 -0
- package/lib/arrays.js.flow +138 -0
- package/lib/arrays.mjs +124 -0
- package/lib/basics.d.ts +93 -0
- package/lib/basics.js +144 -0
- package/lib/basics.js.flow +124 -0
- package/lib/basics.mjs +120 -0
- package/lib/booleans.d.ts +16 -0
- package/lib/booleans.js +35 -0
- package/lib/booleans.js.flow +22 -0
- package/lib/booleans.mjs +25 -0
- package/lib/dates.d.ts +15 -0
- package/lib/dates.js +44 -0
- package/lib/dates.js.flow +40 -0
- package/lib/dates.mjs +34 -0
- package/lib/json.d.ts +35 -0
- package/lib/json.js +55 -0
- package/lib/json.js.flow +50 -0
- package/lib/json.mjs +40 -0
- package/lib/numbers.d.ts +31 -0
- package/lib/numbers.js +51 -0
- package/lib/numbers.js.flow +48 -0
- package/lib/numbers.mjs +41 -0
- package/lib/objects.d.ts +75 -0
- package/lib/objects.js +240 -0
- package/lib/objects.js.flow +246 -0
- package/lib/objects.mjs +223 -0
- package/lib/strings.d.ts +56 -0
- package/lib/strings.js +101 -0
- package/lib/strings.js.flow +90 -0
- package/lib/strings.mjs +82 -0
- package/lib/unions.d.ts +55 -0
- package/lib/unions.js +160 -0
- package/lib/unions.js.flow +155 -0
- package/lib/unions.mjs +146 -0
- package/lib/utilities.d.ts +34 -0
- package/lib/utilities.js +75 -0
- package/lib/utilities.js.flow +65 -0
- package/lib/utilities.mjs +60 -0
- package/package.json +79 -29
- package/result.d.ts +16 -0
- package/result.js +34 -0
- package/result.js.flow +26 -0
- package/result.mjs +27 -0
- package/cjs/_guard.js +0 -26
- package/cjs/_guard.js.flow +0 -20
- package/cjs/_types.js +0 -1
- package/cjs/_types.js.flow +0 -20
- package/cjs/format/index.js +0 -12
- package/cjs/format/index.js.flow +0 -4
- package/cjs/format/short.js +0 -10
- package/cjs/format/short.js.flow +0 -8
- package/cjs/index.js +0 -120
- package/cjs/index.js.flow +0 -63
- package/cjs/result.js +0 -172
- package/cjs/result.js.flow +0 -166
- package/cjs/stdlib/array.js +0 -108
- package/cjs/stdlib/array.js.flow +0 -103
- package/cjs/stdlib/boolean.js +0 -44
- package/cjs/stdlib/boolean.js.flow +0 -29
- package/cjs/stdlib/composition.js +0 -56
- package/cjs/stdlib/composition.js.flow +0 -43
- package/cjs/stdlib/constants.js +0 -69
- package/cjs/stdlib/constants.js.flow +0 -46
- package/cjs/stdlib/date.js +0 -46
- package/cjs/stdlib/date.js.flow +0 -40
- package/cjs/stdlib/describe.js +0 -26
- package/cjs/stdlib/describe.js.flow +0 -17
- package/cjs/stdlib/dispatch.js +0 -62
- package/cjs/stdlib/dispatch.js.flow +0 -58
- package/cjs/stdlib/either.js +0 -117
- package/cjs/stdlib/either.js.flow +0 -151
- package/cjs/stdlib/fail.js +0 -21
- package/cjs/stdlib/fail.js.flow +0 -12
- package/cjs/stdlib/instanceOf.js +0 -19
- package/cjs/stdlib/instanceOf.js.flow +0 -20
- package/cjs/stdlib/json.js +0 -31
- package/cjs/stdlib/json.js.flow +0 -28
- package/cjs/stdlib/lazy.js +0 -16
- package/cjs/stdlib/lazy.js.flow +0 -15
- package/cjs/stdlib/mapping.js +0 -67
- package/cjs/stdlib/mapping.js.flow +0 -54
- package/cjs/stdlib/number.js +0 -40
- package/cjs/stdlib/number.js.flow +0 -34
- package/cjs/stdlib/object.js +0 -194
- package/cjs/stdlib/object.js.flow +0 -203
- package/cjs/stdlib/optional.js +0 -54
- package/cjs/stdlib/optional.js.flow +0 -41
- package/cjs/stdlib/string.js +0 -98
- package/cjs/stdlib/string.js.flow +0 -82
- package/cjs/stdlib/tuple.js +0 -173
- package/cjs/stdlib/tuple.js.flow +0 -220
- package/es/_guard.js +0 -15
- package/es/_types.js +0 -0
- package/es/format/index.js +0 -2
- package/es/format/short.js +0 -4
- package/es/index.js +0 -37
- package/es/result.js +0 -139
- package/es/stdlib/array.js +0 -91
- package/es/stdlib/boolean.js +0 -28
- package/es/stdlib/composition.js +0 -42
- package/es/stdlib/constants.js +0 -46
- package/es/stdlib/date.js +0 -28
- package/es/stdlib/describe.js +0 -16
- package/es/stdlib/dispatch.js +0 -51
- package/es/stdlib/either.js +0 -90
- package/es/stdlib/fail.js +0 -11
- package/es/stdlib/instanceOf.js +0 -8
- package/es/stdlib/json.js +0 -15
- package/es/stdlib/lazy.js +0 -11
- package/es/stdlib/mapping.js +0 -54
- package/es/stdlib/number.js +0 -25
- package/es/stdlib/object.js +0 -175
- package/es/stdlib/optional.js +0 -38
- package/es/stdlib/string.js +0 -76
- package/es/stdlib/tuple.js +0 -155
- package/ts/_guard.d.ts +0 -7
- package/ts/_types.d.ts +0 -16
- package/ts/_utils.d.ts +0 -13
- package/ts/array.d.ts +0 -5
- package/ts/boolean.d.ts +0 -5
- package/ts/constants.d.ts +0 -11
- package/ts/date.d.ts +0 -4
- package/ts/describe.d.ts +0 -3
- package/ts/dispatch.d.ts +0 -8
- package/ts/either.d.ts +0 -61
- package/ts/fail.d.ts +0 -3
- package/ts/index.d.ts +0 -42
- package/ts/inline.d.ts +0 -3
- package/ts/instanceOf.d.ts +0 -3
- package/ts/json.d.ts +0 -11
- package/ts/lazy.d.ts +0 -3
- package/ts/mapping.d.ts +0 -4
- package/ts/number.d.ts +0 -6
- package/ts/object.d.ts +0 -33
- package/ts/optional.d.ts +0 -5
- package/ts/result.d.ts +0 -39
- package/ts/short.d.ts +0 -3
- package/ts/string.d.ts +0 -7
- package/ts/tuple.d.ts +0 -30
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
import { Decoder } from '../Decoder';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Accepts any value that is an ``instanceof`` the given class.
|
|
5
|
+
*/
|
|
6
|
+
export function instanceOf<T>(klass: new (...args: readonly any[]) => T): Decoder<T>;
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* Lazily evaluate the given decoder. This is useful to build self-referential
|
|
10
|
+
* types for recursive data structures.
|
|
11
|
+
*/
|
|
12
|
+
export function lazy<T>(decoderFn: () => Decoder<T>): Decoder<T>;
|
|
13
|
+
|
|
14
|
+
/**
|
|
15
|
+
* Pre-process the data input before passing it into the decoder. This gives
|
|
16
|
+
* you the ability to arbitrarily customize the input on the fly before passing
|
|
17
|
+
* it to the decoder. Of course, the input value at that point is still of
|
|
18
|
+
* ``unknown`` type, so you will have to deal with that accordingly.
|
|
19
|
+
*/
|
|
20
|
+
export function prep<T>(
|
|
21
|
+
mapperFn: (blob: unknown) => unknown,
|
|
22
|
+
decoder: Decoder<T>,
|
|
23
|
+
): Decoder<T>;
|
|
24
|
+
|
|
25
|
+
/**
|
|
26
|
+
* Rejects all inputs, and always fails with the given error message. May be
|
|
27
|
+
* useful for explicitly disallowing keys, or for testing purposes.
|
|
28
|
+
*/
|
|
29
|
+
export function never(msg: string): Decoder<never>;
|
|
30
|
+
|
|
31
|
+
/**
|
|
32
|
+
* Alias of never().
|
|
33
|
+
*/
|
|
34
|
+
export function fail(msg: string): Decoder<never>;
|
package/lib/utilities.js
ADDED
|
@@ -0,0 +1,75 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
exports.__esModule = true;
|
|
4
|
+
exports.fail = void 0;
|
|
5
|
+
exports.instanceOf = instanceOf;
|
|
6
|
+
exports.lazy = lazy;
|
|
7
|
+
exports.never = never;
|
|
8
|
+
exports.prep = prep;
|
|
9
|
+
|
|
10
|
+
var _annotate = require("../annotate");
|
|
11
|
+
|
|
12
|
+
var _Decoder = require("../Decoder");
|
|
13
|
+
|
|
14
|
+
/**
|
|
15
|
+
* Accepts any value that is an ``instanceof`` the given class.
|
|
16
|
+
*/
|
|
17
|
+
function instanceOf(klass) {
|
|
18
|
+
return (0, _Decoder.define)(function (blob, ok, err) {
|
|
19
|
+
return blob instanceof klass ? ok(blob) : err("Must be " + // $FlowFixMe[incompatible-use] - klass.name is fine?
|
|
20
|
+
klass.name + " instance");
|
|
21
|
+
});
|
|
22
|
+
}
|
|
23
|
+
/**
|
|
24
|
+
* Lazily evaluate the given decoder. This is useful to build self-referential
|
|
25
|
+
* types for recursive data structures.
|
|
26
|
+
*/
|
|
27
|
+
|
|
28
|
+
|
|
29
|
+
function lazy(decoderFn) {
|
|
30
|
+
return (0, _Decoder.define)(function (blob) {
|
|
31
|
+
return decoderFn().decode(blob);
|
|
32
|
+
});
|
|
33
|
+
}
|
|
34
|
+
/**
|
|
35
|
+
* Pre-process the data input before passing it into the decoder. This gives
|
|
36
|
+
* you the ability to arbitrarily customize the input on the fly before passing
|
|
37
|
+
* it to the decoder. Of course, the input value at that point is still of
|
|
38
|
+
* ``unknown`` type, so you will have to deal with that accordingly.
|
|
39
|
+
*/
|
|
40
|
+
|
|
41
|
+
|
|
42
|
+
function prep(mapperFn, decoder) {
|
|
43
|
+
return (0, _Decoder.define)(function (originalInput, _, err) {
|
|
44
|
+
var blob;
|
|
45
|
+
|
|
46
|
+
try {
|
|
47
|
+
blob = mapperFn(originalInput);
|
|
48
|
+
} catch (e) {
|
|
49
|
+
return err((0, _annotate.annotate)(originalInput, e.message));
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
var r = decoder.decode(blob);
|
|
53
|
+
return r.ok ? r : err((0, _annotate.annotate)(originalInput, r.error.text)); // ^^^^^^^^^^^^^
|
|
54
|
+
// Annotates the _original_ input value
|
|
55
|
+
// (instead of echoing back blob)
|
|
56
|
+
});
|
|
57
|
+
}
|
|
58
|
+
/**
|
|
59
|
+
* Rejects all inputs, and always fails with the given error message. May be
|
|
60
|
+
* useful for explicitly disallowing keys, or for testing purposes.
|
|
61
|
+
*/
|
|
62
|
+
|
|
63
|
+
|
|
64
|
+
function never(msg) {
|
|
65
|
+
return (0, _Decoder.define)(function (_, __, err) {
|
|
66
|
+
return err(msg);
|
|
67
|
+
});
|
|
68
|
+
}
|
|
69
|
+
/**
|
|
70
|
+
* Alias of never().
|
|
71
|
+
*/
|
|
72
|
+
|
|
73
|
+
|
|
74
|
+
var fail = never;
|
|
75
|
+
exports.fail = fail;
|
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
// @flow strict
|
|
2
|
+
|
|
3
|
+
import { annotate } from '../annotate';
|
|
4
|
+
import { define } from '../Decoder';
|
|
5
|
+
import type { Decoder } from '../Decoder';
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* Accepts any value that is an ``instanceof`` the given class.
|
|
9
|
+
*/
|
|
10
|
+
export function instanceOf<T>(klass: Class<T>): Decoder<T> {
|
|
11
|
+
return define((blob, ok, err) =>
|
|
12
|
+
blob instanceof klass
|
|
13
|
+
? ok(blob)
|
|
14
|
+
: err(
|
|
15
|
+
`Must be ${
|
|
16
|
+
// $FlowFixMe[incompatible-use] - klass.name is fine?
|
|
17
|
+
klass.name
|
|
18
|
+
} instance`,
|
|
19
|
+
),
|
|
20
|
+
);
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
/**
|
|
24
|
+
* Lazily evaluate the given decoder. This is useful to build self-referential
|
|
25
|
+
* types for recursive data structures.
|
|
26
|
+
*/
|
|
27
|
+
export function lazy<T>(decoderFn: () => Decoder<T>): Decoder<T> {
|
|
28
|
+
return define((blob) => decoderFn().decode(blob));
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
/**
|
|
32
|
+
* Pre-process the data input before passing it into the decoder. This gives
|
|
33
|
+
* you the ability to arbitrarily customize the input on the fly before passing
|
|
34
|
+
* it to the decoder. Of course, the input value at that point is still of
|
|
35
|
+
* ``unknown`` type, so you will have to deal with that accordingly.
|
|
36
|
+
*/
|
|
37
|
+
export function prep<T>(mapperFn: (mixed) => mixed, decoder: Decoder<T>): Decoder<T> {
|
|
38
|
+
return define((originalInput, _, err) => {
|
|
39
|
+
let blob;
|
|
40
|
+
try {
|
|
41
|
+
blob = mapperFn(originalInput);
|
|
42
|
+
} catch (e) {
|
|
43
|
+
return err(annotate(originalInput, e.message));
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
const r = decoder.decode(blob);
|
|
47
|
+
return r.ok ? r : err(annotate(originalInput, r.error.text));
|
|
48
|
+
// ^^^^^^^^^^^^^
|
|
49
|
+
// Annotates the _original_ input value
|
|
50
|
+
// (instead of echoing back blob)
|
|
51
|
+
});
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
/**
|
|
55
|
+
* Rejects all inputs, and always fails with the given error message. May be
|
|
56
|
+
* useful for explicitly disallowing keys, or for testing purposes.
|
|
57
|
+
*/
|
|
58
|
+
export function never(msg: string): Decoder<empty> {
|
|
59
|
+
return define((_, __, err) => err(msg));
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
/**
|
|
63
|
+
* Alias of never().
|
|
64
|
+
*/
|
|
65
|
+
export const fail: (msg: string) => Decoder<empty> = never;
|
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
import { annotate } from '../annotate.mjs';
|
|
2
|
+
import { define } from '../Decoder.mjs';
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* Accepts any value that is an ``instanceof`` the given class.
|
|
6
|
+
*/
|
|
7
|
+
export function instanceOf(klass) {
|
|
8
|
+
return define(function (blob, ok, err) {
|
|
9
|
+
return blob instanceof klass ? ok(blob) : err("Must be " + // $FlowFixMe[incompatible-use] - klass.name is fine?
|
|
10
|
+
klass.name + " instance");
|
|
11
|
+
});
|
|
12
|
+
}
|
|
13
|
+
/**
|
|
14
|
+
* Lazily evaluate the given decoder. This is useful to build self-referential
|
|
15
|
+
* types for recursive data structures.
|
|
16
|
+
*/
|
|
17
|
+
|
|
18
|
+
export function lazy(decoderFn) {
|
|
19
|
+
return define(function (blob) {
|
|
20
|
+
return decoderFn().decode(blob);
|
|
21
|
+
});
|
|
22
|
+
}
|
|
23
|
+
/**
|
|
24
|
+
* Pre-process the data input before passing it into the decoder. This gives
|
|
25
|
+
* you the ability to arbitrarily customize the input on the fly before passing
|
|
26
|
+
* it to the decoder. Of course, the input value at that point is still of
|
|
27
|
+
* ``unknown`` type, so you will have to deal with that accordingly.
|
|
28
|
+
*/
|
|
29
|
+
|
|
30
|
+
export function prep(mapperFn, decoder) {
|
|
31
|
+
return define(function (originalInput, _, err) {
|
|
32
|
+
var blob;
|
|
33
|
+
|
|
34
|
+
try {
|
|
35
|
+
blob = mapperFn(originalInput);
|
|
36
|
+
} catch (e) {
|
|
37
|
+
return err(annotate(originalInput, e.message));
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
var r = decoder.decode(blob);
|
|
41
|
+
return r.ok ? r : err(annotate(originalInput, r.error.text)); // ^^^^^^^^^^^^^
|
|
42
|
+
// Annotates the _original_ input value
|
|
43
|
+
// (instead of echoing back blob)
|
|
44
|
+
});
|
|
45
|
+
}
|
|
46
|
+
/**
|
|
47
|
+
* Rejects all inputs, and always fails with the given error message. May be
|
|
48
|
+
* useful for explicitly disallowing keys, or for testing purposes.
|
|
49
|
+
*/
|
|
50
|
+
|
|
51
|
+
export function never(msg) {
|
|
52
|
+
return define(function (_, __, err) {
|
|
53
|
+
return err(msg);
|
|
54
|
+
});
|
|
55
|
+
}
|
|
56
|
+
/**
|
|
57
|
+
* Alias of never().
|
|
58
|
+
*/
|
|
59
|
+
|
|
60
|
+
export var fail = never;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "decoders",
|
|
3
|
-
"version": "2.0.0-
|
|
3
|
+
"version": "2.0.0-beta13",
|
|
4
4
|
"description": "Elegant and battle-tested validation library for type-safe input data (for TypeScript and Flow)",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"repository": {
|
|
@@ -12,43 +12,93 @@
|
|
|
12
12
|
"bugs": {
|
|
13
13
|
"url": "https://github.com/nvie/decoders/issues"
|
|
14
14
|
},
|
|
15
|
-
"main": "./
|
|
16
|
-
"module": "./
|
|
17
|
-
"type": "module",
|
|
18
|
-
"exports": {
|
|
19
|
-
"require": "./cjs/index.js",
|
|
20
|
-
"import": "./es/index.js"
|
|
21
|
-
},
|
|
22
|
-
"engines": {
|
|
23
|
-
"node": ">=12"
|
|
24
|
-
},
|
|
15
|
+
"main": "./index.js",
|
|
16
|
+
"module": "./index.mjs",
|
|
25
17
|
"keywords": [
|
|
26
|
-
"decoder",
|
|
27
18
|
"decoders",
|
|
28
|
-
"
|
|
29
|
-
"
|
|
19
|
+
"Decoder",
|
|
20
|
+
"always",
|
|
21
|
+
"and",
|
|
30
22
|
"array",
|
|
31
|
-
"string",
|
|
32
|
-
"email",
|
|
33
|
-
"regex",
|
|
34
|
-
"number",
|
|
35
|
-
"integer",
|
|
36
23
|
"boolean",
|
|
37
|
-
"
|
|
24
|
+
"then",
|
|
25
|
+
"compose",
|
|
26
|
+
"constant",
|
|
38
27
|
"date",
|
|
39
|
-
"
|
|
28
|
+
"decode",
|
|
29
|
+
"decoder",
|
|
30
|
+
"decoders",
|
|
31
|
+
"define",
|
|
32
|
+
"describe",
|
|
40
33
|
"dict",
|
|
34
|
+
"either",
|
|
35
|
+
"email",
|
|
36
|
+
"exact",
|
|
37
|
+
"fail",
|
|
38
|
+
"hardcoded",
|
|
39
|
+
"httpsUrl",
|
|
40
|
+
"inexact",
|
|
41
|
+
"instanceOf",
|
|
42
|
+
"integer",
|
|
43
|
+
"iso8601",
|
|
44
|
+
"json",
|
|
45
|
+
"jsonArray",
|
|
46
|
+
"jsonObject",
|
|
47
|
+
"lazy",
|
|
48
|
+
"map",
|
|
49
|
+
"mapping",
|
|
50
|
+
"maybe",
|
|
41
51
|
"mixed",
|
|
52
|
+
"mixedarray",
|
|
53
|
+
"never",
|
|
54
|
+
"nonEmptyArray",
|
|
55
|
+
"nonEmptyString",
|
|
56
|
+
"nullable",
|
|
57
|
+
"null_",
|
|
58
|
+
"number",
|
|
59
|
+
"numericBoolean",
|
|
60
|
+
"object",
|
|
61
|
+
"oneOf",
|
|
62
|
+
"optional",
|
|
63
|
+
"poja",
|
|
64
|
+
"pojo",
|
|
65
|
+
"positiveInteger",
|
|
66
|
+
"positiveNumber",
|
|
67
|
+
"predicate",
|
|
68
|
+
"prep",
|
|
69
|
+
"regex",
|
|
70
|
+
"set",
|
|
71
|
+
"string",
|
|
72
|
+
"taggedUnion",
|
|
73
|
+
"transform",
|
|
74
|
+
"truthy",
|
|
75
|
+
"tuple",
|
|
42
76
|
"tuple2",
|
|
77
|
+
"undefined_",
|
|
43
78
|
"unknown",
|
|
44
|
-
"
|
|
45
|
-
"
|
|
46
|
-
"
|
|
47
|
-
"
|
|
48
|
-
"
|
|
49
|
-
"
|
|
79
|
+
"url",
|
|
80
|
+
"uuid",
|
|
81
|
+
"uuidv1",
|
|
82
|
+
"uuidv4",
|
|
83
|
+
"verify",
|
|
84
|
+
"DecodeResult",
|
|
85
|
+
"DecoderType",
|
|
86
|
+
"JSONArray",
|
|
87
|
+
"JSONObject",
|
|
88
|
+
"JSONValue"
|
|
50
89
|
],
|
|
51
|
-
"types": "./ts/index.d.ts",
|
|
52
90
|
"githubUrl": "https://github.com/nvie/decoders",
|
|
53
|
-
"sideEffects": false
|
|
91
|
+
"sideEffects": false,
|
|
92
|
+
"typesVersions": {
|
|
93
|
+
">=4.1.0": {
|
|
94
|
+
"*": [
|
|
95
|
+
"*"
|
|
96
|
+
]
|
|
97
|
+
},
|
|
98
|
+
"*": {
|
|
99
|
+
"*": [
|
|
100
|
+
"NotSupportedTSVersion.d.ts"
|
|
101
|
+
]
|
|
102
|
+
}
|
|
103
|
+
}
|
|
54
104
|
}
|
package/result.d.ts
ADDED
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
export interface Ok<T> {
|
|
2
|
+
ok: true;
|
|
3
|
+
value: T;
|
|
4
|
+
error: undefined;
|
|
5
|
+
}
|
|
6
|
+
|
|
7
|
+
export interface Err<E> {
|
|
8
|
+
ok: false;
|
|
9
|
+
value: undefined;
|
|
10
|
+
error: E;
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
export type Result<T, E> = Ok<T> | Err<E>;
|
|
14
|
+
|
|
15
|
+
export function ok<T>(value: T): Ok<T>;
|
|
16
|
+
export function err<E>(error: E): Err<E>;
|
package/result.js
ADDED
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
exports.__esModule = true;
|
|
4
|
+
exports.err = err;
|
|
5
|
+
exports.ok = ok;
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* Result <value> <error>
|
|
9
|
+
* = Ok <value>
|
|
10
|
+
* | Err <error>
|
|
11
|
+
*/
|
|
12
|
+
|
|
13
|
+
/**
|
|
14
|
+
* Create a new Result instance representing a successful computation.
|
|
15
|
+
*/
|
|
16
|
+
function ok(value) {
|
|
17
|
+
return {
|
|
18
|
+
ok: true,
|
|
19
|
+
value: value,
|
|
20
|
+
error: undefined
|
|
21
|
+
};
|
|
22
|
+
}
|
|
23
|
+
/**
|
|
24
|
+
* Create a new Result instance representing a failed computation.
|
|
25
|
+
*/
|
|
26
|
+
|
|
27
|
+
|
|
28
|
+
function err(error) {
|
|
29
|
+
return {
|
|
30
|
+
ok: false,
|
|
31
|
+
value: undefined,
|
|
32
|
+
error: error
|
|
33
|
+
};
|
|
34
|
+
}
|
package/result.js.flow
ADDED
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
// @flow strict
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Result <value> <error>
|
|
5
|
+
* = Ok <value>
|
|
6
|
+
* | Err <error>
|
|
7
|
+
*/
|
|
8
|
+
|
|
9
|
+
type Ok<+T> = {| +ok: true, +value: T, +error: void |};
|
|
10
|
+
type Err<+E> = {| +ok: false, +value: void, +error: E |};
|
|
11
|
+
|
|
12
|
+
export type Result<+T, +E> = Ok<T> | Err<E>;
|
|
13
|
+
|
|
14
|
+
/**
|
|
15
|
+
* Create a new Result instance representing a successful computation.
|
|
16
|
+
*/
|
|
17
|
+
export function ok<T>(value: T): Ok<T> {
|
|
18
|
+
return { ok: true, value, error: undefined };
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
/**
|
|
22
|
+
* Create a new Result instance representing a failed computation.
|
|
23
|
+
*/
|
|
24
|
+
export function err<E>(error: E): Err<E> {
|
|
25
|
+
return { ok: false, value: undefined, error };
|
|
26
|
+
}
|
package/result.mjs
ADDED
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Result <value> <error>
|
|
3
|
+
* = Ok <value>
|
|
4
|
+
* | Err <error>
|
|
5
|
+
*/
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* Create a new Result instance representing a successful computation.
|
|
9
|
+
*/
|
|
10
|
+
export function ok(value) {
|
|
11
|
+
return {
|
|
12
|
+
ok: true,
|
|
13
|
+
value: value,
|
|
14
|
+
error: undefined
|
|
15
|
+
};
|
|
16
|
+
}
|
|
17
|
+
/**
|
|
18
|
+
* Create a new Result instance representing a failed computation.
|
|
19
|
+
*/
|
|
20
|
+
|
|
21
|
+
export function err(error) {
|
|
22
|
+
return {
|
|
23
|
+
ok: false,
|
|
24
|
+
value: undefined,
|
|
25
|
+
error: error
|
|
26
|
+
};
|
|
27
|
+
}
|
package/cjs/_guard.js
DELETED
|
@@ -1,26 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
|
|
3
|
-
exports.__esModule = true;
|
|
4
|
-
exports.guard = guard;
|
|
5
|
-
|
|
6
|
-
var Result = _interopRequireWildcard(require("./result"));
|
|
7
|
-
|
|
8
|
-
var _format = require("./format");
|
|
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 guard(decoder, formatter) {
|
|
15
|
-
if (formatter === void 0) {
|
|
16
|
-
formatter = _format.formatInline;
|
|
17
|
-
}
|
|
18
|
-
|
|
19
|
-
return function (blob) {
|
|
20
|
-
return Result.unwrap(Result.mapError(decoder(blob), function (annotation) {
|
|
21
|
-
var err = new Error('\n' + formatter(annotation));
|
|
22
|
-
err.name = 'Decoding error';
|
|
23
|
-
return err;
|
|
24
|
-
}));
|
|
25
|
-
};
|
|
26
|
-
}
|
package/cjs/_guard.js.flow
DELETED
|
@@ -1,20 +0,0 @@
|
|
|
1
|
-
// @flow strict
|
|
2
|
-
|
|
3
|
-
import * as Result from './result';
|
|
4
|
-
import { formatInline } from './format';
|
|
5
|
-
import type { Annotation } from './annotate';
|
|
6
|
-
import type { Decoder, Guard } from './_types';
|
|
7
|
-
|
|
8
|
-
export function guard<T>(
|
|
9
|
-
decoder: Decoder<T>,
|
|
10
|
-
formatter: (Annotation) => string = formatInline,
|
|
11
|
-
): Guard<T> {
|
|
12
|
-
return (blob: mixed) =>
|
|
13
|
-
Result.unwrap(
|
|
14
|
-
Result.mapError(decoder(blob), (annotation) => {
|
|
15
|
-
const err = new Error('\n' + formatter(annotation));
|
|
16
|
-
err.name = 'Decoding error';
|
|
17
|
-
return err;
|
|
18
|
-
}),
|
|
19
|
-
);
|
|
20
|
-
}
|
package/cjs/_types.js
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
"use strict";
|
package/cjs/_types.js.flow
DELETED
|
@@ -1,20 +0,0 @@
|
|
|
1
|
-
// @flow strict
|
|
2
|
-
|
|
3
|
-
import type { Annotation } from './annotate';
|
|
4
|
-
import type { Result } from './result';
|
|
5
|
-
|
|
6
|
-
export type Scalar = string | number | boolean | symbol | void | null;
|
|
7
|
-
|
|
8
|
-
export type Predicate<T> = (T) => boolean;
|
|
9
|
-
export type DecodeResult<T> = Result<T, Annotation>;
|
|
10
|
-
|
|
11
|
-
export type Decoder<T, F = mixed> = (F) => DecodeResult<T>;
|
|
12
|
-
export type Guard<T> = (mixed) => T;
|
|
13
|
-
|
|
14
|
-
/**
|
|
15
|
-
* A "type function" which informs Flow about how a type will be modified at runtime.
|
|
16
|
-
* Read this as "given a Guard of type T, I can produce a value of type T". This
|
|
17
|
-
* definition helps construct $ObjMap types.
|
|
18
|
-
*/
|
|
19
|
-
export type DecoderType = <T>(Decoder<T>) => T;
|
|
20
|
-
export type GuardType = <T>(Guard<T>) => T;
|
package/cjs/format/index.js
DELETED
|
@@ -1,12 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
|
|
3
|
-
exports.__esModule = true;
|
|
4
|
-
exports.formatShort = exports.formatInline = void 0;
|
|
5
|
-
|
|
6
|
-
var _inline = require("./inline");
|
|
7
|
-
|
|
8
|
-
exports.formatInline = _inline.formatInline;
|
|
9
|
-
|
|
10
|
-
var _short = require("./short");
|
|
11
|
-
|
|
12
|
-
exports.formatShort = _short.formatShort;
|
package/cjs/format/index.js.flow
DELETED
package/cjs/format/short.js
DELETED
package/cjs/format/short.js.flow
DELETED