decoders 2.0.0-beta5 → 2.0.0-beta9

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 (139) hide show
  1. package/CHANGELOG.md +19 -0
  2. package/NotSupportedTSVersion.d.ts +1 -0
  3. package/README.md +934 -387
  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/_utils.js +1 -1
  12. package/_utils.js.flow +3 -3
  13. package/{_esm/_utils.js → _utils.mjs} +1 -1
  14. package/annotate.d.ts +62 -0
  15. package/{_esm/annotate.js → annotate.mjs} +0 -0
  16. package/core/_helpers.d.ts +79 -0
  17. package/core/array.d.ts +8 -0
  18. package/core/array.js +19 -12
  19. package/core/array.js.flow +15 -11
  20. package/{_esm/core/array.js → core/array.mjs} +19 -10
  21. package/core/boolean.d.ts +5 -0
  22. package/core/boolean.js +5 -9
  23. package/core/boolean.js.flow +5 -7
  24. package/{_esm/core/boolean.js → core/boolean.mjs} +7 -7
  25. package/core/composition.d.ts +18 -0
  26. package/core/composition.js +41 -15
  27. package/core/composition.js.flow +41 -10
  28. package/core/composition.mjs +70 -0
  29. package/core/constants.d.ts +11 -0
  30. package/core/constants.js +6 -10
  31. package/core/constants.js.flow +7 -9
  32. package/{_esm/core/constants.js → core/constants.mjs} +7 -7
  33. package/core/date.d.ts +4 -0
  34. package/core/date.js +5 -9
  35. package/core/date.js.flow +4 -6
  36. package/{_esm/core/date.js → core/date.mjs} +7 -7
  37. package/core/describe.d.ts +3 -0
  38. package/core/describe.js +2 -6
  39. package/core/describe.js.flow +2 -2
  40. package/{_esm/core/describe.js → core/describe.mjs} +3 -3
  41. package/core/dispatch.d.ts +8 -0
  42. package/core/dispatch.js +11 -13
  43. package/core/dispatch.js.flow +13 -12
  44. package/{_esm/core/dispatch.js → core/dispatch.mjs} +12 -11
  45. package/core/either.d.ts +66 -0
  46. package/core/either.js +34 -50
  47. package/core/either.js.flow +40 -86
  48. package/core/either.mjs +90 -0
  49. package/core/fail.d.ts +3 -0
  50. package/core/fail.js +2 -6
  51. package/core/fail.js.flow +2 -2
  52. package/{_esm/core/fail.js → core/fail.mjs} +3 -3
  53. package/core/instanceOf.d.ts +3 -0
  54. package/core/instanceOf.js +2 -6
  55. package/core/instanceOf.js.flow +3 -3
  56. package/core/instanceOf.mjs +8 -0
  57. package/core/json.d.ts +11 -0
  58. package/core/json.js +3 -3
  59. package/core/json.js.flow +3 -3
  60. package/core/json.mjs +15 -0
  61. package/core/lazy.d.ts +3 -0
  62. package/{_esm/core/lazy.js → core/lazy.mjs} +0 -0
  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 +38 -0
  68. package/core/object.js +66 -13
  69. package/core/object.js.flow +84 -28
  70. package/{_esm/core/object.js → core/object.mjs} +64 -11
  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 +13 -0
  76. package/core/string.js +31 -49
  77. package/core/string.js.flow +29 -39
  78. package/core/string.mjs +58 -0
  79. package/core/tuple.d.ts +30 -0
  80. package/core/tuple.js +30 -149
  81. package/core/tuple.js.flow +33 -197
  82. package/core/tuple.mjs +45 -0
  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 +42 -0
  88. package/index.js +33 -42
  89. package/index.js.flow +17 -18
  90. package/{_esm/index.js → index.mjs} +18 -19
  91. package/package.json +15 -3
  92. package/result.d.ts +39 -0
  93. package/result.js +9 -90
  94. package/result.js.flow +11 -87
  95. package/result.mjs +81 -0
  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 +0 -42
  103. package/_esm/core/composition.js.flow +0 -43
  104. package/_esm/core/constants.js.flow +0 -46
  105. package/_esm/core/date.js.flow +0 -40
  106. package/_esm/core/describe.js.flow +0 -17
  107. package/_esm/core/dispatch.js.flow +0 -58
  108. package/_esm/core/either.js +0 -90
  109. package/_esm/core/either.js.flow +0 -151
  110. package/_esm/core/fail.js.flow +0 -12
  111. package/_esm/core/instanceOf.js +0 -8
  112. package/_esm/core/instanceOf.js.flow +0 -20
  113. package/_esm/core/json.js +0 -15
  114. package/_esm/core/json.js.flow +0 -28
  115. package/_esm/core/lazy.js.flow +0 -15
  116. package/_esm/core/mapping.js +0 -54
  117. package/_esm/core/mapping.js.flow +0 -54
  118. package/_esm/core/number.js +0 -25
  119. package/_esm/core/number.js.flow +0 -34
  120. package/_esm/core/object.js.flow +0 -203
  121. package/_esm/core/optional.js.flow +0 -41
  122. package/_esm/core/string.js +0 -76
  123. package/_esm/core/string.js.flow +0 -82
  124. package/_esm/core/tuple.js +0 -155
  125. package/_esm/core/tuple.js.flow +0 -215
  126. package/_esm/format/index.js +0 -2
  127. package/_esm/format/index.js.flow +0 -4
  128. package/_esm/format/short.js +0 -4
  129. package/_esm/format/short.js.flow +0 -8
  130. package/_esm/index.js.flow +0 -63
  131. package/_esm/result.js +0 -148
  132. package/_esm/result.js.flow +0 -174
  133. package/core/mapping.js +0 -67
  134. package/core/mapping.js.flow +0 -54
  135. package/format/index.js +0 -12
  136. package/format/index.js.flow +0 -4
  137. package/format/inline.js.flow +0 -122
  138. package/format/short.js +0 -10
  139. package/format/short.js.flow +0 -8
@@ -1,13 +1,12 @@
1
1
  // @flow strict
2
2
 
3
- import * as Result from '../result';
3
+ import { andThen } from '../result';
4
4
  import { object } from './object';
5
5
  import { oneOf } from './either';
6
+ import { prep } from './composition';
7
+ import type { _Any } from '../_utils';
6
8
  import type { Decoder, DecoderType } from '../_types';
7
9
 
8
- // $FlowFixMe[unclear-type] (not really an issue) - deliberate use of `any` - not sure how we should get rid of this
9
- type anything = any;
10
-
11
10
  /**
12
11
  * Dispatches to one of several given decoders, based on the value found at
13
12
  * runtime in the given field. For example, suppose you have these decoders:
@@ -30,26 +29,28 @@ type anything = any;
30
29
  * Then these two decoders are equivalent:
31
30
  *
32
31
  * const shape = either(rectangle, circle)
33
- * const shape = dispatch('type', { rectangle, circle })
32
+ * const shape = taggedUnion('type', { rectangle, circle })
34
33
  *
35
34
  * Will be of type Decoder<Rectangle | Circle>.
36
35
  *
37
- * But the dispatch version will typically be more runtime-efficient. The
38
- * reason is that it will first do minimal work to "look ahead" into the `type`
39
- * field here, and based on that value, pick the decoder to invoke.
36
+ * But `taggedUnion` will typically be more runtime-efficient. The reason is
37
+ * that it will first do minimal work to "look ahead" into the `type` field
38
+ * here, and based on that value, pick the decoder to invoke.
40
39
  *
41
40
  * The `either` version will simply try to invoke each decoder, until it finds
42
41
  * one that matches.
43
42
  *
44
- * Also, the error messages will be less ambiguous using `dispatch()`.
43
+ * Also, the error messages will be less ambiguous using `taggedUnion()`.
45
44
  */
46
- export function dispatch<O: { +[field: string]: Decoder<anything>, ... }>(
45
+ export function taggedUnion<O: { +[field: string]: Decoder<_Any>, ... }>(
47
46
  field: string,
48
47
  mapping: O,
49
48
  ): Decoder<$Values<$ObjMap<O, DecoderType>>> {
50
- const base = object({ [field]: oneOf(Object.keys(mapping)) });
49
+ const base = object({
50
+ [field]: prep(String, oneOf(Object.keys(mapping))),
51
+ });
51
52
  return (blob: mixed) => {
52
- return Result.andThen(base(blob), (baseObj) => {
53
+ return andThen(base(blob), (baseObj) => {
53
54
  const decoderName = baseObj[field];
54
55
  const decoder = mapping[decoderName];
55
56
  return decoder(blob);
@@ -1,6 +1,7 @@
1
- import * as Result from '../result';
2
- import { object } from './object';
3
- import { oneOf } from './either';
1
+ import { andThen } from '../result.mjs';
2
+ import { object } from './object.mjs';
3
+ import { oneOf } from './either.mjs';
4
+ import { prep } from './composition.mjs';
4
5
 
5
6
  /**
6
7
  * Dispatches to one of several given decoders, based on the value found at
@@ -24,25 +25,25 @@ import { oneOf } from './either';
24
25
  * Then these two decoders are equivalent:
25
26
  *
26
27
  * const shape = either(rectangle, circle)
27
- * const shape = dispatch('type', { rectangle, circle })
28
+ * const shape = taggedUnion('type', { rectangle, circle })
28
29
  *
29
30
  * Will be of type Decoder<Rectangle | Circle>.
30
31
  *
31
- * But the dispatch version will typically be more runtime-efficient. The
32
- * reason is that it will first do minimal work to "look ahead" into the `type`
33
- * field here, and based on that value, pick the decoder to invoke.
32
+ * But `taggedUnion` will typically be more runtime-efficient. The reason is
33
+ * that it will first do minimal work to "look ahead" into the `type` field
34
+ * here, and based on that value, pick the decoder to invoke.
34
35
  *
35
36
  * The `either` version will simply try to invoke each decoder, until it finds
36
37
  * one that matches.
37
38
  *
38
- * Also, the error messages will be less ambiguous using `dispatch()`.
39
+ * Also, the error messages will be less ambiguous using `taggedUnion()`.
39
40
  */
40
- export function dispatch(field, mapping) {
41
+ export function taggedUnion(field, mapping) {
41
42
  var _object;
42
43
 
43
- var base = object((_object = {}, _object[field] = oneOf(Object.keys(mapping)), _object));
44
+ var base = object((_object = {}, _object[field] = prep(String, oneOf(Object.keys(mapping))), _object));
44
45
  return function (blob) {
45
- return Result.andThen(base(blob), function (baseObj) {
46
+ return andThen(base(blob), function (baseObj) {
46
47
  var decoderName = baseObj[field];
47
48
  var decoder = mapping[decoderName];
48
49
  return decoder(blob);
@@ -0,0 +1,66 @@
1
+ import { Decoder, Scalar } from '../_types';
2
+
3
+ export type DecoderTypes<T> = T extends ReadonlyArray<Decoder<infer U>> ? U : never;
4
+
5
+ export function either<T extends ReadonlyArray<Decoder<any>>>(
6
+ ...args: T
7
+ ): Decoder<DecoderTypes<T>>;
8
+ // export function either<T1, T2>(d1: Decoder<T1>, d2: Decoder<T2>): Decoder<T1 | T2>;
9
+ // export function either2<T1, T2>(d1: Decoder<T1>, d2: Decoder<T2>): Decoder<T1 | T2>;
10
+ // export function either3<T1, T2, T3>(
11
+ // d1: Decoder<T1>,
12
+ // d2: Decoder<T2>,
13
+ // d3: Decoder<T3>,
14
+ // ): Decoder<T1 | T2 | T3>;
15
+ // export function either4<T1, T2, T3, T4>(
16
+ // d1: Decoder<T1>,
17
+ // d2: Decoder<T2>,
18
+ // d3: Decoder<T3>,
19
+ // d4: Decoder<T4>,
20
+ // ): Decoder<T1 | T2 | T3 | T4>;
21
+ // export function either5<T1, T2, T3, T4, T5>(
22
+ // d1: Decoder<T1>,
23
+ // d2: Decoder<T2>,
24
+ // d3: Decoder<T3>,
25
+ // d4: Decoder<T4>,
26
+ // d5: Decoder<T5>,
27
+ // ): Decoder<T1 | T2 | T3 | T4 | T5>;
28
+ // export function either6<T1, T2, T3, T4, T5, T6>(
29
+ // d1: Decoder<T1>,
30
+ // d2: Decoder<T2>,
31
+ // d3: Decoder<T3>,
32
+ // d4: Decoder<T4>,
33
+ // d5: Decoder<T5>,
34
+ // d6: Decoder<T6>,
35
+ // ): Decoder<T1 | T2 | T3 | T4 | T5 | T6>;
36
+ // export function either7<T1, T2, T3, T4, T5, T6, T7>(
37
+ // d1: Decoder<T1>,
38
+ // d2: Decoder<T2>,
39
+ // d3: Decoder<T3>,
40
+ // d4: Decoder<T4>,
41
+ // d5: Decoder<T5>,
42
+ // d6: Decoder<T6>,
43
+ // d7: Decoder<T7>,
44
+ // ): Decoder<T1 | T2 | T3 | T4 | T5 | T6 | T7>;
45
+ // export function either8<T1, T2, T3, T4, T5, T6, T7, T8>(
46
+ // d1: Decoder<T1>,
47
+ // d2: Decoder<T2>,
48
+ // d3: Decoder<T3>,
49
+ // d4: Decoder<T4>,
50
+ // d5: Decoder<T5>,
51
+ // d6: Decoder<T6>,
52
+ // d7: Decoder<T7>,
53
+ // d8: Decoder<T8>,
54
+ // ): Decoder<T1 | T2 | T3 | T4 | T5 | T6 | T7 | T8>;
55
+ // export function either9<T1, T2, T3, T4, T5, T6, T7, T8, T9>(
56
+ // d1: Decoder<T1>,
57
+ // d2: Decoder<T2>,
58
+ // d3: Decoder<T3>,
59
+ // d4: Decoder<T4>,
60
+ // d5: Decoder<T5>,
61
+ // d6: Decoder<T6>,
62
+ // d7: Decoder<T7>,
63
+ // d8: Decoder<T8>,
64
+ // d9: Decoder<T9>,
65
+ // ): Decoder<T1 | T2 | T3 | T4 | T5 | T6 | T7 | T8 | T9>;
66
+ export function oneOf<T extends Scalar>(constants: readonly T[]): Decoder<T>;
package/core/either.js CHANGED
@@ -1,29 +1,20 @@
1
1
  "use strict";
2
2
 
3
3
  exports.__esModule = true;
4
- exports.either = either;
5
- exports.either3 = either3;
6
- exports.either4 = either4;
7
- exports.either5 = either5;
8
- exports.either6 = either6;
9
- exports.either7 = either7;
10
- exports.either8 = either8;
11
- exports.either9 = either9;
4
+ exports.either = void 0;
12
5
  exports.oneOf = oneOf;
13
6
 
14
- var Result = _interopRequireWildcard(require("../result"));
15
-
16
7
  var _annotate = require("../annotate");
17
8
 
18
- var _utils = require("../_utils");
19
-
20
- function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function _getRequireWildcardCache(nodeInterop) { return nodeInterop ? cacheNodeInterop : cacheBabelInterop; })(nodeInterop); }
9
+ var _result = require("../result");
21
10
 
22
- function _interopRequireWildcard(obj, nodeInterop) { if (!nodeInterop && obj && obj.__esModule) { return obj; } if (obj === null || typeof obj !== "object" && typeof obj !== "function") { return { "default": obj }; } var cache = _getRequireWildcardCache(nodeInterop); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj["default"] = obj; if (cache) { cache.set(obj, newObj); } return newObj; }
11
+ var _utils = require("../_utils");
23
12
 
13
+ var EITHER_PREFIX = 'Either:\n';
24
14
  /**
25
15
  * Indents and adds a dash in front of this (potentially multiline) string.
26
16
  */
17
+
27
18
  function itemize(s) {
28
19
  return '-' + (0, _utils.indent)(s).substring(1);
29
20
  }
@@ -55,51 +46,44 @@ function itemize(s) {
55
46
 
56
47
 
57
48
  function nest(errText) {
58
- var EITHER_PREFIX = 'Either:\n';
59
49
  return errText.startsWith(EITHER_PREFIX) ? errText.substr(EITHER_PREFIX.length) : itemize(errText);
60
- }
50
+ } // prettier-ignore
61
51
 
62
- function either(d1, d2) {
63
- return function (blob) {
64
- return Result.orElse(d1(blob), function (err1) {
65
- return Result.orElse(d2(blob), function (err2) {
66
- var serr1 = (0, _utils.summarize)(err1).join('\n');
67
- var serr2 = (0, _utils.summarize)(err2).join('\n');
68
- var text = ['Either:', nest(serr1), nest(serr2)].join('\n');
69
- return Result.err((0, _annotate.annotate)(blob, text));
70
- });
71
- });
72
- };
73
- }
74
52
 
75
- function either3(d1, d2, d3) {
76
- return either(d1, either(d2, d3));
77
- }
53
+ function _either() {
54
+ for (var _len = arguments.length, decoders = new Array(_len), _key = 0; _key < _len; _key++) {
55
+ decoders[_key] = arguments[_key];
56
+ }
78
57
 
79
- function either4(d1, d2, d3, d4) {
80
- return either(d1, either3(d2, d3, d4));
81
- }
58
+ if (decoders.length === 0) {
59
+ throw new Error('Pass at least one decoder to either()');
60
+ }
82
61
 
83
- function either5(d1, d2, d3, d4, d5) {
84
- return either(d1, either4(d2, d3, d4, d5));
85
- }
62
+ return function (blob) {
63
+ // Collect errors here along the way
64
+ var errors = [];
86
65
 
87
- function either6(d1, d2, d3, d4, d5, d6) {
88
- return either(d1, either5(d2, d3, d4, d5, d6));
89
- }
66
+ for (var _i = 0; _i < decoders.length; _i++) {
67
+ var result = decoders[_i](blob);
90
68
 
91
- function either7(d1, d2, d3, d4, d5, d6, d7) {
92
- return either(d1, either6(d2, d3, d4, d5, d6, d7));
93
- }
69
+ if (result.ok) {
70
+ return result;
71
+ } else {
72
+ errors.push(result.error);
73
+ }
74
+ } // Decoding all alternatives failed, return the combined error message
94
75
 
95
- function either8(d1, d2, d3, d4, d5, d6, d7, d8) {
96
- return either(d1, either7(d2, d3, d4, d5, d6, d7, d8));
97
- }
98
76
 
99
- function either9(d1, d2, d3, d4, d5, d6, d7, d8, d9) {
100
- return either(d1, either8(d2, d3, d4, d5, d6, d7, d8, d9));
77
+ var text = EITHER_PREFIX + errors.map(function (err) {
78
+ return nest((0, _utils.summarize)(err).join('\n'));
79
+ }).join('\n');
80
+ return (0, _result.err)((0, _annotate.annotate)(blob, text));
81
+ };
101
82
  }
102
83
 
84
+ var either = _either;
85
+ exports.either = either;
86
+
103
87
  function oneOf(constants) {
104
88
  return function (blob) {
105
89
  var winner = constants.find(function (c) {
@@ -107,10 +91,10 @@ function oneOf(constants) {
107
91
  });
108
92
 
109
93
  if (winner !== undefined) {
110
- return Result.ok(winner);
94
+ return (0, _result.ok)(winner);
111
95
  }
112
96
 
113
- return Result.err((0, _annotate.annotate)(blob, "Must be one of " + constants.map(function (value) {
97
+ return (0, _result.err)((0, _annotate.annotate)(blob, "Must be one of " + constants.map(function (value) {
114
98
  return JSON.stringify(value);
115
99
  }).join(', ')));
116
100
  };
@@ -1,9 +1,12 @@
1
1
  // @flow strict
2
2
 
3
- import * as Result from '../result';
4
3
  import { annotate } from '../annotate';
4
+ import { err, ok } from '../result';
5
5
  import { indent, summarize } from '../_utils';
6
- import type { Decoder, Scalar } from '../_types';
6
+ import type { _Any } from '../_utils';
7
+ import type { Decoder, DecodeResult, Scalar } from '../_types';
8
+
9
+ const EITHER_PREFIX = 'Either:\n';
7
10
 
8
11
  /**
9
12
  * Indents and adds a dash in front of this (potentially multiline) string.
@@ -38,108 +41,59 @@ function itemize(s: string): string {
38
41
  *
39
42
  */
40
43
  function nest(errText: string): string {
41
- const EITHER_PREFIX = 'Either:\n';
42
44
  return errText.startsWith(EITHER_PREFIX)
43
45
  ? errText.substr(EITHER_PREFIX.length)
44
46
  : itemize(errText);
45
47
  }
46
48
 
47
- export function either<T1, T2>(d1: Decoder<T1>, d2: Decoder<T2>): Decoder<T1 | T2> {
48
- return (blob: mixed) =>
49
- Result.orElse(d1(blob), (err1) =>
50
- Result.orElse(d2(blob), (err2) => {
51
- const serr1 = summarize(err1).join('\n');
52
- const serr2 = summarize(err2).join('\n');
53
- const text = ['Either:', nest(serr1), nest(serr2)].join('\n');
54
- return Result.err(annotate(blob, text));
55
- }),
56
- );
57
- }
58
-
59
- export function either3<T1, T2, T3>(
60
- d1: Decoder<T1>,
61
- d2: Decoder<T2>,
62
- d3: Decoder<T3>,
63
- ): Decoder<T1 | T2 | T3> {
64
- return either(d1, either(d2, d3));
49
+ // prettier-ignore
50
+ interface EitherDecoderSignatures {
51
+ <A>(a: Decoder<A>): Decoder<A>;
52
+ <A, B>(a: Decoder<A>, b: Decoder<B>): Decoder<A | B>;
53
+ <A, B, C>(a: Decoder<A>, b: Decoder<B>, c: Decoder<C>): Decoder<A | B | C>;
54
+ <A, B, C, D>(a: Decoder<A>, b: Decoder<B>, c: Decoder<C>, d: Decoder<D>): Decoder<A | B | C | D>;
55
+ <A, B, C, D, E>(a: Decoder<A>, b: Decoder<B>, c: Decoder<C>, d: Decoder<D>, e: Decoder<E>): Decoder<A | B | C | D | E>;
56
+ <A, B, C, D, E, F>(a: Decoder<A>, b: Decoder<B>, c: Decoder<C>, d: Decoder<D>, e: Decoder<E>, f: Decoder<F>): Decoder<A | B | C | D | E | F>;
57
+ <A, B, C, D, E, F, G>(a: Decoder<A>, b: Decoder<B>, c: Decoder<C>, d: Decoder<D>, e: Decoder<E>, f: Decoder<F>, g: Decoder<G>): Decoder<A | B | C | D | E | F | G>;
58
+ <A, B, C, D, E, F, G, H>(a: Decoder<A>, b: Decoder<B>, c: Decoder<C>, d: Decoder<D>, e: Decoder<E>, f: Decoder<F>, g: Decoder<G>, h: Decoder<H>): Decoder<A | B | C | D | E | F | G | H>;
59
+ <A, B, C, D, E, F, G, H, I>(a: Decoder<A>, b: Decoder<B>, c: Decoder<C>, d: Decoder<D>, e: Decoder<E>, f: Decoder<F>, g: Decoder<G>, h: Decoder<H>, i: Decoder<I>): Decoder<A | B | C | D | E | F | G | H | I>;
65
60
  }
66
61
 
67
- export function either4<T1, T2, T3, T4>(
68
- d1: Decoder<T1>,
69
- d2: Decoder<T2>,
70
- d3: Decoder<T3>,
71
- d4: Decoder<T4>,
72
- ): Decoder<T1 | T2 | T3 | T4> {
73
- return either(d1, either3(d2, d3, d4));
74
- }
75
-
76
- export function either5<T1, T2, T3, T4, T5>(
77
- d1: Decoder<T1>,
78
- d2: Decoder<T2>,
79
- d3: Decoder<T3>,
80
- d4: Decoder<T4>,
81
- d5: Decoder<T5>,
82
- ): Decoder<T1 | T2 | T3 | T4 | T5> {
83
- return either(d1, either4(d2, d3, d4, d5));
84
- }
62
+ function _either(...decoders: $ReadOnlyArray<Decoder<mixed>>): Decoder<mixed> {
63
+ if (decoders.length === 0) {
64
+ throw new Error('Pass at least one decoder to either()');
65
+ }
85
66
 
86
- export function either6<T1, T2, T3, T4, T5, T6>(
87
- d1: Decoder<T1>,
88
- d2: Decoder<T2>,
89
- d3: Decoder<T3>,
90
- d4: Decoder<T4>,
91
- d5: Decoder<T5>,
92
- d6: Decoder<T6>,
93
- ): Decoder<T1 | T2 | T3 | T4 | T5 | T6> {
94
- return either(d1, either5(d2, d3, d4, d5, d6));
95
- }
67
+ return (blob: mixed) => {
68
+ // Collect errors here along the way
69
+ const errors = [];
96
70
 
97
- export function either7<T1, T2, T3, T4, T5, T6, T7>(
98
- d1: Decoder<T1>,
99
- d2: Decoder<T2>,
100
- d3: Decoder<T3>,
101
- d4: Decoder<T4>,
102
- d5: Decoder<T5>,
103
- d6: Decoder<T6>,
104
- d7: Decoder<T7>,
105
- ): Decoder<T1 | T2 | T3 | T4 | T5 | T6 | T7> {
106
- return either(d1, either6(d2, d3, d4, d5, d6, d7));
107
- }
71
+ for (let i = 0; i < decoders.length; i++) {
72
+ const result: DecodeResult<mixed> = decoders[i](blob);
73
+ if (result.ok) {
74
+ return result;
75
+ } else {
76
+ errors.push(result.error);
77
+ }
78
+ }
108
79
 
109
- export function either8<T1, T2, T3, T4, T5, T6, T7, T8>(
110
- d1: Decoder<T1>,
111
- d2: Decoder<T2>,
112
- d3: Decoder<T3>,
113
- d4: Decoder<T4>,
114
- d5: Decoder<T5>,
115
- d6: Decoder<T6>,
116
- d7: Decoder<T7>,
117
- d8: Decoder<T8>,
118
- ): Decoder<T1 | T2 | T3 | T4 | T5 | T6 | T7 | T8> {
119
- return either(d1, either7(d2, d3, d4, d5, d6, d7, d8));
80
+ // Decoding all alternatives failed, return the combined error message
81
+ const text =
82
+ EITHER_PREFIX +
83
+ errors.map((err) => nest(summarize(err).join('\n'))).join('\n');
84
+ return err(annotate(blob, text));
85
+ };
120
86
  }
121
87
 
122
- export function either9<T1, T2, T3, T4, T5, T6, T7, T8, T9>(
123
- d1: Decoder<T1>,
124
- d2: Decoder<T2>,
125
- d3: Decoder<T3>,
126
- d4: Decoder<T4>,
127
- d5: Decoder<T5>,
128
- d6: Decoder<T6>,
129
- d7: Decoder<T7>,
130
- d8: Decoder<T8>,
131
- d9: Decoder<T9>,
132
- ): Decoder<T1 | T2 | T3 | T4 | T5 | T6 | T7 | T8 | T9> {
133
- return either(d1, either8(d2, d3, d4, d5, d6, d7, d8, d9));
134
- }
88
+ export const either: EitherDecoderSignatures = (_either: _Any);
135
89
 
136
90
  export function oneOf<T: Scalar>(constants: $ReadOnlyArray<T>): Decoder<T> {
137
91
  return (blob: mixed) => {
138
92
  const winner = constants.find((c) => c === blob);
139
93
  if (winner !== undefined) {
140
- return Result.ok(winner);
94
+ return ok(winner);
141
95
  }
142
- return Result.err(
96
+ return err(
143
97
  annotate(
144
98
  blob,
145
99
  `Must be one of ${constants
@@ -0,0 +1,90 @@
1
+ import { annotate } from '../annotate.mjs';
2
+ import { err, ok } from '../result.mjs';
3
+ import { indent, summarize } from '../_utils.mjs';
4
+ var EITHER_PREFIX = 'Either:\n';
5
+ /**
6
+ * Indents and adds a dash in front of this (potentially multiline) string.
7
+ */
8
+
9
+ function itemize(s) {
10
+ return '-' + indent(s).substring(1);
11
+ }
12
+ /**
13
+ * Nests another error as an item under a new-to-be-created "Either error". If
14
+ * the given subitem already is an "Either error" of itself, don't indent, but
15
+ * just "inject" its items at the same error level, for nicely flattened either
16
+ * expressions.
17
+ *
18
+ * Avoids:
19
+ *
20
+ * Either:
21
+ * - Either:
22
+ * - Must be P
23
+ * - Either:
24
+ * - Must be Q
25
+ * - Must be R
26
+ * - Must be S
27
+ *
28
+ * And "flattens" these to:
29
+ *
30
+ * Either:
31
+ * - Must be P
32
+ * - Must be Q
33
+ * - Must be R
34
+ * - Must be S
35
+ *
36
+ */
37
+
38
+
39
+ function nest(errText) {
40
+ return errText.startsWith(EITHER_PREFIX) ? errText.substr(EITHER_PREFIX.length) : itemize(errText);
41
+ } // prettier-ignore
42
+
43
+
44
+ function _either() {
45
+ for (var _len = arguments.length, decoders = new Array(_len), _key = 0; _key < _len; _key++) {
46
+ decoders[_key] = arguments[_key];
47
+ }
48
+
49
+ if (decoders.length === 0) {
50
+ throw new Error('Pass at least one decoder to either()');
51
+ }
52
+
53
+ return function (blob) {
54
+ // Collect errors here along the way
55
+ var errors = [];
56
+
57
+ for (var _i = 0; _i < decoders.length; _i++) {
58
+ var result = decoders[_i](blob);
59
+
60
+ if (result.ok) {
61
+ return result;
62
+ } else {
63
+ errors.push(result.error);
64
+ }
65
+ } // Decoding all alternatives failed, return the combined error message
66
+
67
+
68
+ var text = EITHER_PREFIX + errors.map(function (err) {
69
+ return nest(summarize(err).join('\n'));
70
+ }).join('\n');
71
+ return err(annotate(blob, text));
72
+ };
73
+ }
74
+
75
+ export var either = _either;
76
+ export function oneOf(constants) {
77
+ return function (blob) {
78
+ var winner = constants.find(function (c) {
79
+ return c === blob;
80
+ });
81
+
82
+ if (winner !== undefined) {
83
+ return ok(winner);
84
+ }
85
+
86
+ return err(annotate(blob, "Must be one of " + constants.map(function (value) {
87
+ return JSON.stringify(value);
88
+ }).join(', ')));
89
+ };
90
+ }
package/core/fail.d.ts ADDED
@@ -0,0 +1,3 @@
1
+ import { Decoder } from '../_types';
2
+
3
+ export function fail(msg: string): Decoder<never>;
package/core/fail.js CHANGED
@@ -3,19 +3,15 @@
3
3
  exports.__esModule = true;
4
4
  exports.fail = fail;
5
5
 
6
- var Result = _interopRequireWildcard(require("../result"));
7
-
8
6
  var _annotate = require("../annotate");
9
7
 
10
- function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function _getRequireWildcardCache(nodeInterop) { return nodeInterop ? cacheNodeInterop : cacheBabelInterop; })(nodeInterop); }
11
-
12
- function _interopRequireWildcard(obj, nodeInterop) { if (!nodeInterop && obj && obj.__esModule) { return obj; } if (obj === null || typeof obj !== "object" && typeof obj !== "function") { return { "default": obj }; } var cache = _getRequireWildcardCache(nodeInterop); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj["default"] = obj; if (cache) { cache.set(obj, newObj); } return newObj; }
8
+ var _result = require("../result");
13
9
 
14
10
  /**
15
11
  * Decoder that always fails with the given error message, no matter what the input.
16
12
  */
17
13
  function fail(msg) {
18
14
  return function (blob) {
19
- return Result.err((0, _annotate.annotate)(blob, msg));
15
+ return (0, _result.err)((0, _annotate.annotate)(blob, msg));
20
16
  };
21
17
  }
package/core/fail.js.flow CHANGED
@@ -1,12 +1,12 @@
1
1
  // @flow strict
2
2
 
3
- import * as Result from '../result';
4
3
  import { annotate } from '../annotate';
4
+ import { err } from '../result';
5
5
  import type { Decoder } from '../_types';
6
6
 
7
7
  /**
8
8
  * Decoder that always fails with the given error message, no matter what the input.
9
9
  */
10
10
  export function fail(msg: string): Decoder<empty> {
11
- return (blob: mixed) => Result.err(annotate(blob, msg));
11
+ return (blob: mixed) => err(annotate(blob, msg));
12
12
  }
@@ -1,11 +1,11 @@
1
- import * as Result from '../result';
2
- import { annotate } from '../annotate';
1
+ import { annotate } from '../annotate.mjs';
2
+ import { err } from '../result.mjs';
3
3
 
4
4
  /**
5
5
  * Decoder that always fails with the given error message, no matter what the input.
6
6
  */
7
7
  export function fail(msg) {
8
8
  return function (blob) {
9
- return Result.err(annotate(blob, msg));
9
+ return err(annotate(blob, msg));
10
10
  };
11
11
  }
@@ -0,0 +1,3 @@
1
+ import { Decoder } from '../_types';
2
+
3
+ export function instanceOf<T>(klass: new (...args: readonly any[]) => T): Decoder<T>;
@@ -3,17 +3,13 @@
3
3
  exports.__esModule = true;
4
4
  exports.instanceOf = instanceOf;
5
5
 
6
- var Result = _interopRequireWildcard(require("../result"));
7
-
8
6
  var _annotate = require("../annotate");
9
7
 
10
- function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function _getRequireWildcardCache(nodeInterop) { return nodeInterop ? cacheNodeInterop : cacheBabelInterop; })(nodeInterop); }
11
-
12
- function _interopRequireWildcard(obj, nodeInterop) { if (!nodeInterop && obj && obj.__esModule) { return obj; } if (obj === null || typeof obj !== "object" && typeof obj !== "function") { return { "default": obj }; } var cache = _getRequireWildcardCache(nodeInterop); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj["default"] = obj; if (cache) { cache.set(obj, newObj); } return newObj; }
8
+ var _result = require("../result");
13
9
 
14
10
  function instanceOf(klass) {
15
11
  return function (blob) {
16
- return blob instanceof klass ? Result.ok(blob) : Result.err((0, _annotate.annotate)(blob, "Must be " + // $FlowFixMe[incompatible-use] - klass.name is fine?
12
+ return blob instanceof klass ? (0, _result.ok)(blob) : (0, _result.err)((0, _annotate.annotate)(blob, "Must be " + // $FlowFixMe[incompatible-use] - klass.name is fine?
17
13
  klass.name + " instance"));
18
14
  };
19
15
  }
@@ -1,14 +1,14 @@
1
1
  // @flow strict
2
2
 
3
- import * as Result from '../result';
4
3
  import { annotate } from '../annotate';
4
+ import { err, ok } from '../result';
5
5
  import type { Decoder } from '../_types';
6
6
 
7
7
  export function instanceOf<T>(klass: Class<T>): Decoder<T> {
8
8
  return (blob: mixed) =>
9
9
  blob instanceof klass
10
- ? Result.ok(blob)
11
- : Result.err(
10
+ ? ok(blob)
11
+ : err(
12
12
  annotate(
13
13
  blob,
14
14
  `Must be ${
@@ -0,0 +1,8 @@
1
+ import { annotate } from '../annotate.mjs';
2
+ import { err, ok } from '../result.mjs';
3
+ export function instanceOf(klass) {
4
+ return function (blob) {
5
+ return blob instanceof klass ? ok(blob) : err(annotate(blob, "Must be " + // $FlowFixMe[incompatible-use] - klass.name is fine?
6
+ klass.name + " instance"));
7
+ };
8
+ }