decoders 1.25.5 → 2.0.0-beta2

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 (151) hide show
  1. package/CHANGELOG.md +23 -3
  2. package/_esm/_guard.js +15 -0
  3. package/_esm/_guard.js.flow +20 -0
  4. package/_esm/_types.js +0 -0
  5. package/_esm/_types.js.flow +20 -0
  6. package/_esm/_utils.js +93 -0
  7. package/_esm/_utils.js.flow +97 -0
  8. package/_esm/annotate.js +144 -0
  9. package/_esm/annotate.js.flow +218 -0
  10. package/_esm/core/array.js +91 -0
  11. package/{array.js.flow → _esm/core/array.js.flow} +22 -25
  12. package/_esm/core/boolean.js +28 -0
  13. package/{boolean.js.flow → _esm/core/boolean.js.flow} +8 -7
  14. package/_esm/core/composition.js +42 -0
  15. package/{utils.js.flow → _esm/core/composition.js.flow} +7 -22
  16. package/_esm/core/constants.js +46 -0
  17. package/{constants.js.flow → _esm/core/constants.js.flow} +13 -12
  18. package/_esm/core/date.js +28 -0
  19. package/{date.js.flow → _esm/core/date.js.flow} +9 -7
  20. package/{describe.js → _esm/core/describe.js} +5 -11
  21. package/{describe.js.flow → _esm/core/describe.js.flow} +4 -4
  22. package/{dispatch.js → _esm/core/dispatch.js} +8 -15
  23. package/{dispatch.js.flow → _esm/core/dispatch.js.flow} +6 -5
  24. package/_esm/core/either.js +90 -0
  25. package/{either.js.flow → _esm/core/either.js.flow} +55 -35
  26. package/_esm/core/fail.js +11 -0
  27. package/_esm/core/fail.js.flow +12 -0
  28. package/_esm/core/instanceOf.js +8 -0
  29. package/{instanceOf.js.flow → _esm/core/instanceOf.js.flow} +7 -8
  30. package/_esm/core/json.js +15 -0
  31. package/{json.js.flow → _esm/core/json.js.flow} +4 -4
  32. package/_esm/core/lazy.js +11 -0
  33. package/{lazy.js.flow → _esm/core/lazy.js.flow} +1 -1
  34. package/_esm/core/mapping.js +54 -0
  35. package/_esm/core/mapping.js.flow +54 -0
  36. package/_esm/core/number.js +25 -0
  37. package/{number.js.flow → _esm/core/number.js.flow} +10 -11
  38. package/_esm/core/object.js +175 -0
  39. package/{object.js.flow → _esm/core/object.js.flow} +44 -52
  40. package/_esm/core/optional.js +38 -0
  41. package/{optional.js.flow → _esm/core/optional.js.flow} +6 -7
  42. package/{string.js → _esm/core/string.js} +18 -35
  43. package/{string.js.flow → _esm/core/string.js.flow} +13 -12
  44. package/_esm/core/tuple.js +155 -0
  45. package/{tuple.js.flow → _esm/core/tuple.js.flow} +62 -63
  46. package/_esm/format/index.js +2 -0
  47. package/_esm/format/index.js.flow +4 -0
  48. package/_esm/format/inline.js +137 -0
  49. package/_esm/format/inline.js.flow +122 -0
  50. package/_esm/format/short.js +4 -0
  51. package/_esm/format/short.js.flow +8 -0
  52. package/_esm/index.js +37 -0
  53. package/_esm/index.js.flow +56 -0
  54. package/_esm/result.js +139 -0
  55. package/_esm/result.js.flow +166 -0
  56. package/_guard.js +26 -0
  57. package/_guard.js.flow +20 -0
  58. package/_types.js +1 -0
  59. package/_types.js.flow +20 -0
  60. package/_utils.js +108 -0
  61. package/_utils.js.flow +97 -0
  62. package/annotate.js +161 -0
  63. package/annotate.js.flow +218 -0
  64. package/core/array.js +108 -0
  65. package/core/array.js.flow +103 -0
  66. package/core/boolean.js +44 -0
  67. package/core/boolean.js.flow +29 -0
  68. package/core/composition.js +56 -0
  69. package/core/composition.js.flow +43 -0
  70. package/core/constants.js +69 -0
  71. package/core/constants.js.flow +46 -0
  72. package/core/date.js +46 -0
  73. package/core/date.js.flow +40 -0
  74. package/core/describe.js +26 -0
  75. package/core/describe.js.flow +17 -0
  76. package/core/dispatch.js +62 -0
  77. package/core/dispatch.js.flow +58 -0
  78. package/core/either.js +117 -0
  79. package/core/either.js.flow +151 -0
  80. package/core/fail.js +21 -0
  81. package/core/fail.js.flow +12 -0
  82. package/core/instanceOf.js +19 -0
  83. package/core/instanceOf.js.flow +20 -0
  84. package/{json.js → core/json.js} +3 -5
  85. package/core/json.js.flow +28 -0
  86. package/{lazy.js → core/lazy.js} +1 -3
  87. package/core/lazy.js.flow +15 -0
  88. package/core/mapping.js +67 -0
  89. package/core/mapping.js.flow +54 -0
  90. package/core/number.js +40 -0
  91. package/core/number.js.flow +34 -0
  92. package/core/object.js +194 -0
  93. package/core/object.js.flow +203 -0
  94. package/core/optional.js +54 -0
  95. package/core/optional.js.flow +41 -0
  96. package/core/string.js +98 -0
  97. package/core/string.js.flow +82 -0
  98. package/core/tuple.js +173 -0
  99. package/core/tuple.js.flow +220 -0
  100. package/format/index.js +12 -0
  101. package/format/index.js.flow +4 -0
  102. package/format/inline.js +146 -0
  103. package/format/inline.js.flow +122 -0
  104. package/format/short.js +10 -0
  105. package/format/short.js.flow +8 -0
  106. package/index.js +118 -395
  107. package/index.js.flow +24 -24
  108. package/package.json +2 -6
  109. package/result.js +172 -0
  110. package/result.js.flow +166 -0
  111. package/array.d.ts +0 -5
  112. package/array.js +0 -133
  113. package/boolean.d.ts +0 -5
  114. package/boolean.js +0 -42
  115. package/constants.d.ts +0 -11
  116. package/constants.js +0 -67
  117. package/date.d.ts +0 -4
  118. package/date.js +0 -42
  119. package/describe.d.ts +0 -3
  120. package/dispatch.d.ts +0 -8
  121. package/either.d.ts +0 -61
  122. package/either.js +0 -85
  123. package/fail.d.ts +0 -3
  124. package/fail.js +0 -19
  125. package/fail.js.flow +0 -13
  126. package/guard.d.ts +0 -7
  127. package/guard.js +0 -30
  128. package/guard.js.flow +0 -36
  129. package/helpers.d.ts +0 -79
  130. package/index.d.ts +0 -38
  131. package/instanceOf.d.ts +0 -3
  132. package/instanceOf.js +0 -17
  133. package/json.d.ts +0 -11
  134. package/lazy.d.ts +0 -3
  135. package/mapping.d.ts +0 -4
  136. package/mapping.js +0 -113
  137. package/mapping.js.flow +0 -71
  138. package/number.d.ts +0 -6
  139. package/number.js +0 -38
  140. package/object.d.ts +0 -33
  141. package/object.js +0 -254
  142. package/optional.d.ts +0 -5
  143. package/optional.js +0 -52
  144. package/string.d.ts +0 -7
  145. package/tuple.d.ts +0 -30
  146. package/tuple.js +0 -199
  147. package/types.d.ts +0 -18
  148. package/types.js +0 -5
  149. package/types.js.flow +0 -26
  150. package/utils.d.ts +0 -13
  151. package/utils.js +0 -70
@@ -0,0 +1,90 @@
1
+ import * as Result from '../result';
2
+ import { annotate } from '../annotate';
3
+ import { indent, summarize } from '../_utils';
4
+
5
+ /**
6
+ * Indents and adds a dash in front of this (potentially multiline) string.
7
+ */
8
+ function itemize(s) {
9
+ return '-' + indent(s).substring(1);
10
+ }
11
+ /**
12
+ * Nests another error as an item under a new-to-be-created "Either error". If
13
+ * the given subitem already is an "Either error" of itself, don't indent, but
14
+ * just "inject" its items at the same error level, for nicely flattened either
15
+ * expressions.
16
+ *
17
+ * Avoids:
18
+ *
19
+ * Either:
20
+ * - Either:
21
+ * - Must be P
22
+ * - Either:
23
+ * - Must be Q
24
+ * - Must be R
25
+ * - Must be S
26
+ *
27
+ * And "flattens" these to:
28
+ *
29
+ * Either:
30
+ * - Must be P
31
+ * - Must be Q
32
+ * - Must be R
33
+ * - Must be S
34
+ *
35
+ */
36
+
37
+
38
+ function nest(errText) {
39
+ var EITHER_PREFIX = 'Either:\n';
40
+ return errText.startsWith(EITHER_PREFIX) ? errText.substr(EITHER_PREFIX.length) : itemize(errText);
41
+ }
42
+
43
+ export function either(d1, d2) {
44
+ return function (blob) {
45
+ return Result.orElse(d1(blob), function (err1) {
46
+ return Result.orElse(d2(blob), function (err2) {
47
+ var serr1 = summarize(err1).join('\n');
48
+ var serr2 = summarize(err2).join('\n');
49
+ var text = ['Either:', nest(serr1), nest(serr2)].join('\n');
50
+ return Result.err(annotate(blob, text));
51
+ });
52
+ });
53
+ };
54
+ }
55
+ export function either3(d1, d2, d3) {
56
+ return either(d1, either(d2, d3));
57
+ }
58
+ export function either4(d1, d2, d3, d4) {
59
+ return either(d1, either3(d2, d3, d4));
60
+ }
61
+ export function either5(d1, d2, d3, d4, d5) {
62
+ return either(d1, either4(d2, d3, d4, d5));
63
+ }
64
+ export function either6(d1, d2, d3, d4, d5, d6) {
65
+ return either(d1, either5(d2, d3, d4, d5, d6));
66
+ }
67
+ export function either7(d1, d2, d3, d4, d5, d6, d7) {
68
+ return either(d1, either6(d2, d3, d4, d5, d6, d7));
69
+ }
70
+ export function either8(d1, d2, d3, d4, d5, d6, d7, d8) {
71
+ return either(d1, either7(d2, d3, d4, d5, d6, d7, d8));
72
+ }
73
+ export function either9(d1, d2, d3, d4, d5, d6, d7, d8, d9) {
74
+ return either(d1, either8(d2, d3, d4, d5, d6, d7, d8, d9));
75
+ }
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 Result.ok(winner);
84
+ }
85
+
86
+ return Result.err(annotate(blob, "Must be one of " + constants.map(function (value) {
87
+ return JSON.stringify(value);
88
+ }).join(', ')));
89
+ };
90
+ }
@@ -1,45 +1,65 @@
1
1
  // @flow strict
2
2
 
3
- import { annotate, indent } from 'debrief';
4
- import { summarize } from 'debrief';
5
- import { Err, Ok } from 'lemons/Result';
6
-
7
- import type { Decoder, Scalar } from './types';
3
+ import * as Result from '../result';
4
+ import { annotate } from '../annotate';
5
+ import { indent, summarize } from '../_utils';
6
+ import type { Decoder, Scalar } from '../_types';
8
7
 
9
8
  /**
10
9
  * Indents and adds a dash in front of this (potentially multiline) string.
11
10
  */
12
- // istanbul ignore next
13
- function itemize(s: string = ''): string {
11
+ function itemize(s: string): string {
14
12
  return '-' + indent(s).substring(1);
15
13
  }
16
14
 
15
+ /**
16
+ * Nests another error as an item under a new-to-be-created "Either error". If
17
+ * the given subitem already is an "Either error" of itself, don't indent, but
18
+ * just "inject" its items at the same error level, for nicely flattened either
19
+ * expressions.
20
+ *
21
+ * Avoids:
22
+ *
23
+ * Either:
24
+ * - Either:
25
+ * - Must be P
26
+ * - Either:
27
+ * - Must be Q
28
+ * - Must be R
29
+ * - Must be S
30
+ *
31
+ * And "flattens" these to:
32
+ *
33
+ * Either:
34
+ * - Must be P
35
+ * - Must be Q
36
+ * - Must be R
37
+ * - Must be S
38
+ *
39
+ */
40
+ function nest(errText: string): string {
41
+ const EITHER_PREFIX = 'Either:\n';
42
+ return errText.startsWith(EITHER_PREFIX)
43
+ ? errText.substr(EITHER_PREFIX.length)
44
+ : itemize(errText);
45
+ }
46
+
17
47
  export function either<T1, T2>(d1: Decoder<T1>, d2: Decoder<T2>): Decoder<T1 | T2> {
18
48
  return (blob: mixed) =>
19
- d1(blob).dispatch(
20
- (value1) => Ok(value1),
21
- (err1) =>
22
- d2(blob).dispatch(
23
- (value2) => Ok(value2),
24
- (err2) =>
25
- Err(
26
- annotate(
27
- blob,
28
- [
29
- 'Either:',
30
- itemize(summarize(err1).join('\n')),
31
- itemize(summarize(err2).join('\n')),
32
- ].join('\n')
33
- )
34
- )
35
- )
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
+ }),
36
56
  );
37
57
  }
38
58
 
39
59
  export function either3<T1, T2, T3>(
40
60
  d1: Decoder<T1>,
41
61
  d2: Decoder<T2>,
42
- d3: Decoder<T3>
62
+ d3: Decoder<T3>,
43
63
  ): Decoder<T1 | T2 | T3> {
44
64
  return either(d1, either(d2, d3));
45
65
  }
@@ -48,7 +68,7 @@ export function either4<T1, T2, T3, T4>(
48
68
  d1: Decoder<T1>,
49
69
  d2: Decoder<T2>,
50
70
  d3: Decoder<T3>,
51
- d4: Decoder<T4>
71
+ d4: Decoder<T4>,
52
72
  ): Decoder<T1 | T2 | T3 | T4> {
53
73
  return either(d1, either3(d2, d3, d4));
54
74
  }
@@ -58,7 +78,7 @@ export function either5<T1, T2, T3, T4, T5>(
58
78
  d2: Decoder<T2>,
59
79
  d3: Decoder<T3>,
60
80
  d4: Decoder<T4>,
61
- d5: Decoder<T5>
81
+ d5: Decoder<T5>,
62
82
  ): Decoder<T1 | T2 | T3 | T4 | T5> {
63
83
  return either(d1, either4(d2, d3, d4, d5));
64
84
  }
@@ -69,7 +89,7 @@ export function either6<T1, T2, T3, T4, T5, T6>(
69
89
  d3: Decoder<T3>,
70
90
  d4: Decoder<T4>,
71
91
  d5: Decoder<T5>,
72
- d6: Decoder<T6>
92
+ d6: Decoder<T6>,
73
93
  ): Decoder<T1 | T2 | T3 | T4 | T5 | T6> {
74
94
  return either(d1, either5(d2, d3, d4, d5, d6));
75
95
  }
@@ -81,7 +101,7 @@ export function either7<T1, T2, T3, T4, T5, T6, T7>(
81
101
  d4: Decoder<T4>,
82
102
  d5: Decoder<T5>,
83
103
  d6: Decoder<T6>,
84
- d7: Decoder<T7>
104
+ d7: Decoder<T7>,
85
105
  ): Decoder<T1 | T2 | T3 | T4 | T5 | T6 | T7> {
86
106
  return either(d1, either6(d2, d3, d4, d5, d6, d7));
87
107
  }
@@ -94,7 +114,7 @@ export function either8<T1, T2, T3, T4, T5, T6, T7, T8>(
94
114
  d5: Decoder<T5>,
95
115
  d6: Decoder<T6>,
96
116
  d7: Decoder<T7>,
97
- d8: Decoder<T8>
117
+ d8: Decoder<T8>,
98
118
  ): Decoder<T1 | T2 | T3 | T4 | T5 | T6 | T7 | T8> {
99
119
  return either(d1, either7(d2, d3, d4, d5, d6, d7, d8));
100
120
  }
@@ -108,7 +128,7 @@ export function either9<T1, T2, T3, T4, T5, T6, T7, T8, T9>(
108
128
  d6: Decoder<T6>,
109
129
  d7: Decoder<T7>,
110
130
  d8: Decoder<T8>,
111
- d9: Decoder<T9>
131
+ d9: Decoder<T9>,
112
132
  ): Decoder<T1 | T2 | T3 | T4 | T5 | T6 | T7 | T8 | T9> {
113
133
  return either(d1, either8(d2, d3, d4, d5, d6, d7, d8, d9));
114
134
  }
@@ -117,15 +137,15 @@ export function oneOf<T: Scalar>(constants: $ReadOnlyArray<T>): Decoder<T> {
117
137
  return (blob: mixed) => {
118
138
  const winner = constants.find((c) => c === blob);
119
139
  if (winner !== undefined) {
120
- return Ok(winner);
140
+ return Result.ok(winner);
121
141
  }
122
- return Err(
142
+ return Result.err(
123
143
  annotate(
124
144
  blob,
125
145
  `Must be one of ${constants
126
146
  .map((value) => JSON.stringify(value))
127
- .join(', ')}`
128
- )
147
+ .join(', ')}`,
148
+ ),
129
149
  );
130
150
  };
131
151
  }
@@ -0,0 +1,11 @@
1
+ import * as Result from '../result';
2
+ import { annotate } from '../annotate';
3
+
4
+ /**
5
+ * Decoder that always fails with the given error message, no matter what the input.
6
+ */
7
+ export function fail(msg) {
8
+ return function (blob) {
9
+ return Result.err(annotate(blob, msg));
10
+ };
11
+ }
@@ -0,0 +1,12 @@
1
+ // @flow strict
2
+
3
+ import * as Result from '../result';
4
+ import { annotate } from '../annotate';
5
+ import type { Decoder } from '../_types';
6
+
7
+ /**
8
+ * Decoder that always fails with the given error message, no matter what the input.
9
+ */
10
+ export function fail(msg: string): Decoder<empty> {
11
+ return (blob: mixed) => Result.err(annotate(blob, msg));
12
+ }
@@ -0,0 +1,8 @@
1
+ import * as Result from '../result';
2
+ import { annotate } from '../annotate';
3
+ export function instanceOf(klass) {
4
+ return function (blob) {
5
+ return blob instanceof klass ? Result.ok(blob) : Result.err(annotate(blob, "Must be " + // $FlowFixMe[incompatible-use] - klass.name is fine?
6
+ klass.name + " instance"));
7
+ };
8
+ }
@@ -1,21 +1,20 @@
1
1
  // @flow strict
2
2
 
3
- import { annotate } from 'debrief';
4
- import { Err, Ok } from 'lemons/Result';
5
-
6
- import type { Decoder } from './types';
3
+ import * as Result from '../result';
4
+ import { annotate } from '../annotate';
5
+ import type { Decoder } from '../_types';
7
6
 
8
7
  export function instanceOf<T>(klass: Class<T>): Decoder<T> {
9
8
  return (blob: mixed) =>
10
9
  blob instanceof klass
11
- ? Ok(blob)
12
- : Err(
10
+ ? Result.ok(blob)
11
+ : Result.err(
13
12
  annotate(
14
13
  blob,
15
14
  `Must be ${
16
15
  // $FlowFixMe[incompatible-use] - klass.name is fine?
17
16
  klass.name
18
- } instance`
19
- )
17
+ } instance`,
18
+ ),
20
19
  );
21
20
  }
@@ -0,0 +1,15 @@
1
+ import { array } from './array';
2
+ import { boolean as _boolean } from './boolean';
3
+ import { dict } from './mapping';
4
+ import { either6 } from './either';
5
+ import { lazy } from './lazy';
6
+ import { null_ } from './constants';
7
+ import { number } from './number';
8
+ import { string } from './string';
9
+ export var jsonObject = lazy(function () {
10
+ return dict(json);
11
+ });
12
+ export var jsonArray = lazy(function () {
13
+ return array(json);
14
+ });
15
+ export var json = either6(null_, string, number, _boolean, jsonObject, jsonArray);
@@ -2,13 +2,13 @@
2
2
 
3
3
  import { array } from './array';
4
4
  import { boolean } from './boolean';
5
- import { null_ } from './constants';
5
+ import { dict } from './mapping';
6
6
  import { either6 } from './either';
7
7
  import { lazy } from './lazy';
8
- import { dict } from './mapping';
8
+ import { null_ } from './constants';
9
9
  import { number } from './number';
10
10
  import { string } from './string';
11
- import type { Decoder } from './types';
11
+ import type { Decoder } from '../_types';
12
12
 
13
13
  export type JSONValue = null | string | number | boolean | JSONObject | JSONArray;
14
14
  export type JSONObject = { [string]: JSONValue };
@@ -24,5 +24,5 @@ export const json: Decoder<JSONValue> = either6(
24
24
  number,
25
25
  boolean,
26
26
  jsonObject,
27
- jsonArray
27
+ jsonArray,
28
28
  );
@@ -0,0 +1,11 @@
1
+ /**
2
+ * Given an function returning a Decoder, will use that decoder to decode the
3
+ * value. This is typically used to build decoders for recursive or
4
+ * self-referential types.
5
+ */
6
+ export function lazy(decoderFn) {
7
+ return function (blob) {
8
+ var decoder = decoderFn();
9
+ return decoder(blob);
10
+ };
11
+ }
@@ -1,6 +1,6 @@
1
1
  // @flow strict
2
2
 
3
- import type { Decoder } from './types';
3
+ import type { Decoder } from '../_types';
4
4
 
5
5
  /**
6
6
  * Given an function returning a Decoder, will use that decoder to decode the
@@ -0,0 +1,54 @@
1
+ import * as Result from '../result';
2
+ import { annotateObject } from '../annotate';
3
+ import { compose, map } from './composition';
4
+ import { merge } from '../annotate';
5
+ import { pojo } from './object';
6
+
7
+ /**
8
+ * Given an object, will decode a Map of string keys to whatever values.
9
+ *
10
+ * For example, given a decoder for a Person, we can verify a Person lookup
11
+ * table structure (of type Map<string, Person>) like so:
12
+ *
13
+ * mapping(person)
14
+ *
15
+ */
16
+ export function mapping(decoder) {
17
+ return compose(pojo, function (blob) {
18
+ var tuples = [];
19
+ var errors = null;
20
+ Object.keys(blob).forEach(function (key) {
21
+ var value = blob[key];
22
+ var result = decoder(value);
23
+
24
+ if (result.type === 'ok') {
25
+ if (errors === null) {
26
+ tuples.push([key, result.value]);
27
+ }
28
+ } else {
29
+ tuples.length = 0; // Clear the tuples array
30
+
31
+ if (errors === null) {
32
+ errors = {};
33
+ }
34
+
35
+ errors[key] = result.error;
36
+ }
37
+ });
38
+
39
+ if (errors !== null) {
40
+ return Result.err(merge(annotateObject(blob), errors));
41
+ } else {
42
+ return Result.ok(new Map(tuples));
43
+ }
44
+ });
45
+ }
46
+ /**
47
+ * Like mapping(), but returns an object rather than a Map instance.
48
+ */
49
+
50
+ export function dict(decoder) {
51
+ return map(mapping(decoder), function (m) {
52
+ return Object.fromEntries(m);
53
+ });
54
+ }
@@ -0,0 +1,54 @@
1
+ // @flow strict
2
+
3
+ import * as Result from '../result';
4
+ import { annotateObject } from '../annotate';
5
+ import { compose, map } from './composition';
6
+ import { merge } from '../annotate';
7
+ import { pojo } from './object';
8
+ import type { Annotation } from '../annotate';
9
+ import type { Decoder } from '../_types';
10
+
11
+ /**
12
+ * Given an object, will decode a Map of string keys to whatever values.
13
+ *
14
+ * For example, given a decoder for a Person, we can verify a Person lookup
15
+ * table structure (of type Map<string, Person>) like so:
16
+ *
17
+ * mapping(person)
18
+ *
19
+ */
20
+ export function mapping<T>(decoder: Decoder<T>): Decoder<Map<string, T>> {
21
+ return compose(pojo, (blob: { +[key: string]: mixed }) => {
22
+ let tuples: Array<[string, T]> = [];
23
+ let errors: { [key: string]: Annotation } | null = null;
24
+
25
+ Object.keys(blob).forEach((key: string) => {
26
+ const value = blob[key];
27
+ const result = decoder(value);
28
+ if (result.type === 'ok') {
29
+ if (errors === null) {
30
+ tuples.push([key, result.value]);
31
+ }
32
+ } else {
33
+ tuples.length = 0; // Clear the tuples array
34
+ if (errors === null) {
35
+ errors = {};
36
+ }
37
+ errors[key] = result.error;
38
+ }
39
+ });
40
+
41
+ if (errors !== null) {
42
+ return Result.err(merge(annotateObject(blob), errors));
43
+ } else {
44
+ return Result.ok(new Map(tuples));
45
+ }
46
+ });
47
+ }
48
+
49
+ /**
50
+ * Like mapping(), but returns an object rather than a Map instance.
51
+ */
52
+ export function dict<T>(decoder: Decoder<T>): Decoder<{ [string]: T }> {
53
+ return map(mapping(decoder), (m) => Object.fromEntries(m));
54
+ }
@@ -0,0 +1,25 @@
1
+ import * as Result from '../result';
2
+ import { annotate } from '../annotate';
3
+ import { compose, predicate } from './composition';
4
+
5
+ var anyNumber = function anyNumber(blob) {
6
+ return typeof blob === 'number' && !Number.isNaN(blob) ? Result.ok(blob) : Result.err(annotate(blob, 'Must be number'));
7
+ };
8
+
9
+ var isInteger = function isInteger(n) {
10
+ return Number.isInteger(n);
11
+ };
12
+
13
+ var isFinite = function isFinite(n) {
14
+ return Number.isFinite(n);
15
+ };
16
+
17
+ export var number = compose(anyNumber, predicate(isFinite, 'Number must be finite'));
18
+ export var positiveNumber = compose(number, predicate(function (n) {
19
+ return n >= 0;
20
+ }, 'Number must be positive')); // Integers
21
+
22
+ export var integer = compose(number, predicate(isInteger, 'Number must be an integer'));
23
+ export var positiveInteger = compose(integer, predicate(function (n) {
24
+ return n >= 0;
25
+ }, 'Number must be positive'));
@@ -1,15 +1,14 @@
1
1
  // @flow strict
2
2
 
3
- import { annotate } from 'debrief';
4
- import { Err, Ok } from 'lemons/Result';
5
-
6
- import type { Decoder } from './types';
7
- import { compose, predicate } from './utils';
3
+ import * as Result from '../result';
4
+ import { annotate } from '../annotate';
5
+ import { compose, predicate } from './composition';
6
+ import type { Decoder } from '../_types';
8
7
 
9
8
  const anyNumber: Decoder<number> = (blob: mixed) => {
10
9
  return typeof blob === 'number' && !Number.isNaN(blob)
11
- ? Ok(blob)
12
- : Err(annotate(blob, 'Must be number'));
10
+ ? Result.ok(blob)
11
+ : Result.err(annotate(blob, 'Must be number'));
13
12
  };
14
13
 
15
14
  const isInteger = (n: number) => Number.isInteger(n);
@@ -17,19 +16,19 @@ const isFinite = (n: number) => Number.isFinite(n);
17
16
 
18
17
  export const number: Decoder<number> = compose(
19
18
  anyNumber,
20
- predicate(isFinite, 'Number must be finite')
19
+ predicate(isFinite, 'Number must be finite'),
21
20
  );
22
21
  export const positiveNumber: Decoder<number> = compose(
23
22
  number,
24
- predicate((n) => n >= 0, 'Number must be positive')
23
+ predicate((n) => n >= 0, 'Number must be positive'),
25
24
  );
26
25
 
27
26
  // Integers
28
27
  export const integer: Decoder<number> = compose(
29
28
  number,
30
- predicate(isInteger, 'Number must be an integer')
29
+ predicate(isInteger, 'Number must be an integer'),
31
30
  );
32
31
  export const positiveInteger: Decoder<number> = compose(
33
32
  integer,
34
- predicate((n) => n >= 0, 'Number must be positive')
33
+ predicate((n) => n >= 0, 'Number must be positive'),
35
34
  );