decoders 2.0.0-beta1 → 2.0.0-beta5

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 +4 -0
  2. package/{es → _esm}/_guard.js +0 -0
  3. package/{cjs → _esm}/_guard.js.flow +0 -0
  4. package/{es → _esm}/_types.js +0 -0
  5. package/{cjs → _esm}/_types.js.flow +0 -0
  6. package/{es → _esm}/_utils.js +0 -0
  7. package/{cjs → _esm}/_utils.js.flow +0 -0
  8. package/{es → _esm}/annotate.js +0 -0
  9. package/{cjs → _esm}/annotate.js.flow +0 -0
  10. package/{es/stdlib → _esm/core}/array.js +0 -0
  11. package/{cjs/stdlib → _esm/core}/array.js.flow +0 -0
  12. package/{es/stdlib → _esm/core}/boolean.js +0 -0
  13. package/{cjs/stdlib → _esm/core}/boolean.js.flow +0 -0
  14. package/{es/stdlib → _esm/core}/composition.js +0 -0
  15. package/{cjs/stdlib → _esm/core}/composition.js.flow +0 -0
  16. package/{es/stdlib → _esm/core}/constants.js +0 -0
  17. package/{cjs/stdlib → _esm/core}/constants.js.flow +0 -0
  18. package/{es/stdlib → _esm/core}/date.js +0 -0
  19. package/{cjs/stdlib → _esm/core}/date.js.flow +0 -0
  20. package/{es/stdlib → _esm/core}/describe.js +0 -0
  21. package/{cjs/stdlib → _esm/core}/describe.js.flow +0 -0
  22. package/{es/stdlib → _esm/core}/dispatch.js +0 -0
  23. package/{cjs/stdlib → _esm/core}/dispatch.js.flow +0 -0
  24. package/{es/stdlib → _esm/core}/either.js +0 -0
  25. package/{cjs/stdlib → _esm/core}/either.js.flow +0 -0
  26. package/{es/stdlib → _esm/core}/fail.js +0 -0
  27. package/{cjs/stdlib → _esm/core}/fail.js.flow +0 -0
  28. package/{es/stdlib → _esm/core}/instanceOf.js +0 -0
  29. package/{cjs/stdlib → _esm/core}/instanceOf.js.flow +0 -0
  30. package/{es/stdlib → _esm/core}/json.js +0 -0
  31. package/{cjs/stdlib → _esm/core}/json.js.flow +0 -0
  32. package/{es/stdlib → _esm/core}/lazy.js +0 -0
  33. package/{cjs/stdlib → _esm/core}/lazy.js.flow +0 -0
  34. package/{es/stdlib → _esm/core}/mapping.js +0 -0
  35. package/{cjs/stdlib → _esm/core}/mapping.js.flow +0 -0
  36. package/{es/stdlib → _esm/core}/number.js +0 -0
  37. package/{cjs/stdlib → _esm/core}/number.js.flow +0 -0
  38. package/{es/stdlib → _esm/core}/object.js +0 -0
  39. package/{cjs/stdlib → _esm/core}/object.js.flow +0 -0
  40. package/{es/stdlib → _esm/core}/optional.js +0 -0
  41. package/{cjs/stdlib → _esm/core}/optional.js.flow +0 -0
  42. package/{es/stdlib → _esm/core}/string.js +0 -0
  43. package/{cjs/stdlib → _esm/core}/string.js.flow +0 -0
  44. package/{es/stdlib → _esm/core}/tuple.js +7 -7
  45. package/{cjs/stdlib → _esm/core}/tuple.js.flow +21 -26
  46. package/{es → _esm}/format/index.js +0 -0
  47. package/{cjs → _esm}/format/index.js.flow +0 -0
  48. package/{es → _esm}/format/inline.js +0 -0
  49. package/{cjs → _esm}/format/inline.js.flow +0 -0
  50. package/{es → _esm}/format/short.js +0 -0
  51. package/{cjs → _esm}/format/short.js.flow +0 -0
  52. package/{es → _esm}/index.js +18 -18
  53. package/{cjs → _esm}/index.js.flow +29 -29
  54. package/{es → _esm}/result.js +14 -5
  55. package/{cjs → _esm}/result.js.flow +14 -6
  56. package/{cjs/_guard.js → _guard.js} +0 -0
  57. package/_guard.js.flow +20 -0
  58. package/{cjs/_types.js → _types.js} +0 -0
  59. package/_types.js.flow +20 -0
  60. package/{cjs/_utils.js → _utils.js} +0 -0
  61. package/_utils.js.flow +97 -0
  62. package/{cjs/annotate.js → annotate.js} +0 -0
  63. package/annotate.js.flow +218 -0
  64. package/{cjs/stdlib → core}/array.js +0 -0
  65. package/core/array.js.flow +103 -0
  66. package/{cjs/stdlib → core}/boolean.js +0 -0
  67. package/core/boolean.js.flow +29 -0
  68. package/{cjs/stdlib → core}/composition.js +0 -0
  69. package/core/composition.js.flow +43 -0
  70. package/{cjs/stdlib → core}/constants.js +0 -0
  71. package/core/constants.js.flow +46 -0
  72. package/{cjs/stdlib → core}/date.js +0 -0
  73. package/core/date.js.flow +40 -0
  74. package/{cjs/stdlib → core}/describe.js +0 -0
  75. package/core/describe.js.flow +17 -0
  76. package/{cjs/stdlib → core}/dispatch.js +0 -0
  77. package/core/dispatch.js.flow +58 -0
  78. package/{cjs/stdlib → core}/either.js +0 -0
  79. package/core/either.js.flow +151 -0
  80. package/{cjs/stdlib → core}/fail.js +0 -0
  81. package/core/fail.js.flow +12 -0
  82. package/{cjs/stdlib → core}/instanceOf.js +0 -0
  83. package/core/instanceOf.js.flow +20 -0
  84. package/{cjs/stdlib → core}/json.js +0 -0
  85. package/core/json.js.flow +28 -0
  86. package/{cjs/stdlib → core}/lazy.js +0 -0
  87. package/core/lazy.js.flow +15 -0
  88. package/{cjs/stdlib → core}/mapping.js +0 -0
  89. package/core/mapping.js.flow +54 -0
  90. package/{cjs/stdlib → core}/number.js +0 -0
  91. package/core/number.js.flow +34 -0
  92. package/{cjs/stdlib → core}/object.js +0 -0
  93. package/core/object.js.flow +203 -0
  94. package/{cjs/stdlib → core}/optional.js +0 -0
  95. package/core/optional.js.flow +41 -0
  96. package/{cjs/stdlib → core}/string.js +0 -0
  97. package/core/string.js.flow +82 -0
  98. package/{cjs/stdlib → core}/tuple.js +6 -6
  99. package/core/tuple.js.flow +215 -0
  100. package/{cjs/format → format}/index.js +0 -0
  101. package/format/index.js.flow +4 -0
  102. package/{cjs/format → format}/inline.js +0 -0
  103. package/format/inline.js.flow +122 -0
  104. package/{cjs/format → format}/short.js +0 -0
  105. package/format/short.js.flow +8 -0
  106. package/{cjs/index.js → index.js} +18 -18
  107. package/index.js.flow +63 -0
  108. package/package.json +3 -12
  109. package/{cjs/result.js → result.js} +18 -7
  110. package/result.js.flow +174 -0
  111. package/ts/_guard.d.ts +0 -7
  112. package/ts/_helpers.d.ts +0 -79
  113. package/ts/_types.d.ts +0 -16
  114. package/ts/_utils.d.ts +0 -13
  115. package/ts/annotate.d.ts +0 -58
  116. package/ts/array.d.ts +0 -5
  117. package/ts/boolean.d.ts +0 -5
  118. package/ts/constants.d.ts +0 -11
  119. package/ts/date.d.ts +0 -4
  120. package/ts/describe.d.ts +0 -3
  121. package/ts/dispatch.d.ts +0 -8
  122. package/ts/either.d.ts +0 -61
  123. package/ts/fail.d.ts +0 -3
  124. package/ts/index.d.ts +0 -42
  125. package/ts/inline.d.ts +0 -3
  126. package/ts/instanceOf.d.ts +0 -3
  127. package/ts/json.d.ts +0 -11
  128. package/ts/lazy.d.ts +0 -3
  129. package/ts/mapping.d.ts +0 -4
  130. package/ts/number.d.ts +0 -6
  131. package/ts/object.d.ts +0 -33
  132. package/ts/optional.d.ts +0 -5
  133. package/ts/result.d.ts +0 -39
  134. package/ts/short.d.ts +0 -3
  135. package/ts/string.d.ts +0 -7
  136. package/ts/tuple.d.ts +0 -30
package/CHANGELOG.md CHANGED
@@ -1,5 +1,9 @@
1
1
  ## v2.0.0-beta
2
2
 
3
+ Upgrading to v2 _can_, but doesn't _have_ to be a breaking change for you. If upgrading
4
+ causes errors for you, please see the [migration guide](./MIGRATING-v2.md) for
5
+ instructions.
6
+
3
7
  Potentially breaking changes:
4
8
 
5
9
  - Drop support for all Node versions below 12.x
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
@@ -1,6 +1,6 @@
1
1
  import { annotate } from '../annotate';
2
2
  import { compose, predicate } from './composition';
3
- import { err, errValue, isErr, ok, unwrap, value } from '../result';
3
+ import { err, ok, okOrErrValue, unwrap } from '../result';
4
4
  import { poja } from './array';
5
5
 
6
6
  var ntuple = function ntuple(n) {
@@ -24,7 +24,7 @@ export function tuple1(decoder1) {
24
24
  } catch (e) {
25
25
  // If a decoder error has happened while unwrapping all the
26
26
  // results, try to construct a good error message
27
- return err(annotate(errValue(result1)));
27
+ return err(annotate([okOrErrValue(result1)]));
28
28
  }
29
29
  });
30
30
  }
@@ -45,7 +45,7 @@ export function tuple2(decoder1, decoder2) {
45
45
  } catch (e) {
46
46
  // If a decoder error has happened while unwrapping all the
47
47
  // results, try to construct a good error message
48
- return err(annotate([isErr(result1) ? errValue(result1) : value(result1), isErr(result2) ? errValue(result2) : value(result2)]));
48
+ return err(annotate([okOrErrValue(result1), okOrErrValue(result2)]));
49
49
  }
50
50
  });
51
51
  }
@@ -68,7 +68,7 @@ export function tuple3(decoder1, decoder2, decoder3) {
68
68
  } catch (e) {
69
69
  // If a decoder error has happened while unwrapping all the
70
70
  // results, try to construct a good error message
71
- return err(annotate([isErr(result1) ? errValue(result1) : value(result1), isErr(result2) ? errValue(result2) : value(result2), isErr(result3) ? errValue(result3) : value(result3)]));
71
+ return err(annotate([okOrErrValue(result1), okOrErrValue(result2), okOrErrValue(result3)]));
72
72
  }
73
73
  });
74
74
  }
@@ -93,7 +93,7 @@ export function tuple4(decoder1, decoder2, decoder3, decoder4) {
93
93
  } catch (e) {
94
94
  // If a decoder error has happened while unwrapping all the
95
95
  // results, try to construct a good error message
96
- return err(annotate([isErr(result1) ? errValue(result1) : value(result1), isErr(result2) ? errValue(result2) : value(result2), isErr(result3) ? errValue(result3) : value(result3), isErr(result4) ? errValue(result4) : value(result4)]));
96
+ return err(annotate([okOrErrValue(result1), okOrErrValue(result2), okOrErrValue(result3), okOrErrValue(result4)]));
97
97
  }
98
98
  });
99
99
  }
@@ -120,7 +120,7 @@ export function tuple5(decoder1, decoder2, decoder3, decoder4, decoder5) {
120
120
  } catch (e) {
121
121
  // If a decoder error has happened while unwrapping all the
122
122
  // results, try to construct a good error message
123
- return err(annotate([isErr(result1) ? errValue(result1) : value(result1), isErr(result2) ? errValue(result2) : value(result2), isErr(result3) ? errValue(result3) : value(result3), isErr(result4) ? errValue(result4) : value(result4), isErr(result5) ? errValue(result5) : value(result5)]));
123
+ return err(annotate([okOrErrValue(result1), okOrErrValue(result2), okOrErrValue(result3), okOrErrValue(result4), okOrErrValue(result5)]));
124
124
  }
125
125
  });
126
126
  }
@@ -149,7 +149,7 @@ export function tuple6(decoder1, decoder2, decoder3, decoder4, decoder5, decoder
149
149
  } catch (e) {
150
150
  // If a decoder error has happened while unwrapping all the
151
151
  // results, try to construct a good error message
152
- return err(annotate([isErr(result1) ? errValue(result1) : value(result1), isErr(result2) ? errValue(result2) : value(result2), isErr(result3) ? errValue(result3) : value(result3), isErr(result4) ? errValue(result4) : value(result4), isErr(result5) ? errValue(result5) : value(result5), isErr(result6) ? errValue(result6) : value(result6)]));
152
+ return err(annotate([okOrErrValue(result1), okOrErrValue(result2), okOrErrValue(result3), okOrErrValue(result4), okOrErrValue(result5), okOrErrValue(result6)]));
153
153
  }
154
154
  });
155
155
  }
@@ -2,7 +2,7 @@
2
2
 
3
3
  import { annotate } from '../annotate';
4
4
  import { compose, predicate } from './composition';
5
- import { err, errValue, isErr, ok, unwrap, value } from '../result';
5
+ import { err, ok, okOrErrValue, unwrap } from '../result';
6
6
  import { poja } from './array';
7
7
  import type { Decoder } from '../_types';
8
8
 
@@ -26,7 +26,7 @@ export function tuple1<T>(decoder1: Decoder<T>): Decoder<[T]> {
26
26
  } catch (e) {
27
27
  // If a decoder error has happened while unwrapping all the
28
28
  // results, try to construct a good error message
29
- return err(annotate(errValue(result1)));
29
+ return err(annotate([okOrErrValue(result1)]));
30
30
  }
31
31
  });
32
32
  }
@@ -49,12 +49,7 @@ export function tuple2<T1, T2>(
49
49
  } catch (e) {
50
50
  // If a decoder error has happened while unwrapping all the
51
51
  // results, try to construct a good error message
52
- return err(
53
- annotate([
54
- isErr(result1) ? errValue(result1) : value(result1),
55
- isErr(result2) ? errValue(result2) : value(result2),
56
- ]),
57
- );
52
+ return err(annotate([okOrErrValue(result1), okOrErrValue(result2)]));
58
53
  }
59
54
  });
60
55
  }
@@ -81,9 +76,9 @@ export function tuple3<T1, T2, T3>(
81
76
  // results, try to construct a good error message
82
77
  return err(
83
78
  annotate([
84
- isErr(result1) ? errValue(result1) : value(result1),
85
- isErr(result2) ? errValue(result2) : value(result2),
86
- isErr(result3) ? errValue(result3) : value(result3),
79
+ okOrErrValue(result1),
80
+ okOrErrValue(result2),
81
+ okOrErrValue(result3),
87
82
  ]),
88
83
  );
89
84
  }
@@ -119,10 +114,10 @@ export function tuple4<T1, T2, T3, T4>(
119
114
  // results, try to construct a good error message
120
115
  return err(
121
116
  annotate([
122
- isErr(result1) ? errValue(result1) : value(result1),
123
- isErr(result2) ? errValue(result2) : value(result2),
124
- isErr(result3) ? errValue(result3) : value(result3),
125
- isErr(result4) ? errValue(result4) : value(result4),
117
+ okOrErrValue(result1),
118
+ okOrErrValue(result2),
119
+ okOrErrValue(result3),
120
+ okOrErrValue(result4),
126
121
  ]),
127
122
  );
128
123
  }
@@ -161,11 +156,11 @@ export function tuple5<T1, T2, T3, T4, T5>(
161
156
  // results, try to construct a good error message
162
157
  return err(
163
158
  annotate([
164
- isErr(result1) ? errValue(result1) : value(result1),
165
- isErr(result2) ? errValue(result2) : value(result2),
166
- isErr(result3) ? errValue(result3) : value(result3),
167
- isErr(result4) ? errValue(result4) : value(result4),
168
- isErr(result5) ? errValue(result5) : value(result5),
159
+ okOrErrValue(result1),
160
+ okOrErrValue(result2),
161
+ okOrErrValue(result3),
162
+ okOrErrValue(result4),
163
+ okOrErrValue(result5),
169
164
  ]),
170
165
  );
171
166
  }
@@ -207,12 +202,12 @@ export function tuple6<T1, T2, T3, T4, T5, T6>(
207
202
  // results, try to construct a good error message
208
203
  return err(
209
204
  annotate([
210
- isErr(result1) ? errValue(result1) : value(result1),
211
- isErr(result2) ? errValue(result2) : value(result2),
212
- isErr(result3) ? errValue(result3) : value(result3),
213
- isErr(result4) ? errValue(result4) : value(result4),
214
- isErr(result5) ? errValue(result5) : value(result5),
215
- isErr(result6) ? errValue(result6) : value(result6),
205
+ okOrErrValue(result1),
206
+ okOrErrValue(result2),
207
+ okOrErrValue(result3),
208
+ okOrErrValue(result4),
209
+ okOrErrValue(result5),
210
+ okOrErrValue(result6),
216
211
  ]),
217
212
  );
218
213
  }
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
@@ -17,21 +17,21 @@
17
17
  *
18
18
  */
19
19
  export { guard } from './_guard';
20
- export { compose, map, predicate } from './stdlib/composition';
21
- export { array, nonEmptyArray, poja } from './stdlib/array';
22
- export { boolean, numericBoolean, truthy } from './stdlib/boolean';
23
- export { constant, hardcoded, mixed, null_, undefined_, unknown } from './stdlib/constants';
24
- export { date, iso8601 } from './stdlib/date';
25
- export { describe } from './stdlib/describe';
26
- export { dispatch } from './stdlib/dispatch';
27
- export { either, either3, either4, either5, either6, either7, either8, either9, oneOf } from './stdlib/either';
28
- export { fail } from './stdlib/fail';
29
- export { instanceOf } from './stdlib/instanceOf';
30
- export { json, jsonObject, jsonArray } from './stdlib/json';
31
- export { lazy } from './stdlib/lazy';
32
- export { mapping, dict } from './stdlib/mapping';
33
- export { integer, number, positiveInteger, positiveNumber } from './stdlib/number';
34
- export { exact, inexact, object, pojo } from './stdlib/object';
35
- export { maybe, nullable, optional } from './stdlib/optional';
36
- export { email, nonEmptyString, regex, string, url } from './stdlib/string';
37
- export { tuple1, tuple2, tuple3, tuple4, tuple5, tuple6 } from './stdlib/tuple';
20
+ export { compose, map, predicate } from './core/composition';
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 { dispatch } from './core/dispatch';
27
+ export { either, either3, either4, either5, either6, either7, either8, either9, oneOf } from './core/either';
28
+ export { fail } from './core/fail';
29
+ export { instanceOf } from './core/instanceOf';
30
+ export { json, jsonObject, jsonArray } from './core/json';
31
+ export { lazy } from './core/lazy';
32
+ export { mapping, dict } from './core/mapping';
33
+ export { integer, number, positiveInteger, positiveNumber } from './core/number';
34
+ export { exact, inexact, object, pojo } from './core/object';
35
+ export { maybe, nullable, optional } from './core/optional';
36
+ export { email, nonEmptyString, regex, string, url } from './core/string';
37
+ export { tuple1, tuple2, tuple3, tuple4, tuple5, tuple6 } from './core/tuple';
@@ -18,28 +18,28 @@
18
18
  * type. In our case, it's fine to fail with a runtime error.
19
19
  *
20
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';
21
+ export type {
22
+ Decoder,
23
+ DecodeResult,
24
+ DecoderType,
25
+ Guard,
26
+ GuardType,
27
+ Predicate,
28
+ Scalar,
29
+ } from './_types';
30
+ export type { Result } from './result';
31
+ export type { JSONValue, JSONObject, JSONArray } from './core/json';
25
32
 
26
33
  export { guard } from './_guard';
27
34
 
28
- export { compose, map, predicate } from './stdlib/composition';
35
+ export { compose, map, predicate } from './core/composition';
29
36
 
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';
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
43
  export {
44
44
  either,
45
45
  either3,
@@ -50,14 +50,14 @@ export {
50
50
  either8,
51
51
  either9,
52
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';
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';
@@ -10,7 +10,8 @@
10
10
  export function ok(value) {
11
11
  return {
12
12
  type: 'ok',
13
- value: value
13
+ value: value,
14
+ error: undefined
14
15
  };
15
16
  }
16
17
  /**
@@ -20,6 +21,7 @@ export function ok(value) {
20
21
  export function err(error) {
21
22
  return {
22
23
  type: 'err',
24
+ value: undefined,
23
25
  error: error
24
26
  };
25
27
  }
@@ -34,13 +36,20 @@ export function isErr(result) {
34
36
  }
35
37
  export function withDefault(result, defaultValue) {
36
38
  return result.type === 'ok' ? result.value : defaultValue;
37
- }
38
- export function value(result) {
39
+ } // TODO: Remove this from the public API? The same can be achieved now with
40
+ // TODO: const { value } = result;
41
+
42
+ export function okValue(result) {
39
43
  return result.type === 'ok' ? result.value : undefined;
40
- }
44
+ } // TODO: Remove this from the public API? The same can be achieved now with
45
+ // TODO: const { error } = result;
46
+
41
47
  export function errValue(result) {
42
48
  return result.type === 'err' ? result.error : undefined;
43
49
  }
50
+ export function okOrErrValue(result) {
51
+ return result.type === 'ok' ? result.value : result.error;
52
+ }
44
53
  /**
45
54
  * Unwrap the value from this Result instance if this is an "Ok" result.
46
55
  * Otherwise, will throw the "Err" error via a runtime exception.
@@ -127,7 +136,7 @@ export function orElse(result1, lazyResult2) {
127
136
  * Transform an Ok result. Will not touch Err results.
128
137
  */
129
138
 
130
- export function map(result, mapper) {
139
+ export function mapOk(result, mapper) {
131
140
  return result.type === 'ok' ? ok(mapper(result.value)) : result;
132
141
  }
133
142
  /**
@@ -6,8 +6,8 @@
6
6
  * | Err <error>
7
7
  */
8
8
 
9
- type Ok<+T> = {| +type: 'ok', +value: T |};
10
- type Err<+E> = {| +type: 'err', +error: E |};
9
+ type Ok<+T> = {| +type: 'ok', +value: T, +error: void |};
10
+ type Err<+E> = {| +type: 'err', +value: void, +error: E |};
11
11
 
12
12
  export type Result<+T, +E> = Ok<T> | Err<E>;
13
13
 
@@ -15,14 +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 { type: 'ok', value };
18
+ return { type: 'ok', 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 { type: 'err', error };
25
+ return { type: 'err', value: undefined, error };
26
26
  }
27
27
 
28
28
  export function toString(result: Result<mixed, mixed>): string {
@@ -43,14 +43,22 @@ export function withDefault<T>(result: Result<T, mixed>, defaultValue: T): T {
43
43
  return result.type === 'ok' ? result.value : defaultValue;
44
44
  }
45
45
 
46
- export function value<T>(result: Result<T, mixed>): void | T {
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 {
47
49
  return result.type === 'ok' ? result.value : undefined;
48
50
  }
49
51
 
52
+ // TODO: Remove this from the public API? The same can be achieved now with
53
+ // TODO: const { error } = result;
50
54
  export function errValue<E>(result: Result<mixed, E>): void | E {
51
55
  return result.type === 'err' ? result.error : undefined;
52
56
  }
53
57
 
58
+ export function okOrErrValue<T, E>(result: Result<T, E>): T | E {
59
+ return result.type === 'ok' ? result.value : result.error;
60
+ }
61
+
54
62
  /**
55
63
  * Unwrap the value from this Result instance if this is an "Ok" result.
56
64
  * Otherwise, will throw the "Err" error via a runtime exception.
@@ -148,7 +156,7 @@ export function orElse<T, E, E2>(
148
156
  /**
149
157
  * Transform an Ok result. Will not touch Err results.
150
158
  */
151
- export function map<T, E, T2>(
159
+ export function mapOk<T, E, T2>(
152
160
  result: Result<T, E>,
153
161
  mapper: (value: T) => T2,
154
162
  ): Result<T2, E> {
File without changes
package/_guard.js.flow ADDED
@@ -0,0 +1,20 @@
1
+ // @flow strict
2
+
3
+ import * as Result from './result';
4
+ import { formatInline } from './format';
5
+ import type { Annotation } from './annotate';
6
+ import type { Decoder, Guard } from './_types';
7
+
8
+ export function guard<T>(
9
+ decoder: Decoder<T>,
10
+ formatter: (Annotation) => string = formatInline,
11
+ ): Guard<T> {
12
+ return (blob: mixed) =>
13
+ Result.unwrap(
14
+ Result.mapError(decoder(blob), (annotation) => {
15
+ const err = new Error('\n' + formatter(annotation));
16
+ err.name = 'Decoding error';
17
+ return err;
18
+ }),
19
+ );
20
+ }
File without changes
package/_types.js.flow ADDED
@@ -0,0 +1,20 @@
1
+ // @flow strict
2
+
3
+ import type { Annotation } from './annotate';
4
+ import type { Result } from './result';
5
+
6
+ export type Scalar = string | number | boolean | symbol | void | null;
7
+
8
+ export type Predicate<T> = (T) => boolean;
9
+ export type DecodeResult<T> = Result<T, Annotation>;
10
+
11
+ export type Decoder<T, F = mixed> = (F) => DecodeResult<T>;
12
+ export type Guard<T> = (mixed) => T;
13
+
14
+ /**
15
+ * A "type function" which informs Flow about how a type will be modified at runtime.
16
+ * Read this as "given a Guard of type T, I can produce a value of type T". This
17
+ * definition helps construct $ObjMap types.
18
+ */
19
+ export type DecoderType = <T>(Decoder<T>) => T;
20
+ export type GuardType = <T>(Guard<T>) => T;
File without changes