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
package/cjs/index.js
DELETED
|
@@ -1,120 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
|
|
3
|
-
exports.__esModule = true;
|
|
4
|
-
exports.url = exports.unknown = exports.undefined_ = exports.tuple6 = exports.tuple5 = exports.tuple4 = exports.tuple3 = exports.tuple2 = exports.tuple1 = exports.truthy = exports.string = exports.regex = 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.map = exports.lazy = exports.jsonObject = exports.jsonArray = exports.json = exports.iso8601 = exports.integer = exports.instanceOf = exports.inexact = exports.hardcoded = exports.guard = exports.fail = exports.exact = exports.email = exports.either9 = exports.either8 = exports.either7 = exports.either6 = exports.either5 = exports.either4 = exports.either3 = exports.either = exports.dispatch = exports.dict = exports.describe = exports.date = exports.constant = exports.compose = exports["boolean"] = exports.array = void 0;
|
|
5
|
-
|
|
6
|
-
var _guard = require("./_guard");
|
|
7
|
-
|
|
8
|
-
exports.guard = _guard.guard;
|
|
9
|
-
|
|
10
|
-
var _composition = require("./stdlib/composition");
|
|
11
|
-
|
|
12
|
-
exports.compose = _composition.compose;
|
|
13
|
-
exports.map = _composition.map;
|
|
14
|
-
exports.predicate = _composition.predicate;
|
|
15
|
-
|
|
16
|
-
var _array = require("./stdlib/array");
|
|
17
|
-
|
|
18
|
-
exports.array = _array.array;
|
|
19
|
-
exports.nonEmptyArray = _array.nonEmptyArray;
|
|
20
|
-
exports.poja = _array.poja;
|
|
21
|
-
|
|
22
|
-
var _boolean = require("./stdlib/boolean");
|
|
23
|
-
|
|
24
|
-
exports["boolean"] = _boolean["boolean"];
|
|
25
|
-
exports.numericBoolean = _boolean.numericBoolean;
|
|
26
|
-
exports.truthy = _boolean.truthy;
|
|
27
|
-
|
|
28
|
-
var _constants = require("./stdlib/constants");
|
|
29
|
-
|
|
30
|
-
exports.constant = _constants.constant;
|
|
31
|
-
exports.hardcoded = _constants.hardcoded;
|
|
32
|
-
exports.mixed = _constants.mixed;
|
|
33
|
-
exports.null_ = _constants.null_;
|
|
34
|
-
exports.undefined_ = _constants.undefined_;
|
|
35
|
-
exports.unknown = _constants.unknown;
|
|
36
|
-
|
|
37
|
-
var _date = require("./stdlib/date");
|
|
38
|
-
|
|
39
|
-
exports.date = _date.date;
|
|
40
|
-
exports.iso8601 = _date.iso8601;
|
|
41
|
-
|
|
42
|
-
var _describe = require("./stdlib/describe");
|
|
43
|
-
|
|
44
|
-
exports.describe = _describe.describe;
|
|
45
|
-
|
|
46
|
-
var _dispatch = require("./stdlib/dispatch");
|
|
47
|
-
|
|
48
|
-
exports.dispatch = _dispatch.dispatch;
|
|
49
|
-
|
|
50
|
-
var _either = require("./stdlib/either");
|
|
51
|
-
|
|
52
|
-
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
|
-
exports.oneOf = _either.oneOf;
|
|
61
|
-
|
|
62
|
-
var _fail = require("./stdlib/fail");
|
|
63
|
-
|
|
64
|
-
exports.fail = _fail.fail;
|
|
65
|
-
|
|
66
|
-
var _instanceOf = require("./stdlib/instanceOf");
|
|
67
|
-
|
|
68
|
-
exports.instanceOf = _instanceOf.instanceOf;
|
|
69
|
-
|
|
70
|
-
var _json = require("./stdlib/json");
|
|
71
|
-
|
|
72
|
-
exports.json = _json.json;
|
|
73
|
-
exports.jsonObject = _json.jsonObject;
|
|
74
|
-
exports.jsonArray = _json.jsonArray;
|
|
75
|
-
|
|
76
|
-
var _lazy = require("./stdlib/lazy");
|
|
77
|
-
|
|
78
|
-
exports.lazy = _lazy.lazy;
|
|
79
|
-
|
|
80
|
-
var _mapping = require("./stdlib/mapping");
|
|
81
|
-
|
|
82
|
-
exports.mapping = _mapping.mapping;
|
|
83
|
-
exports.dict = _mapping.dict;
|
|
84
|
-
|
|
85
|
-
var _number = require("./stdlib/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("./stdlib/object");
|
|
93
|
-
|
|
94
|
-
exports.exact = _object.exact;
|
|
95
|
-
exports.inexact = _object.inexact;
|
|
96
|
-
exports.object = _object.object;
|
|
97
|
-
exports.pojo = _object.pojo;
|
|
98
|
-
|
|
99
|
-
var _optional = require("./stdlib/optional");
|
|
100
|
-
|
|
101
|
-
exports.maybe = _optional.maybe;
|
|
102
|
-
exports.nullable = _optional.nullable;
|
|
103
|
-
exports.optional = _optional.optional;
|
|
104
|
-
|
|
105
|
-
var _string = require("./stdlib/string");
|
|
106
|
-
|
|
107
|
-
exports.email = _string.email;
|
|
108
|
-
exports.nonEmptyString = _string.nonEmptyString;
|
|
109
|
-
exports.regex = _string.regex;
|
|
110
|
-
exports.string = _string.string;
|
|
111
|
-
exports.url = _string.url;
|
|
112
|
-
|
|
113
|
-
var _tuple = require("./stdlib/tuple");
|
|
114
|
-
|
|
115
|
-
exports.tuple1 = _tuple.tuple1;
|
|
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;
|
package/cjs/index.js.flow
DELETED
|
@@ -1,63 +0,0 @@
|
|
|
1
|
-
// @flow strict
|
|
2
|
-
|
|
3
|
-
/**
|
|
4
|
-
* Elm-like JSON decoders, for use with Flow.
|
|
5
|
-
* See http://elmplayground.com/decoding-json-in-elm-1 for an introduction.
|
|
6
|
-
*
|
|
7
|
-
* Why? All JSON responses coming from our API endpoints are just that: free-form
|
|
8
|
-
* JSON data. To Flow, the only type classification possilbe is "any" -- effectively
|
|
9
|
-
* turning off all type checks for anything related to JSON. To the receiving end
|
|
10
|
-
* (our frontend), the structure of that data is completely opaque to any type
|
|
11
|
-
* checkers since JSON values can be anything: an object, an array, null, a string,
|
|
12
|
-
* a bool, etc. Our type system is not a runtime type system, so we need a way of
|
|
13
|
-
* "converting" an any-type JSON value into a type that we want to work with in our
|
|
14
|
-
* frontend code base.
|
|
15
|
-
*
|
|
16
|
-
* Elm's solution to this problem is to define composable decoders: functions that
|
|
17
|
-
* take anything and either fail with an error, or guarantee to return the expected
|
|
18
|
-
* type. In our case, it's fine to fail with a runtime error.
|
|
19
|
-
*
|
|
20
|
-
*/
|
|
21
|
-
export type { Decoder, Guard } from './_types';
|
|
22
|
-
export type { DecoderType, GuardType } from './_types';
|
|
23
|
-
|
|
24
|
-
export type { JSONValue, JSONObject, JSONArray } from './stdlib/json';
|
|
25
|
-
|
|
26
|
-
export { guard } from './_guard';
|
|
27
|
-
|
|
28
|
-
export { compose, map, predicate } from './stdlib/composition';
|
|
29
|
-
|
|
30
|
-
export { array, nonEmptyArray, poja } from './stdlib/array';
|
|
31
|
-
export { boolean, numericBoolean, truthy } from './stdlib/boolean';
|
|
32
|
-
export {
|
|
33
|
-
constant,
|
|
34
|
-
hardcoded,
|
|
35
|
-
mixed,
|
|
36
|
-
null_,
|
|
37
|
-
undefined_,
|
|
38
|
-
unknown,
|
|
39
|
-
} from './stdlib/constants';
|
|
40
|
-
export { date, iso8601 } from './stdlib/date';
|
|
41
|
-
export { describe } from './stdlib/describe';
|
|
42
|
-
export { dispatch } from './stdlib/dispatch';
|
|
43
|
-
export {
|
|
44
|
-
either,
|
|
45
|
-
either3,
|
|
46
|
-
either4,
|
|
47
|
-
either5,
|
|
48
|
-
either6,
|
|
49
|
-
either7,
|
|
50
|
-
either8,
|
|
51
|
-
either9,
|
|
52
|
-
oneOf,
|
|
53
|
-
} from './stdlib/either';
|
|
54
|
-
export { fail } from './stdlib/fail';
|
|
55
|
-
export { instanceOf } from './stdlib/instanceOf';
|
|
56
|
-
export { json, jsonObject, jsonArray } from './stdlib/json';
|
|
57
|
-
export { lazy } from './stdlib/lazy';
|
|
58
|
-
export { mapping, dict } from './stdlib/mapping';
|
|
59
|
-
export { integer, number, positiveInteger, positiveNumber } from './stdlib/number';
|
|
60
|
-
export { exact, inexact, object, pojo } from './stdlib/object';
|
|
61
|
-
export { maybe, nullable, optional } from './stdlib/optional';
|
|
62
|
-
export { email, nonEmptyString, regex, string, url } from './stdlib/string';
|
|
63
|
-
export { tuple1, tuple2, tuple3, tuple4, tuple5, tuple6 } from './stdlib/tuple';
|
package/cjs/result.js
DELETED
|
@@ -1,172 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
|
|
3
|
-
exports.__esModule = true;
|
|
4
|
-
exports.andThen = andThen;
|
|
5
|
-
exports.dispatch = dispatch;
|
|
6
|
-
exports.err = err;
|
|
7
|
-
exports.errValue = errValue;
|
|
8
|
-
exports.expect = expect;
|
|
9
|
-
exports.isErr = isErr;
|
|
10
|
-
exports.isOk = isOk;
|
|
11
|
-
exports.map = map;
|
|
12
|
-
exports.mapError = mapError;
|
|
13
|
-
exports.ok = ok;
|
|
14
|
-
exports.orElse = orElse;
|
|
15
|
-
exports.toString = toString;
|
|
16
|
-
exports.unwrap = unwrap;
|
|
17
|
-
exports.value = value;
|
|
18
|
-
exports.withDefault = withDefault;
|
|
19
|
-
|
|
20
|
-
/**
|
|
21
|
-
* Result <value> <error>
|
|
22
|
-
* = Ok <value>
|
|
23
|
-
* | Err <error>
|
|
24
|
-
*/
|
|
25
|
-
|
|
26
|
-
/**
|
|
27
|
-
* Create a new Result instance representing a successful computation.
|
|
28
|
-
*/
|
|
29
|
-
function ok(value) {
|
|
30
|
-
return {
|
|
31
|
-
type: 'ok',
|
|
32
|
-
value: value
|
|
33
|
-
};
|
|
34
|
-
}
|
|
35
|
-
/**
|
|
36
|
-
* Create a new Result instance representing a failed computation.
|
|
37
|
-
*/
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
function err(error) {
|
|
41
|
-
return {
|
|
42
|
-
type: 'err',
|
|
43
|
-
error: error
|
|
44
|
-
};
|
|
45
|
-
}
|
|
46
|
-
|
|
47
|
-
function toString(result) {
|
|
48
|
-
return result.type === 'ok' ? "Ok(" + String(result.value) + ")" : "Err(" + String(result.error) + ")";
|
|
49
|
-
}
|
|
50
|
-
|
|
51
|
-
function isOk(result) {
|
|
52
|
-
return result.type === 'ok';
|
|
53
|
-
}
|
|
54
|
-
|
|
55
|
-
function isErr(result) {
|
|
56
|
-
return result.type === 'err';
|
|
57
|
-
}
|
|
58
|
-
|
|
59
|
-
function withDefault(result, defaultValue) {
|
|
60
|
-
return result.type === 'ok' ? result.value : defaultValue;
|
|
61
|
-
}
|
|
62
|
-
|
|
63
|
-
function value(result) {
|
|
64
|
-
return result.type === 'ok' ? result.value : undefined;
|
|
65
|
-
}
|
|
66
|
-
|
|
67
|
-
function errValue(result) {
|
|
68
|
-
return result.type === 'err' ? result.error : undefined;
|
|
69
|
-
}
|
|
70
|
-
/**
|
|
71
|
-
* Unwrap the value from this Result instance if this is an "Ok" result.
|
|
72
|
-
* Otherwise, will throw the "Err" error via a runtime exception.
|
|
73
|
-
*/
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
function unwrap(result) {
|
|
77
|
-
if (result.type === 'ok') {
|
|
78
|
-
return result.value;
|
|
79
|
-
} else {
|
|
80
|
-
throw result.error;
|
|
81
|
-
}
|
|
82
|
-
}
|
|
83
|
-
|
|
84
|
-
function expect(result, message) {
|
|
85
|
-
if (result.type === 'ok') {
|
|
86
|
-
return result.value;
|
|
87
|
-
} else {
|
|
88
|
-
throw message instanceof Error ? message : new Error(message);
|
|
89
|
-
}
|
|
90
|
-
}
|
|
91
|
-
|
|
92
|
-
function dispatch(result, okCallback, errCallback) {
|
|
93
|
-
return result.type === 'ok' ? okCallback(result.value) : errCallback(result.error);
|
|
94
|
-
}
|
|
95
|
-
/**
|
|
96
|
-
* If the given result is OK, defers to the other result. Otherwise returns the
|
|
97
|
-
* error result.
|
|
98
|
-
*
|
|
99
|
-
* It's like saying A && B, but on Result.
|
|
100
|
-
*
|
|
101
|
-
* Examples:
|
|
102
|
-
*
|
|
103
|
-
* Result.ok(42) && Result.ok('hi') // => Ok('hi')
|
|
104
|
-
* Result.err('boo') && Result.ok('hi') // => Err('boo')
|
|
105
|
-
* Result.ok(42) && Result.err('boo') // => Err('boo')
|
|
106
|
-
* Result.err('boo') && Result.err('boo') // => Err('boo')
|
|
107
|
-
*
|
|
108
|
-
*/
|
|
109
|
-
// export function and<T, E, T2>(
|
|
110
|
-
// result1: Result<T, E>,
|
|
111
|
-
// result2: Result<T2, E>,
|
|
112
|
-
// ): Result<T2, E> {
|
|
113
|
-
// return result1.type === 'ok' ? result2 : result1;
|
|
114
|
-
// }
|
|
115
|
-
|
|
116
|
-
/**
|
|
117
|
-
* If the given result is OK, return that result. Otherwise, defers to the
|
|
118
|
-
* other result.
|
|
119
|
-
*
|
|
120
|
-
* It's like saying A || B, but on Result.
|
|
121
|
-
*
|
|
122
|
-
* Examples:
|
|
123
|
-
*
|
|
124
|
-
* Result.ok(42) || Result.ok('hi') // => Ok(42)
|
|
125
|
-
* Result.err('boo') || Result.ok('hi') // => Ok('hi')
|
|
126
|
-
* Result.ok(42) || Result.err('boo') // => Ok(42)
|
|
127
|
-
* Result.err('bleh') || Result.err('boo') // => Err('boo')
|
|
128
|
-
*
|
|
129
|
-
*/
|
|
130
|
-
// export function or<T, E, E2>(
|
|
131
|
-
// result1: Result<T, E>,
|
|
132
|
-
// result2: Result<T, E2>,
|
|
133
|
-
// ): Result<T, E2> {
|
|
134
|
-
// return result1.type === 'ok' ? result1 : result2;
|
|
135
|
-
// }
|
|
136
|
-
|
|
137
|
-
/**
|
|
138
|
-
* Like .and(), aka &&, but the second argument gets evaluated lazily only if
|
|
139
|
-
* the first result is an Ok result. If so, it has access to the Ok value from
|
|
140
|
-
* the first argument.
|
|
141
|
-
*/
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
function andThen(result1, lazyResult2) {
|
|
145
|
-
return result1.type === 'ok' ? lazyResult2(result1.value) : result1;
|
|
146
|
-
}
|
|
147
|
-
/**
|
|
148
|
-
* Like .or(), aka ||, but the second argument gets evaluated lazily only if
|
|
149
|
-
* the first result is an Err result. If so, it has access to the Err value
|
|
150
|
-
* from the first argument.
|
|
151
|
-
*/
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
function orElse(result1, lazyResult2) {
|
|
155
|
-
return result1.type === 'ok' ? result1 : lazyResult2(result1.error);
|
|
156
|
-
}
|
|
157
|
-
/**
|
|
158
|
-
* Transform an Ok result. Will not touch Err results.
|
|
159
|
-
*/
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
function map(result, mapper) {
|
|
163
|
-
return result.type === 'ok' ? ok(mapper(result.value)) : result;
|
|
164
|
-
}
|
|
165
|
-
/**
|
|
166
|
-
* Transform an Err value. Will not touch Ok results.
|
|
167
|
-
*/
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
function mapError(result, mapper) {
|
|
171
|
-
return result.type === 'ok' ? result : err(mapper(result.error));
|
|
172
|
-
}
|
package/cjs/result.js.flow
DELETED
|
@@ -1,166 +0,0 @@
|
|
|
1
|
-
// @flow strict
|
|
2
|
-
|
|
3
|
-
/**
|
|
4
|
-
* Result <value> <error>
|
|
5
|
-
* = Ok <value>
|
|
6
|
-
* | Err <error>
|
|
7
|
-
*/
|
|
8
|
-
|
|
9
|
-
type Ok<+T> = {| +type: 'ok', +value: T |};
|
|
10
|
-
type Err<+E> = {| +type: 'err', +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 { type: 'ok', value };
|
|
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 { type: 'err', error };
|
|
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
|
-
export function value<T>(result: Result<T, mixed>): void | T {
|
|
47
|
-
return result.type === 'ok' ? result.value : undefined;
|
|
48
|
-
}
|
|
49
|
-
|
|
50
|
-
export function errValue<E>(result: Result<mixed, E>): void | E {
|
|
51
|
-
return result.type === 'err' ? result.error : undefined;
|
|
52
|
-
}
|
|
53
|
-
|
|
54
|
-
/**
|
|
55
|
-
* Unwrap the value from this Result instance if this is an "Ok" result.
|
|
56
|
-
* Otherwise, will throw the "Err" error via a runtime exception.
|
|
57
|
-
*/
|
|
58
|
-
export function unwrap<T>(result: Result<T, mixed>): T {
|
|
59
|
-
if (result.type === 'ok') {
|
|
60
|
-
return result.value;
|
|
61
|
-
} else {
|
|
62
|
-
throw result.error;
|
|
63
|
-
}
|
|
64
|
-
}
|
|
65
|
-
|
|
66
|
-
export function expect<T>(result: Result<T, mixed>, message: string | Error): T {
|
|
67
|
-
if (result.type === 'ok') {
|
|
68
|
-
return result.value;
|
|
69
|
-
} else {
|
|
70
|
-
throw message instanceof Error ? message : new Error(message);
|
|
71
|
-
}
|
|
72
|
-
}
|
|
73
|
-
|
|
74
|
-
export function dispatch<T, E, O>(
|
|
75
|
-
result: Result<T, E>,
|
|
76
|
-
okCallback: (value: T) => O,
|
|
77
|
-
errCallback: (error: E) => O,
|
|
78
|
-
): O {
|
|
79
|
-
return result.type === 'ok' ? okCallback(result.value) : errCallback(result.error);
|
|
80
|
-
}
|
|
81
|
-
|
|
82
|
-
/**
|
|
83
|
-
* If the given result is OK, defers to the other result. Otherwise returns the
|
|
84
|
-
* error result.
|
|
85
|
-
*
|
|
86
|
-
* It's like saying A && B, but on Result.
|
|
87
|
-
*
|
|
88
|
-
* Examples:
|
|
89
|
-
*
|
|
90
|
-
* Result.ok(42) && Result.ok('hi') // => Ok('hi')
|
|
91
|
-
* Result.err('boo') && Result.ok('hi') // => Err('boo')
|
|
92
|
-
* Result.ok(42) && Result.err('boo') // => Err('boo')
|
|
93
|
-
* Result.err('boo') && Result.err('boo') // => Err('boo')
|
|
94
|
-
*
|
|
95
|
-
*/
|
|
96
|
-
// export function and<T, E, T2>(
|
|
97
|
-
// result1: Result<T, E>,
|
|
98
|
-
// result2: Result<T2, E>,
|
|
99
|
-
// ): Result<T2, E> {
|
|
100
|
-
// return result1.type === 'ok' ? result2 : result1;
|
|
101
|
-
// }
|
|
102
|
-
|
|
103
|
-
/**
|
|
104
|
-
* If the given result is OK, return that result. Otherwise, defers to the
|
|
105
|
-
* other result.
|
|
106
|
-
*
|
|
107
|
-
* It's like saying A || B, but on Result.
|
|
108
|
-
*
|
|
109
|
-
* Examples:
|
|
110
|
-
*
|
|
111
|
-
* Result.ok(42) || Result.ok('hi') // => Ok(42)
|
|
112
|
-
* Result.err('boo') || Result.ok('hi') // => Ok('hi')
|
|
113
|
-
* Result.ok(42) || Result.err('boo') // => Ok(42)
|
|
114
|
-
* Result.err('bleh') || Result.err('boo') // => Err('boo')
|
|
115
|
-
*
|
|
116
|
-
*/
|
|
117
|
-
// export function or<T, E, E2>(
|
|
118
|
-
// result1: Result<T, E>,
|
|
119
|
-
// result2: Result<T, E2>,
|
|
120
|
-
// ): Result<T, E2> {
|
|
121
|
-
// return result1.type === 'ok' ? result1 : result2;
|
|
122
|
-
// }
|
|
123
|
-
|
|
124
|
-
/**
|
|
125
|
-
* Like .and(), aka &&, but the second argument gets evaluated lazily only if
|
|
126
|
-
* the first result is an Ok result. If so, it has access to the Ok value from
|
|
127
|
-
* the first argument.
|
|
128
|
-
*/
|
|
129
|
-
export function andThen<T, E, T2>(
|
|
130
|
-
result1: Result<T, E>,
|
|
131
|
-
lazyResult2: (value: T) => Result<T2, E>,
|
|
132
|
-
): Result<T2, E> {
|
|
133
|
-
return result1.type === 'ok' ? lazyResult2(result1.value) : result1;
|
|
134
|
-
}
|
|
135
|
-
|
|
136
|
-
/**
|
|
137
|
-
* Like .or(), aka ||, but the second argument gets evaluated lazily only if
|
|
138
|
-
* the first result is an Err result. If so, it has access to the Err value
|
|
139
|
-
* from the first argument.
|
|
140
|
-
*/
|
|
141
|
-
export function orElse<T, E, E2>(
|
|
142
|
-
result1: Result<T, E>,
|
|
143
|
-
lazyResult2: (errValue: E) => Result<T, E2>,
|
|
144
|
-
): Result<T, E2> {
|
|
145
|
-
return result1.type === 'ok' ? result1 : lazyResult2(result1.error);
|
|
146
|
-
}
|
|
147
|
-
|
|
148
|
-
/**
|
|
149
|
-
* Transform an Ok result. Will not touch Err results.
|
|
150
|
-
*/
|
|
151
|
-
export function map<T, E, T2>(
|
|
152
|
-
result: Result<T, E>,
|
|
153
|
-
mapper: (value: T) => T2,
|
|
154
|
-
): Result<T2, E> {
|
|
155
|
-
return result.type === 'ok' ? ok(mapper(result.value)) : result;
|
|
156
|
-
}
|
|
157
|
-
|
|
158
|
-
/**
|
|
159
|
-
* Transform an Err value. Will not touch Ok results.
|
|
160
|
-
*/
|
|
161
|
-
export function mapError<T, E, E2>(
|
|
162
|
-
result: Result<T, E>,
|
|
163
|
-
mapper: (error: E) => E2,
|
|
164
|
-
): Result<T, E2> {
|
|
165
|
-
return result.type === 'ok' ? result : err(mapper(result.error));
|
|
166
|
-
}
|
package/cjs/stdlib/array.js
DELETED
|
@@ -1,108 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
|
|
3
|
-
exports.__esModule = true;
|
|
4
|
-
exports.array = array;
|
|
5
|
-
exports.nonEmptyArray = nonEmptyArray;
|
|
6
|
-
exports.poja = void 0;
|
|
7
|
-
|
|
8
|
-
var Result = _interopRequireWildcard(require("../result"));
|
|
9
|
-
|
|
10
|
-
var _annotate = require("../annotate");
|
|
11
|
-
|
|
12
|
-
var _composition = require("./composition");
|
|
13
|
-
|
|
14
|
-
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); }
|
|
15
|
-
|
|
16
|
-
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; }
|
|
17
|
-
|
|
18
|
-
/**
|
|
19
|
-
* Like a "Plain Old JavaScript Object", but for arrays: "Plain Old JavaScript
|
|
20
|
-
* Array" ^_^
|
|
21
|
-
*/
|
|
22
|
-
var poja = function poja(blob) {
|
|
23
|
-
if (!Array.isArray(blob)) {
|
|
24
|
-
return Result.err((0, _annotate.annotate)(blob, 'Must be an array'));
|
|
25
|
-
}
|
|
26
|
-
|
|
27
|
-
return Result.ok( // NOTE: Since Flow 0.98, Array.isArray() returns $ReadOnlyArray<mixed>
|
|
28
|
-
// instead of Array<mixed>. For rationale, see
|
|
29
|
-
// https://github.com/facebook/flow/issues/7684. In this case, we
|
|
30
|
-
// don't want to output read-only types because it's up to the user of
|
|
31
|
-
// decoders to determine what they want to do with the decoded output.
|
|
32
|
-
// If they want to write items into the array, that's fine!
|
|
33
|
-
// The fastest way to turn a read-only array into a normal array in
|
|
34
|
-
// Javascript is to use .slice() on it, see this benchmark:
|
|
35
|
-
// http://jsben.ch/lO6C5
|
|
36
|
-
blob.slice());
|
|
37
|
-
};
|
|
38
|
-
/**
|
|
39
|
-
* Given an array of Result instances, loop over them all and return:
|
|
40
|
-
* - An [index, err] tuple, indicating the (index of the) first Err instance
|
|
41
|
-
* encountered; or
|
|
42
|
-
* - a new Ok with an array of all unwrapped Ok'ed values
|
|
43
|
-
*/
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
exports.poja = poja;
|
|
47
|
-
|
|
48
|
-
function all(items, blobs) {
|
|
49
|
-
var results = [];
|
|
50
|
-
|
|
51
|
-
for (var index = 0; index < items.length; ++index) {
|
|
52
|
-
var result = items[index];
|
|
53
|
-
|
|
54
|
-
if (result.type === 'ok') {
|
|
55
|
-
results.push(result.value);
|
|
56
|
-
} else {
|
|
57
|
-
var ann = result.error; // Rewrite the annotation to include the index information, and inject it into the original blob
|
|
58
|
-
|
|
59
|
-
var clone = [].concat(blobs);
|
|
60
|
-
clone.splice(index, 1, (0, _annotate.annotate)(ann, ann.text ? ann.text + " (at index " + index + ")" : "index " + index)); // const errValue = [];
|
|
61
|
-
// if (index > 0) {
|
|
62
|
-
// errValue.push('...'); // TODO: make special mark, not string!
|
|
63
|
-
// }
|
|
64
|
-
// errValue.push(
|
|
65
|
-
// );
|
|
66
|
-
// if (index < iterable.length - 1) {
|
|
67
|
-
// errValue.push('...'); // TODO: make special mark, not string!
|
|
68
|
-
// }
|
|
69
|
-
|
|
70
|
-
return Result.err((0, _annotate.annotate)(clone));
|
|
71
|
-
}
|
|
72
|
-
}
|
|
73
|
-
|
|
74
|
-
return Result.ok(results);
|
|
75
|
-
}
|
|
76
|
-
/**
|
|
77
|
-
* Given a T, builds a decoder that assumes an array input and returns an
|
|
78
|
-
* Array<T>.
|
|
79
|
-
*/
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
function members(decoder) {
|
|
83
|
-
return function (blobs) {
|
|
84
|
-
var results = blobs.map(decoder);
|
|
85
|
-
var result = all(results, blobs);
|
|
86
|
-
return result;
|
|
87
|
-
};
|
|
88
|
-
}
|
|
89
|
-
/**
|
|
90
|
-
* Builds a Decoder that returns Ok for values of `Array<T>`, given a Decoder
|
|
91
|
-
* for `T`. Err otherwise.
|
|
92
|
-
*/
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
function array(decoder) {
|
|
96
|
-
return (0, _composition.compose)(poja, members(decoder));
|
|
97
|
-
}
|
|
98
|
-
/**
|
|
99
|
-
* Builds a Decoder that returns Ok for values of `Array<T>`, but will reject
|
|
100
|
-
* empty arrays.
|
|
101
|
-
*/
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
function nonEmptyArray(decoder) {
|
|
105
|
-
return (0, _composition.compose)(array(decoder), (0, _composition.predicate)(function (arr) {
|
|
106
|
-
return arr.length > 0;
|
|
107
|
-
}, 'Must be non-empty array'));
|
|
108
|
-
}
|