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
@@ -2,27 +2,26 @@
2
2
 
3
3
  exports.__esModule = true;
4
4
  exports.compose = compose;
5
- exports.map = map;
6
5
  exports.predicate = predicate;
6
+ exports.prep = prep;
7
+ exports.transform = transform;
7
8
 
8
- var Result = _interopRequireWildcard(require("../result"));
9
+ var _result = require("../result");
9
10
 
10
11
  var _annotate = require("../annotate");
11
12
 
12
- 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); }
13
-
14
- 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; }
15
-
16
13
  /**
17
- * Given a decoder T and a mapping function from T's to V's, returns a decoder
18
- * for V's. This is useful to change the original input data.
14
+ * Accepts any value the given decoder accepts, and on success, will call the
15
+ * mapper value **on the decoded result**. If the mapper function throws an
16
+ * error, the whole decoder will fail using the error message as the failure
17
+ * reason.
19
18
  */
20
- function map(decoder, mapper) {
19
+ function transform(decoder, transformFn) {
21
20
  return compose(decoder, function (x) {
22
21
  try {
23
- return Result.ok(mapper(x));
22
+ return (0, _result.ok)(transformFn(x));
24
23
  } catch (e) {
25
- return Result.err((0, _annotate.annotate)(x, e instanceof Error ? e.message : String(e)));
24
+ return (0, _result.err)((0, _annotate.annotate)(x, e instanceof Error ? e.message : String(e)));
26
25
  }
27
26
  });
28
27
  }
@@ -40,7 +39,7 @@ function map(decoder, mapper) {
40
39
 
41
40
  function compose(decoder, next) {
42
41
  return function (blob) {
43
- return Result.andThen(decoder(blob), next);
42
+ return (0, _result.andThen)(decoder(blob), next);
44
43
  };
45
44
  }
46
45
  /**
@@ -49,8 +48,35 @@ function compose(decoder, next) {
49
48
  */
50
49
 
51
50
 
52
- function predicate(predicate, msg) {
53
- return function (value) {
54
- return predicate(value) ? Result.ok(value) : Result.err((0, _annotate.annotate)(value, msg));
51
+ function predicate(decoder, predicateFn, msg) {
52
+ return function (blob) {
53
+ return (0, _result.andThen)(decoder(blob), function (value) {
54
+ return predicateFn(value) ? (0, _result.ok)(value) : (0, _result.err)((0, _annotate.annotate)(value, msg));
55
+ });
56
+ };
57
+ }
58
+ /**
59
+ * Pre-process the data input before passing it into the decoder. This gives
60
+ * you the ability to arbitrarily customize the input on the fly before passing
61
+ * it to the decoder. Of course, the input value at that point is still of
62
+ * `unknown` type, so you will have to deal with that accordingly.
63
+ */
64
+
65
+
66
+ function prep(mapperFn, decoder) {
67
+ return function (blob) {
68
+ var blob2;
69
+
70
+ try {
71
+ blob2 = mapperFn(blob);
72
+ } catch (e) {
73
+ return (0, _result.err)((0, _annotate.annotate)(blob, e.message));
74
+ }
75
+
76
+ return (0, _result.orElse)(decoder(blob2), function (ann) {
77
+ return (0, _result.err)((0, _annotate.annotate)(blob, ann.text));
78
+ } // ^^^^ Annotates the _original_ input value
79
+ // (instead of echoing back blob2 in the output)
80
+ );
55
81
  };
56
82
  }
@@ -1,19 +1,21 @@
1
1
  // @flow strict
2
2
 
3
- import * as Result from '../result';
3
+ import { andThen, err, ok, orElse } from '../result';
4
4
  import { annotate } from '../annotate';
5
5
  import type { Decoder } from '../_types';
6
6
 
7
7
  /**
8
- * Given a decoder T and a mapping function from T's to V's, returns a decoder
9
- * for V's. This is useful to change the original input data.
8
+ * Accepts any value the given decoder accepts, and on success, will call the
9
+ * mapper value **on the decoded result**. If the mapper function throws an
10
+ * error, the whole decoder will fail using the error message as the failure
11
+ * reason.
10
12
  */
11
- export function map<T, V>(decoder: Decoder<T>, mapper: (T) => V): Decoder<V> {
13
+ export function transform<T, V>(decoder: Decoder<T>, transformFn: (T) => V): Decoder<V> {
12
14
  return compose(decoder, (x) => {
13
15
  try {
14
- return Result.ok(mapper(x));
16
+ return ok(transformFn(x));
15
17
  } catch (e) {
16
- return Result.err(annotate(x, e instanceof Error ? e.message : String(e)));
18
+ return err(annotate(x, e instanceof Error ? e.message : String(e)));
17
19
  }
18
20
  });
19
21
  }
@@ -29,15 +31,44 @@ export function map<T, V>(decoder: Decoder<T>, mapper: (T) => V): Decoder<V> {
29
31
  * argument.
30
32
  */
31
33
  export function compose<T, V>(decoder: Decoder<T>, next: Decoder<V, T>): Decoder<V> {
32
- return (blob: mixed) => Result.andThen(decoder(blob), next);
34
+ return (blob: mixed) => andThen(decoder(blob), next);
33
35
  }
34
36
 
35
37
  /**
36
38
  * Factory function returning a Decoder<T>, given a predicate function that
37
39
  * accepts/rejects the input of type T.
38
40
  */
39
- export function predicate<T>(predicate: (T) => boolean, msg: string): Decoder<T, T> {
40
- return (value: T) => {
41
- return predicate(value) ? Result.ok(value) : Result.err(annotate(value, msg));
41
+ export function predicate<T>(
42
+ decoder: Decoder<T>,
43
+ predicateFn: (T) => boolean,
44
+ msg: string,
45
+ ): Decoder<T> {
46
+ return (blob: mixed) =>
47
+ andThen(decoder(blob), (value) =>
48
+ predicateFn(value) ? ok(value) : err(annotate(value, msg)),
49
+ );
50
+ }
51
+
52
+ /**
53
+ * Pre-process the data input before passing it into the decoder. This gives
54
+ * you the ability to arbitrarily customize the input on the fly before passing
55
+ * it to the decoder. Of course, the input value at that point is still of
56
+ * `unknown` type, so you will have to deal with that accordingly.
57
+ */
58
+ export function prep<I, T>(mapperFn: (mixed) => I, decoder: Decoder<T, I>): Decoder<T> {
59
+ return (blob: mixed) => {
60
+ let blob2;
61
+ try {
62
+ blob2 = mapperFn(blob);
63
+ } catch (e) {
64
+ return err(annotate(blob, e.message));
65
+ }
66
+
67
+ return orElse(
68
+ decoder(blob2),
69
+ (ann) => err(annotate(blob, ann.text)),
70
+ // ^^^^ Annotates the _original_ input value
71
+ // (instead of echoing back blob2 in the output)
72
+ );
42
73
  };
43
74
  }
@@ -0,0 +1,70 @@
1
+ import { andThen, err, ok, orElse } from '../result.mjs';
2
+ import { annotate } from '../annotate.mjs';
3
+
4
+ /**
5
+ * Accepts any value the given decoder accepts, and on success, will call the
6
+ * mapper value **on the decoded result**. If the mapper function throws an
7
+ * error, the whole decoder will fail using the error message as the failure
8
+ * reason.
9
+ */
10
+ export function transform(decoder, transformFn) {
11
+ return compose(decoder, function (x) {
12
+ try {
13
+ return ok(transformFn(x));
14
+ } catch (e) {
15
+ return err(annotate(x, e instanceof Error ? e.message : String(e)));
16
+ }
17
+ });
18
+ }
19
+ /**
20
+ * Compose two decoders by passing the result of the first into the second.
21
+ * The second decoder may assume as its input type the output type of the first
22
+ * decoder (so it's not necessary to accept the typical "mixed"). This is
23
+ * useful for "narrowing down" the checks. For example, if you want to write
24
+ * a decoder for positive numbers, you can compose it from an existing decoder
25
+ * for any number, and a decoder that, assuming a number, checks if it's
26
+ * positive. Very often combined with the predicate() helper as the second
27
+ * argument.
28
+ */
29
+
30
+ export function compose(decoder, next) {
31
+ return function (blob) {
32
+ return andThen(decoder(blob), next);
33
+ };
34
+ }
35
+ /**
36
+ * Factory function returning a Decoder<T>, given a predicate function that
37
+ * accepts/rejects the input of type T.
38
+ */
39
+
40
+ export function predicate(decoder, predicateFn, msg) {
41
+ return function (blob) {
42
+ return andThen(decoder(blob), function (value) {
43
+ return predicateFn(value) ? ok(value) : err(annotate(value, msg));
44
+ });
45
+ };
46
+ }
47
+ /**
48
+ * Pre-process the data input before passing it into the decoder. This gives
49
+ * you the ability to arbitrarily customize the input on the fly before passing
50
+ * it to the decoder. Of course, the input value at that point is still of
51
+ * `unknown` type, so you will have to deal with that accordingly.
52
+ */
53
+
54
+ export function prep(mapperFn, decoder) {
55
+ return function (blob) {
56
+ var blob2;
57
+
58
+ try {
59
+ blob2 = mapperFn(blob);
60
+ } catch (e) {
61
+ return err(annotate(blob, e.message));
62
+ }
63
+
64
+ return orElse(decoder(blob2), function (ann) {
65
+ return err(annotate(blob, ann.text));
66
+ } // ^^^^ Annotates the _original_ input value
67
+ // (instead of echoing back blob2 in the output)
68
+ );
69
+ };
70
+ }
@@ -0,0 +1,11 @@
1
+ import { Decoder, Scalar } from '../_types';
2
+
3
+ // Constants
4
+
5
+ export const null_: Decoder<null>;
6
+ export const undefined_: Decoder<undefined>;
7
+ export function constant<T extends Scalar>(value: T): Decoder<T>;
8
+ export function hardcoded<T extends Scalar>(value: T): Decoder<T>;
9
+ export function hardcoded<T>(value: T): Decoder<T>;
10
+ export const mixed: Decoder<unknown>;
11
+ export const unknown: Decoder<unknown>;
package/core/constants.js CHANGED
@@ -5,19 +5,15 @@ exports.constant = constant;
5
5
  exports.hardcoded = hardcoded;
6
6
  exports.unknown = exports.undefined_ = exports.null_ = exports.mixed = void 0;
7
7
 
8
- var Result = _interopRequireWildcard(require("../result"));
9
-
10
8
  var _annotate = require("../annotate");
11
9
 
12
- 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); }
13
-
14
- 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; }
10
+ var _result = require("../result");
15
11
 
16
12
  /**
17
13
  * Decoder that only returns Ok for `null` inputs. Err otherwise.
18
14
  */
19
15
  var null_ = function null_(blob) {
20
- return blob === null ? Result.ok(blob) : Result.err((0, _annotate.annotate)(blob, 'Must be null'));
16
+ return blob === null ? (0, _result.ok)(blob) : (0, _result.err)((0, _annotate.annotate)(blob, 'Must be null'));
21
17
  };
22
18
  /**
23
19
  * Decoder that only returns Ok for `undefined` inputs. Err otherwise.
@@ -27,7 +23,7 @@ var null_ = function null_(blob) {
27
23
  exports.null_ = null_;
28
24
 
29
25
  var undefined_ = function undefined_(blob) {
30
- return blob === undefined ? Result.ok(blob) : Result.err((0, _annotate.annotate)(blob, 'Must be undefined'));
26
+ return blob === undefined ? (0, _result.ok)(blob) : (0, _result.err)((0, _annotate.annotate)(blob, 'Must be undefined'));
31
27
  };
32
28
  /**
33
29
  * Decoder that only returns Ok for the given value constant. Err otherwise.
@@ -38,7 +34,7 @@ exports.undefined_ = undefined_;
38
34
 
39
35
  function constant(value) {
40
36
  return function (blob) {
41
- return blob === value ? Result.ok(value) : Result.err((0, _annotate.annotate)(blob, "Must be constant " + String(value)));
37
+ return blob === value ? (0, _result.ok)(value) : (0, _result.err)((0, _annotate.annotate)(blob, "Must be constant " + String(value)));
42
38
  };
43
39
  }
44
40
  /**
@@ -48,7 +44,7 @@ function constant(value) {
48
44
 
49
45
  function hardcoded(value) {
50
46
  return function () {
51
- return Result.ok(value);
47
+ return (0, _result.ok)(value);
52
48
  };
53
49
  }
54
50
  /**
@@ -57,7 +53,7 @@ function hardcoded(value) {
57
53
 
58
54
 
59
55
  var unknown = function unknown(blob) {
60
- return Result.ok(blob);
56
+ return (0, _result.ok)(blob);
61
57
  };
62
58
  /**
63
59
  * Alias of unknown.
@@ -1,22 +1,20 @@
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, Scalar } from '../_types';
6
6
 
7
7
  /**
8
8
  * Decoder that only returns Ok for `null` inputs. Err otherwise.
9
9
  */
10
10
  export const null_: Decoder<null> = (blob: mixed) =>
11
- blob === null ? Result.ok(blob) : Result.err(annotate(blob, 'Must be null'));
11
+ blob === null ? ok(blob) : err(annotate(blob, 'Must be null'));
12
12
 
13
13
  /**
14
14
  * Decoder that only returns Ok for `undefined` inputs. Err otherwise.
15
15
  */
16
16
  export const undefined_: Decoder<void> = (blob: mixed) =>
17
- blob === undefined
18
- ? Result.ok(blob)
19
- : Result.err(annotate(blob, 'Must be undefined'));
17
+ blob === undefined ? ok(blob) : err(annotate(blob, 'Must be undefined'));
20
18
 
21
19
  /**
22
20
  * Decoder that only returns Ok for the given value constant. Err otherwise.
@@ -24,21 +22,21 @@ export const undefined_: Decoder<void> = (blob: mixed) =>
24
22
  export function constant<T: Scalar>(value: T): Decoder<T> {
25
23
  return (blob: mixed) =>
26
24
  blob === value
27
- ? Result.ok(value)
28
- : Result.err(annotate(blob, `Must be constant ${String(value)}`));
25
+ ? ok(value)
26
+ : err(annotate(blob, `Must be constant ${String(value)}`));
29
27
  }
30
28
 
31
29
  /**
32
30
  * Decoder that always returns Ok for the given hardcoded value, no matter what the input.
33
31
  */
34
32
  export function hardcoded<T>(value: T): Decoder<T> {
35
- return () => Result.ok(value);
33
+ return () => ok(value);
36
34
  }
37
35
 
38
36
  /**
39
37
  * Decoder that always returns Ok for the given hardcoded value, no matter what the input.
40
38
  */
41
- export const unknown: Decoder<mixed> = (blob: mixed) => Result.ok(blob);
39
+ export const unknown: Decoder<mixed> = (blob: mixed) => ok(blob);
42
40
 
43
41
  /**
44
42
  * Alias of unknown.
@@ -1,18 +1,18 @@
1
- import * as Result from '../result';
2
- import { annotate } from '../annotate';
1
+ import { annotate } from '../annotate.mjs';
2
+ import { err, ok } from '../result.mjs';
3
3
 
4
4
  /**
5
5
  * Decoder that only returns Ok for `null` inputs. Err otherwise.
6
6
  */
7
7
  export var null_ = function null_(blob) {
8
- return blob === null ? Result.ok(blob) : Result.err(annotate(blob, 'Must be null'));
8
+ return blob === null ? ok(blob) : err(annotate(blob, 'Must be null'));
9
9
  };
10
10
  /**
11
11
  * Decoder that only returns Ok for `undefined` inputs. Err otherwise.
12
12
  */
13
13
 
14
14
  export var undefined_ = function undefined_(blob) {
15
- return blob === undefined ? Result.ok(blob) : Result.err(annotate(blob, 'Must be undefined'));
15
+ return blob === undefined ? ok(blob) : err(annotate(blob, 'Must be undefined'));
16
16
  };
17
17
  /**
18
18
  * Decoder that only returns Ok for the given value constant. Err otherwise.
@@ -20,7 +20,7 @@ export var undefined_ = function undefined_(blob) {
20
20
 
21
21
  export function constant(value) {
22
22
  return function (blob) {
23
- return blob === value ? Result.ok(value) : Result.err(annotate(blob, "Must be constant " + String(value)));
23
+ return blob === value ? ok(value) : err(annotate(blob, "Must be constant " + String(value)));
24
24
  };
25
25
  }
26
26
  /**
@@ -29,7 +29,7 @@ export function constant(value) {
29
29
 
30
30
  export function hardcoded(value) {
31
31
  return function () {
32
- return Result.ok(value);
32
+ return ok(value);
33
33
  };
34
34
  }
35
35
  /**
@@ -37,7 +37,7 @@ export function hardcoded(value) {
37
37
  */
38
38
 
39
39
  export var unknown = function unknown(blob) {
40
- return Result.ok(blob);
40
+ return ok(blob);
41
41
  };
42
42
  /**
43
43
  * Alias of unknown.
package/core/date.d.ts ADDED
@@ -0,0 +1,4 @@
1
+ import { Decoder } from '../_types';
2
+
3
+ export const date: Decoder<Date>;
4
+ export const iso8601: Decoder<Date>;
package/core/date.js CHANGED
@@ -3,19 +3,15 @@
3
3
  exports.__esModule = true;
4
4
  exports.iso8601 = exports.date = void 0;
5
5
 
6
- var Result = _interopRequireWildcard(require("../result"));
7
-
8
6
  var _annotate = require("../annotate");
9
7
 
10
- var _utils = require("../_utils");
8
+ var _result = require("../result");
11
9
 
12
- var _composition = require("./composition");
10
+ var _utils = require("../_utils");
13
11
 
14
12
  var _string = require("./string");
15
13
 
16
- 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); }
17
-
18
- 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; }
14
+ var _composition = require("./composition");
19
15
 
20
16
  // Only matches the shape. This "over-matches" some values that still aren't
21
17
  // valid dates (like 9999-99-99), but those will be caught by JS Date's
@@ -23,7 +19,7 @@ function _interopRequireWildcard(obj, nodeInterop) { if (!nodeInterop && obj &&
23
19
  var iso8601_re = /^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}(?:[.]\d+)?(?:Z|[+-]\d{2}:?\d{2})$/;
24
20
 
25
21
  var date = function date(value) {
26
- return (0, _utils.isDate)(value) ? Result.ok(value) : Result.err((0, _annotate.annotate)(value, 'Must be a Date'));
22
+ return (0, _utils.isDate)(value) ? (0, _result.ok)(value) : (0, _result.err)((0, _annotate.annotate)(value, 'Must be a Date'));
27
23
  };
28
24
  /**
29
25
  * Decoder that only returns Ok for strings that are valid ISO8601 date
@@ -32,7 +28,7 @@ var date = function date(value) {
32
28
 
33
29
 
34
30
  exports.date = date;
35
- var iso8601 = (0, _composition.map)( // Input itself needs to match the ISO8601 regex...
31
+ var iso8601 = (0, _composition.transform)( // Input itself needs to match the ISO8601 regex...
36
32
  (0, _string.regex)(iso8601_re, 'Must be ISO8601 format'), // Make sure it is a _valid_ date
37
33
  function (value) {
38
34
  var date = new Date(value);
package/core/date.js.flow CHANGED
@@ -1,10 +1,10 @@
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 { isDate } from '../_utils';
6
- import { map } from './composition';
7
6
  import { regex } from './string';
7
+ import { transform } from './composition';
8
8
  import type { Decoder } from '../_types';
9
9
 
10
10
  // $FlowFixMe[unclear-type] (not really an issue) - deliberate casting
@@ -17,15 +17,13 @@ const iso8601_re =
17
17
  /^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}(?:[.]\d+)?(?:Z|[+-]\d{2}:?\d{2})$/;
18
18
 
19
19
  export const date: Decoder<Date> = (value: mixed) =>
20
- isDate(value)
21
- ? Result.ok(((value: cast): Date))
22
- : Result.err(annotate(value, 'Must be a Date'));
20
+ isDate(value) ? ok(((value: cast): Date)) : err(annotate(value, 'Must be a Date'));
23
21
 
24
22
  /**
25
23
  * Decoder that only returns Ok for strings that are valid ISO8601 date
26
24
  * strings. Err otherwise.
27
25
  */
28
- export const iso8601: Decoder<Date> = map(
26
+ export const iso8601: Decoder<Date> = transform(
29
27
  // Input itself needs to match the ISO8601 regex...
30
28
  regex(iso8601_re, 'Must be ISO8601 format'),
31
29
 
@@ -1,21 +1,21 @@
1
- import * as Result from '../result';
2
- import { annotate } from '../annotate';
3
- import { isDate } from '../_utils';
4
- import { map } from './composition';
5
- import { regex } from './string';
1
+ import { annotate } from '../annotate.mjs';
2
+ import { err, ok } from '../result.mjs';
3
+ import { isDate } from '../_utils.mjs';
4
+ import { regex } from './string.mjs';
5
+ import { transform } from './composition.mjs';
6
6
  // Only matches the shape. This "over-matches" some values that still aren't
7
7
  // valid dates (like 9999-99-99), but those will be caught by JS Date's
8
8
  // internal validations
9
9
  var iso8601_re = /^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}(?:[.]\d+)?(?:Z|[+-]\d{2}:?\d{2})$/;
10
10
  export var date = function date(value) {
11
- return isDate(value) ? Result.ok(value) : Result.err(annotate(value, 'Must be a Date'));
11
+ return isDate(value) ? ok(value) : err(annotate(value, 'Must be a Date'));
12
12
  };
13
13
  /**
14
14
  * Decoder that only returns Ok for strings that are valid ISO8601 date
15
15
  * strings. Err otherwise.
16
16
  */
17
17
 
18
- export var iso8601 = map( // Input itself needs to match the ISO8601 regex...
18
+ export var iso8601 = transform( // Input itself needs to match the ISO8601 regex...
19
19
  regex(iso8601_re, 'Must be ISO8601 format'), // Make sure it is a _valid_ date
20
20
  function (value) {
21
21
  var date = new Date(value);
@@ -0,0 +1,3 @@
1
+ import { Decoder } from '../_types';
2
+
3
+ export function describe<T>(decoder: Decoder<T>, msg: string): Decoder<T>;
package/core/describe.js CHANGED
@@ -3,13 +3,9 @@
3
3
  exports.__esModule = true;
4
4
  exports.describe = describe;
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
  * Wrap another decoder, and override the error message in case it fails. This
@@ -19,7 +15,7 @@ function _interopRequireWildcard(obj, nodeInterop) { if (!nodeInterop && obj &&
19
15
  */
20
16
  function describe(decoder, message) {
21
17
  return function (blob) {
22
- return Result.mapError(decoder(blob), function (err) {
18
+ return (0, _result.mapError)(decoder(blob), function (err) {
23
19
  return (0, _annotate.annotate)(err, message);
24
20
  });
25
21
  };
@@ -1,7 +1,7 @@
1
1
  // @flow strict
2
2
 
3
- import * as Result from '../result';
4
3
  import { annotate } from '../annotate';
4
+ import { mapError } from '../result';
5
5
  import type { Decoder } from '../_types';
6
6
 
7
7
  /**
@@ -12,6 +12,6 @@ import type { Decoder } from '../_types';
12
12
  */
13
13
  export function describe<T>(decoder: Decoder<T>, message: string): Decoder<T> {
14
14
  return (blob: mixed) => {
15
- return Result.mapError(decoder(blob), (err) => annotate(err, message));
15
+ return mapError(decoder(blob), (err) => annotate(err, message));
16
16
  };
17
17
  }
@@ -1,5 +1,5 @@
1
- import * as Result from '../result';
2
- import { annotate } from '../annotate';
1
+ import { annotate } from '../annotate.mjs';
2
+ import { mapError } from '../result.mjs';
3
3
 
4
4
  /**
5
5
  * Wrap another decoder, and override the error message in case it fails. This
@@ -9,7 +9,7 @@ import { annotate } from '../annotate';
9
9
  */
10
10
  export function describe(decoder, message) {
11
11
  return function (blob) {
12
- return Result.mapError(decoder(blob), function (err) {
12
+ return mapError(decoder(blob), function (err) {
13
13
  return annotate(err, message);
14
14
  });
15
15
  };
@@ -0,0 +1,8 @@
1
+ import { Decoder, DecoderType } from '../_types';
2
+
3
+ export type Values<T extends object> = T[keyof T];
4
+
5
+ export function taggedUnion<O extends { [key: string]: Decoder<any> }>(
6
+ field: string,
7
+ mapping: O,
8
+ ): Decoder<Values<{ [key in keyof O]: DecoderType<O[key]> }>>;
package/core/dispatch.js CHANGED
@@ -1,17 +1,15 @@
1
1
  "use strict";
2
2
 
3
3
  exports.__esModule = true;
4
- exports.dispatch = dispatch;
4
+ exports.taggedUnion = taggedUnion;
5
5
 
6
- var Result = _interopRequireWildcard(require("../result"));
6
+ var _result = require("../result");
7
7
 
8
8
  var _object2 = require("./object");
9
9
 
10
10
  var _either = require("./either");
11
11
 
12
- 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); }
13
-
14
- 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; }
12
+ var _composition = require("./composition");
15
13
 
16
14
  /**
17
15
  * Dispatches to one of several given decoders, based on the value found at
@@ -35,25 +33,25 @@ function _interopRequireWildcard(obj, nodeInterop) { if (!nodeInterop && obj &&
35
33
  * Then these two decoders are equivalent:
36
34
  *
37
35
  * const shape = either(rectangle, circle)
38
- * const shape = dispatch('type', { rectangle, circle })
36
+ * const shape = taggedUnion('type', { rectangle, circle })
39
37
  *
40
38
  * Will be of type Decoder<Rectangle | Circle>.
41
39
  *
42
- * But the dispatch version will typically be more runtime-efficient. The
43
- * reason is that it will first do minimal work to "look ahead" into the `type`
44
- * field here, and based on that value, pick the decoder to invoke.
40
+ * But `taggedUnion` will typically be more runtime-efficient. The reason is
41
+ * that it will first do minimal work to "look ahead" into the `type` field
42
+ * here, and based on that value, pick the decoder to invoke.
45
43
  *
46
44
  * The `either` version will simply try to invoke each decoder, until it finds
47
45
  * one that matches.
48
46
  *
49
- * Also, the error messages will be less ambiguous using `dispatch()`.
47
+ * Also, the error messages will be less ambiguous using `taggedUnion()`.
50
48
  */
51
- function dispatch(field, mapping) {
49
+ function taggedUnion(field, mapping) {
52
50
  var _object;
53
51
 
54
- var base = (0, _object2.object)((_object = {}, _object[field] = (0, _either.oneOf)(Object.keys(mapping)), _object));
52
+ var base = (0, _object2.object)((_object = {}, _object[field] = (0, _composition.prep)(String, (0, _either.oneOf)(Object.keys(mapping))), _object));
55
53
  return function (blob) {
56
- return Result.andThen(base(blob), function (baseObj) {
54
+ return (0, _result.andThen)(base(blob), function (baseObj) {
57
55
  var decoderName = baseObj[field];
58
56
  var decoder = mapping[decoderName];
59
57
  return decoder(blob);