decoders 2.0.0-beta9 → 2.0.0

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.
Files changed (136) hide show
  1. package/CHANGELOG.md +56 -29
  2. package/Decoder.d.ts +94 -0
  3. package/Decoder.js +222 -0
  4. package/Decoder.js.flow +286 -0
  5. package/Decoder.mjs +215 -0
  6. package/README.md +122 -1507
  7. package/_utils.d.ts +0 -1
  8. package/_utils.js +11 -17
  9. package/_utils.js.flow +13 -17
  10. package/_utils.mjs +10 -14
  11. package/format.d.ts +4 -2
  12. package/format.js +1 -1
  13. package/format.js.flow +3 -1
  14. package/format.mjs +1 -1
  15. package/index.d.ts +29 -31
  16. package/index.js +62 -84
  17. package/index.js.flow +30 -48
  18. package/index.mjs +11 -36
  19. package/{core → lib}/_helpers.d.ts +0 -0
  20. package/lib/arrays.d.ts +59 -0
  21. package/lib/arrays.js +139 -0
  22. package/lib/arrays.js.flow +138 -0
  23. package/lib/arrays.mjs +124 -0
  24. package/lib/basics.d.ts +93 -0
  25. package/lib/basics.js +144 -0
  26. package/lib/basics.js.flow +124 -0
  27. package/lib/basics.mjs +120 -0
  28. package/lib/booleans.d.ts +16 -0
  29. package/lib/booleans.js +35 -0
  30. package/lib/booleans.js.flow +22 -0
  31. package/lib/booleans.mjs +25 -0
  32. package/lib/dates.d.ts +15 -0
  33. package/lib/dates.js +44 -0
  34. package/lib/dates.js.flow +40 -0
  35. package/lib/dates.mjs +34 -0
  36. package/lib/json.d.ts +35 -0
  37. package/lib/json.js +55 -0
  38. package/lib/json.js.flow +50 -0
  39. package/lib/json.mjs +40 -0
  40. package/lib/numbers.d.ts +31 -0
  41. package/lib/numbers.js +51 -0
  42. package/lib/numbers.js.flow +48 -0
  43. package/lib/numbers.mjs +41 -0
  44. package/lib/objects.d.ts +75 -0
  45. package/{core/object.js → lib/objects.js} +78 -85
  46. package/{core/object.js.flow → lib/objects.js.flow} +89 -102
  47. package/{core/object.mjs → lib/objects.mjs} +77 -82
  48. package/lib/strings.d.ts +56 -0
  49. package/lib/strings.js +101 -0
  50. package/lib/strings.js.flow +90 -0
  51. package/lib/strings.mjs +82 -0
  52. package/lib/unions.d.ts +55 -0
  53. package/lib/unions.js +160 -0
  54. package/{core/either.js.flow → lib/unions.js.flow} +67 -17
  55. package/lib/unions.mjs +146 -0
  56. package/lib/utilities.d.ts +34 -0
  57. package/lib/utilities.js +75 -0
  58. package/lib/utilities.js.flow +65 -0
  59. package/lib/utilities.mjs +60 -0
  60. package/package.json +64 -17
  61. package/result.d.ts +0 -23
  62. package/result.js +0 -68
  63. package/result.js.flow +0 -72
  64. package/result.mjs +0 -54
  65. package/_guard.d.ts +0 -7
  66. package/_guard.js +0 -22
  67. package/_guard.js.flow +0 -20
  68. package/_guard.mjs +0 -15
  69. package/_types.d.ts +0 -13
  70. package/_types.js +0 -1
  71. package/_types.js.flow +0 -20
  72. package/_types.mjs +0 -0
  73. package/core/array.d.ts +0 -8
  74. package/core/array.js +0 -115
  75. package/core/array.js.flow +0 -107
  76. package/core/array.mjs +0 -100
  77. package/core/boolean.d.ts +0 -5
  78. package/core/boolean.js +0 -40
  79. package/core/boolean.js.flow +0 -27
  80. package/core/boolean.mjs +0 -28
  81. package/core/composition.d.ts +0 -18
  82. package/core/composition.js +0 -82
  83. package/core/composition.js.flow +0 -74
  84. package/core/composition.mjs +0 -70
  85. package/core/constants.d.ts +0 -11
  86. package/core/constants.js +0 -65
  87. package/core/constants.js.flow +0 -44
  88. package/core/constants.mjs +0 -46
  89. package/core/date.d.ts +0 -4
  90. package/core/date.js +0 -42
  91. package/core/date.js.flow +0 -38
  92. package/core/date.mjs +0 -28
  93. package/core/describe.d.ts +0 -3
  94. package/core/describe.js +0 -22
  95. package/core/describe.js.flow +0 -17
  96. package/core/describe.mjs +0 -16
  97. package/core/dispatch.d.ts +0 -8
  98. package/core/dispatch.js +0 -60
  99. package/core/dispatch.js.flow +0 -59
  100. package/core/dispatch.mjs +0 -52
  101. package/core/either.d.ts +0 -66
  102. package/core/either.js +0 -101
  103. package/core/either.mjs +0 -90
  104. package/core/fail.d.ts +0 -3
  105. package/core/fail.js +0 -17
  106. package/core/fail.js.flow +0 -12
  107. package/core/fail.mjs +0 -11
  108. package/core/instanceOf.d.ts +0 -3
  109. package/core/instanceOf.js +0 -15
  110. package/core/instanceOf.js.flow +0 -20
  111. package/core/instanceOf.mjs +0 -8
  112. package/core/json.d.ts +0 -11
  113. package/core/json.js +0 -31
  114. package/core/json.js.flow +0 -28
  115. package/core/json.mjs +0 -15
  116. package/core/lazy.d.ts +0 -3
  117. package/core/lazy.js +0 -16
  118. package/core/lazy.js.flow +0 -15
  119. package/core/lazy.mjs +0 -11
  120. package/core/number.d.ts +0 -6
  121. package/core/number.js +0 -36
  122. package/core/number.js.flow +0 -40
  123. package/core/number.mjs +0 -25
  124. package/core/object.d.ts +0 -38
  125. package/core/optional.d.ts +0 -5
  126. package/core/optional.js +0 -50
  127. package/core/optional.js.flow +0 -41
  128. package/core/optional.mjs +0 -38
  129. package/core/string.d.ts +0 -13
  130. package/core/string.js +0 -80
  131. package/core/string.js.flow +0 -72
  132. package/core/string.mjs +0 -58
  133. package/core/tuple.d.ts +0 -30
  134. package/core/tuple.js +0 -54
  135. package/core/tuple.js.flow +0 -51
  136. package/core/tuple.mjs +0 -45
package/lib/unions.mjs ADDED
@@ -0,0 +1,146 @@
1
+ import { define } from '../Decoder.mjs';
2
+ import { indent, summarize } from '../_utils.mjs';
3
+ import { object } from './objects.mjs';
4
+ import { prep } from './utilities.mjs';
5
+ var EITHER_PREFIX = 'Either:\n';
6
+ /**
7
+ * Indents and adds a dash in front of this (potentially multiline) string.
8
+ */
9
+
10
+ function itemize(s) {
11
+ return '-' + indent(s).substring(1);
12
+ }
13
+ /**
14
+ * Nests another error as an item under a new-to-be-created "Either error". If
15
+ * the given subitem already is an "Either error" of itself, don't indent, but
16
+ * just "inject" its items at the same error level, for nicely flattened either
17
+ * expressions.
18
+ *
19
+ * Avoids:
20
+ *
21
+ * Either:
22
+ * - Either:
23
+ * - Must be P
24
+ * - Either:
25
+ * - Must be Q
26
+ * - Must be R
27
+ * - Must be S
28
+ *
29
+ * And "flattens" these to:
30
+ *
31
+ * Either:
32
+ * - Must be P
33
+ * - Must be Q
34
+ * - Must be R
35
+ * - Must be S
36
+ *
37
+ */
38
+
39
+
40
+ function nest(errText) {
41
+ return errText.startsWith(EITHER_PREFIX) ? errText.substr(EITHER_PREFIX.length) : itemize(errText);
42
+ } // prettier-ignore
43
+
44
+
45
+ function _either() {
46
+ for (var _len = arguments.length, decoders = new Array(_len), _key = 0; _key < _len; _key++) {
47
+ decoders[_key] = arguments[_key];
48
+ }
49
+
50
+ if (decoders.length === 0) {
51
+ throw new Error('Pass at least one decoder to either()');
52
+ }
53
+
54
+ return define(function (blob, _, err) {
55
+ // Collect errors here along the way
56
+ var errors = [];
57
+
58
+ for (var _i = 0; _i < decoders.length; _i++) {
59
+ var result = decoders[_i].decode(blob);
60
+
61
+ if (result.ok) {
62
+ return result;
63
+ } else {
64
+ errors.push(result.error);
65
+ }
66
+ } // Decoding all alternatives failed, return the combined error message
67
+
68
+
69
+ var text = EITHER_PREFIX + errors.map(function (err) {
70
+ return nest(summarize(err).join('\n'));
71
+ }).join('\n');
72
+ return err(text);
73
+ });
74
+ }
75
+ /**
76
+ * Accepts values accepted by any of the given decoders.
77
+ *
78
+ * The decoders are tried on the input one by one, in the given order. The
79
+ * first one that accepts the input "wins". If all decoders reject the input,
80
+ * the input gets rejected.
81
+ */
82
+
83
+
84
+ export var either = _either;
85
+ /**
86
+ * Accepts any value that is strictly-equal (using `===`) to one of the
87
+ * specified values.
88
+ */
89
+
90
+ export function oneOf(constants) {
91
+ return define(function (blob, ok, err) {
92
+ var winner = constants.find(function (c) {
93
+ return c === blob;
94
+ });
95
+
96
+ if (winner !== undefined) {
97
+ return ok(winner);
98
+ }
99
+
100
+ return err("Must be one of " + constants.map(function (value) {
101
+ return JSON.stringify(value);
102
+ }).join(', '));
103
+ });
104
+ }
105
+ /**
106
+ * If you are decoding tagged unions you may want to use the `taggedUnion()`
107
+ * decoder instead of the general purpose `either()` decoder to get better
108
+ * error messages and better performance.
109
+ *
110
+ * This decoder is optimized for [tagged
111
+ * unions](https://en.wikipedia.org/wiki/Tagged_union), i.e. a union of
112
+ * objects where one field is used as the discriminator.
113
+ *
114
+ * ```ts
115
+ * const A = object({ tag: constant('A'), foo: string });
116
+ * const B = object({ tag: constant('B'), bar: number });
117
+ *
118
+ * const AorB = taggedUnion('tag', { A, B });
119
+ * // ^^^
120
+ * ```
121
+ *
122
+ * Decoding now works in two steps:
123
+ *
124
+ * 1. Look at the `'tag'` field in the incoming object (this is the field
125
+ * that decides which decoder will be used)
126
+ * 2. If the value is `'A'`, then decoder `A` will be used. If it's `'B'`, then
127
+ * decoder `B` will be used. Otherwise, this will fail.
128
+ *
129
+ * This is effectively equivalent to `either(A, B)`, but will provide better
130
+ * error messages and is more performant at runtime because it doesn't have to
131
+ * try all decoders one by one.
132
+ */
133
+
134
+ export function taggedUnion(field, mapping) {
135
+ var _object;
136
+
137
+ var base = object((_object = {}, _object[field] = prep(String, oneOf(Object.keys(mapping))), _object)).transform(function (o) {
138
+ return o[field];
139
+ });
140
+ return base.peek_UNSTABLE(function (_ref) {
141
+ var blob = _ref[0],
142
+ key = _ref[1];
143
+ var decoder = mapping[key];
144
+ return decoder.decode(blob);
145
+ });
146
+ }
@@ -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>;
@@ -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-beta9",
3
+ "version": "2.0.0",
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
- "guard",
21
- "object",
19
+ "Decoder",
20
+ "always",
21
+ "and",
22
22
  "array",
23
- "string",
24
- "email",
25
- "regex",
26
- "number",
27
- "integer",
28
23
  "boolean",
29
- "truthy",
24
+ "then",
25
+ "compose",
26
+ "constant",
30
27
  "date",
31
- "mapping",
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
- "optional",
37
- "nullable",
38
- "maybe",
39
- "compose",
40
- "map",
41
- "predicate"
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
@@ -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,15 +1,8 @@
1
1
  "use strict";
2
2
 
3
3
  exports.__esModule = true;
4
- exports.andThen = andThen;
5
- exports.dispatch = dispatch;
6
4
  exports.err = err;
7
- exports.expect = expect;
8
- exports.mapError = mapError;
9
- exports.mapOk = mapOk;
10
5
  exports.ok = ok;
11
- exports.orElse = orElse;
12
- exports.unwrap = unwrap;
13
6
 
14
7
  /**
15
8
  * Result <value> <error>
@@ -38,65 +31,4 @@ function err(error) {
38
31
  value: undefined,
39
32
  error: error
40
33
  };
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.ok) {
50
- return result.value;
51
- } else {
52
- throw result.error;
53
- }
54
- }
55
-
56
- function expect(result, message) {
57
- if (result.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.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.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.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.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.ok ? result : err(mapper(result.error));
102
34
  }
package/result.js.flow CHANGED
@@ -24,75 +24,3 @@ export function ok<T>(value: T): Ok<T> {
24
24
  export function err<E>(error: E): Err<E> {
25
25
  return { ok: false, value: undefined, error };
26
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.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.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.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.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.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.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.ok ? result : err(mapper(result.error));
98
- }