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
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
import { annotate } from '../annotate.mjs'
|
|
2
|
+
import { define } from '../Decoder.mjs'
|
|
3
|
+
|
|
4
|
+
export function instanceOf(klass) {
|
|
5
|
+
return define(function (blob, ok, err) {
|
|
6
|
+
return blob instanceof klass ? ok(blob) : err('Must be ' + klass.name + ' instance')
|
|
7
|
+
})
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
export function lazy(decoderFn) {
|
|
11
|
+
return define(function (blob) {
|
|
12
|
+
return decoderFn().decode(blob)
|
|
13
|
+
})
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
export function prep(mapperFn, decoder) {
|
|
17
|
+
return define(function (originalInput, _, err) {
|
|
18
|
+
var blob
|
|
19
|
+
|
|
20
|
+
try {
|
|
21
|
+
blob = mapperFn(originalInput)
|
|
22
|
+
} catch (e) {
|
|
23
|
+
return err(annotate(originalInput, e.message))
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
var r = decoder.decode(blob)
|
|
27
|
+
return r.ok ? r : err(annotate(originalInput, r.error.text))
|
|
28
|
+
})
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
export function never(msg) {
|
|
32
|
+
return define(function (_, __, err) {
|
|
33
|
+
return err(msg)
|
|
34
|
+
})
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
export var fail = never
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "decoders",
|
|
3
|
-
"version": "2.0.
|
|
3
|
+
"version": "2.0.1",
|
|
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": {
|
|
@@ -15,30 +15,77 @@
|
|
|
15
15
|
"main": "./index.js",
|
|
16
16
|
"module": "./index.mjs",
|
|
17
17
|
"keywords": [
|
|
18
|
-
"decoder",
|
|
19
18
|
"decoders",
|
|
20
|
-
"
|
|
21
|
-
"
|
|
19
|
+
"Decoder",
|
|
20
|
+
"always",
|
|
21
|
+
"and",
|
|
22
22
|
"array",
|
|
23
|
-
"string",
|
|
24
|
-
"email",
|
|
25
|
-
"regex",
|
|
26
|
-
"number",
|
|
27
|
-
"integer",
|
|
28
23
|
"boolean",
|
|
29
|
-
"
|
|
24
|
+
"then",
|
|
25
|
+
"compose",
|
|
26
|
+
"constant",
|
|
30
27
|
"date",
|
|
31
|
-
"
|
|
28
|
+
"decode",
|
|
29
|
+
"decoder",
|
|
30
|
+
"decoders",
|
|
31
|
+
"define",
|
|
32
|
+
"describe",
|
|
32
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",
|
|
33
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",
|
|
34
76
|
"tuple2",
|
|
77
|
+
"undefined_",
|
|
35
78
|
"unknown",
|
|
36
|
-
"
|
|
37
|
-
"
|
|
38
|
-
"
|
|
39
|
-
"
|
|
40
|
-
"
|
|
41
|
-
"
|
|
79
|
+
"url",
|
|
80
|
+
"uuid",
|
|
81
|
+
"uuidv1",
|
|
82
|
+
"uuidv4",
|
|
83
|
+
"verify",
|
|
84
|
+
"DecodeResult",
|
|
85
|
+
"DecoderType",
|
|
86
|
+
"JSONArray",
|
|
87
|
+
"JSONObject",
|
|
88
|
+
"JSONValue"
|
|
42
89
|
],
|
|
43
90
|
"githubUrl": "https://github.com/nvie/decoders",
|
|
44
91
|
"sideEffects": false,
|
package/result.d.ts
CHANGED
|
@@ -1,11 +1,11 @@
|
|
|
1
1
|
export interface Ok<T> {
|
|
2
|
-
|
|
2
|
+
ok: true;
|
|
3
3
|
value: T;
|
|
4
4
|
error: undefined;
|
|
5
5
|
}
|
|
6
6
|
|
|
7
7
|
export interface Err<E> {
|
|
8
|
-
|
|
8
|
+
ok: false;
|
|
9
9
|
value: undefined;
|
|
10
10
|
error: E;
|
|
11
11
|
}
|
|
@@ -14,26 +14,3 @@ export type Result<T, E> = Ok<T> | Err<E>;
|
|
|
14
14
|
|
|
15
15
|
export function ok<T>(value: T): Ok<T>;
|
|
16
16
|
export function err<E>(error: E): Err<E>;
|
|
17
|
-
export function unwrap<T>(result: Result<T, unknown>): T;
|
|
18
|
-
export function expect<T>(result: Result<T, unknown>, message: string | Error): T;
|
|
19
|
-
export function dispatch<T, E, O>(
|
|
20
|
-
result: Result<T, E>,
|
|
21
|
-
okCallback: (value: T) => O,
|
|
22
|
-
errCallback: (error: E) => O,
|
|
23
|
-
): O;
|
|
24
|
-
export function andThen<T, E, T2>(
|
|
25
|
-
result1: Result<T, E>,
|
|
26
|
-
lazyResult2: (value: T) => Result<T2, E>,
|
|
27
|
-
): Result<T2, E>;
|
|
28
|
-
export function orElse<T, E, E2>(
|
|
29
|
-
result1: Result<T, E>,
|
|
30
|
-
lazyResult2: (errValue: E) => Result<T, E2>,
|
|
31
|
-
): Result<T, E2>;
|
|
32
|
-
export function mapOk<T, E, T2>(
|
|
33
|
-
result: Result<T, E>,
|
|
34
|
-
mapper: (value: T) => T2,
|
|
35
|
-
): Result<T2, E>;
|
|
36
|
-
export function mapError<T, E, E2>(
|
|
37
|
-
result: Result<T, E>,
|
|
38
|
-
mapper: (error: E) => E2,
|
|
39
|
-
): Result<T, E2>;
|
package/result.js
CHANGED
|
@@ -1,102 +1,21 @@
|
|
|
1
|
-
|
|
1
|
+
'use strict'
|
|
2
2
|
|
|
3
|
-
exports.__esModule = true
|
|
4
|
-
exports.
|
|
5
|
-
exports.
|
|
6
|
-
exports.err = err;
|
|
7
|
-
exports.expect = expect;
|
|
8
|
-
exports.mapError = mapError;
|
|
9
|
-
exports.mapOk = mapOk;
|
|
10
|
-
exports.ok = ok;
|
|
11
|
-
exports.orElse = orElse;
|
|
12
|
-
exports.unwrap = unwrap;
|
|
3
|
+
exports.__esModule = true
|
|
4
|
+
exports.err = err
|
|
5
|
+
exports.ok = ok
|
|
13
6
|
|
|
14
|
-
/**
|
|
15
|
-
* Result <value> <error>
|
|
16
|
-
* = Ok <value>
|
|
17
|
-
* | Err <error>
|
|
18
|
-
*/
|
|
19
|
-
|
|
20
|
-
/**
|
|
21
|
-
* Create a new Result instance representing a successful computation.
|
|
22
|
-
*/
|
|
23
7
|
function ok(value) {
|
|
24
8
|
return {
|
|
25
|
-
|
|
9
|
+
ok: true,
|
|
26
10
|
value: value,
|
|
27
|
-
error: undefined
|
|
28
|
-
}
|
|
11
|
+
error: undefined,
|
|
12
|
+
}
|
|
29
13
|
}
|
|
30
|
-
/**
|
|
31
|
-
* Create a new Result instance representing a failed computation.
|
|
32
|
-
*/
|
|
33
|
-
|
|
34
14
|
|
|
35
15
|
function err(error) {
|
|
36
16
|
return {
|
|
37
|
-
|
|
17
|
+
ok: false,
|
|
38
18
|
value: undefined,
|
|
39
|
-
error: error
|
|
40
|
-
};
|
|
41
|
-
}
|
|
42
|
-
/**
|
|
43
|
-
* Unwrap the value from this Result instance if this is an "Ok" result.
|
|
44
|
-
* Otherwise, will throw the "Err" error via a runtime exception.
|
|
45
|
-
*/
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
function unwrap(result) {
|
|
49
|
-
if (result.type === 'ok') {
|
|
50
|
-
return result.value;
|
|
51
|
-
} else {
|
|
52
|
-
throw result.error;
|
|
19
|
+
error: error,
|
|
53
20
|
}
|
|
54
21
|
}
|
|
55
|
-
|
|
56
|
-
function expect(result, message) {
|
|
57
|
-
if (result.type === 'ok') {
|
|
58
|
-
return result.value;
|
|
59
|
-
} else {
|
|
60
|
-
throw message instanceof Error ? message : new Error(message);
|
|
61
|
-
}
|
|
62
|
-
}
|
|
63
|
-
|
|
64
|
-
function dispatch(result, okCallback, errCallback) {
|
|
65
|
-
return result.type === 'ok' ? okCallback(result.value) : errCallback(result.error);
|
|
66
|
-
}
|
|
67
|
-
/**
|
|
68
|
-
* Like .and(), aka &&, but the second argument gets evaluated lazily only if
|
|
69
|
-
* the first result is an Ok result. If so, it has access to the Ok value from
|
|
70
|
-
* the first argument.
|
|
71
|
-
*/
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
function andThen(result1, lazyResult2) {
|
|
75
|
-
return result1.type === 'ok' ? lazyResult2(result1.value) : result1;
|
|
76
|
-
}
|
|
77
|
-
/**
|
|
78
|
-
* Like .or(), aka ||, but the second argument gets evaluated lazily only if
|
|
79
|
-
* the first result is an Err result. If so, it has access to the Err value
|
|
80
|
-
* from the first argument.
|
|
81
|
-
*/
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
function orElse(result1, lazyResult2) {
|
|
85
|
-
return result1.type === 'ok' ? result1 : lazyResult2(result1.error);
|
|
86
|
-
}
|
|
87
|
-
/**
|
|
88
|
-
* Transform an Ok result. Will not touch Err results.
|
|
89
|
-
*/
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
function mapOk(result, mapper) {
|
|
93
|
-
return result.type === 'ok' ? ok(mapper(result.value)) : result;
|
|
94
|
-
}
|
|
95
|
-
/**
|
|
96
|
-
* Transform an Err value. Will not touch Ok results.
|
|
97
|
-
*/
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
function mapError(result, mapper) {
|
|
101
|
-
return result.type === 'ok' ? result : err(mapper(result.error));
|
|
102
|
-
}
|
package/result.js.flow
CHANGED
|
@@ -6,8 +6,8 @@
|
|
|
6
6
|
* | Err <error>
|
|
7
7
|
*/
|
|
8
8
|
|
|
9
|
-
type Ok<+T> = {| +
|
|
10
|
-
type Err<+E> = {| +
|
|
9
|
+
type Ok<+T> = {| +ok: true, +value: T, +error: void |};
|
|
10
|
+
type Err<+E> = {| +ok: false, +value: void, +error: E |};
|
|
11
11
|
|
|
12
12
|
export type Result<+T, +E> = Ok<T> | Err<E>;
|
|
13
13
|
|
|
@@ -15,84 +15,12 @@ export type Result<+T, +E> = Ok<T> | Err<E>;
|
|
|
15
15
|
* Create a new Result instance representing a successful computation.
|
|
16
16
|
*/
|
|
17
17
|
export function ok<T>(value: T): Ok<T> {
|
|
18
|
-
return {
|
|
18
|
+
return { ok: true, value, error: undefined };
|
|
19
19
|
}
|
|
20
20
|
|
|
21
21
|
/**
|
|
22
22
|
* Create a new Result instance representing a failed computation.
|
|
23
23
|
*/
|
|
24
24
|
export function err<E>(error: E): Err<E> {
|
|
25
|
-
return {
|
|
26
|
-
}
|
|
27
|
-
|
|
28
|
-
/**
|
|
29
|
-
* Unwrap the value from this Result instance if this is an "Ok" result.
|
|
30
|
-
* Otherwise, will throw the "Err" error via a runtime exception.
|
|
31
|
-
*/
|
|
32
|
-
export function unwrap<T>(result: Result<T, mixed>): T {
|
|
33
|
-
if (result.type === 'ok') {
|
|
34
|
-
return result.value;
|
|
35
|
-
} else {
|
|
36
|
-
throw result.error;
|
|
37
|
-
}
|
|
38
|
-
}
|
|
39
|
-
|
|
40
|
-
export function expect<T>(result: Result<T, mixed>, message: string | Error): T {
|
|
41
|
-
if (result.type === 'ok') {
|
|
42
|
-
return result.value;
|
|
43
|
-
} else {
|
|
44
|
-
throw message instanceof Error ? message : new Error(message);
|
|
45
|
-
}
|
|
46
|
-
}
|
|
47
|
-
|
|
48
|
-
export function dispatch<T, E, O>(
|
|
49
|
-
result: Result<T, E>,
|
|
50
|
-
okCallback: (value: T) => O,
|
|
51
|
-
errCallback: (error: E) => O,
|
|
52
|
-
): O {
|
|
53
|
-
return result.type === 'ok' ? okCallback(result.value) : errCallback(result.error);
|
|
54
|
-
}
|
|
55
|
-
|
|
56
|
-
/**
|
|
57
|
-
* Like .and(), aka &&, but the second argument gets evaluated lazily only if
|
|
58
|
-
* the first result is an Ok result. If so, it has access to the Ok value from
|
|
59
|
-
* the first argument.
|
|
60
|
-
*/
|
|
61
|
-
export function andThen<T, E, T2>(
|
|
62
|
-
result1: Result<T, E>,
|
|
63
|
-
lazyResult2: (value: T) => Result<T2, E>,
|
|
64
|
-
): Result<T2, E> {
|
|
65
|
-
return result1.type === 'ok' ? lazyResult2(result1.value) : result1;
|
|
66
|
-
}
|
|
67
|
-
|
|
68
|
-
/**
|
|
69
|
-
* Like .or(), aka ||, but the second argument gets evaluated lazily only if
|
|
70
|
-
* the first result is an Err result. If so, it has access to the Err value
|
|
71
|
-
* from the first argument.
|
|
72
|
-
*/
|
|
73
|
-
export function orElse<T, E, E2>(
|
|
74
|
-
result1: Result<T, E>,
|
|
75
|
-
lazyResult2: (errValue: E) => Result<T, E2>,
|
|
76
|
-
): Result<T, E2> {
|
|
77
|
-
return result1.type === 'ok' ? result1 : lazyResult2(result1.error);
|
|
78
|
-
}
|
|
79
|
-
|
|
80
|
-
/**
|
|
81
|
-
* Transform an Ok result. Will not touch Err results.
|
|
82
|
-
*/
|
|
83
|
-
export function mapOk<T, E, T2>(
|
|
84
|
-
result: Result<T, E>,
|
|
85
|
-
mapper: (value: T) => T2,
|
|
86
|
-
): Result<T2, E> {
|
|
87
|
-
return result.type === 'ok' ? ok(mapper(result.value)) : result;
|
|
88
|
-
}
|
|
89
|
-
|
|
90
|
-
/**
|
|
91
|
-
* Transform an Err value. Will not touch Ok results.
|
|
92
|
-
*/
|
|
93
|
-
export function mapError<T, E, E2>(
|
|
94
|
-
result: Result<T, E>,
|
|
95
|
-
mapper: (error: E) => E2,
|
|
96
|
-
): Result<T, E2> {
|
|
97
|
-
return result.type === 'ok' ? result : err(mapper(result.error));
|
|
25
|
+
return { ok: false, value: undefined, error };
|
|
98
26
|
}
|
package/result.mjs
CHANGED
|
@@ -1,81 +1,15 @@
|
|
|
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
1
|
export function ok(value) {
|
|
11
2
|
return {
|
|
12
|
-
|
|
3
|
+
ok: true,
|
|
13
4
|
value: value,
|
|
14
|
-
error: undefined
|
|
15
|
-
}
|
|
5
|
+
error: undefined,
|
|
6
|
+
}
|
|
16
7
|
}
|
|
17
|
-
/**
|
|
18
|
-
* Create a new Result instance representing a failed computation.
|
|
19
|
-
*/
|
|
20
8
|
|
|
21
9
|
export function err(error) {
|
|
22
10
|
return {
|
|
23
|
-
|
|
11
|
+
ok: false,
|
|
24
12
|
value: undefined,
|
|
25
|
-
error: error
|
|
26
|
-
};
|
|
27
|
-
}
|
|
28
|
-
/**
|
|
29
|
-
* Unwrap the value from this Result instance if this is an "Ok" result.
|
|
30
|
-
* Otherwise, will throw the "Err" error via a runtime exception.
|
|
31
|
-
*/
|
|
32
|
-
|
|
33
|
-
export function unwrap(result) {
|
|
34
|
-
if (result.type === 'ok') {
|
|
35
|
-
return result.value;
|
|
36
|
-
} else {
|
|
37
|
-
throw result.error;
|
|
13
|
+
error: error,
|
|
38
14
|
}
|
|
39
15
|
}
|
|
40
|
-
export function expect(result, message) {
|
|
41
|
-
if (result.type === 'ok') {
|
|
42
|
-
return result.value;
|
|
43
|
-
} else {
|
|
44
|
-
throw message instanceof Error ? message : new Error(message);
|
|
45
|
-
}
|
|
46
|
-
}
|
|
47
|
-
export function dispatch(result, okCallback, errCallback) {
|
|
48
|
-
return result.type === 'ok' ? okCallback(result.value) : errCallback(result.error);
|
|
49
|
-
}
|
|
50
|
-
/**
|
|
51
|
-
* Like .and(), aka &&, but the second argument gets evaluated lazily only if
|
|
52
|
-
* the first result is an Ok result. If so, it has access to the Ok value from
|
|
53
|
-
* the first argument.
|
|
54
|
-
*/
|
|
55
|
-
|
|
56
|
-
export function andThen(result1, lazyResult2) {
|
|
57
|
-
return result1.type === 'ok' ? lazyResult2(result1.value) : result1;
|
|
58
|
-
}
|
|
59
|
-
/**
|
|
60
|
-
* Like .or(), aka ||, but the second argument gets evaluated lazily only if
|
|
61
|
-
* the first result is an Err result. If so, it has access to the Err value
|
|
62
|
-
* from the first argument.
|
|
63
|
-
*/
|
|
64
|
-
|
|
65
|
-
export function orElse(result1, lazyResult2) {
|
|
66
|
-
return result1.type === 'ok' ? result1 : lazyResult2(result1.error);
|
|
67
|
-
}
|
|
68
|
-
/**
|
|
69
|
-
* Transform an Ok result. Will not touch Err results.
|
|
70
|
-
*/
|
|
71
|
-
|
|
72
|
-
export function mapOk(result, mapper) {
|
|
73
|
-
return result.type === 'ok' ? ok(mapper(result.value)) : result;
|
|
74
|
-
}
|
|
75
|
-
/**
|
|
76
|
-
* Transform an Err value. Will not touch Ok results.
|
|
77
|
-
*/
|
|
78
|
-
|
|
79
|
-
export function mapError(result, mapper) {
|
|
80
|
-
return result.type === 'ok' ? result : err(mapper(result.error));
|
|
81
|
-
}
|
package/_guard.d.ts
DELETED
package/_guard.js
DELETED
|
@@ -1,22 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
|
|
3
|
-
exports.__esModule = true;
|
|
4
|
-
exports.guard = guard;
|
|
5
|
-
|
|
6
|
-
var _format = require("./format");
|
|
7
|
-
|
|
8
|
-
var _result = require("./result");
|
|
9
|
-
|
|
10
|
-
function guard(decoder, formatter) {
|
|
11
|
-
if (formatter === void 0) {
|
|
12
|
-
formatter = _format.formatInline;
|
|
13
|
-
}
|
|
14
|
-
|
|
15
|
-
return function (blob) {
|
|
16
|
-
return (0, _result.unwrap)((0, _result.mapError)(decoder(blob), function (annotation) {
|
|
17
|
-
var err = new Error('\n' + formatter(annotation));
|
|
18
|
-
err.name = 'Decoding error';
|
|
19
|
-
return err;
|
|
20
|
-
}));
|
|
21
|
-
};
|
|
22
|
-
}
|
package/_guard.js.flow
DELETED
|
@@ -1,20 +0,0 @@
|
|
|
1
|
-
// @flow strict
|
|
2
|
-
|
|
3
|
-
import { formatInline } from './format';
|
|
4
|
-
import { mapError, unwrap } from './result';
|
|
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
|
-
unwrap(
|
|
14
|
-
mapError(decoder(blob), (annotation) => {
|
|
15
|
-
const err = new Error('\n' + formatter(annotation));
|
|
16
|
-
err.name = 'Decoding error';
|
|
17
|
-
return err;
|
|
18
|
-
}),
|
|
19
|
-
);
|
|
20
|
-
}
|
package/_guard.mjs
DELETED
|
@@ -1,15 +0,0 @@
|
|
|
1
|
-
import { formatInline } from './format.mjs';
|
|
2
|
-
import { mapError, unwrap } from './result.mjs';
|
|
3
|
-
export function guard(decoder, formatter) {
|
|
4
|
-
if (formatter === void 0) {
|
|
5
|
-
formatter = formatInline;
|
|
6
|
-
}
|
|
7
|
-
|
|
8
|
-
return function (blob) {
|
|
9
|
-
return unwrap(mapError(decoder(blob), function (annotation) {
|
|
10
|
-
var err = new Error('\n' + formatter(annotation));
|
|
11
|
-
err.name = 'Decoding error';
|
|
12
|
-
return err;
|
|
13
|
-
}));
|
|
14
|
-
};
|
|
15
|
-
}
|
package/_types.d.ts
DELETED
|
@@ -1,13 +0,0 @@
|
|
|
1
|
-
import { Annotation } from './annotate';
|
|
2
|
-
import { Result } from './result';
|
|
3
|
-
|
|
4
|
-
export type Scalar = string | number | boolean | symbol | undefined | null;
|
|
5
|
-
|
|
6
|
-
export type Predicate<T> = (value: T) => boolean;
|
|
7
|
-
export type DecodeResult<T> = Result<T, Annotation>;
|
|
8
|
-
|
|
9
|
-
export type Decoder<T, F = unknown> = (blob: F) => DecodeResult<T>;
|
|
10
|
-
export type Guard<T> = (blob: unknown) => T;
|
|
11
|
-
|
|
12
|
-
export type DecoderType<T> = T extends Decoder<infer V> ? V : never;
|
|
13
|
-
export type GuardType<T> = T extends Guard<infer V> ? V : never;
|
package/_types.js
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
"use strict";
|
package/_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/_types.mjs
DELETED
|
File without changes
|