decoders 2.0.0-beta5 → 2.0.0-beta9
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 +19 -0
- package/NotSupportedTSVersion.d.ts +1 -0
- package/README.md +934 -387
- package/_guard.d.ts +7 -0
- package/_guard.js +2 -6
- package/_guard.js.flow +3 -3
- package/{_esm/_guard.js → _guard.mjs} +3 -3
- package/_types.d.ts +13 -0
- package/{_esm/_types.js → _types.mjs} +0 -0
- package/_utils.d.ts +10 -0
- package/_utils.js +1 -1
- package/_utils.js.flow +3 -3
- package/{_esm/_utils.js → _utils.mjs} +1 -1
- package/annotate.d.ts +62 -0
- package/{_esm/annotate.js → annotate.mjs} +0 -0
- package/core/_helpers.d.ts +79 -0
- package/core/array.d.ts +8 -0
- package/core/array.js +19 -12
- package/core/array.js.flow +15 -11
- package/{_esm/core/array.js → core/array.mjs} +19 -10
- package/core/boolean.d.ts +5 -0
- package/core/boolean.js +5 -9
- package/core/boolean.js.flow +5 -7
- package/{_esm/core/boolean.js → core/boolean.mjs} +7 -7
- package/core/composition.d.ts +18 -0
- package/core/composition.js +41 -15
- package/core/composition.js.flow +41 -10
- package/core/composition.mjs +70 -0
- package/core/constants.d.ts +11 -0
- package/core/constants.js +6 -10
- package/core/constants.js.flow +7 -9
- package/{_esm/core/constants.js → core/constants.mjs} +7 -7
- package/core/date.d.ts +4 -0
- package/core/date.js +5 -9
- package/core/date.js.flow +4 -6
- package/{_esm/core/date.js → core/date.mjs} +7 -7
- package/core/describe.d.ts +3 -0
- package/core/describe.js +2 -6
- package/core/describe.js.flow +2 -2
- package/{_esm/core/describe.js → core/describe.mjs} +3 -3
- package/core/dispatch.d.ts +8 -0
- package/core/dispatch.js +11 -13
- package/core/dispatch.js.flow +13 -12
- package/{_esm/core/dispatch.js → core/dispatch.mjs} +12 -11
- package/core/either.d.ts +66 -0
- package/core/either.js +34 -50
- package/core/either.js.flow +40 -86
- package/core/either.mjs +90 -0
- package/core/fail.d.ts +3 -0
- package/core/fail.js +2 -6
- package/core/fail.js.flow +2 -2
- package/{_esm/core/fail.js → core/fail.mjs} +3 -3
- package/core/instanceOf.d.ts +3 -0
- package/core/instanceOf.js +2 -6
- package/core/instanceOf.js.flow +3 -3
- package/core/instanceOf.mjs +8 -0
- package/core/json.d.ts +11 -0
- package/core/json.js +3 -3
- package/core/json.js.flow +3 -3
- package/core/json.mjs +15 -0
- package/core/lazy.d.ts +3 -0
- package/{_esm/core/lazy.js → core/lazy.mjs} +0 -0
- package/core/number.d.ts +6 -0
- package/core/number.js +9 -13
- package/core/number.js.flow +18 -12
- package/core/number.mjs +25 -0
- package/core/object.d.ts +38 -0
- package/core/object.js +66 -13
- package/core/object.js.flow +84 -28
- package/{_esm/core/object.js → core/object.mjs} +64 -11
- package/core/optional.d.ts +5 -0
- package/core/optional.js +4 -8
- package/core/optional.js.flow +3 -3
- package/{_esm/core/optional.js → core/optional.mjs} +6 -6
- package/core/string.d.ts +13 -0
- package/core/string.js +31 -49
- package/core/string.js.flow +29 -39
- package/core/string.mjs +58 -0
- package/core/tuple.d.ts +30 -0
- package/core/tuple.js +30 -149
- package/core/tuple.js.flow +33 -197
- package/core/tuple.mjs +45 -0
- package/format.d.ts +4 -0
- package/{format/inline.js → format.js} +6 -1
- package/{_esm/format/inline.js.flow → format.js.flow} +6 -2
- package/{_esm/format/inline.js → format.mjs} +4 -1
- package/index.d.ts +42 -0
- package/index.js +33 -42
- package/index.js.flow +17 -18
- package/{_esm/index.js → index.mjs} +18 -19
- package/package.json +15 -3
- package/result.d.ts +39 -0
- package/result.js +9 -90
- package/result.js.flow +11 -87
- package/result.mjs +81 -0
- package/_esm/_guard.js.flow +0 -20
- package/_esm/_types.js.flow +0 -20
- package/_esm/_utils.js.flow +0 -97
- package/_esm/annotate.js.flow +0 -218
- package/_esm/core/array.js.flow +0 -103
- package/_esm/core/boolean.js.flow +0 -29
- package/_esm/core/composition.js +0 -42
- package/_esm/core/composition.js.flow +0 -43
- package/_esm/core/constants.js.flow +0 -46
- package/_esm/core/date.js.flow +0 -40
- package/_esm/core/describe.js.flow +0 -17
- package/_esm/core/dispatch.js.flow +0 -58
- package/_esm/core/either.js +0 -90
- package/_esm/core/either.js.flow +0 -151
- package/_esm/core/fail.js.flow +0 -12
- package/_esm/core/instanceOf.js +0 -8
- package/_esm/core/instanceOf.js.flow +0 -20
- package/_esm/core/json.js +0 -15
- package/_esm/core/json.js.flow +0 -28
- package/_esm/core/lazy.js.flow +0 -15
- package/_esm/core/mapping.js +0 -54
- package/_esm/core/mapping.js.flow +0 -54
- package/_esm/core/number.js +0 -25
- package/_esm/core/number.js.flow +0 -34
- package/_esm/core/object.js.flow +0 -203
- package/_esm/core/optional.js.flow +0 -41
- package/_esm/core/string.js +0 -76
- package/_esm/core/string.js.flow +0 -82
- package/_esm/core/tuple.js +0 -155
- package/_esm/core/tuple.js.flow +0 -215
- package/_esm/format/index.js +0 -2
- package/_esm/format/index.js.flow +0 -4
- package/_esm/format/short.js +0 -4
- package/_esm/format/short.js.flow +0 -8
- package/_esm/index.js.flow +0 -63
- package/_esm/result.js +0 -148
- package/_esm/result.js.flow +0 -174
- package/core/mapping.js +0 -67
- package/core/mapping.js.flow +0 -54
- package/format/index.js +0 -12
- package/format/index.js.flow +0 -4
- package/format/inline.js.flow +0 -122
- package/format/short.js +0 -10
- package/format/short.js.flow +0 -8
package/index.js
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
|
|
3
3
|
exports.__esModule = true;
|
|
4
|
-
exports.
|
|
4
|
+
exports.uuidv4 = exports.uuidv1 = exports.uuid = exports.url = exports.unknown = exports.undefined_ = exports.tuple = exports.truthy = exports.transform = exports.taggedUnion = exports.string = exports.set = exports.regex = exports.prep = exports.predicate = exports.positiveNumber = exports.positiveInteger = exports.pojo = exports.poja = exports.optional = exports.oneOf = exports.object = exports.numericBoolean = exports.number = exports.nullable = exports.null_ = exports.nonEmptyString = exports.nonEmptyArray = exports.mixed = exports.maybe = exports.mapping = exports.lazy = exports.jsonObject = exports.jsonArray = exports.json = exports.iso8601 = exports.integer = exports.instanceOf = exports.inexact = exports.httpsUrl = exports.hardcoded = exports.guard = exports.fail = exports.exact = exports.email = exports.either = exports.dict = exports.describe = exports.date = exports.constant = exports.compose = exports["boolean"] = exports.array = void 0;
|
|
5
5
|
|
|
6
6
|
var _guard = require("./_guard");
|
|
7
7
|
|
|
@@ -10,14 +10,16 @@ exports.guard = _guard.guard;
|
|
|
10
10
|
var _composition = require("./core/composition");
|
|
11
11
|
|
|
12
12
|
exports.compose = _composition.compose;
|
|
13
|
-
exports.map = _composition.map;
|
|
14
13
|
exports.predicate = _composition.predicate;
|
|
14
|
+
exports.prep = _composition.prep;
|
|
15
|
+
exports.transform = _composition.transform;
|
|
15
16
|
|
|
16
17
|
var _array = require("./core/array");
|
|
17
18
|
|
|
18
19
|
exports.array = _array.array;
|
|
19
20
|
exports.nonEmptyArray = _array.nonEmptyArray;
|
|
20
21
|
exports.poja = _array.poja;
|
|
22
|
+
exports.set = _array.set;
|
|
21
23
|
|
|
22
24
|
var _boolean = require("./core/boolean");
|
|
23
25
|
|
|
@@ -43,22 +45,32 @@ var _describe = require("./core/describe");
|
|
|
43
45
|
|
|
44
46
|
exports.describe = _describe.describe;
|
|
45
47
|
|
|
46
|
-
var
|
|
48
|
+
var _object = require("./core/object");
|
|
47
49
|
|
|
48
|
-
exports.
|
|
50
|
+
exports.dict = _object.dict;
|
|
51
|
+
exports.exact = _object.exact;
|
|
52
|
+
exports.inexact = _object.inexact;
|
|
53
|
+
exports.mapping = _object.mapping;
|
|
54
|
+
exports.object = _object.object;
|
|
55
|
+
exports.pojo = _object.pojo;
|
|
49
56
|
|
|
50
57
|
var _either = require("./core/either");
|
|
51
58
|
|
|
52
59
|
exports.either = _either.either;
|
|
53
|
-
exports.either3 = _either.either3;
|
|
54
|
-
exports.either4 = _either.either4;
|
|
55
|
-
exports.either5 = _either.either5;
|
|
56
|
-
exports.either6 = _either.either6;
|
|
57
|
-
exports.either7 = _either.either7;
|
|
58
|
-
exports.either8 = _either.either8;
|
|
59
|
-
exports.either9 = _either.either9;
|
|
60
60
|
exports.oneOf = _either.oneOf;
|
|
61
61
|
|
|
62
|
+
var _string = require("./core/string");
|
|
63
|
+
|
|
64
|
+
exports.email = _string.email;
|
|
65
|
+
exports.httpsUrl = _string.httpsUrl;
|
|
66
|
+
exports.nonEmptyString = _string.nonEmptyString;
|
|
67
|
+
exports.regex = _string.regex;
|
|
68
|
+
exports.string = _string.string;
|
|
69
|
+
exports.url = _string.url;
|
|
70
|
+
exports.uuid = _string.uuid;
|
|
71
|
+
exports.uuidv1 = _string.uuidv1;
|
|
72
|
+
exports.uuidv4 = _string.uuidv4;
|
|
73
|
+
|
|
62
74
|
var _fail = require("./core/fail");
|
|
63
75
|
|
|
64
76
|
exports.fail = _fail.fail;
|
|
@@ -67,6 +79,13 @@ var _instanceOf = require("./core/instanceOf");
|
|
|
67
79
|
|
|
68
80
|
exports.instanceOf = _instanceOf.instanceOf;
|
|
69
81
|
|
|
82
|
+
var _number = require("./core/number");
|
|
83
|
+
|
|
84
|
+
exports.integer = _number.integer;
|
|
85
|
+
exports.number = _number.number;
|
|
86
|
+
exports.positiveInteger = _number.positiveInteger;
|
|
87
|
+
exports.positiveNumber = _number.positiveNumber;
|
|
88
|
+
|
|
70
89
|
var _json = require("./core/json");
|
|
71
90
|
|
|
72
91
|
exports.json = _json.json;
|
|
@@ -77,44 +96,16 @@ var _lazy = require("./core/lazy");
|
|
|
77
96
|
|
|
78
97
|
exports.lazy = _lazy.lazy;
|
|
79
98
|
|
|
80
|
-
var _mapping = require("./core/mapping");
|
|
81
|
-
|
|
82
|
-
exports.mapping = _mapping.mapping;
|
|
83
|
-
exports.dict = _mapping.dict;
|
|
84
|
-
|
|
85
|
-
var _number = require("./core/number");
|
|
86
|
-
|
|
87
|
-
exports.integer = _number.integer;
|
|
88
|
-
exports.number = _number.number;
|
|
89
|
-
exports.positiveInteger = _number.positiveInteger;
|
|
90
|
-
exports.positiveNumber = _number.positiveNumber;
|
|
91
|
-
|
|
92
|
-
var _object = require("./core/object");
|
|
93
|
-
|
|
94
|
-
exports.exact = _object.exact;
|
|
95
|
-
exports.inexact = _object.inexact;
|
|
96
|
-
exports.object = _object.object;
|
|
97
|
-
exports.pojo = _object.pojo;
|
|
98
|
-
|
|
99
99
|
var _optional = require("./core/optional");
|
|
100
100
|
|
|
101
101
|
exports.maybe = _optional.maybe;
|
|
102
102
|
exports.nullable = _optional.nullable;
|
|
103
103
|
exports.optional = _optional.optional;
|
|
104
104
|
|
|
105
|
-
var
|
|
105
|
+
var _dispatch = require("./core/dispatch");
|
|
106
106
|
|
|
107
|
-
exports.
|
|
108
|
-
exports.nonEmptyString = _string.nonEmptyString;
|
|
109
|
-
exports.regex = _string.regex;
|
|
110
|
-
exports.string = _string.string;
|
|
111
|
-
exports.url = _string.url;
|
|
107
|
+
exports.taggedUnion = _dispatch.taggedUnion;
|
|
112
108
|
|
|
113
109
|
var _tuple = require("./core/tuple");
|
|
114
110
|
|
|
115
|
-
exports.
|
|
116
|
-
exports.tuple2 = _tuple.tuple2;
|
|
117
|
-
exports.tuple3 = _tuple.tuple3;
|
|
118
|
-
exports.tuple4 = _tuple.tuple4;
|
|
119
|
-
exports.tuple5 = _tuple.tuple5;
|
|
120
|
-
exports.tuple6 = _tuple.tuple6;
|
|
111
|
+
exports.tuple = _tuple.tuple;
|
package/index.js.flow
CHANGED
|
@@ -32,32 +32,31 @@ export type { JSONValue, JSONObject, JSONArray } from './core/json';
|
|
|
32
32
|
|
|
33
33
|
export { guard } from './_guard';
|
|
34
34
|
|
|
35
|
-
export { compose,
|
|
35
|
+
export { compose, predicate, prep, transform } from './core/composition';
|
|
36
36
|
|
|
37
|
-
export { array, nonEmptyArray, poja } from './core/array';
|
|
37
|
+
export { array, nonEmptyArray, poja, set } from './core/array';
|
|
38
38
|
export { boolean, numericBoolean, truthy } from './core/boolean';
|
|
39
39
|
export { constant, hardcoded, mixed, null_, undefined_, unknown } from './core/constants';
|
|
40
40
|
export { date, iso8601 } from './core/date';
|
|
41
41
|
export { describe } from './core/describe';
|
|
42
|
-
export {
|
|
42
|
+
export { dict, exact, inexact, mapping, object, pojo } from './core/object';
|
|
43
|
+
export { either, oneOf } from './core/either';
|
|
43
44
|
export {
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
} from './core/
|
|
45
|
+
email,
|
|
46
|
+
httpsUrl,
|
|
47
|
+
nonEmptyString,
|
|
48
|
+
regex,
|
|
49
|
+
string,
|
|
50
|
+
url,
|
|
51
|
+
uuid,
|
|
52
|
+
uuidv1,
|
|
53
|
+
uuidv4,
|
|
54
|
+
} from './core/string';
|
|
54
55
|
export { fail } from './core/fail';
|
|
55
56
|
export { instanceOf } from './core/instanceOf';
|
|
57
|
+
export { integer, number, positiveInteger, positiveNumber } from './core/number';
|
|
56
58
|
export { json, jsonObject, jsonArray } from './core/json';
|
|
57
59
|
export { lazy } from './core/lazy';
|
|
58
|
-
export { mapping, dict } from './core/mapping';
|
|
59
|
-
export { integer, number, positiveInteger, positiveNumber } from './core/number';
|
|
60
|
-
export { exact, inexact, object, pojo } from './core/object';
|
|
61
60
|
export { maybe, nullable, optional } from './core/optional';
|
|
62
|
-
export {
|
|
63
|
-
export {
|
|
61
|
+
export { taggedUnion } from './core/dispatch';
|
|
62
|
+
export { tuple } from './core/tuple';
|
|
@@ -16,22 +16,21 @@
|
|
|
16
16
|
* type. In our case, it's fine to fail with a runtime error.
|
|
17
17
|
*
|
|
18
18
|
*/
|
|
19
|
-
export { guard } from './_guard';
|
|
20
|
-
export { compose,
|
|
21
|
-
export { array, nonEmptyArray, poja } from './core/array';
|
|
22
|
-
export { boolean, numericBoolean, truthy } from './core/boolean';
|
|
23
|
-
export { constant, hardcoded, mixed, null_, undefined_, unknown } from './core/constants';
|
|
24
|
-
export { date, iso8601 } from './core/date';
|
|
25
|
-
export { describe } from './core/describe';
|
|
26
|
-
export {
|
|
27
|
-
export { either,
|
|
28
|
-
export {
|
|
29
|
-
export {
|
|
30
|
-
export {
|
|
31
|
-
export {
|
|
32
|
-
export {
|
|
33
|
-
export {
|
|
34
|
-
export {
|
|
35
|
-
export {
|
|
36
|
-
export {
|
|
37
|
-
export { tuple1, tuple2, tuple3, tuple4, tuple5, tuple6 } from './core/tuple';
|
|
19
|
+
export { guard } from './_guard.mjs';
|
|
20
|
+
export { compose, predicate, prep, transform } from './core/composition.mjs';
|
|
21
|
+
export { array, nonEmptyArray, poja, set } from './core/array.mjs';
|
|
22
|
+
export { boolean, numericBoolean, truthy } from './core/boolean.mjs';
|
|
23
|
+
export { constant, hardcoded, mixed, null_, undefined_, unknown } from './core/constants.mjs';
|
|
24
|
+
export { date, iso8601 } from './core/date.mjs';
|
|
25
|
+
export { describe } from './core/describe.mjs';
|
|
26
|
+
export { dict, exact, inexact, mapping, object, pojo } from './core/object.mjs';
|
|
27
|
+
export { either, oneOf } from './core/either.mjs';
|
|
28
|
+
export { email, httpsUrl, nonEmptyString, regex, string, url, uuid, uuidv1, uuidv4 } from './core/string.mjs';
|
|
29
|
+
export { fail } from './core/fail.mjs';
|
|
30
|
+
export { instanceOf } from './core/instanceOf.mjs';
|
|
31
|
+
export { integer, number, positiveInteger, positiveNumber } from './core/number.mjs';
|
|
32
|
+
export { json, jsonObject, jsonArray } from './core/json.mjs';
|
|
33
|
+
export { lazy } from './core/lazy.mjs';
|
|
34
|
+
export { maybe, nullable, optional } from './core/optional.mjs';
|
|
35
|
+
export { taggedUnion } from './core/dispatch.mjs';
|
|
36
|
+
export { tuple } from './core/tuple.mjs';
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "decoders",
|
|
3
|
-
"version": "2.0.0-
|
|
3
|
+
"version": "2.0.0-beta9",
|
|
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": {
|
|
@@ -13,7 +13,7 @@
|
|
|
13
13
|
"url": "https://github.com/nvie/decoders/issues"
|
|
14
14
|
},
|
|
15
15
|
"main": "./index.js",
|
|
16
|
-
"module": "./
|
|
16
|
+
"module": "./index.mjs",
|
|
17
17
|
"keywords": [
|
|
18
18
|
"decoder",
|
|
19
19
|
"decoders",
|
|
@@ -41,5 +41,17 @@
|
|
|
41
41
|
"predicate"
|
|
42
42
|
],
|
|
43
43
|
"githubUrl": "https://github.com/nvie/decoders",
|
|
44
|
-
"sideEffects": false
|
|
44
|
+
"sideEffects": false,
|
|
45
|
+
"typesVersions": {
|
|
46
|
+
">=4.1.0": {
|
|
47
|
+
"*": [
|
|
48
|
+
"*"
|
|
49
|
+
]
|
|
50
|
+
},
|
|
51
|
+
"*": {
|
|
52
|
+
"*": [
|
|
53
|
+
"NotSupportedTSVersion.d.ts"
|
|
54
|
+
]
|
|
55
|
+
}
|
|
56
|
+
}
|
|
45
57
|
}
|
package/result.d.ts
ADDED
|
@@ -0,0 +1,39 @@
|
|
|
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>;
|
|
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
|
@@ -4,19 +4,12 @@ exports.__esModule = true;
|
|
|
4
4
|
exports.andThen = andThen;
|
|
5
5
|
exports.dispatch = dispatch;
|
|
6
6
|
exports.err = err;
|
|
7
|
-
exports.errValue = errValue;
|
|
8
7
|
exports.expect = expect;
|
|
9
|
-
exports.isErr = isErr;
|
|
10
|
-
exports.isOk = isOk;
|
|
11
8
|
exports.mapError = mapError;
|
|
12
9
|
exports.mapOk = mapOk;
|
|
13
10
|
exports.ok = ok;
|
|
14
|
-
exports.okOrErrValue = okOrErrValue;
|
|
15
|
-
exports.okValue = okValue;
|
|
16
11
|
exports.orElse = orElse;
|
|
17
|
-
exports.toString = toString;
|
|
18
12
|
exports.unwrap = unwrap;
|
|
19
|
-
exports.withDefault = withDefault;
|
|
20
13
|
|
|
21
14
|
/**
|
|
22
15
|
* Result <value> <error>
|
|
@@ -29,7 +22,7 @@ exports.withDefault = withDefault;
|
|
|
29
22
|
*/
|
|
30
23
|
function ok(value) {
|
|
31
24
|
return {
|
|
32
|
-
|
|
25
|
+
ok: true,
|
|
33
26
|
value: value,
|
|
34
27
|
error: undefined
|
|
35
28
|
};
|
|
@@ -41,43 +34,11 @@ function ok(value) {
|
|
|
41
34
|
|
|
42
35
|
function err(error) {
|
|
43
36
|
return {
|
|
44
|
-
|
|
37
|
+
ok: false,
|
|
45
38
|
value: undefined,
|
|
46
39
|
error: error
|
|
47
40
|
};
|
|
48
41
|
}
|
|
49
|
-
|
|
50
|
-
function toString(result) {
|
|
51
|
-
return result.type === 'ok' ? "Ok(" + String(result.value) + ")" : "Err(" + String(result.error) + ")";
|
|
52
|
-
}
|
|
53
|
-
|
|
54
|
-
function isOk(result) {
|
|
55
|
-
return result.type === 'ok';
|
|
56
|
-
}
|
|
57
|
-
|
|
58
|
-
function isErr(result) {
|
|
59
|
-
return result.type === 'err';
|
|
60
|
-
}
|
|
61
|
-
|
|
62
|
-
function withDefault(result, defaultValue) {
|
|
63
|
-
return result.type === 'ok' ? result.value : defaultValue;
|
|
64
|
-
} // TODO: Remove this from the public API? The same can be achieved now with
|
|
65
|
-
// TODO: const { value } = result;
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
function okValue(result) {
|
|
69
|
-
return result.type === 'ok' ? result.value : undefined;
|
|
70
|
-
} // TODO: Remove this from the public API? The same can be achieved now with
|
|
71
|
-
// TODO: const { error } = result;
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
function errValue(result) {
|
|
75
|
-
return result.type === 'err' ? result.error : undefined;
|
|
76
|
-
}
|
|
77
|
-
|
|
78
|
-
function okOrErrValue(result) {
|
|
79
|
-
return result.type === 'ok' ? result.value : result.error;
|
|
80
|
-
}
|
|
81
42
|
/**
|
|
82
43
|
* Unwrap the value from this Result instance if this is an "Ok" result.
|
|
83
44
|
* Otherwise, will throw the "Err" error via a runtime exception.
|
|
@@ -85,7 +46,7 @@ function okOrErrValue(result) {
|
|
|
85
46
|
|
|
86
47
|
|
|
87
48
|
function unwrap(result) {
|
|
88
|
-
if (result.
|
|
49
|
+
if (result.ok) {
|
|
89
50
|
return result.value;
|
|
90
51
|
} else {
|
|
91
52
|
throw result.error;
|
|
@@ -93,7 +54,7 @@ function unwrap(result) {
|
|
|
93
54
|
}
|
|
94
55
|
|
|
95
56
|
function expect(result, message) {
|
|
96
|
-
if (result.
|
|
57
|
+
if (result.ok) {
|
|
97
58
|
return result.value;
|
|
98
59
|
} else {
|
|
99
60
|
throw message instanceof Error ? message : new Error(message);
|
|
@@ -101,50 +62,8 @@ function expect(result, message) {
|
|
|
101
62
|
}
|
|
102
63
|
|
|
103
64
|
function dispatch(result, okCallback, errCallback) {
|
|
104
|
-
return result.
|
|
65
|
+
return result.ok ? okCallback(result.value) : errCallback(result.error);
|
|
105
66
|
}
|
|
106
|
-
/**
|
|
107
|
-
* If the given result is OK, defers to the other result. Otherwise returns the
|
|
108
|
-
* error result.
|
|
109
|
-
*
|
|
110
|
-
* It's like saying A && B, but on Result.
|
|
111
|
-
*
|
|
112
|
-
* Examples:
|
|
113
|
-
*
|
|
114
|
-
* Result.ok(42) && Result.ok('hi') // => Ok('hi')
|
|
115
|
-
* Result.err('boo') && Result.ok('hi') // => Err('boo')
|
|
116
|
-
* Result.ok(42) && Result.err('boo') // => Err('boo')
|
|
117
|
-
* Result.err('boo') && Result.err('boo') // => Err('boo')
|
|
118
|
-
*
|
|
119
|
-
*/
|
|
120
|
-
// export function and<T, E, T2>(
|
|
121
|
-
// result1: Result<T, E>,
|
|
122
|
-
// result2: Result<T2, E>,
|
|
123
|
-
// ): Result<T2, E> {
|
|
124
|
-
// return result1.type === 'ok' ? result2 : result1;
|
|
125
|
-
// }
|
|
126
|
-
|
|
127
|
-
/**
|
|
128
|
-
* If the given result is OK, return that result. Otherwise, defers to the
|
|
129
|
-
* other result.
|
|
130
|
-
*
|
|
131
|
-
* It's like saying A || B, but on Result.
|
|
132
|
-
*
|
|
133
|
-
* Examples:
|
|
134
|
-
*
|
|
135
|
-
* Result.ok(42) || Result.ok('hi') // => Ok(42)
|
|
136
|
-
* Result.err('boo') || Result.ok('hi') // => Ok('hi')
|
|
137
|
-
* Result.ok(42) || Result.err('boo') // => Ok(42)
|
|
138
|
-
* Result.err('bleh') || Result.err('boo') // => Err('boo')
|
|
139
|
-
*
|
|
140
|
-
*/
|
|
141
|
-
// export function or<T, E, E2>(
|
|
142
|
-
// result1: Result<T, E>,
|
|
143
|
-
// result2: Result<T, E2>,
|
|
144
|
-
// ): Result<T, E2> {
|
|
145
|
-
// return result1.type === 'ok' ? result1 : result2;
|
|
146
|
-
// }
|
|
147
|
-
|
|
148
67
|
/**
|
|
149
68
|
* Like .and(), aka &&, but the second argument gets evaluated lazily only if
|
|
150
69
|
* the first result is an Ok result. If so, it has access to the Ok value from
|
|
@@ -153,7 +72,7 @@ function dispatch(result, okCallback, errCallback) {
|
|
|
153
72
|
|
|
154
73
|
|
|
155
74
|
function andThen(result1, lazyResult2) {
|
|
156
|
-
return result1.
|
|
75
|
+
return result1.ok ? lazyResult2(result1.value) : result1;
|
|
157
76
|
}
|
|
158
77
|
/**
|
|
159
78
|
* Like .or(), aka ||, but the second argument gets evaluated lazily only if
|
|
@@ -163,7 +82,7 @@ function andThen(result1, lazyResult2) {
|
|
|
163
82
|
|
|
164
83
|
|
|
165
84
|
function orElse(result1, lazyResult2) {
|
|
166
|
-
return result1.
|
|
85
|
+
return result1.ok ? result1 : lazyResult2(result1.error);
|
|
167
86
|
}
|
|
168
87
|
/**
|
|
169
88
|
* Transform an Ok result. Will not touch Err results.
|
|
@@ -171,7 +90,7 @@ function orElse(result1, lazyResult2) {
|
|
|
171
90
|
|
|
172
91
|
|
|
173
92
|
function mapOk(result, mapper) {
|
|
174
|
-
return result.
|
|
93
|
+
return result.ok ? ok(mapper(result.value)) : result;
|
|
175
94
|
}
|
|
176
95
|
/**
|
|
177
96
|
* Transform an Err value. Will not touch Ok results.
|
|
@@ -179,5 +98,5 @@ function mapOk(result, mapper) {
|
|
|
179
98
|
|
|
180
99
|
|
|
181
100
|
function mapError(result, mapper) {
|
|
182
|
-
return result.
|
|
101
|
+
return result.ok ? result : err(mapper(result.error));
|
|
183
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,48 +15,14 @@ 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
|
-
export function toString(result: Result<mixed, mixed>): string {
|
|
29
|
-
return result.type === 'ok'
|
|
30
|
-
? `Ok(${String(result.value)})`
|
|
31
|
-
: `Err(${String(result.error)})`;
|
|
32
|
-
}
|
|
33
|
-
|
|
34
|
-
export function isOk(result: Result<mixed, mixed>): boolean {
|
|
35
|
-
return result.type === 'ok';
|
|
36
|
-
}
|
|
37
|
-
|
|
38
|
-
export function isErr(result: Result<mixed, mixed>): boolean {
|
|
39
|
-
return result.type === 'err';
|
|
40
|
-
}
|
|
41
|
-
|
|
42
|
-
export function withDefault<T>(result: Result<T, mixed>, defaultValue: T): T {
|
|
43
|
-
return result.type === 'ok' ? result.value : defaultValue;
|
|
44
|
-
}
|
|
45
|
-
|
|
46
|
-
// TODO: Remove this from the public API? The same can be achieved now with
|
|
47
|
-
// TODO: const { value } = result;
|
|
48
|
-
export function okValue<T>(result: Result<T, mixed>): void | T {
|
|
49
|
-
return result.type === 'ok' ? result.value : undefined;
|
|
50
|
-
}
|
|
51
|
-
|
|
52
|
-
// TODO: Remove this from the public API? The same can be achieved now with
|
|
53
|
-
// TODO: const { error } = result;
|
|
54
|
-
export function errValue<E>(result: Result<mixed, E>): void | E {
|
|
55
|
-
return result.type === 'err' ? result.error : undefined;
|
|
56
|
-
}
|
|
57
|
-
|
|
58
|
-
export function okOrErrValue<T, E>(result: Result<T, E>): T | E {
|
|
59
|
-
return result.type === 'ok' ? result.value : result.error;
|
|
25
|
+
return { ok: false, value: undefined, error };
|
|
60
26
|
}
|
|
61
27
|
|
|
62
28
|
/**
|
|
@@ -64,7 +30,7 @@ export function okOrErrValue<T, E>(result: Result<T, E>): T | E {
|
|
|
64
30
|
* Otherwise, will throw the "Err" error via a runtime exception.
|
|
65
31
|
*/
|
|
66
32
|
export function unwrap<T>(result: Result<T, mixed>): T {
|
|
67
|
-
if (result.
|
|
33
|
+
if (result.ok) {
|
|
68
34
|
return result.value;
|
|
69
35
|
} else {
|
|
70
36
|
throw result.error;
|
|
@@ -72,7 +38,7 @@ export function unwrap<T>(result: Result<T, mixed>): T {
|
|
|
72
38
|
}
|
|
73
39
|
|
|
74
40
|
export function expect<T>(result: Result<T, mixed>, message: string | Error): T {
|
|
75
|
-
if (result.
|
|
41
|
+
if (result.ok) {
|
|
76
42
|
return result.value;
|
|
77
43
|
} else {
|
|
78
44
|
throw message instanceof Error ? message : new Error(message);
|
|
@@ -84,51 +50,9 @@ export function dispatch<T, E, O>(
|
|
|
84
50
|
okCallback: (value: T) => O,
|
|
85
51
|
errCallback: (error: E) => O,
|
|
86
52
|
): O {
|
|
87
|
-
return result.
|
|
53
|
+
return result.ok ? okCallback(result.value) : errCallback(result.error);
|
|
88
54
|
}
|
|
89
55
|
|
|
90
|
-
/**
|
|
91
|
-
* If the given result is OK, defers to the other result. Otherwise returns the
|
|
92
|
-
* error result.
|
|
93
|
-
*
|
|
94
|
-
* It's like saying A && B, but on Result.
|
|
95
|
-
*
|
|
96
|
-
* Examples:
|
|
97
|
-
*
|
|
98
|
-
* Result.ok(42) && Result.ok('hi') // => Ok('hi')
|
|
99
|
-
* Result.err('boo') && Result.ok('hi') // => Err('boo')
|
|
100
|
-
* Result.ok(42) && Result.err('boo') // => Err('boo')
|
|
101
|
-
* Result.err('boo') && Result.err('boo') // => Err('boo')
|
|
102
|
-
*
|
|
103
|
-
*/
|
|
104
|
-
// export function and<T, E, T2>(
|
|
105
|
-
// result1: Result<T, E>,
|
|
106
|
-
// result2: Result<T2, E>,
|
|
107
|
-
// ): Result<T2, E> {
|
|
108
|
-
// return result1.type === 'ok' ? result2 : result1;
|
|
109
|
-
// }
|
|
110
|
-
|
|
111
|
-
/**
|
|
112
|
-
* If the given result is OK, return that result. Otherwise, defers to the
|
|
113
|
-
* other result.
|
|
114
|
-
*
|
|
115
|
-
* It's like saying A || B, but on Result.
|
|
116
|
-
*
|
|
117
|
-
* Examples:
|
|
118
|
-
*
|
|
119
|
-
* Result.ok(42) || Result.ok('hi') // => Ok(42)
|
|
120
|
-
* Result.err('boo') || Result.ok('hi') // => Ok('hi')
|
|
121
|
-
* Result.ok(42) || Result.err('boo') // => Ok(42)
|
|
122
|
-
* Result.err('bleh') || Result.err('boo') // => Err('boo')
|
|
123
|
-
*
|
|
124
|
-
*/
|
|
125
|
-
// export function or<T, E, E2>(
|
|
126
|
-
// result1: Result<T, E>,
|
|
127
|
-
// result2: Result<T, E2>,
|
|
128
|
-
// ): Result<T, E2> {
|
|
129
|
-
// return result1.type === 'ok' ? result1 : result2;
|
|
130
|
-
// }
|
|
131
|
-
|
|
132
56
|
/**
|
|
133
57
|
* Like .and(), aka &&, but the second argument gets evaluated lazily only if
|
|
134
58
|
* the first result is an Ok result. If so, it has access to the Ok value from
|
|
@@ -138,7 +62,7 @@ export function andThen<T, E, T2>(
|
|
|
138
62
|
result1: Result<T, E>,
|
|
139
63
|
lazyResult2: (value: T) => Result<T2, E>,
|
|
140
64
|
): Result<T2, E> {
|
|
141
|
-
return result1.
|
|
65
|
+
return result1.ok ? lazyResult2(result1.value) : result1;
|
|
142
66
|
}
|
|
143
67
|
|
|
144
68
|
/**
|
|
@@ -150,7 +74,7 @@ export function orElse<T, E, E2>(
|
|
|
150
74
|
result1: Result<T, E>,
|
|
151
75
|
lazyResult2: (errValue: E) => Result<T, E2>,
|
|
152
76
|
): Result<T, E2> {
|
|
153
|
-
return result1.
|
|
77
|
+
return result1.ok ? result1 : lazyResult2(result1.error);
|
|
154
78
|
}
|
|
155
79
|
|
|
156
80
|
/**
|
|
@@ -160,7 +84,7 @@ export function mapOk<T, E, T2>(
|
|
|
160
84
|
result: Result<T, E>,
|
|
161
85
|
mapper: (value: T) => T2,
|
|
162
86
|
): Result<T2, E> {
|
|
163
|
-
return result.
|
|
87
|
+
return result.ok ? ok(mapper(result.value)) : result;
|
|
164
88
|
}
|
|
165
89
|
|
|
166
90
|
/**
|
|
@@ -170,5 +94,5 @@ export function mapError<T, E, E2>(
|
|
|
170
94
|
result: Result<T, E>,
|
|
171
95
|
mapper: (error: E) => E2,
|
|
172
96
|
): Result<T, E2> {
|
|
173
|
-
return result.
|
|
97
|
+
return result.ok ? result : err(mapper(result.error));
|
|
174
98
|
}
|