decoders 2.0.0-beta4 → 2.0.0-beta8

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 (132) hide show
  1. package/CHANGELOG.md +10 -0
  2. package/NotSupportedTSVersion.d.ts +1 -0
  3. package/README.md +728 -343
  4. package/_guard.d.ts +7 -0
  5. package/_guard.js +2 -6
  6. package/_guard.js.flow +3 -3
  7. package/{_esm/_guard.js → _guard.mjs} +3 -3
  8. package/_types.d.ts +13 -0
  9. package/{_esm/_types.js → _types.mjs} +0 -0
  10. package/_utils.d.ts +10 -0
  11. package/{_esm/_utils.js → _utils.mjs} +0 -0
  12. package/annotate.d.ts +62 -0
  13. package/{_esm/annotate.js → annotate.mjs} +0 -0
  14. package/core/_helpers.d.ts +79 -0
  15. package/core/array.d.ts +5 -0
  16. package/core/array.js +7 -11
  17. package/core/array.js.flow +6 -9
  18. package/{_esm/core/array.js → core/array.mjs} +9 -9
  19. package/core/boolean.d.ts +5 -0
  20. package/core/boolean.js +4 -8
  21. package/core/boolean.js.flow +3 -5
  22. package/{_esm/core/boolean.js → core/boolean.mjs} +6 -6
  23. package/core/composition.d.ts +14 -0
  24. package/core/composition.js +9 -11
  25. package/core/composition.js.flow +13 -8
  26. package/{_esm/core/composition.js → core/composition.mjs} +10 -8
  27. package/core/constants.d.ts +11 -0
  28. package/core/constants.js +6 -10
  29. package/core/constants.js.flow +7 -9
  30. package/{_esm/core/constants.js → core/constants.mjs} +7 -7
  31. package/core/date.d.ts +4 -0
  32. package/core/date.js +3 -7
  33. package/core/date.js.flow +2 -4
  34. package/{_esm/core/date.js → core/date.mjs} +6 -6
  35. package/core/describe.d.ts +3 -0
  36. package/core/describe.js +2 -6
  37. package/core/describe.js.flow +2 -2
  38. package/{_esm/core/describe.js → core/describe.mjs} +3 -3
  39. package/core/dispatch.d.ts +8 -0
  40. package/core/dispatch.js +9 -13
  41. package/core/dispatch.js.flow +8 -8
  42. package/{_esm/core/dispatch.js → core/dispatch.mjs} +10 -10
  43. package/core/either.d.ts +61 -0
  44. package/core/either.js +7 -11
  45. package/core/either.js.flow +6 -6
  46. package/{_esm/core/either.js → core/either.mjs} +8 -8
  47. package/core/fail.d.ts +3 -0
  48. package/core/fail.js +2 -6
  49. package/core/fail.js.flow +2 -2
  50. package/{_esm/core/fail.js → core/fail.mjs} +3 -3
  51. package/core/instanceOf.d.ts +3 -0
  52. package/core/instanceOf.js +2 -6
  53. package/core/instanceOf.js.flow +3 -3
  54. package/core/instanceOf.mjs +8 -0
  55. package/core/json.d.ts +11 -0
  56. package/core/json.mjs +15 -0
  57. package/core/lazy.d.ts +3 -0
  58. package/{_esm/core/lazy.js → core/lazy.mjs} +0 -0
  59. package/core/mapping.d.ts +6 -0
  60. package/core/mapping.js +23 -23
  61. package/core/mapping.js.flow +25 -17
  62. package/{_esm/core/mapping.js → core/mapping.mjs} +26 -22
  63. package/core/number.d.ts +6 -0
  64. package/core/number.js +9 -13
  65. package/core/number.js.flow +18 -12
  66. package/core/number.mjs +25 -0
  67. package/core/object.d.ts +33 -0
  68. package/core/object.js +7 -11
  69. package/core/object.js.flow +7 -7
  70. package/{_esm/core/object.js → core/object.mjs} +9 -9
  71. package/core/optional.d.ts +5 -0
  72. package/core/optional.js +4 -8
  73. package/core/optional.js.flow +3 -3
  74. package/{_esm/core/optional.js → core/optional.mjs} +6 -6
  75. package/core/string.d.ts +10 -0
  76. package/core/string.js +18 -48
  77. package/core/string.js.flow +16 -39
  78. package/core/string.mjs +49 -0
  79. package/core/tuple.d.ts +30 -0
  80. package/core/tuple.js +12 -8
  81. package/core/tuple.js.flow +25 -29
  82. package/{_esm/core/tuple.js → core/tuple.mjs} +16 -12
  83. package/format.d.ts +4 -0
  84. package/{format/inline.js → format.js} +6 -1
  85. package/{_esm/format/inline.js.flow → format.js.flow} +6 -2
  86. package/{_esm/format/inline.js → format.mjs} +4 -1
  87. package/index.d.ts +43 -0
  88. package/index.js +3 -2
  89. package/index.js.flow +2 -2
  90. package/{_esm/index.js → index.mjs} +19 -19
  91. package/package.json +15 -3
  92. package/result.d.ts +39 -0
  93. package/result.js +3 -78
  94. package/result.js.flow +4 -76
  95. package/{_esm/result.js → result.mjs} +3 -64
  96. package/_esm/_guard.js.flow +0 -20
  97. package/_esm/_types.js.flow +0 -20
  98. package/_esm/_utils.js.flow +0 -97
  99. package/_esm/annotate.js.flow +0 -218
  100. package/_esm/core/array.js.flow +0 -103
  101. package/_esm/core/boolean.js.flow +0 -29
  102. package/_esm/core/composition.js.flow +0 -43
  103. package/_esm/core/constants.js.flow +0 -46
  104. package/_esm/core/date.js.flow +0 -40
  105. package/_esm/core/describe.js.flow +0 -17
  106. package/_esm/core/dispatch.js.flow +0 -58
  107. package/_esm/core/either.js.flow +0 -151
  108. package/_esm/core/fail.js.flow +0 -12
  109. package/_esm/core/instanceOf.js +0 -8
  110. package/_esm/core/instanceOf.js.flow +0 -20
  111. package/_esm/core/json.js +0 -15
  112. package/_esm/core/json.js.flow +0 -28
  113. package/_esm/core/lazy.js.flow +0 -15
  114. package/_esm/core/mapping.js.flow +0 -54
  115. package/_esm/core/number.js +0 -25
  116. package/_esm/core/number.js.flow +0 -34
  117. package/_esm/core/object.js.flow +0 -203
  118. package/_esm/core/optional.js.flow +0 -41
  119. package/_esm/core/string.js +0 -76
  120. package/_esm/core/string.js.flow +0 -82
  121. package/_esm/core/tuple.js.flow +0 -215
  122. package/_esm/format/index.js +0 -2
  123. package/_esm/format/index.js.flow +0 -4
  124. package/_esm/format/short.js +0 -4
  125. package/_esm/format/short.js.flow +0 -8
  126. package/_esm/index.js.flow +0 -63
  127. package/_esm/result.js.flow +0 -170
  128. package/format/index.js +0 -12
  129. package/format/index.js.flow +0 -4
  130. package/format/inline.js.flow +0 -122
  131. package/format/short.js +0 -10
  132. package/format/short.js.flow +0 -8
@@ -2,15 +2,17 @@
2
2
 
3
3
  import { annotate } from '../annotate';
4
4
  import { compose, predicate } from './composition';
5
- import { err, ok, okOrErrValue, unwrap } from '../result';
5
+ import { err, ok, unwrap } from '../result';
6
6
  import { poja } from './array';
7
7
  import type { Decoder } from '../_types';
8
+ import type { Result } from '../result';
9
+
10
+ function okOrErr<T, E>(result: Result<T, E>): T | E {
11
+ return result.type === 'ok' ? result.value : result.error;
12
+ }
8
13
 
9
14
  const ntuple = (n: number) =>
10
- compose(
11
- poja,
12
- predicate((arr) => arr.length === n, `Must be a ${n}-tuple`),
13
- );
15
+ predicate(poja, (arr) => arr.length === n, `Must be a ${n}-tuple`);
14
16
 
15
17
  /**
16
18
  * Builds a Decoder that returns Ok for 1-tuple of [T], given a Decoder for T.
@@ -26,7 +28,7 @@ export function tuple1<T>(decoder1: Decoder<T>): Decoder<[T]> {
26
28
  } catch (e) {
27
29
  // If a decoder error has happened while unwrapping all the
28
30
  // results, try to construct a good error message
29
- return err(annotate([okOrErrValue(result1)]));
31
+ return err(annotate([okOrErr(result1)]));
30
32
  }
31
33
  });
32
34
  }
@@ -49,7 +51,7 @@ export function tuple2<T1, T2>(
49
51
  } catch (e) {
50
52
  // If a decoder error has happened while unwrapping all the
51
53
  // results, try to construct a good error message
52
- return err(annotate([okOrErrValue(result1), okOrErrValue(result2)]));
54
+ return err(annotate([okOrErr(result1), okOrErr(result2)]));
53
55
  }
54
56
  });
55
57
  }
@@ -74,13 +76,7 @@ export function tuple3<T1, T2, T3>(
74
76
  } catch (e) {
75
77
  // If a decoder error has happened while unwrapping all the
76
78
  // results, try to construct a good error message
77
- return err(
78
- annotate([
79
- okOrErrValue(result1),
80
- okOrErrValue(result2),
81
- okOrErrValue(result3),
82
- ]),
83
- );
79
+ return err(annotate([okOrErr(result1), okOrErr(result2), okOrErr(result3)]));
84
80
  }
85
81
  });
86
82
  }
@@ -114,10 +110,10 @@ export function tuple4<T1, T2, T3, T4>(
114
110
  // results, try to construct a good error message
115
111
  return err(
116
112
  annotate([
117
- okOrErrValue(result1),
118
- okOrErrValue(result2),
119
- okOrErrValue(result3),
120
- okOrErrValue(result4),
113
+ okOrErr(result1),
114
+ okOrErr(result2),
115
+ okOrErr(result3),
116
+ okOrErr(result4),
121
117
  ]),
122
118
  );
123
119
  }
@@ -156,11 +152,11 @@ export function tuple5<T1, T2, T3, T4, T5>(
156
152
  // results, try to construct a good error message
157
153
  return err(
158
154
  annotate([
159
- okOrErrValue(result1),
160
- okOrErrValue(result2),
161
- okOrErrValue(result3),
162
- okOrErrValue(result4),
163
- okOrErrValue(result5),
155
+ okOrErr(result1),
156
+ okOrErr(result2),
157
+ okOrErr(result3),
158
+ okOrErr(result4),
159
+ okOrErr(result5),
164
160
  ]),
165
161
  );
166
162
  }
@@ -202,12 +198,12 @@ export function tuple6<T1, T2, T3, T4, T5, T6>(
202
198
  // results, try to construct a good error message
203
199
  return err(
204
200
  annotate([
205
- okOrErrValue(result1),
206
- okOrErrValue(result2),
207
- okOrErrValue(result3),
208
- okOrErrValue(result4),
209
- okOrErrValue(result5),
210
- okOrErrValue(result6),
201
+ okOrErr(result1),
202
+ okOrErr(result2),
203
+ okOrErr(result3),
204
+ okOrErr(result4),
205
+ okOrErr(result5),
206
+ okOrErr(result6),
211
207
  ]),
212
208
  );
213
209
  }
@@ -1,12 +1,16 @@
1
- import { annotate } from '../annotate';
2
- import { compose, predicate } from './composition';
3
- import { err, ok, okOrErrValue, unwrap } from '../result';
4
- import { poja } from './array';
1
+ import { annotate } from '../annotate.mjs';
2
+ import { compose, predicate } from './composition.mjs';
3
+ import { err, ok, unwrap } from '../result.mjs';
4
+ import { poja } from './array.mjs';
5
+
6
+ function okOrErr(result) {
7
+ return result.type === 'ok' ? result.value : result.error;
8
+ }
5
9
 
6
10
  var ntuple = function ntuple(n) {
7
- return compose(poja, predicate(function (arr) {
11
+ return predicate(poja, function (arr) {
8
12
  return arr.length === n;
9
- }, "Must be a " + n + "-tuple"));
13
+ }, "Must be a " + n + "-tuple");
10
14
  };
11
15
  /**
12
16
  * Builds a Decoder that returns Ok for 1-tuple of [T], given a Decoder for T.
@@ -24,7 +28,7 @@ export function tuple1(decoder1) {
24
28
  } catch (e) {
25
29
  // If a decoder error has happened while unwrapping all the
26
30
  // results, try to construct a good error message
27
- return err(annotate([okOrErrValue(result1)]));
31
+ return err(annotate([okOrErr(result1)]));
28
32
  }
29
33
  });
30
34
  }
@@ -45,7 +49,7 @@ export function tuple2(decoder1, decoder2) {
45
49
  } catch (e) {
46
50
  // If a decoder error has happened while unwrapping all the
47
51
  // results, try to construct a good error message
48
- return err(annotate([okOrErrValue(result1), okOrErrValue(result2)]));
52
+ return err(annotate([okOrErr(result1), okOrErr(result2)]));
49
53
  }
50
54
  });
51
55
  }
@@ -68,7 +72,7 @@ export function tuple3(decoder1, decoder2, decoder3) {
68
72
  } catch (e) {
69
73
  // If a decoder error has happened while unwrapping all the
70
74
  // results, try to construct a good error message
71
- return err(annotate([okOrErrValue(result1), okOrErrValue(result2), okOrErrValue(result3)]));
75
+ return err(annotate([okOrErr(result1), okOrErr(result2), okOrErr(result3)]));
72
76
  }
73
77
  });
74
78
  }
@@ -93,7 +97,7 @@ export function tuple4(decoder1, decoder2, decoder3, decoder4) {
93
97
  } catch (e) {
94
98
  // If a decoder error has happened while unwrapping all the
95
99
  // results, try to construct a good error message
96
- return err(annotate([okOrErrValue(result1), okOrErrValue(result2), okOrErrValue(result3), okOrErrValue(result4)]));
100
+ return err(annotate([okOrErr(result1), okOrErr(result2), okOrErr(result3), okOrErr(result4)]));
97
101
  }
98
102
  });
99
103
  }
@@ -120,7 +124,7 @@ export function tuple5(decoder1, decoder2, decoder3, decoder4, decoder5) {
120
124
  } catch (e) {
121
125
  // If a decoder error has happened while unwrapping all the
122
126
  // results, try to construct a good error message
123
- return err(annotate([okOrErrValue(result1), okOrErrValue(result2), okOrErrValue(result3), okOrErrValue(result4), okOrErrValue(result5)]));
127
+ return err(annotate([okOrErr(result1), okOrErr(result2), okOrErr(result3), okOrErr(result4), okOrErr(result5)]));
124
128
  }
125
129
  });
126
130
  }
@@ -149,7 +153,7 @@ export function tuple6(decoder1, decoder2, decoder3, decoder4, decoder5, decoder
149
153
  } catch (e) {
150
154
  // If a decoder error has happened while unwrapping all the
151
155
  // results, try to construct a good error message
152
- return err(annotate([okOrErrValue(result1), okOrErrValue(result2), okOrErrValue(result3), okOrErrValue(result4), okOrErrValue(result5), okOrErrValue(result6)]));
156
+ return err(annotate([okOrErr(result1), okOrErr(result2), okOrErr(result3), okOrErr(result4), okOrErr(result5), okOrErr(result6)]));
153
157
  }
154
158
  });
155
159
  }
package/format.d.ts ADDED
@@ -0,0 +1,4 @@
1
+ import { Annotation } from './annotate';
2
+
3
+ export function formatInline(ann: Annotation): string;
4
+ export function formatShort(ann: Annotation): string;
@@ -2,10 +2,11 @@
2
2
 
3
3
  exports.__esModule = true;
4
4
  exports.formatInline = formatInline;
5
+ exports.formatShort = formatShort;
5
6
  exports.serializeAnnotation = serializeAnnotation;
6
7
  exports.serializeValue = serializeValue;
7
8
 
8
- var _utils = require("../_utils");
9
+ var _utils = require("./_utils");
9
10
 
10
11
  function serializeString(s, width) {
11
12
  if (width === void 0) {
@@ -143,4 +144,8 @@ function formatInline(ann) {
143
144
  } else {
144
145
  return serialized;
145
146
  }
147
+ }
148
+
149
+ function formatShort(ann) {
150
+ return (0, _utils.summarize)(ann, []).join('\n');
146
151
  }
@@ -1,7 +1,7 @@
1
1
  // @flow strict
2
2
 
3
- import { asDate, indent, INDENT, isMultiline } from '../_utils';
4
- import type { Annotation, ArrayAnnotation, ObjectAnnotation } from '../annotate';
3
+ import { summarize as _summarize, asDate, INDENT, indent, isMultiline } from './_utils';
4
+ import type { Annotation, ArrayAnnotation, ObjectAnnotation } from './annotate';
5
5
 
6
6
  function serializeString(s: string, width: number = 80): string {
7
7
  // Full string
@@ -120,3 +120,7 @@ export function formatInline(ann: Annotation): string {
120
120
  return serialized;
121
121
  }
122
122
  }
123
+
124
+ export function formatShort(ann: Annotation): string {
125
+ return _summarize(ann, []).join('\n');
126
+ }
@@ -1,4 +1,4 @@
1
- import { asDate, indent, INDENT, isMultiline } from '../_utils';
1
+ import { summarize as _summarize, asDate, INDENT, indent, isMultiline } from './_utils.mjs';
2
2
 
3
3
  function serializeString(s, width) {
4
4
  if (width === void 0) {
@@ -134,4 +134,7 @@ export function formatInline(ann) {
134
134
  } else {
135
135
  return serialized;
136
136
  }
137
+ }
138
+ export function formatShort(ann) {
139
+ return _summarize(ann, []).join('\n');
137
140
  }
package/index.d.ts ADDED
@@ -0,0 +1,43 @@
1
+ export {
2
+ Decoder,
3
+ DecodeResult,
4
+ DecoderType,
5
+ Guard,
6
+ GuardType,
7
+ Predicate,
8
+ Scalar,
9
+ } from './_types';
10
+ export { Result } from './result';
11
+ export { JSONValue, JSONObject, JSONArray } from './core/json';
12
+
13
+ export { guard } from './_guard';
14
+
15
+ export { compose, map, predicate } from './core/composition';
16
+
17
+ export { array, nonEmptyArray, poja } from './core/array';
18
+ export { boolean, numericBoolean, truthy } from './core/boolean';
19
+ export { constant, hardcoded, mixed, null_, undefined_, unknown } from './core/constants';
20
+ export { date, iso8601 } from './core/date';
21
+ export { describe } from './core/describe';
22
+ export { disjointUnion } from './core/dispatch';
23
+ export {
24
+ either,
25
+ either3,
26
+ either4,
27
+ either5,
28
+ either6,
29
+ either7,
30
+ either8,
31
+ either9,
32
+ oneOf,
33
+ } from './core/either';
34
+ export { fail } from './core/fail';
35
+ export { instanceOf } from './core/instanceOf';
36
+ export { json, jsonObject, jsonArray } from './core/json';
37
+ export { lazy } from './core/lazy';
38
+ export { mapping, dict } from './core/mapping';
39
+ export { integer, number, positiveInteger, positiveNumber } from './core/number';
40
+ export { exact, inexact, object, pojo } from './core/object';
41
+ export { maybe, nullable, optional } from './core/optional';
42
+ export { email, httpsUrl, nonEmptyString, regex, string, url } from './core/string';
43
+ export { tuple1, tuple2, tuple3, tuple4, tuple5, tuple6 } from './core/tuple';
package/index.js CHANGED
@@ -1,7 +1,7 @@
1
1
  "use strict";
2
2
 
3
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;
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.httpsUrl = 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.disjointUnion = exports.dict = exports.describe = exports.date = exports.constant = exports.compose = exports["boolean"] = exports.array = void 0;
5
5
 
6
6
  var _guard = require("./_guard");
7
7
 
@@ -45,7 +45,7 @@ exports.describe = _describe.describe;
45
45
 
46
46
  var _dispatch = require("./core/dispatch");
47
47
 
48
- exports.dispatch = _dispatch.dispatch;
48
+ exports.disjointUnion = _dispatch.disjointUnion;
49
49
 
50
50
  var _either = require("./core/either");
51
51
 
@@ -105,6 +105,7 @@ exports.optional = _optional.optional;
105
105
  var _string = require("./core/string");
106
106
 
107
107
  exports.email = _string.email;
108
+ exports.httpsUrl = _string.httpsUrl;
108
109
  exports.nonEmptyString = _string.nonEmptyString;
109
110
  exports.regex = _string.regex;
110
111
  exports.string = _string.string;
package/index.js.flow CHANGED
@@ -39,7 +39,7 @@ export { boolean, numericBoolean, truthy } from './core/boolean';
39
39
  export { constant, hardcoded, mixed, null_, undefined_, unknown } from './core/constants';
40
40
  export { date, iso8601 } from './core/date';
41
41
  export { describe } from './core/describe';
42
- export { dispatch } from './core/dispatch';
42
+ export { disjointUnion } from './core/dispatch';
43
43
  export {
44
44
  either,
45
45
  either3,
@@ -59,5 +59,5 @@ export { mapping, dict } from './core/mapping';
59
59
  export { integer, number, positiveInteger, positiveNumber } from './core/number';
60
60
  export { exact, inexact, object, pojo } from './core/object';
61
61
  export { maybe, nullable, optional } from './core/optional';
62
- export { email, nonEmptyString, regex, string, url } from './core/string';
62
+ export { email, httpsUrl, nonEmptyString, regex, string, url } from './core/string';
63
63
  export { tuple1, tuple2, tuple3, tuple4, tuple5, tuple6 } from './core/tuple';
@@ -16,22 +16,22 @@
16
16
  * type. In our case, it's fine to fail with a runtime error.
17
17
  *
18
18
  */
19
- export { guard } from './_guard';
20
- export { compose, 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';
19
+ export { guard } from './_guard.mjs';
20
+ export { compose, map, predicate } from './core/composition.mjs';
21
+ export { array, nonEmptyArray, poja } from './core/array.mjs';
22
+ export { boolean, numericBoolean, truthy } from './core/boolean.mjs';
23
+ export { constant, hardcoded, mixed, null_, undefined_, unknown } from './core/constants.mjs';
24
+ export { date, iso8601 } from './core/date.mjs';
25
+ export { describe } from './core/describe.mjs';
26
+ export { disjointUnion } from './core/dispatch.mjs';
27
+ export { either, either3, either4, either5, either6, either7, either8, either9, oneOf } from './core/either.mjs';
28
+ export { fail } from './core/fail.mjs';
29
+ export { instanceOf } from './core/instanceOf.mjs';
30
+ export { json, jsonObject, jsonArray } from './core/json.mjs';
31
+ export { lazy } from './core/lazy.mjs';
32
+ export { mapping, dict } from './core/mapping.mjs';
33
+ export { integer, number, positiveInteger, positiveNumber } from './core/number.mjs';
34
+ export { exact, inexact, object, pojo } from './core/object.mjs';
35
+ export { maybe, nullable, optional } from './core/optional.mjs';
36
+ export { email, httpsUrl, nonEmptyString, regex, string, url } from './core/string.mjs';
37
+ export { tuple1, tuple2, tuple3, tuple4, tuple5, tuple6 } from './core/tuple.mjs';
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "decoders",
3
- "version": "2.0.0-beta4",
3
+ "version": "2.0.0-beta8",
4
4
  "description": "Elegant and battle-tested validation library for type-safe input data (for TypeScript and Flow)",
5
5
  "license": "MIT",
6
6
  "repository": {
@@ -13,7 +13,7 @@
13
13
  "url": "https://github.com/nvie/decoders/issues"
14
14
  },
15
15
  "main": "./index.js",
16
- "module": "./_esm/index.js",
16
+ "module": "./index.mjs",
17
17
  "keywords": [
18
18
  "decoder",
19
19
  "decoders",
@@ -41,5 +41,17 @@
41
41
  "predicate"
42
42
  ],
43
43
  "githubUrl": "https://github.com/nvie/decoders",
44
- "sideEffects": false
44
+ "sideEffects": false,
45
+ "typesVersions": {
46
+ ">=4.1.0": {
47
+ "*": [
48
+ "*"
49
+ ]
50
+ },
51
+ "*": {
52
+ "*": [
53
+ "NotSupportedTSVersion.d.ts"
54
+ ]
55
+ }
56
+ }
45
57
  }
package/result.d.ts ADDED
@@ -0,0 +1,39 @@
1
+ export interface Ok<T> {
2
+ type: 'ok';
3
+ value: T;
4
+ error: undefined;
5
+ }
6
+
7
+ export interface Err<E> {
8
+ type: 'err';
9
+ value: undefined;
10
+ error: E;
11
+ }
12
+
13
+ export type Result<T, E> = Ok<T> | Err<E>;
14
+
15
+ export function ok<T>(value: T): Ok<T>;
16
+ export function err<E>(error: E): Err<E>;
17
+ export function unwrap<T>(result: Result<T, unknown>): T;
18
+ export function expect<T>(result: Result<T, unknown>, message: string | Error): T;
19
+ export function dispatch<T, E, O>(
20
+ result: Result<T, E>,
21
+ okCallback: (value: T) => O,
22
+ errCallback: (error: E) => O,
23
+ ): O;
24
+ export function andThen<T, E, T2>(
25
+ result1: Result<T, E>,
26
+ lazyResult2: (value: T) => Result<T2, E>,
27
+ ): Result<T2, E>;
28
+ export function orElse<T, E, E2>(
29
+ result1: Result<T, E>,
30
+ lazyResult2: (errValue: E) => Result<T, E2>,
31
+ ): Result<T, E2>;
32
+ export function mapOk<T, E, T2>(
33
+ result: Result<T, E>,
34
+ mapper: (value: T) => T2,
35
+ ): Result<T2, E>;
36
+ export function mapError<T, E, E2>(
37
+ result: Result<T, E>,
38
+ mapper: (error: E) => E2,
39
+ ): Result<T, E2>;
package/result.js CHANGED
@@ -4,19 +4,12 @@ exports.__esModule = true;
4
4
  exports.andThen = andThen;
5
5
  exports.dispatch = dispatch;
6
6
  exports.err = err;
7
- exports.errValue = errValue;
8
7
  exports.expect = expect;
9
- exports.isErr = isErr;
10
- exports.isOk = isOk;
11
8
  exports.mapError = mapError;
12
9
  exports.mapOk = mapOk;
13
10
  exports.ok = ok;
14
- exports.okOrErrValue = okOrErrValue;
15
- exports.okValue = okValue;
16
11
  exports.orElse = orElse;
17
- exports.toString = toString;
18
12
  exports.unwrap = unwrap;
19
- exports.withDefault = withDefault;
20
13
 
21
14
  /**
22
15
  * Result <value> <error>
@@ -30,7 +23,8 @@ exports.withDefault = withDefault;
30
23
  function ok(value) {
31
24
  return {
32
25
  type: 'ok',
33
- value: value
26
+ value: value,
27
+ error: undefined
34
28
  };
35
29
  }
36
30
  /**
@@ -41,37 +35,10 @@ function ok(value) {
41
35
  function err(error) {
42
36
  return {
43
37
  type: 'err',
38
+ value: undefined,
44
39
  error: error
45
40
  };
46
41
  }
47
-
48
- function toString(result) {
49
- return result.type === 'ok' ? "Ok(" + String(result.value) + ")" : "Err(" + String(result.error) + ")";
50
- }
51
-
52
- function isOk(result) {
53
- return result.type === 'ok';
54
- }
55
-
56
- function isErr(result) {
57
- return result.type === 'err';
58
- }
59
-
60
- function withDefault(result, defaultValue) {
61
- return result.type === 'ok' ? result.value : defaultValue;
62
- }
63
-
64
- function okValue(result) {
65
- return result.type === 'ok' ? result.value : undefined;
66
- }
67
-
68
- function errValue(result) {
69
- return result.type === 'err' ? result.error : undefined;
70
- }
71
-
72
- function okOrErrValue(result) {
73
- return result.type === 'ok' ? result.value : result.error;
74
- }
75
42
  /**
76
43
  * Unwrap the value from this Result instance if this is an "Ok" result.
77
44
  * Otherwise, will throw the "Err" error via a runtime exception.
@@ -97,48 +64,6 @@ function expect(result, message) {
97
64
  function dispatch(result, okCallback, errCallback) {
98
65
  return result.type === 'ok' ? okCallback(result.value) : errCallback(result.error);
99
66
  }
100
- /**
101
- * If the given result is OK, defers to the other result. Otherwise returns the
102
- * error result.
103
- *
104
- * It's like saying A && B, but on Result.
105
- *
106
- * Examples:
107
- *
108
- * Result.ok(42) && Result.ok('hi') // => Ok('hi')
109
- * Result.err('boo') && Result.ok('hi') // => Err('boo')
110
- * Result.ok(42) && Result.err('boo') // => Err('boo')
111
- * Result.err('boo') && Result.err('boo') // => Err('boo')
112
- *
113
- */
114
- // export function and<T, E, T2>(
115
- // result1: Result<T, E>,
116
- // result2: Result<T2, E>,
117
- // ): Result<T2, E> {
118
- // return result1.type === 'ok' ? result2 : result1;
119
- // }
120
-
121
- /**
122
- * If the given result is OK, return that result. Otherwise, defers to the
123
- * other result.
124
- *
125
- * It's like saying A || B, but on Result.
126
- *
127
- * Examples:
128
- *
129
- * Result.ok(42) || Result.ok('hi') // => Ok(42)
130
- * Result.err('boo') || Result.ok('hi') // => Ok('hi')
131
- * Result.ok(42) || Result.err('boo') // => Ok(42)
132
- * Result.err('bleh') || Result.err('boo') // => Err('boo')
133
- *
134
- */
135
- // export function or<T, E, E2>(
136
- // result1: Result<T, E>,
137
- // result2: Result<T, E2>,
138
- // ): Result<T, E2> {
139
- // return result1.type === 'ok' ? result1 : result2;
140
- // }
141
-
142
67
  /**
143
68
  * Like .and(), aka &&, but the second argument gets evaluated lazily only if
144
69
  * the first result is an Ok result. If so, it has access to the Ok value from
package/result.js.flow CHANGED
@@ -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,44 +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 };
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 okValue<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
- export function okOrErrValue<T, E>(result: Result<T, E>): T | E {
55
- return result.type === 'ok' ? result.value : result.error;
25
+ return { type: 'err', value: undefined, error };
56
26
  }
57
27
 
58
28
  /**
@@ -83,48 +53,6 @@ export function dispatch<T, E, O>(
83
53
  return result.type === 'ok' ? okCallback(result.value) : errCallback(result.error);
84
54
  }
85
55
 
86
- /**
87
- * If the given result is OK, defers to the other result. Otherwise returns the
88
- * error result.
89
- *
90
- * It's like saying A && B, but on Result.
91
- *
92
- * Examples:
93
- *
94
- * Result.ok(42) && Result.ok('hi') // => Ok('hi')
95
- * Result.err('boo') && Result.ok('hi') // => Err('boo')
96
- * Result.ok(42) && Result.err('boo') // => Err('boo')
97
- * Result.err('boo') && Result.err('boo') // => Err('boo')
98
- *
99
- */
100
- // export function and<T, E, T2>(
101
- // result1: Result<T, E>,
102
- // result2: Result<T2, E>,
103
- // ): Result<T2, E> {
104
- // return result1.type === 'ok' ? result2 : result1;
105
- // }
106
-
107
- /**
108
- * If the given result is OK, return that result. Otherwise, defers to the
109
- * other result.
110
- *
111
- * It's like saying A || B, but on Result.
112
- *
113
- * Examples:
114
- *
115
- * Result.ok(42) || Result.ok('hi') // => Ok(42)
116
- * Result.err('boo') || Result.ok('hi') // => Ok('hi')
117
- * Result.ok(42) || Result.err('boo') // => Ok(42)
118
- * Result.err('bleh') || Result.err('boo') // => Err('boo')
119
- *
120
- */
121
- // export function or<T, E, E2>(
122
- // result1: Result<T, E>,
123
- // result2: Result<T, E2>,
124
- // ): Result<T, E2> {
125
- // return result1.type === 'ok' ? result1 : result2;
126
- // }
127
-
128
56
  /**
129
57
  * Like .and(), aka &&, but the second argument gets evaluated lazily only if
130
58
  * the first result is an Ok result. If so, it has access to the Ok value from