decoders 1.26.0-beta2 → 2.0.0-beta3

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 (153) hide show
  1. package/CHANGELOG.md +21 -5
  2. package/_esm/_guard.js +15 -0
  3. package/_esm/_guard.js.flow +20 -0
  4. package/_esm/_types.js +0 -0
  5. package/_esm/_types.js.flow +20 -0
  6. package/_esm/_utils.js +93 -0
  7. package/_esm/_utils.js.flow +97 -0
  8. package/_esm/annotate.js +144 -0
  9. package/_esm/annotate.js.flow +218 -0
  10. package/_esm/core/array.js +91 -0
  11. package/{cjs → _esm/core}/array.js.flow +22 -25
  12. package/_esm/core/boolean.js +28 -0
  13. package/{cjs → _esm/core}/boolean.js.flow +8 -7
  14. package/_esm/core/composition.js +42 -0
  15. package/{cjs/utils.js.flow → _esm/core/composition.js.flow} +7 -22
  16. package/_esm/core/constants.js +46 -0
  17. package/{cjs → _esm/core}/constants.js.flow +13 -12
  18. package/_esm/core/date.js +28 -0
  19. package/{cjs → _esm/core}/date.js.flow +9 -7
  20. package/{cjs → _esm/core}/describe.js +5 -11
  21. package/{cjs → _esm/core}/describe.js.flow +4 -4
  22. package/{cjs → _esm/core}/dispatch.js +8 -15
  23. package/{cjs → _esm/core}/dispatch.js.flow +6 -5
  24. package/_esm/core/either.js +90 -0
  25. package/{cjs → _esm/core}/either.js.flow +55 -35
  26. package/_esm/core/fail.js +11 -0
  27. package/_esm/core/fail.js.flow +12 -0
  28. package/_esm/core/instanceOf.js +8 -0
  29. package/{cjs → _esm/core}/instanceOf.js.flow +7 -8
  30. package/_esm/core/json.js +15 -0
  31. package/{cjs → _esm/core}/json.js.flow +4 -4
  32. package/_esm/core/lazy.js +11 -0
  33. package/{cjs → _esm/core}/lazy.js.flow +1 -1
  34. package/_esm/core/mapping.js +54 -0
  35. package/_esm/core/mapping.js.flow +54 -0
  36. package/_esm/core/number.js +25 -0
  37. package/{cjs → _esm/core}/number.js.flow +10 -11
  38. package/_esm/core/object.js +175 -0
  39. package/{cjs → _esm/core}/object.js.flow +44 -52
  40. package/_esm/core/optional.js +38 -0
  41. package/{cjs → _esm/core}/optional.js.flow +6 -7
  42. package/{cjs → _esm/core}/string.js +18 -35
  43. package/{cjs → _esm/core}/string.js.flow +13 -12
  44. package/_esm/core/tuple.js +155 -0
  45. package/{cjs → _esm/core}/tuple.js.flow +62 -63
  46. package/_esm/format/index.js +2 -0
  47. package/_esm/format/index.js.flow +4 -0
  48. package/_esm/format/inline.js +137 -0
  49. package/_esm/format/inline.js.flow +122 -0
  50. package/_esm/format/short.js +4 -0
  51. package/_esm/format/short.js.flow +8 -0
  52. package/_esm/index.js +37 -0
  53. package/{cjs → _esm}/index.js.flow +31 -24
  54. package/_esm/result.js +139 -0
  55. package/_esm/result.js.flow +166 -0
  56. package/_guard.js +26 -0
  57. package/_guard.js.flow +20 -0
  58. package/_types.js +1 -0
  59. package/_types.js.flow +20 -0
  60. package/_utils.js +108 -0
  61. package/_utils.js.flow +97 -0
  62. package/annotate.js +161 -0
  63. package/annotate.js.flow +218 -0
  64. package/core/array.js +108 -0
  65. package/core/array.js.flow +103 -0
  66. package/core/boolean.js +44 -0
  67. package/core/boolean.js.flow +29 -0
  68. package/core/composition.js +56 -0
  69. package/core/composition.js.flow +43 -0
  70. package/core/constants.js +69 -0
  71. package/core/constants.js.flow +46 -0
  72. package/core/date.js +46 -0
  73. package/core/date.js.flow +40 -0
  74. package/core/describe.js +26 -0
  75. package/core/describe.js.flow +17 -0
  76. package/core/dispatch.js +62 -0
  77. package/core/dispatch.js.flow +58 -0
  78. package/core/either.js +117 -0
  79. package/core/either.js.flow +151 -0
  80. package/core/fail.js +21 -0
  81. package/core/fail.js.flow +12 -0
  82. package/core/instanceOf.js +19 -0
  83. package/core/instanceOf.js.flow +20 -0
  84. package/{cjs → core}/json.js +3 -5
  85. package/core/json.js.flow +28 -0
  86. package/{cjs → core}/lazy.js +1 -3
  87. package/core/lazy.js.flow +15 -0
  88. package/core/mapping.js +67 -0
  89. package/core/mapping.js.flow +54 -0
  90. package/core/number.js +40 -0
  91. package/core/number.js.flow +34 -0
  92. package/core/object.js +194 -0
  93. package/core/object.js.flow +203 -0
  94. package/core/optional.js +54 -0
  95. package/core/optional.js.flow +41 -0
  96. package/core/string.js +98 -0
  97. package/core/string.js.flow +82 -0
  98. package/core/tuple.js +173 -0
  99. package/core/tuple.js.flow +220 -0
  100. package/format/index.js +12 -0
  101. package/format/index.js.flow +4 -0
  102. package/format/inline.js +146 -0
  103. package/format/inline.js.flow +122 -0
  104. package/format/short.js +10 -0
  105. package/format/short.js.flow +8 -0
  106. package/index.js +120 -0
  107. package/index.js.flow +63 -0
  108. package/package.json +3 -11
  109. package/result.js +172 -0
  110. package/result.js.flow +166 -0
  111. package/cjs/array.js +0 -133
  112. package/cjs/boolean.js +0 -42
  113. package/cjs/constants.js +0 -67
  114. package/cjs/date.js +0 -42
  115. package/cjs/either.js +0 -85
  116. package/cjs/fail.js +0 -19
  117. package/cjs/fail.js.flow +0 -13
  118. package/cjs/guard.js +0 -30
  119. package/cjs/guard.js.flow +0 -36
  120. package/cjs/index.js +0 -397
  121. package/cjs/instanceOf.js +0 -17
  122. package/cjs/mapping.js +0 -113
  123. package/cjs/mapping.js.flow +0 -71
  124. package/cjs/number.js +0 -38
  125. package/cjs/object.js +0 -254
  126. package/cjs/optional.js +0 -52
  127. package/cjs/tuple.js +0 -199
  128. package/cjs/types.js +0 -3
  129. package/cjs/types.js.flow +0 -26
  130. package/cjs/utils.js +0 -70
  131. package/es/index.js +0 -1039
  132. package/ts/array.d.ts +0 -5
  133. package/ts/boolean.d.ts +0 -5
  134. package/ts/constants.d.ts +0 -11
  135. package/ts/date.d.ts +0 -4
  136. package/ts/describe.d.ts +0 -3
  137. package/ts/dispatch.d.ts +0 -8
  138. package/ts/either.d.ts +0 -61
  139. package/ts/fail.d.ts +0 -3
  140. package/ts/guard.d.ts +0 -7
  141. package/ts/helpers.d.ts +0 -79
  142. package/ts/index.d.ts +0 -38
  143. package/ts/instanceOf.d.ts +0 -3
  144. package/ts/json.d.ts +0 -11
  145. package/ts/lazy.d.ts +0 -3
  146. package/ts/mapping.d.ts +0 -4
  147. package/ts/number.d.ts +0 -6
  148. package/ts/object.d.ts +0 -33
  149. package/ts/optional.d.ts +0 -5
  150. package/ts/string.d.ts +0 -7
  151. package/ts/tuple.d.ts +0 -30
  152. package/ts/types.d.ts +0 -18
  153. package/ts/utils.d.ts +0 -13
package/index.js ADDED
@@ -0,0 +1,120 @@
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("./core/composition");
11
+
12
+ exports.compose = _composition.compose;
13
+ exports.map = _composition.map;
14
+ exports.predicate = _composition.predicate;
15
+
16
+ var _array = require("./core/array");
17
+
18
+ exports.array = _array.array;
19
+ exports.nonEmptyArray = _array.nonEmptyArray;
20
+ exports.poja = _array.poja;
21
+
22
+ var _boolean = require("./core/boolean");
23
+
24
+ exports["boolean"] = _boolean["boolean"];
25
+ exports.numericBoolean = _boolean.numericBoolean;
26
+ exports.truthy = _boolean.truthy;
27
+
28
+ var _constants = require("./core/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("./core/date");
38
+
39
+ exports.date = _date.date;
40
+ exports.iso8601 = _date.iso8601;
41
+
42
+ var _describe = require("./core/describe");
43
+
44
+ exports.describe = _describe.describe;
45
+
46
+ var _dispatch = require("./core/dispatch");
47
+
48
+ exports.dispatch = _dispatch.dispatch;
49
+
50
+ var _either = require("./core/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("./core/fail");
63
+
64
+ exports.fail = _fail.fail;
65
+
66
+ var _instanceOf = require("./core/instanceOf");
67
+
68
+ exports.instanceOf = _instanceOf.instanceOf;
69
+
70
+ var _json = require("./core/json");
71
+
72
+ exports.json = _json.json;
73
+ exports.jsonObject = _json.jsonObject;
74
+ exports.jsonArray = _json.jsonArray;
75
+
76
+ var _lazy = require("./core/lazy");
77
+
78
+ exports.lazy = _lazy.lazy;
79
+
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
+ var _optional = require("./core/optional");
100
+
101
+ exports.maybe = _optional.maybe;
102
+ exports.nullable = _optional.nullable;
103
+ exports.optional = _optional.optional;
104
+
105
+ var _string = require("./core/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("./core/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/index.js.flow ADDED
@@ -0,0 +1,63 @@
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 {
22
+ Decoder,
23
+ DecodeResult,
24
+ DecoderType,
25
+ Guard,
26
+ GuardType,
27
+ Predicate,
28
+ Scalar,
29
+ } from './_types';
30
+
31
+ export type { JSONValue, JSONObject, JSONArray } from './core/json';
32
+
33
+ export { guard } from './_guard';
34
+
35
+ export { compose, map, predicate } from './core/composition';
36
+
37
+ export { array, nonEmptyArray, poja } from './core/array';
38
+ export { boolean, numericBoolean, truthy } from './core/boolean';
39
+ export { constant, hardcoded, mixed, null_, undefined_, unknown } from './core/constants';
40
+ export { date, iso8601 } from './core/date';
41
+ export { describe } from './core/describe';
42
+ export { dispatch } from './core/dispatch';
43
+ export {
44
+ either,
45
+ either3,
46
+ either4,
47
+ either5,
48
+ either6,
49
+ either7,
50
+ either8,
51
+ either9,
52
+ oneOf,
53
+ } from './core/either';
54
+ export { fail } from './core/fail';
55
+ export { instanceOf } from './core/instanceOf';
56
+ export { json, jsonObject, jsonArray } from './core/json';
57
+ 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
+ export { maybe, nullable, optional } from './core/optional';
62
+ export { email, nonEmptyString, regex, string, url } from './core/string';
63
+ export { tuple1, tuple2, tuple3, tuple4, tuple5, tuple6 } from './core/tuple';
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "decoders",
3
- "version": "1.26.0-beta2",
3
+ "version": "2.0.0-beta3",
4
4
  "description": "Elegant and battle-tested validation library for type-safe input data (for TypeScript and Flow)",
5
5
  "license": "MIT",
6
6
  "repository": {
@@ -12,8 +12,8 @@
12
12
  "bugs": {
13
13
  "url": "https://github.com/nvie/decoders/issues"
14
14
  },
15
- "main": "./cjs/index.js",
16
- "module": "./es/index.js",
15
+ "main": "./index.js",
16
+ "module": "./_esm/index.js",
17
17
  "keywords": [
18
18
  "decoder",
19
19
  "decoders",
@@ -40,14 +40,6 @@
40
40
  "map",
41
41
  "predicate"
42
42
  ],
43
- "peerDependencies": {
44
- "@babel/runtime": "^7.16.0"
45
- },
46
- "dependencies": {
47
- "debrief": "^1.6.0-beta2",
48
- "lemons": "^1.7.0-beta2"
49
- },
50
- "types": "./ts/index.d.ts",
51
43
  "githubUrl": "https://github.com/nvie/decoders",
52
44
  "sideEffects": false
53
45
  }
package/result.js ADDED
@@ -0,0 +1,172 @@
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/result.js.flow ADDED
@@ -0,0 +1,166 @@
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/array.js DELETED
@@ -1,133 +0,0 @@
1
- "use strict";
2
-
3
- Object.defineProperty(exports, "__esModule", {
4
- value: true
5
- });
6
- exports.array = array;
7
- exports.nonEmptyArray = nonEmptyArray;
8
- exports.poja = void 0;
9
-
10
- var _debrief = require("debrief");
11
-
12
- var _lemons = require("lemons");
13
-
14
- var _utils = require("./utils");
15
-
16
- function _toConsumableArray(arr) { return _arrayWithoutHoles(arr) || _iterableToArray(arr) || _unsupportedIterableToArray(arr) || _nonIterableSpread(); }
17
-
18
- function _nonIterableSpread() { throw new TypeError("Invalid attempt to spread non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); }
19
-
20
- function _iterableToArray(iter) { if (typeof Symbol !== "undefined" && iter[Symbol.iterator] != null || iter["@@iterator"] != null) return Array.from(iter); }
21
-
22
- function _arrayWithoutHoles(arr) { if (Array.isArray(arr)) return _arrayLikeToArray(arr); }
23
-
24
- function _createForOfIteratorHelper(o, allowArrayLike) { var it = typeof Symbol !== "undefined" && o[Symbol.iterator] || o["@@iterator"]; if (!it) { if (Array.isArray(o) || (it = _unsupportedIterableToArray(o)) || allowArrayLike && o && typeof o.length === "number") { if (it) o = it; var i = 0; var F = function F() {}; return { s: F, n: function n() { if (i >= o.length) return { done: true }; return { done: false, value: o[i++] }; }, e: function e(_e) { throw _e; }, f: F }; } throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); } var normalCompletion = true, didErr = false, err; return { s: function s() { it = it.call(o); }, n: function n() { var step = it.next(); normalCompletion = step.done; return step; }, e: function e(_e2) { didErr = true; err = _e2; }, f: function f() { try { if (!normalCompletion && it["return"] != null) it["return"](); } finally { if (didErr) throw err; } } }; }
25
-
26
- function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); }
27
-
28
- function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) { arr2[i] = arr[i]; } return arr2; }
29
-
30
- /**
31
- * Like a "Plain Old JavaScript Object", but for arrays: "Plain Old JavaScript
32
- * Array" ^_^
33
- */
34
- var poja = function poja(blob) {
35
- if (!Array.isArray(blob)) {
36
- return (0, _lemons.Err)((0, _debrief.annotate)(blob, 'Must be an array'));
37
- }
38
-
39
- return (0, _lemons.Ok)( // NOTE: Since Flow 0.98, Array.isArray() returns $ReadOnlyArray<mixed>
40
- // instead of Array<mixed>. For rationale, see
41
- // https://github.com/facebook/flow/issues/7684. In this case, we
42
- // don't want to output read-only types because it's up to the user of
43
- // decoders to determine what they want to do with the decoded output.
44
- // If they want to write items into the array, that's fine!
45
- // The fastest way to turn a read-only array into a normal array in
46
- // Javascript is to use .slice() on it, see this benchmark:
47
- // http://jsben.ch/lO6C5
48
- blob.slice());
49
- };
50
- /**
51
- * Given an iterable of Result instances, exhaust them all and return:
52
- * - An [index, err] tuple, indicating the (index of the) first Err instance
53
- * encountered; or
54
- * - a new Ok with an array of all unwrapped Ok'ed values
55
- */
56
-
57
-
58
- exports.poja = poja;
59
-
60
- function all(iterable, blobs) {
61
- var results = [];
62
- var index = 0;
63
-
64
- var _iterator = _createForOfIteratorHelper(iterable),
65
- _step;
66
-
67
- try {
68
- for (_iterator.s(); !(_step = _iterator.n()).done;) {
69
- var result = _step.value;
70
-
71
- try {
72
- var value = result.unwrap();
73
- results.push(value);
74
- } catch (ann) {
75
- // Rewrite the annotation to include the index information, and inject it into the original blob
76
- var clone = _toConsumableArray(blobs);
77
-
78
- clone.splice(index, 1, (0, _debrief.annotate)(ann, ann.annotation !== undefined ? "".concat(ann.annotation, " (at index ").concat(index, ")") : "index ".concat(index))); // const errValue = [];
79
- // if (index > 0) {
80
- // errValue.push('...'); // TODO: make special mark, not string!
81
- // }
82
- // errValue.push(
83
- // );
84
- // if (index < iterable.length - 1) {
85
- // errValue.push('...'); // TODO: make special mark, not string!
86
- // }
87
-
88
- return (0, _lemons.Err)((0, _debrief.annotate)(clone));
89
- }
90
-
91
- index++;
92
- }
93
- } catch (err) {
94
- _iterator.e(err);
95
- } finally {
96
- _iterator.f();
97
- }
98
-
99
- return (0, _lemons.Ok)(results);
100
- }
101
- /**
102
- * Given a T, builds a decoder that assumes an array input and returns an
103
- * Array<T>.
104
- */
105
-
106
-
107
- function members(decoder) {
108
- return function (blobs) {
109
- var results = blobs.map(decoder);
110
- var result = all(results, blobs);
111
- return result;
112
- };
113
- }
114
- /**
115
- * Builds a Decoder that returns Ok for values of `Array<T>`, given a Decoder
116
- * for `T`. Err otherwise.
117
- */
118
-
119
-
120
- function array(decoder) {
121
- return (0, _utils.compose)(poja, members(decoder));
122
- }
123
- /**
124
- * Builds a Decoder that returns Ok for values of `Array<T>`, but will reject
125
- * empty arrays.
126
- */
127
-
128
-
129
- function nonEmptyArray(decoder) {
130
- return (0, _utils.compose)(array(decoder), (0, _utils.predicate)(function (arr) {
131
- return arr.length > 0;
132
- }, 'Must be non-empty array'));
133
- }