decoders 1.26.0-test1 → 2.0.0-beta4

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 (153) hide show
  1. package/CHANGELOG.md +27 -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/{cjs → _esm/core}/array.js.flow +22 -25
  12. package/_esm/core/boolean.js +28 -0
  13. package/{cjs → _esm/core}/boolean.js.flow +8 -7
  14. package/_esm/core/composition.js +42 -0
  15. package/{cjs/utils.js.flow → _esm/core/composition.js.flow} +7 -22
  16. package/_esm/core/constants.js +46 -0
  17. package/{cjs → _esm/core}/constants.js.flow +13 -12
  18. package/_esm/core/date.js +28 -0
  19. package/{cjs → _esm/core}/date.js.flow +9 -7
  20. package/{cjs → _esm/core}/describe.js +5 -11
  21. package/{cjs → _esm/core}/describe.js.flow +4 -4
  22. package/{cjs → _esm/core}/dispatch.js +8 -15
  23. package/{cjs → _esm/core}/dispatch.js.flow +6 -5
  24. package/_esm/core/either.js +90 -0
  25. package/{cjs → _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/{cjs → _esm/core}/instanceOf.js.flow +7 -8
  30. package/_esm/core/json.js +15 -0
  31. package/{cjs → _esm/core}/json.js.flow +4 -4
  32. package/_esm/core/lazy.js +11 -0
  33. package/{cjs → _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/{cjs → _esm/core}/number.js.flow +10 -11
  38. package/_esm/core/object.js +175 -0
  39. package/{cjs → _esm/core}/object.js.flow +44 -52
  40. package/_esm/core/optional.js +38 -0
  41. package/{cjs → _esm/core}/optional.js.flow +6 -7
  42. package/{cjs → _esm/core}/string.js +18 -35
  43. package/{cjs → _esm/core}/string.js.flow +13 -12
  44. package/_esm/core/tuple.js +155 -0
  45. package/{cjs → _esm/core}/tuple.js.flow +59 -65
  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/{cjs → _esm}/index.js.flow +31 -24
  54. package/_esm/result.js +142 -0
  55. package/_esm/result.js.flow +170 -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/{cjs → core}/json.js +3 -5
  85. package/core/json.js.flow +28 -0
  86. package/{cjs → 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 +215 -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 +120 -0
  107. package/index.js.flow +63 -0
  108. package/package.json +3 -8
  109. package/result.js +177 -0
  110. package/result.js.flow +170 -0
  111. package/cjs/array.js +0 -133
  112. package/cjs/boolean.js +0 -42
  113. package/cjs/constants.js +0 -67
  114. package/cjs/date.js +0 -42
  115. package/cjs/either.js +0 -85
  116. package/cjs/fail.js +0 -19
  117. package/cjs/fail.js.flow +0 -13
  118. package/cjs/guard.js +0 -30
  119. package/cjs/guard.js.flow +0 -36
  120. package/cjs/index.js +0 -397
  121. package/cjs/instanceOf.js +0 -17
  122. package/cjs/mapping.js +0 -113
  123. package/cjs/mapping.js.flow +0 -71
  124. package/cjs/number.js +0 -38
  125. package/cjs/object.js +0 -254
  126. package/cjs/optional.js +0 -52
  127. package/cjs/tuple.js +0 -199
  128. package/cjs/types.js +0 -5
  129. package/cjs/types.js.flow +0 -26
  130. package/cjs/utils.js +0 -70
  131. package/es/index.js +0 -1212
  132. package/ts/array.d.ts +0 -5
  133. package/ts/boolean.d.ts +0 -5
  134. package/ts/constants.d.ts +0 -11
  135. package/ts/date.d.ts +0 -4
  136. package/ts/describe.d.ts +0 -3
  137. package/ts/dispatch.d.ts +0 -8
  138. package/ts/either.d.ts +0 -61
  139. package/ts/fail.d.ts +0 -3
  140. package/ts/guard.d.ts +0 -7
  141. package/ts/helpers.d.ts +0 -79
  142. package/ts/index.d.ts +0 -38
  143. package/ts/instanceOf.d.ts +0 -3
  144. package/ts/json.d.ts +0 -11
  145. package/ts/lazy.d.ts +0 -3
  146. package/ts/mapping.d.ts +0 -4
  147. package/ts/number.d.ts +0 -6
  148. package/ts/object.d.ts +0 -33
  149. package/ts/optional.d.ts +0 -5
  150. package/ts/string.d.ts +0 -7
  151. package/ts/tuple.d.ts +0 -30
  152. package/ts/types.d.ts +0 -18
  153. package/ts/utils.d.ts +0 -13
@@ -0,0 +1,155 @@
1
+ import { annotate } from '../annotate';
2
+ import { compose, predicate } from './composition';
3
+ import { err, ok, okOrErrValue, unwrap } from '../result';
4
+ import { poja } from './array';
5
+
6
+ var ntuple = function ntuple(n) {
7
+ return compose(poja, predicate(function (arr) {
8
+ return arr.length === n;
9
+ }, "Must be a " + n + "-tuple"));
10
+ };
11
+ /**
12
+ * Builds a Decoder that returns Ok for 1-tuple of [T], given a Decoder for T.
13
+ * Err otherwise.
14
+ */
15
+
16
+
17
+ export function tuple1(decoder1) {
18
+ return compose(ntuple(1), function (blobs) {
19
+ var blob1 = blobs[0];
20
+ var result1 = decoder1(blob1);
21
+
22
+ try {
23
+ return ok([unwrap(result1)]);
24
+ } catch (e) {
25
+ // If a decoder error has happened while unwrapping all the
26
+ // results, try to construct a good error message
27
+ return err(annotate([okOrErrValue(result1)]));
28
+ }
29
+ });
30
+ }
31
+ /**
32
+ * Builds a Decoder that returns Ok for 2-tuples of [T1, T2], given Decoders
33
+ * for T1 and T2. Err otherwise.
34
+ */
35
+
36
+ export function tuple2(decoder1, decoder2) {
37
+ return compose(ntuple(2), function (blobs) {
38
+ var blob1 = blobs[0],
39
+ blob2 = blobs[1];
40
+ var result1 = decoder1(blob1);
41
+ var result2 = decoder2(blob2);
42
+
43
+ try {
44
+ return ok([unwrap(result1), unwrap(result2)]);
45
+ } catch (e) {
46
+ // If a decoder error has happened while unwrapping all the
47
+ // results, try to construct a good error message
48
+ return err(annotate([okOrErrValue(result1), okOrErrValue(result2)]));
49
+ }
50
+ });
51
+ }
52
+ /**
53
+ * Builds a Decoder that returns Ok for 3-tuples of [T1, T2, T3], given
54
+ * Decoders for T1, T2, and T3. Err otherwise.
55
+ */
56
+
57
+ export function tuple3(decoder1, decoder2, decoder3) {
58
+ return compose(ntuple(3), function (blobs) {
59
+ var blob1 = blobs[0],
60
+ blob2 = blobs[1],
61
+ blob3 = blobs[2];
62
+ var result1 = decoder1(blob1);
63
+ var result2 = decoder2(blob2);
64
+ var result3 = decoder3(blob3);
65
+
66
+ try {
67
+ return ok([unwrap(result1), unwrap(result2), unwrap(result3)]);
68
+ } catch (e) {
69
+ // If a decoder error has happened while unwrapping all the
70
+ // results, try to construct a good error message
71
+ return err(annotate([okOrErrValue(result1), okOrErrValue(result2), okOrErrValue(result3)]));
72
+ }
73
+ });
74
+ }
75
+ /**
76
+ * Builds a Decoder that returns Ok for 4-tuples of [T1, T2, T3, T4], given
77
+ * Decoders for T1, T2, T3, and T4. Err otherwise.
78
+ */
79
+
80
+ export function tuple4(decoder1, decoder2, decoder3, decoder4) {
81
+ return compose(ntuple(4), function (blobs) {
82
+ var blob1 = blobs[0],
83
+ blob2 = blobs[1],
84
+ blob3 = blobs[2],
85
+ blob4 = blobs[3];
86
+ var result1 = decoder1(blob1);
87
+ var result2 = decoder2(blob2);
88
+ var result3 = decoder3(blob3);
89
+ var result4 = decoder4(blob4);
90
+
91
+ try {
92
+ return ok([unwrap(result1), unwrap(result2), unwrap(result3), unwrap(result4)]);
93
+ } catch (e) {
94
+ // If a decoder error has happened while unwrapping all the
95
+ // results, try to construct a good error message
96
+ return err(annotate([okOrErrValue(result1), okOrErrValue(result2), okOrErrValue(result3), okOrErrValue(result4)]));
97
+ }
98
+ });
99
+ }
100
+ /**
101
+ * Builds a Decoder that returns Ok for 5-tuples of [T1, T2, T3, T4, T5], given
102
+ * Decoders for T1, T2, T3, T4, and T5. Err otherwise.
103
+ */
104
+
105
+ export function tuple5(decoder1, decoder2, decoder3, decoder4, decoder5) {
106
+ return compose(ntuple(5), function (blobs) {
107
+ var blob1 = blobs[0],
108
+ blob2 = blobs[1],
109
+ blob3 = blobs[2],
110
+ blob4 = blobs[3],
111
+ blob5 = blobs[4];
112
+ var result1 = decoder1(blob1);
113
+ var result2 = decoder2(blob2);
114
+ var result3 = decoder3(blob3);
115
+ var result4 = decoder4(blob4);
116
+ var result5 = decoder5(blob5);
117
+
118
+ try {
119
+ return ok([unwrap(result1), unwrap(result2), unwrap(result3), unwrap(result4), unwrap(result5)]);
120
+ } catch (e) {
121
+ // If a decoder error has happened while unwrapping all the
122
+ // results, try to construct a good error message
123
+ return err(annotate([okOrErrValue(result1), okOrErrValue(result2), okOrErrValue(result3), okOrErrValue(result4), okOrErrValue(result5)]));
124
+ }
125
+ });
126
+ }
127
+ /**
128
+ * Builds a Decoder that returns Ok for 5-tuples of [T1, T2, T3, T4, T5], given
129
+ * Decoders for T1, T2, T3, T4, T5, and T6. Err otherwise.
130
+ */
131
+
132
+ export function tuple6(decoder1, decoder2, decoder3, decoder4, decoder5, decoder6) {
133
+ return compose(ntuple(6), function (blobs) {
134
+ var blob1 = blobs[0],
135
+ blob2 = blobs[1],
136
+ blob3 = blobs[2],
137
+ blob4 = blobs[3],
138
+ blob5 = blobs[4],
139
+ blob6 = blobs[5];
140
+ var result1 = decoder1(blob1);
141
+ var result2 = decoder2(blob2);
142
+ var result3 = decoder3(blob3);
143
+ var result4 = decoder4(blob4);
144
+ var result5 = decoder5(blob5);
145
+ var result6 = decoder6(blob6);
146
+
147
+ try {
148
+ return ok([unwrap(result1), unwrap(result2), unwrap(result3), unwrap(result4), unwrap(result5), unwrap(result6)]);
149
+ } catch (e) {
150
+ // If a decoder error has happened while unwrapping all the
151
+ // results, try to construct a good error message
152
+ return err(annotate([okOrErrValue(result1), okOrErrValue(result2), okOrErrValue(result3), okOrErrValue(result4), okOrErrValue(result5), okOrErrValue(result6)]));
153
+ }
154
+ });
155
+ }
@@ -1,16 +1,15 @@
1
1
  // @flow strict
2
2
 
3
- import { annotate } from 'debrief';
4
- import { Err, Ok } from 'lemons/Result';
5
-
3
+ import { annotate } from '../annotate';
4
+ import { compose, predicate } from './composition';
5
+ import { err, ok, okOrErrValue, unwrap } from '../result';
6
6
  import { poja } from './array';
7
- import type { Decoder } from './types';
8
- import { compose, predicate } from './utils';
7
+ import type { Decoder } from '../_types';
9
8
 
10
9
  const ntuple = (n: number) =>
11
10
  compose(
12
11
  poja,
13
- predicate((arr) => arr.length === n, `Must be a ${n}-tuple`)
12
+ predicate((arr) => arr.length === n, `Must be a ${n}-tuple`),
14
13
  );
15
14
 
16
15
  /**
@@ -23,11 +22,11 @@ export function tuple1<T>(decoder1: Decoder<T>): Decoder<[T]> {
23
22
 
24
23
  const result1 = decoder1(blob1);
25
24
  try {
26
- return Ok([result1.unwrap()]);
25
+ return ok([unwrap(result1)]);
27
26
  } catch (e) {
28
27
  // If a decoder error has happened while unwrapping all the
29
28
  // results, try to construct a good error message
30
- return Err(annotate(result1.errValue()));
29
+ return err(annotate([okOrErrValue(result1)]));
31
30
  }
32
31
  });
33
32
  }
@@ -38,7 +37,7 @@ export function tuple1<T>(decoder1: Decoder<T>): Decoder<[T]> {
38
37
  */
39
38
  export function tuple2<T1, T2>(
40
39
  decoder1: Decoder<T1>,
41
- decoder2: Decoder<T2>
40
+ decoder2: Decoder<T2>,
42
41
  ): Decoder<[T1, T2]> {
43
42
  return compose(ntuple(2), (blobs: $ReadOnlyArray<mixed>) => {
44
43
  const [blob1, blob2] = blobs;
@@ -46,16 +45,11 @@ export function tuple2<T1, T2>(
46
45
  const result1 = decoder1(blob1);
47
46
  const result2 = decoder2(blob2);
48
47
  try {
49
- return Ok([result1.unwrap(), result2.unwrap()]);
48
+ return ok([unwrap(result1), unwrap(result2)]);
50
49
  } catch (e) {
51
50
  // If a decoder error has happened while unwrapping all the
52
51
  // results, try to construct a good error message
53
- return Err(
54
- annotate([
55
- result1.isErr() ? result1.errValue() : result1.value(),
56
- result2.isErr() ? result2.errValue() : result2.value(),
57
- ])
58
- );
52
+ return err(annotate([okOrErrValue(result1), okOrErrValue(result2)]));
59
53
  }
60
54
  });
61
55
  }
@@ -67,7 +61,7 @@ export function tuple2<T1, T2>(
67
61
  export function tuple3<T1, T2, T3>(
68
62
  decoder1: Decoder<T1>,
69
63
  decoder2: Decoder<T2>,
70
- decoder3: Decoder<T3>
64
+ decoder3: Decoder<T3>,
71
65
  ): Decoder<[T1, T2, T3]> {
72
66
  return compose(ntuple(3), (blobs: $ReadOnlyArray<mixed>) => {
73
67
  const [blob1, blob2, blob3] = blobs;
@@ -76,16 +70,16 @@ export function tuple3<T1, T2, T3>(
76
70
  const result2 = decoder2(blob2);
77
71
  const result3 = decoder3(blob3);
78
72
  try {
79
- return Ok([result1.unwrap(), result2.unwrap(), result3.unwrap()]);
73
+ return ok([unwrap(result1), unwrap(result2), unwrap(result3)]);
80
74
  } catch (e) {
81
75
  // If a decoder error has happened while unwrapping all the
82
76
  // results, try to construct a good error message
83
- return Err(
77
+ return err(
84
78
  annotate([
85
- result1.isErr() ? result1.errValue() : result1.value(),
86
- result2.isErr() ? result2.errValue() : result2.value(),
87
- result3.isErr() ? result3.errValue() : result3.value(),
88
- ])
79
+ okOrErrValue(result1),
80
+ okOrErrValue(result2),
81
+ okOrErrValue(result3),
82
+ ]),
89
83
  );
90
84
  }
91
85
  });
@@ -99,7 +93,7 @@ export function tuple4<T1, T2, T3, T4>(
99
93
  decoder1: Decoder<T1>,
100
94
  decoder2: Decoder<T2>,
101
95
  decoder3: Decoder<T3>,
102
- decoder4: Decoder<T4>
96
+ decoder4: Decoder<T4>,
103
97
  ): Decoder<[T1, T2, T3, T4]> {
104
98
  return compose(ntuple(4), (blobs: $ReadOnlyArray<mixed>) => {
105
99
  const [blob1, blob2, blob3, blob4] = blobs;
@@ -109,22 +103,22 @@ export function tuple4<T1, T2, T3, T4>(
109
103
  const result3 = decoder3(blob3);
110
104
  const result4 = decoder4(blob4);
111
105
  try {
112
- return Ok([
113
- result1.unwrap(),
114
- result2.unwrap(),
115
- result3.unwrap(),
116
- result4.unwrap(),
106
+ return ok([
107
+ unwrap(result1),
108
+ unwrap(result2),
109
+ unwrap(result3),
110
+ unwrap(result4),
117
111
  ]);
118
112
  } catch (e) {
119
113
  // If a decoder error has happened while unwrapping all the
120
114
  // results, try to construct a good error message
121
- return Err(
115
+ return err(
122
116
  annotate([
123
- result1.isErr() ? result1.errValue() : result1.value(),
124
- result2.isErr() ? result2.errValue() : result2.value(),
125
- result3.isErr() ? result3.errValue() : result3.value(),
126
- result4.isErr() ? result4.errValue() : result4.value(),
127
- ])
117
+ okOrErrValue(result1),
118
+ okOrErrValue(result2),
119
+ okOrErrValue(result3),
120
+ okOrErrValue(result4),
121
+ ]),
128
122
  );
129
123
  }
130
124
  });
@@ -139,7 +133,7 @@ export function tuple5<T1, T2, T3, T4, T5>(
139
133
  decoder2: Decoder<T2>,
140
134
  decoder3: Decoder<T3>,
141
135
  decoder4: Decoder<T4>,
142
- decoder5: Decoder<T5>
136
+ decoder5: Decoder<T5>,
143
137
  ): Decoder<[T1, T2, T3, T4, T5]> {
144
138
  return compose(ntuple(5), (blobs: $ReadOnlyArray<mixed>) => {
145
139
  const [blob1, blob2, blob3, blob4, blob5] = blobs;
@@ -150,24 +144,24 @@ export function tuple5<T1, T2, T3, T4, T5>(
150
144
  const result4 = decoder4(blob4);
151
145
  const result5 = decoder5(blob5);
152
146
  try {
153
- return Ok([
154
- result1.unwrap(),
155
- result2.unwrap(),
156
- result3.unwrap(),
157
- result4.unwrap(),
158
- result5.unwrap(),
147
+ return ok([
148
+ unwrap(result1),
149
+ unwrap(result2),
150
+ unwrap(result3),
151
+ unwrap(result4),
152
+ unwrap(result5),
159
153
  ]);
160
154
  } catch (e) {
161
155
  // If a decoder error has happened while unwrapping all the
162
156
  // results, try to construct a good error message
163
- return Err(
157
+ return err(
164
158
  annotate([
165
- result1.isErr() ? result1.errValue() : result1.value(),
166
- result2.isErr() ? result2.errValue() : result2.value(),
167
- result3.isErr() ? result3.errValue() : result3.value(),
168
- result4.isErr() ? result4.errValue() : result4.value(),
169
- result5.isErr() ? result5.errValue() : result5.value(),
170
- ])
159
+ okOrErrValue(result1),
160
+ okOrErrValue(result2),
161
+ okOrErrValue(result3),
162
+ okOrErrValue(result4),
163
+ okOrErrValue(result5),
164
+ ]),
171
165
  );
172
166
  }
173
167
  });
@@ -183,7 +177,7 @@ export function tuple6<T1, T2, T3, T4, T5, T6>(
183
177
  decoder3: Decoder<T3>,
184
178
  decoder4: Decoder<T4>,
185
179
  decoder5: Decoder<T5>,
186
- decoder6: Decoder<T6>
180
+ decoder6: Decoder<T6>,
187
181
  ): Decoder<[T1, T2, T3, T4, T5, T6]> {
188
182
  return compose(ntuple(6), (blobs: $ReadOnlyArray<mixed>) => {
189
183
  const [blob1, blob2, blob3, blob4, blob5, blob6] = blobs;
@@ -195,26 +189,26 @@ export function tuple6<T1, T2, T3, T4, T5, T6>(
195
189
  const result5 = decoder5(blob5);
196
190
  const result6 = decoder6(blob6);
197
191
  try {
198
- return Ok([
199
- result1.unwrap(),
200
- result2.unwrap(),
201
- result3.unwrap(),
202
- result4.unwrap(),
203
- result5.unwrap(),
204
- result6.unwrap(),
192
+ return ok([
193
+ unwrap(result1),
194
+ unwrap(result2),
195
+ unwrap(result3),
196
+ unwrap(result4),
197
+ unwrap(result5),
198
+ unwrap(result6),
205
199
  ]);
206
200
  } catch (e) {
207
201
  // If a decoder error has happened while unwrapping all the
208
202
  // results, try to construct a good error message
209
- return Err(
203
+ return err(
210
204
  annotate([
211
- result1.isErr() ? result1.errValue() : result1.value(),
212
- result2.isErr() ? result2.errValue() : result2.value(),
213
- result3.isErr() ? result3.errValue() : result3.value(),
214
- result4.isErr() ? result4.errValue() : result4.value(),
215
- result5.isErr() ? result5.errValue() : result5.value(),
216
- result6.isErr() ? result6.errValue() : result6.value(),
217
- ])
205
+ okOrErrValue(result1),
206
+ okOrErrValue(result2),
207
+ okOrErrValue(result3),
208
+ okOrErrValue(result4),
209
+ okOrErrValue(result5),
210
+ okOrErrValue(result6),
211
+ ]),
218
212
  );
219
213
  }
220
214
  });
@@ -0,0 +1,2 @@
1
+ export { formatInline } from './inline';
2
+ export { formatShort } from './short';
@@ -0,0 +1,4 @@
1
+ // @flow strict
2
+
3
+ export { formatInline } from './inline';
4
+ export { formatShort } from './short';
@@ -0,0 +1,137 @@
1
+ import { asDate, indent, INDENT, isMultiline } from '../_utils';
2
+
3
+ function serializeString(s, width) {
4
+ if (width === void 0) {
5
+ width = 80;
6
+ }
7
+
8
+ // Full string
9
+ // Abbreviated to $maxlen i.e. "Vincent Driess..." [truncated]
10
+ var ser = JSON.stringify(s);
11
+
12
+ if (ser.length <= width) {
13
+ return ser;
14
+ } // Cut off a bit
15
+
16
+
17
+ var truncated = s.substring(0, width - 15) + '...';
18
+ ser = JSON.stringify(truncated) + ' [truncated]';
19
+ return ser;
20
+ }
21
+
22
+ function serializeArray(annotation, prefix) {
23
+ var items = annotation.items;
24
+
25
+ if (items.length === 0) {
26
+ return '[]';
27
+ }
28
+
29
+ var result = [];
30
+ items.forEach(function (item) {
31
+ var _serializeAnnotation = serializeAnnotation(item, prefix + INDENT),
32
+ ser = _serializeAnnotation[0],
33
+ ann = _serializeAnnotation[1];
34
+
35
+ result.push(prefix + INDENT + ser + ',');
36
+
37
+ if (ann !== undefined) {
38
+ result.push(indent(ann, prefix + INDENT));
39
+ }
40
+ });
41
+ return ['['].concat(result, [prefix + ']']).join('\n');
42
+ }
43
+
44
+ function serializeObject(annotation, prefix) {
45
+ var fields = annotation.fields;
46
+ var fieldNames = Object.keys(fields);
47
+
48
+ if (fieldNames.length === 0) {
49
+ return '{}';
50
+ }
51
+
52
+ var result = [];
53
+ fieldNames.forEach(function (key) {
54
+ var valueAnnotation = fields[key];
55
+ var kser = serializeValue(key);
56
+ var valPrefix = prefix + INDENT + ' '.repeat(kser.length + 2);
57
+
58
+ var _serializeAnnotation2 = serializeAnnotation(valueAnnotation, prefix + INDENT),
59
+ vser = _serializeAnnotation2[0],
60
+ vann = _serializeAnnotation2[1];
61
+
62
+ result.push(prefix + INDENT + kser + ': ' + vser + ',');
63
+
64
+ if (vann !== undefined) {
65
+ result.push(indent(vann, valPrefix));
66
+ }
67
+ });
68
+ return ['{'].concat(result, [prefix + '}']).join('\n');
69
+ }
70
+
71
+ export function serializeValue(value) {
72
+ // istanbul ignore else
73
+ if (typeof value === 'string') {
74
+ return serializeString(value);
75
+ } else if (typeof value === 'number' || typeof value === 'boolean') {
76
+ return value.toString();
77
+ } else if (value === null) {
78
+ return 'null';
79
+ } else if (value === undefined) {
80
+ return 'undefined';
81
+ } else {
82
+ var valueAsDate = asDate(value);
83
+
84
+ if (valueAsDate !== null) {
85
+ return "new Date(" + JSON.stringify(valueAsDate.toISOString()) + ")";
86
+ } else if (value instanceof Date) {
87
+ // NOTE: Using `instanceof Date` is unreliable way of checking dates.
88
+ // If this case occurs (and it didn't pass the prior isDate())
89
+ // check, then this must be the case where it's an invalid date.
90
+ return '(Invalid Date)';
91
+ } else {
92
+ return '(unserializable)';
93
+ }
94
+ }
95
+ }
96
+ export function serializeAnnotation(ann, prefix) {
97
+ if (prefix === void 0) {
98
+ prefix = '';
99
+ }
100
+
101
+ // The serialized data (the input object echoed back)
102
+ var serialized;
103
+
104
+ if (ann.type === 'array') {
105
+ serialized = serializeArray(ann, prefix);
106
+ } else if (ann.type === 'object') {
107
+ serialized = serializeObject(ann, prefix);
108
+ } else if (ann.type === 'function') {
109
+ serialized = '<function>';
110
+ } else if (ann.type === 'circular-ref') {
111
+ serialized = '<circular ref>';
112
+ } else if (ann.type === 'unknown') {
113
+ serialized = '???';
114
+ } else {
115
+ serialized = serializeValue(ann.value);
116
+ }
117
+
118
+ var text = ann.text;
119
+
120
+ if (text !== undefined) {
121
+ var sep = '^'.repeat(isMultiline(serialized) ? 1 : serialized.length);
122
+ return [serialized, [sep, text].join(isMultiline(text) ? '\n' : ' ')];
123
+ } else {
124
+ return [serialized, undefined];
125
+ }
126
+ }
127
+ export function formatInline(ann) {
128
+ var _serializeAnnotation3 = serializeAnnotation(ann),
129
+ serialized = _serializeAnnotation3[0],
130
+ annotation = _serializeAnnotation3[1];
131
+
132
+ if (annotation !== undefined) {
133
+ return serialized + '\n' + annotation;
134
+ } else {
135
+ return serialized;
136
+ }
137
+ }
@@ -0,0 +1,122 @@
1
+ // @flow strict
2
+
3
+ import { asDate, indent, INDENT, isMultiline } from '../_utils';
4
+ import type { Annotation, ArrayAnnotation, ObjectAnnotation } from '../annotate';
5
+
6
+ function serializeString(s: string, width: number = 80): string {
7
+ // Full string
8
+ // Abbreviated to $maxlen i.e. "Vincent Driess..." [truncated]
9
+ let ser = JSON.stringify(s);
10
+ if (ser.length <= width) {
11
+ return ser;
12
+ }
13
+
14
+ // Cut off a bit
15
+ const truncated = s.substring(0, width - 15) + '...';
16
+ ser = JSON.stringify(truncated) + ' [truncated]';
17
+ return ser;
18
+ }
19
+
20
+ function serializeArray(annotation: ArrayAnnotation, prefix: string): string {
21
+ const { items } = annotation;
22
+ if (items.length === 0) {
23
+ return '[]';
24
+ }
25
+
26
+ const result = [];
27
+ items.forEach((item) => {
28
+ const [ser, ann] = serializeAnnotation(item, prefix + INDENT);
29
+ result.push(prefix + INDENT + ser + ',');
30
+ if (ann !== undefined) {
31
+ result.push(indent(ann, prefix + INDENT));
32
+ }
33
+ });
34
+ return ['[', ...result, prefix + ']'].join('\n');
35
+ }
36
+
37
+ function serializeObject(annotation: ObjectAnnotation, prefix: string): string {
38
+ const { fields } = annotation;
39
+
40
+ const fieldNames = Object.keys(fields);
41
+ if (fieldNames.length === 0) {
42
+ return '{}';
43
+ }
44
+
45
+ const result = [];
46
+ fieldNames.forEach((key) => {
47
+ const valueAnnotation = fields[key];
48
+ const kser = serializeValue(key);
49
+
50
+ const valPrefix = prefix + INDENT + ' '.repeat(kser.length + 2);
51
+ const [vser, vann] = serializeAnnotation(valueAnnotation, prefix + INDENT);
52
+
53
+ result.push(prefix + INDENT + kser + ': ' + vser + ',');
54
+ if (vann !== undefined) {
55
+ result.push(indent(vann, valPrefix));
56
+ }
57
+ });
58
+ return ['{', ...result, prefix + '}'].join('\n');
59
+ }
60
+
61
+ export function serializeValue(value: mixed): string {
62
+ // istanbul ignore else
63
+ if (typeof value === 'string') {
64
+ return serializeString(value);
65
+ } else if (typeof value === 'number' || typeof value === 'boolean') {
66
+ return value.toString();
67
+ } else if (value === null) {
68
+ return 'null';
69
+ } else if (value === undefined) {
70
+ return 'undefined';
71
+ } else {
72
+ const valueAsDate = asDate(value);
73
+ if (valueAsDate !== null) {
74
+ return `new Date(${JSON.stringify(valueAsDate.toISOString())})`;
75
+ } else if (value instanceof Date) {
76
+ // NOTE: Using `instanceof Date` is unreliable way of checking dates.
77
+ // If this case occurs (and it didn't pass the prior isDate())
78
+ // check, then this must be the case where it's an invalid date.
79
+ return '(Invalid Date)';
80
+ } else {
81
+ return '(unserializable)';
82
+ }
83
+ }
84
+ }
85
+
86
+ export function serializeAnnotation(
87
+ ann: Annotation,
88
+ prefix: string = '',
89
+ ): [string, string | void] {
90
+ // The serialized data (the input object echoed back)
91
+ let serialized;
92
+ if (ann.type === 'array') {
93
+ serialized = serializeArray(ann, prefix);
94
+ } else if (ann.type === 'object') {
95
+ serialized = serializeObject(ann, prefix);
96
+ } else if (ann.type === 'function') {
97
+ serialized = '<function>';
98
+ } else if (ann.type === 'circular-ref') {
99
+ serialized = '<circular ref>';
100
+ } else if (ann.type === 'unknown') {
101
+ serialized = '???';
102
+ } else {
103
+ serialized = serializeValue(ann.value);
104
+ }
105
+
106
+ const text = ann.text;
107
+ if (text !== undefined) {
108
+ const sep = '^'.repeat(isMultiline(serialized) ? 1 : serialized.length);
109
+ return [serialized, [sep, text].join(isMultiline(text) ? '\n' : ' ')];
110
+ } else {
111
+ return [serialized, undefined];
112
+ }
113
+ }
114
+
115
+ export function formatInline(ann: Annotation): string {
116
+ const [serialized, annotation] = serializeAnnotation(ann);
117
+ if (annotation !== undefined) {
118
+ return serialized + '\n' + annotation;
119
+ } else {
120
+ return serialized;
121
+ }
122
+ }
@@ -0,0 +1,4 @@
1
+ import { summarize as _summarize } from '../_utils';
2
+ export function formatShort(ann) {
3
+ return _summarize(ann, []).join('\n');
4
+ }
@@ -0,0 +1,8 @@
1
+ // @flow strict
2
+
3
+ import { summarize as _summarize } from '../_utils';
4
+ import type { Annotation } from '../annotate';
5
+
6
+ export function formatShort(ann: Annotation): string {
7
+ return _summarize(ann, []).join('\n');
8
+ }