decoders 1.26.0-test1 → 2.0.0-beta1

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 (129) hide show
  1. package/CHANGELOG.md +23 -3
  2. package/cjs/_guard.js +26 -0
  3. package/cjs/_guard.js.flow +20 -0
  4. package/cjs/_types.js +1 -0
  5. package/cjs/_types.js.flow +20 -0
  6. package/cjs/_utils.js +108 -0
  7. package/cjs/_utils.js.flow +97 -0
  8. package/cjs/annotate.js +161 -0
  9. package/cjs/annotate.js.flow +218 -0
  10. package/cjs/format/index.js +12 -0
  11. package/cjs/format/index.js.flow +4 -0
  12. package/cjs/format/inline.js +146 -0
  13. package/cjs/format/inline.js.flow +122 -0
  14. package/cjs/format/short.js +10 -0
  15. package/cjs/format/short.js.flow +8 -0
  16. package/cjs/index.js +118 -395
  17. package/cjs/index.js.flow +31 -24
  18. package/cjs/result.js +172 -0
  19. package/cjs/result.js.flow +166 -0
  20. package/cjs/stdlib/array.js +108 -0
  21. package/cjs/{array.js.flow → stdlib/array.js.flow} +22 -25
  22. package/cjs/stdlib/boolean.js +44 -0
  23. package/cjs/{boolean.js.flow → stdlib/boolean.js.flow} +8 -7
  24. package/cjs/stdlib/composition.js +56 -0
  25. package/cjs/{utils.js.flow → stdlib/composition.js.flow} +7 -22
  26. package/cjs/stdlib/constants.js +69 -0
  27. package/cjs/{constants.js.flow → stdlib/constants.js.flow} +13 -12
  28. package/cjs/stdlib/date.js +46 -0
  29. package/cjs/{date.js.flow → stdlib/date.js.flow} +9 -7
  30. package/cjs/stdlib/describe.js +26 -0
  31. package/cjs/{describe.js.flow → stdlib/describe.js.flow} +4 -4
  32. package/cjs/stdlib/dispatch.js +62 -0
  33. package/cjs/{dispatch.js.flow → stdlib/dispatch.js.flow} +6 -5
  34. package/cjs/stdlib/either.js +117 -0
  35. package/cjs/{either.js.flow → stdlib/either.js.flow} +55 -35
  36. package/cjs/stdlib/fail.js +21 -0
  37. package/cjs/stdlib/fail.js.flow +12 -0
  38. package/cjs/stdlib/instanceOf.js +19 -0
  39. package/cjs/{instanceOf.js.flow → stdlib/instanceOf.js.flow} +7 -8
  40. package/cjs/{json.js → stdlib/json.js} +3 -5
  41. package/cjs/{json.js.flow → stdlib/json.js.flow} +4 -4
  42. package/cjs/{lazy.js → stdlib/lazy.js} +1 -3
  43. package/cjs/{lazy.js.flow → stdlib/lazy.js.flow} +1 -1
  44. package/cjs/stdlib/mapping.js +67 -0
  45. package/cjs/stdlib/mapping.js.flow +54 -0
  46. package/cjs/stdlib/number.js +40 -0
  47. package/cjs/{number.js.flow → stdlib/number.js.flow} +10 -11
  48. package/cjs/stdlib/object.js +194 -0
  49. package/cjs/{object.js.flow → stdlib/object.js.flow} +44 -52
  50. package/cjs/stdlib/optional.js +54 -0
  51. package/cjs/{optional.js.flow → stdlib/optional.js.flow} +6 -7
  52. package/cjs/stdlib/string.js +98 -0
  53. package/cjs/{string.js.flow → stdlib/string.js.flow} +13 -12
  54. package/cjs/stdlib/tuple.js +173 -0
  55. package/cjs/{tuple.js.flow → stdlib/tuple.js.flow} +62 -63
  56. package/es/_guard.js +15 -0
  57. package/es/_types.js +0 -0
  58. package/es/_utils.js +93 -0
  59. package/es/annotate.js +144 -0
  60. package/es/format/index.js +2 -0
  61. package/es/format/inline.js +137 -0
  62. package/es/format/short.js +4 -0
  63. package/es/index.js +36 -1211
  64. package/es/result.js +139 -0
  65. package/es/stdlib/array.js +91 -0
  66. package/es/stdlib/boolean.js +28 -0
  67. package/es/stdlib/composition.js +42 -0
  68. package/es/stdlib/constants.js +46 -0
  69. package/es/stdlib/date.js +28 -0
  70. package/{cjs → es/stdlib}/describe.js +5 -11
  71. package/{cjs → es/stdlib}/dispatch.js +8 -15
  72. package/es/stdlib/either.js +90 -0
  73. package/es/stdlib/fail.js +11 -0
  74. package/es/stdlib/instanceOf.js +8 -0
  75. package/es/stdlib/json.js +15 -0
  76. package/es/stdlib/lazy.js +11 -0
  77. package/es/stdlib/mapping.js +54 -0
  78. package/es/stdlib/number.js +25 -0
  79. package/es/stdlib/object.js +175 -0
  80. package/es/stdlib/optional.js +38 -0
  81. package/{cjs → es/stdlib}/string.js +18 -35
  82. package/es/stdlib/tuple.js +155 -0
  83. package/package.json +9 -5
  84. package/ts/_guard.d.ts +7 -0
  85. package/ts/{helpers.d.ts → _helpers.d.ts} +0 -0
  86. package/ts/{types.d.ts → _types.d.ts} +3 -5
  87. package/ts/{utils.d.ts → _utils.d.ts} +3 -3
  88. package/ts/annotate.d.ts +58 -0
  89. package/ts/array.d.ts +1 -1
  90. package/ts/boolean.d.ts +1 -1
  91. package/ts/constants.d.ts +1 -1
  92. package/ts/date.d.ts +1 -1
  93. package/ts/describe.d.ts +1 -1
  94. package/ts/dispatch.d.ts +3 -3
  95. package/ts/either.d.ts +8 -8
  96. package/ts/fail.d.ts +1 -1
  97. package/ts/index.d.ts +30 -26
  98. package/ts/inline.d.ts +3 -0
  99. package/ts/instanceOf.d.ts +1 -1
  100. package/ts/json.d.ts +1 -1
  101. package/ts/lazy.d.ts +1 -1
  102. package/ts/mapping.d.ts +1 -1
  103. package/ts/number.d.ts +1 -1
  104. package/ts/object.d.ts +6 -6
  105. package/ts/optional.d.ts +1 -1
  106. package/ts/result.d.ts +39 -0
  107. package/ts/short.d.ts +3 -0
  108. package/ts/string.d.ts +1 -1
  109. package/ts/tuple.d.ts +5 -5
  110. package/cjs/array.js +0 -133
  111. package/cjs/boolean.js +0 -42
  112. package/cjs/constants.js +0 -67
  113. package/cjs/date.js +0 -42
  114. package/cjs/either.js +0 -85
  115. package/cjs/fail.js +0 -19
  116. package/cjs/fail.js.flow +0 -13
  117. package/cjs/guard.js +0 -30
  118. package/cjs/guard.js.flow +0 -36
  119. package/cjs/instanceOf.js +0 -17
  120. package/cjs/mapping.js +0 -113
  121. package/cjs/mapping.js.flow +0 -71
  122. package/cjs/number.js +0 -38
  123. package/cjs/object.js +0 -254
  124. package/cjs/optional.js +0 -52
  125. package/cjs/tuple.js +0 -199
  126. package/cjs/types.js +0 -5
  127. package/cjs/types.js.flow +0 -26
  128. package/cjs/utils.js +0 -70
  129. package/ts/guard.d.ts +0 -7
@@ -0,0 +1,173 @@
1
+ "use strict";
2
+
3
+ exports.__esModule = true;
4
+ exports.tuple1 = tuple1;
5
+ exports.tuple2 = tuple2;
6
+ exports.tuple3 = tuple3;
7
+ exports.tuple4 = tuple4;
8
+ exports.tuple5 = tuple5;
9
+ exports.tuple6 = tuple6;
10
+
11
+ var _annotate = require("../annotate");
12
+
13
+ var _composition = require("./composition");
14
+
15
+ var _result = require("../result");
16
+
17
+ var _array = require("./array");
18
+
19
+ var ntuple = function ntuple(n) {
20
+ return (0, _composition.compose)(_array.poja, (0, _composition.predicate)(function (arr) {
21
+ return arr.length === n;
22
+ }, "Must be a " + n + "-tuple"));
23
+ };
24
+ /**
25
+ * Builds a Decoder that returns Ok for 1-tuple of [T], given a Decoder for T.
26
+ * Err otherwise.
27
+ */
28
+
29
+
30
+ function tuple1(decoder1) {
31
+ return (0, _composition.compose)(ntuple(1), function (blobs) {
32
+ var blob1 = blobs[0];
33
+ var result1 = decoder1(blob1);
34
+
35
+ try {
36
+ return (0, _result.ok)([(0, _result.unwrap)(result1)]);
37
+ } catch (e) {
38
+ // If a decoder error has happened while unwrapping all the
39
+ // results, try to construct a good error message
40
+ return (0, _result.err)((0, _annotate.annotate)((0, _result.errValue)(result1)));
41
+ }
42
+ });
43
+ }
44
+ /**
45
+ * Builds a Decoder that returns Ok for 2-tuples of [T1, T2], given Decoders
46
+ * for T1 and T2. Err otherwise.
47
+ */
48
+
49
+
50
+ function tuple2(decoder1, decoder2) {
51
+ return (0, _composition.compose)(ntuple(2), function (blobs) {
52
+ var blob1 = blobs[0],
53
+ blob2 = blobs[1];
54
+ var result1 = decoder1(blob1);
55
+ var result2 = decoder2(blob2);
56
+
57
+ try {
58
+ return (0, _result.ok)([(0, _result.unwrap)(result1), (0, _result.unwrap)(result2)]);
59
+ } catch (e) {
60
+ // If a decoder error has happened while unwrapping all the
61
+ // results, try to construct a good error message
62
+ return (0, _result.err)((0, _annotate.annotate)([(0, _result.isErr)(result1) ? (0, _result.errValue)(result1) : (0, _result.value)(result1), (0, _result.isErr)(result2) ? (0, _result.errValue)(result2) : (0, _result.value)(result2)]));
63
+ }
64
+ });
65
+ }
66
+ /**
67
+ * Builds a Decoder that returns Ok for 3-tuples of [T1, T2, T3], given
68
+ * Decoders for T1, T2, and T3. Err otherwise.
69
+ */
70
+
71
+
72
+ function tuple3(decoder1, decoder2, decoder3) {
73
+ return (0, _composition.compose)(ntuple(3), function (blobs) {
74
+ var blob1 = blobs[0],
75
+ blob2 = blobs[1],
76
+ blob3 = blobs[2];
77
+ var result1 = decoder1(blob1);
78
+ var result2 = decoder2(blob2);
79
+ var result3 = decoder3(blob3);
80
+
81
+ try {
82
+ return (0, _result.ok)([(0, _result.unwrap)(result1), (0, _result.unwrap)(result2), (0, _result.unwrap)(result3)]);
83
+ } catch (e) {
84
+ // If a decoder error has happened while unwrapping all the
85
+ // results, try to construct a good error message
86
+ return (0, _result.err)((0, _annotate.annotate)([(0, _result.isErr)(result1) ? (0, _result.errValue)(result1) : (0, _result.value)(result1), (0, _result.isErr)(result2) ? (0, _result.errValue)(result2) : (0, _result.value)(result2), (0, _result.isErr)(result3) ? (0, _result.errValue)(result3) : (0, _result.value)(result3)]));
87
+ }
88
+ });
89
+ }
90
+ /**
91
+ * Builds a Decoder that returns Ok for 4-tuples of [T1, T2, T3, T4], given
92
+ * Decoders for T1, T2, T3, and T4. Err otherwise.
93
+ */
94
+
95
+
96
+ function tuple4(decoder1, decoder2, decoder3, decoder4) {
97
+ return (0, _composition.compose)(ntuple(4), function (blobs) {
98
+ var blob1 = blobs[0],
99
+ blob2 = blobs[1],
100
+ blob3 = blobs[2],
101
+ blob4 = blobs[3];
102
+ var result1 = decoder1(blob1);
103
+ var result2 = decoder2(blob2);
104
+ var result3 = decoder3(blob3);
105
+ var result4 = decoder4(blob4);
106
+
107
+ try {
108
+ return (0, _result.ok)([(0, _result.unwrap)(result1), (0, _result.unwrap)(result2), (0, _result.unwrap)(result3), (0, _result.unwrap)(result4)]);
109
+ } catch (e) {
110
+ // If a decoder error has happened while unwrapping all the
111
+ // results, try to construct a good error message
112
+ return (0, _result.err)((0, _annotate.annotate)([(0, _result.isErr)(result1) ? (0, _result.errValue)(result1) : (0, _result.value)(result1), (0, _result.isErr)(result2) ? (0, _result.errValue)(result2) : (0, _result.value)(result2), (0, _result.isErr)(result3) ? (0, _result.errValue)(result3) : (0, _result.value)(result3), (0, _result.isErr)(result4) ? (0, _result.errValue)(result4) : (0, _result.value)(result4)]));
113
+ }
114
+ });
115
+ }
116
+ /**
117
+ * Builds a Decoder that returns Ok for 5-tuples of [T1, T2, T3, T4, T5], given
118
+ * Decoders for T1, T2, T3, T4, and T5. Err otherwise.
119
+ */
120
+
121
+
122
+ function tuple5(decoder1, decoder2, decoder3, decoder4, decoder5) {
123
+ return (0, _composition.compose)(ntuple(5), function (blobs) {
124
+ var blob1 = blobs[0],
125
+ blob2 = blobs[1],
126
+ blob3 = blobs[2],
127
+ blob4 = blobs[3],
128
+ blob5 = blobs[4];
129
+ var result1 = decoder1(blob1);
130
+ var result2 = decoder2(blob2);
131
+ var result3 = decoder3(blob3);
132
+ var result4 = decoder4(blob4);
133
+ var result5 = decoder5(blob5);
134
+
135
+ try {
136
+ return (0, _result.ok)([(0, _result.unwrap)(result1), (0, _result.unwrap)(result2), (0, _result.unwrap)(result3), (0, _result.unwrap)(result4), (0, _result.unwrap)(result5)]);
137
+ } catch (e) {
138
+ // If a decoder error has happened while unwrapping all the
139
+ // results, try to construct a good error message
140
+ return (0, _result.err)((0, _annotate.annotate)([(0, _result.isErr)(result1) ? (0, _result.errValue)(result1) : (0, _result.value)(result1), (0, _result.isErr)(result2) ? (0, _result.errValue)(result2) : (0, _result.value)(result2), (0, _result.isErr)(result3) ? (0, _result.errValue)(result3) : (0, _result.value)(result3), (0, _result.isErr)(result4) ? (0, _result.errValue)(result4) : (0, _result.value)(result4), (0, _result.isErr)(result5) ? (0, _result.errValue)(result5) : (0, _result.value)(result5)]));
141
+ }
142
+ });
143
+ }
144
+ /**
145
+ * Builds a Decoder that returns Ok for 5-tuples of [T1, T2, T3, T4, T5], given
146
+ * Decoders for T1, T2, T3, T4, T5, and T6. Err otherwise.
147
+ */
148
+
149
+
150
+ function tuple6(decoder1, decoder2, decoder3, decoder4, decoder5, decoder6) {
151
+ return (0, _composition.compose)(ntuple(6), function (blobs) {
152
+ var blob1 = blobs[0],
153
+ blob2 = blobs[1],
154
+ blob3 = blobs[2],
155
+ blob4 = blobs[3],
156
+ blob5 = blobs[4],
157
+ blob6 = blobs[5];
158
+ var result1 = decoder1(blob1);
159
+ var result2 = decoder2(blob2);
160
+ var result3 = decoder3(blob3);
161
+ var result4 = decoder4(blob4);
162
+ var result5 = decoder5(blob5);
163
+ var result6 = decoder6(blob6);
164
+
165
+ try {
166
+ return (0, _result.ok)([(0, _result.unwrap)(result1), (0, _result.unwrap)(result2), (0, _result.unwrap)(result3), (0, _result.unwrap)(result4), (0, _result.unwrap)(result5), (0, _result.unwrap)(result6)]);
167
+ } catch (e) {
168
+ // If a decoder error has happened while unwrapping all the
169
+ // results, try to construct a good error message
170
+ return (0, _result.err)((0, _annotate.annotate)([(0, _result.isErr)(result1) ? (0, _result.errValue)(result1) : (0, _result.value)(result1), (0, _result.isErr)(result2) ? (0, _result.errValue)(result2) : (0, _result.value)(result2), (0, _result.isErr)(result3) ? (0, _result.errValue)(result3) : (0, _result.value)(result3), (0, _result.isErr)(result4) ? (0, _result.errValue)(result4) : (0, _result.value)(result4), (0, _result.isErr)(result5) ? (0, _result.errValue)(result5) : (0, _result.value)(result5), (0, _result.isErr)(result6) ? (0, _result.errValue)(result6) : (0, _result.value)(result6)]));
171
+ }
172
+ });
173
+ }
@@ -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, errValue, isErr, ok, unwrap, value } 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(errValue(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,15 +45,15 @@ 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(
52
+ return err(
54
53
  annotate([
55
- result1.isErr() ? result1.errValue() : result1.value(),
56
- result2.isErr() ? result2.errValue() : result2.value(),
57
- ])
54
+ isErr(result1) ? errValue(result1) : value(result1),
55
+ isErr(result2) ? errValue(result2) : value(result2),
56
+ ]),
58
57
  );
59
58
  }
60
59
  });
@@ -67,7 +66,7 @@ export function tuple2<T1, T2>(
67
66
  export function tuple3<T1, T2, T3>(
68
67
  decoder1: Decoder<T1>,
69
68
  decoder2: Decoder<T2>,
70
- decoder3: Decoder<T3>
69
+ decoder3: Decoder<T3>,
71
70
  ): Decoder<[T1, T2, T3]> {
72
71
  return compose(ntuple(3), (blobs: $ReadOnlyArray<mixed>) => {
73
72
  const [blob1, blob2, blob3] = blobs;
@@ -76,16 +75,16 @@ export function tuple3<T1, T2, T3>(
76
75
  const result2 = decoder2(blob2);
77
76
  const result3 = decoder3(blob3);
78
77
  try {
79
- return Ok([result1.unwrap(), result2.unwrap(), result3.unwrap()]);
78
+ return ok([unwrap(result1), unwrap(result2), unwrap(result3)]);
80
79
  } catch (e) {
81
80
  // If a decoder error has happened while unwrapping all the
82
81
  // results, try to construct a good error message
83
- return Err(
82
+ return err(
84
83
  annotate([
85
- result1.isErr() ? result1.errValue() : result1.value(),
86
- result2.isErr() ? result2.errValue() : result2.value(),
87
- result3.isErr() ? result3.errValue() : result3.value(),
88
- ])
84
+ isErr(result1) ? errValue(result1) : value(result1),
85
+ isErr(result2) ? errValue(result2) : value(result2),
86
+ isErr(result3) ? errValue(result3) : value(result3),
87
+ ]),
89
88
  );
90
89
  }
91
90
  });
@@ -99,7 +98,7 @@ export function tuple4<T1, T2, T3, T4>(
99
98
  decoder1: Decoder<T1>,
100
99
  decoder2: Decoder<T2>,
101
100
  decoder3: Decoder<T3>,
102
- decoder4: Decoder<T4>
101
+ decoder4: Decoder<T4>,
103
102
  ): Decoder<[T1, T2, T3, T4]> {
104
103
  return compose(ntuple(4), (blobs: $ReadOnlyArray<mixed>) => {
105
104
  const [blob1, blob2, blob3, blob4] = blobs;
@@ -109,22 +108,22 @@ export function tuple4<T1, T2, T3, T4>(
109
108
  const result3 = decoder3(blob3);
110
109
  const result4 = decoder4(blob4);
111
110
  try {
112
- return Ok([
113
- result1.unwrap(),
114
- result2.unwrap(),
115
- result3.unwrap(),
116
- result4.unwrap(),
111
+ return ok([
112
+ unwrap(result1),
113
+ unwrap(result2),
114
+ unwrap(result3),
115
+ unwrap(result4),
117
116
  ]);
118
117
  } catch (e) {
119
118
  // If a decoder error has happened while unwrapping all the
120
119
  // results, try to construct a good error message
121
- return Err(
120
+ return err(
122
121
  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
- ])
122
+ isErr(result1) ? errValue(result1) : value(result1),
123
+ isErr(result2) ? errValue(result2) : value(result2),
124
+ isErr(result3) ? errValue(result3) : value(result3),
125
+ isErr(result4) ? errValue(result4) : value(result4),
126
+ ]),
128
127
  );
129
128
  }
130
129
  });
@@ -139,7 +138,7 @@ export function tuple5<T1, T2, T3, T4, T5>(
139
138
  decoder2: Decoder<T2>,
140
139
  decoder3: Decoder<T3>,
141
140
  decoder4: Decoder<T4>,
142
- decoder5: Decoder<T5>
141
+ decoder5: Decoder<T5>,
143
142
  ): Decoder<[T1, T2, T3, T4, T5]> {
144
143
  return compose(ntuple(5), (blobs: $ReadOnlyArray<mixed>) => {
145
144
  const [blob1, blob2, blob3, blob4, blob5] = blobs;
@@ -150,24 +149,24 @@ export function tuple5<T1, T2, T3, T4, T5>(
150
149
  const result4 = decoder4(blob4);
151
150
  const result5 = decoder5(blob5);
152
151
  try {
153
- return Ok([
154
- result1.unwrap(),
155
- result2.unwrap(),
156
- result3.unwrap(),
157
- result4.unwrap(),
158
- result5.unwrap(),
152
+ return ok([
153
+ unwrap(result1),
154
+ unwrap(result2),
155
+ unwrap(result3),
156
+ unwrap(result4),
157
+ unwrap(result5),
159
158
  ]);
160
159
  } catch (e) {
161
160
  // If a decoder error has happened while unwrapping all the
162
161
  // results, try to construct a good error message
163
- return Err(
162
+ return err(
164
163
  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
- ])
164
+ isErr(result1) ? errValue(result1) : value(result1),
165
+ isErr(result2) ? errValue(result2) : value(result2),
166
+ isErr(result3) ? errValue(result3) : value(result3),
167
+ isErr(result4) ? errValue(result4) : value(result4),
168
+ isErr(result5) ? errValue(result5) : value(result5),
169
+ ]),
171
170
  );
172
171
  }
173
172
  });
@@ -183,7 +182,7 @@ export function tuple6<T1, T2, T3, T4, T5, T6>(
183
182
  decoder3: Decoder<T3>,
184
183
  decoder4: Decoder<T4>,
185
184
  decoder5: Decoder<T5>,
186
- decoder6: Decoder<T6>
185
+ decoder6: Decoder<T6>,
187
186
  ): Decoder<[T1, T2, T3, T4, T5, T6]> {
188
187
  return compose(ntuple(6), (blobs: $ReadOnlyArray<mixed>) => {
189
188
  const [blob1, blob2, blob3, blob4, blob5, blob6] = blobs;
@@ -195,26 +194,26 @@ export function tuple6<T1, T2, T3, T4, T5, T6>(
195
194
  const result5 = decoder5(blob5);
196
195
  const result6 = decoder6(blob6);
197
196
  try {
198
- return Ok([
199
- result1.unwrap(),
200
- result2.unwrap(),
201
- result3.unwrap(),
202
- result4.unwrap(),
203
- result5.unwrap(),
204
- result6.unwrap(),
197
+ return ok([
198
+ unwrap(result1),
199
+ unwrap(result2),
200
+ unwrap(result3),
201
+ unwrap(result4),
202
+ unwrap(result5),
203
+ unwrap(result6),
205
204
  ]);
206
205
  } catch (e) {
207
206
  // If a decoder error has happened while unwrapping all the
208
207
  // results, try to construct a good error message
209
- return Err(
208
+ return err(
210
209
  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
- ])
210
+ isErr(result1) ? errValue(result1) : value(result1),
211
+ isErr(result2) ? errValue(result2) : value(result2),
212
+ isErr(result3) ? errValue(result3) : value(result3),
213
+ isErr(result4) ? errValue(result4) : value(result4),
214
+ isErr(result5) ? errValue(result5) : value(result5),
215
+ isErr(result6) ? errValue(result6) : value(result6),
216
+ ]),
218
217
  );
219
218
  }
220
219
  });
package/es/_guard.js ADDED
@@ -0,0 +1,15 @@
1
+ import * as Result from './result';
2
+ import { formatInline } from './format';
3
+ export function guard(decoder, formatter) {
4
+ if (formatter === void 0) {
5
+ formatter = formatInline;
6
+ }
7
+
8
+ return function (blob) {
9
+ return Result.unwrap(Result.mapError(decoder(blob), function (annotation) {
10
+ var err = new Error('\n' + formatter(annotation));
11
+ err.name = 'Decoding error';
12
+ return err;
13
+ }));
14
+ };
15
+ }
package/es/_types.js ADDED
File without changes
package/es/_utils.js ADDED
@@ -0,0 +1,93 @@
1
+ // $FlowFixMe[unclear-type] - deliberate casting
2
+ // Two spaces of indentation
3
+ export var INDENT = ' ';
4
+ /**
5
+ * `x instanceof Date` checks are unreliable across stack frames (that information
6
+ * might get lost by the JS runtime), so we'll have to reside to more runtime
7
+ * inspection checks.
8
+ *
9
+ * Taken from https://stackoverflow.com/a/44198641
10
+ */
11
+
12
+ export function isDate(value) {
13
+ return !!value && // $FlowFixMe[method-unbinding]
14
+ Object.prototype.toString.call(value) === '[object Date]' && !isNaN(value);
15
+ }
16
+ /**
17
+ * Is value is a valid Date instance, then return that. If not, then return
18
+ * null.
19
+ */
20
+
21
+ export function asDate(value) {
22
+ return isDate(value) ? value : null;
23
+ }
24
+ export function isMultiline(s) {
25
+ return s.indexOf('\n') >= 0;
26
+ }
27
+ export function indent(s, prefix) {
28
+ if (prefix === void 0) {
29
+ prefix = INDENT;
30
+ }
31
+
32
+ if (isMultiline(s)) {
33
+ return s.split('\n').map(function (line) {
34
+ return prefix + line;
35
+ }).join('\n');
36
+ } else {
37
+ return prefix + s;
38
+ }
39
+ }
40
+ /**
41
+ * Walks the annotation tree and emits the annotation's key path within the
42
+ * object tree, and the message as a series of messages (array of strings).
43
+ */
44
+
45
+ export function summarize(ann, keypath) {
46
+ if (keypath === void 0) {
47
+ keypath = [];
48
+ }
49
+
50
+ var result = [];
51
+
52
+ if (ann.type === 'array') {
53
+ var items = ann.items;
54
+ var index = 0;
55
+ items.forEach(function (ann) {
56
+ summarize(ann, [].concat(keypath, [index++])).forEach(function (item) {
57
+ return (// Collect to results
58
+ result.push(item)
59
+ );
60
+ });
61
+ });
62
+ } else if (ann.type === 'object') {
63
+ var fields = ann.fields;
64
+ Object.keys(fields).forEach(function (key) {
65
+ var value = fields[key];
66
+ summarize(value, [].concat(keypath, [key])).forEach(function (item) {
67
+ return (// Collect to results
68
+ result.push(item)
69
+ );
70
+ });
71
+ });
72
+ }
73
+
74
+ var text = ann.text;
75
+
76
+ if (!text) {
77
+ return result;
78
+ }
79
+
80
+ var prefix;
81
+
82
+ if (keypath.length === 0) {
83
+ prefix = '';
84
+ } else if (keypath.length === 1) {
85
+ prefix = typeof keypath[0] === 'number' ? "Value at index " + keypath[0] + ": " : "Value at key " + JSON.stringify(keypath[0]) + ": ";
86
+ } else {
87
+ prefix = "Value at keypath " + keypath.map(function (x) {
88
+ return x.toString();
89
+ }).join('.') + ": ";
90
+ }
91
+
92
+ return [].concat(result, [prefix + text]);
93
+ }
package/es/annotate.js ADDED
@@ -0,0 +1,144 @@
1
+ function _extends() { _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; }; return _extends.apply(this, arguments); }
2
+
3
+ var _register = new WeakSet();
4
+
5
+ function brand(ann) {
6
+ _register.add(ann);
7
+
8
+ return ann;
9
+ }
10
+
11
+ export function object(fields, text) {
12
+ return brand({
13
+ type: 'object',
14
+ fields: fields,
15
+ text: text
16
+ });
17
+ }
18
+ export function array(items, text) {
19
+ return brand({
20
+ type: 'array',
21
+ items: items,
22
+ text: text
23
+ });
24
+ }
25
+ export function func(text) {
26
+ return brand({
27
+ type: 'function',
28
+ text: text
29
+ });
30
+ }
31
+ export function unknown(value, text) {
32
+ return brand({
33
+ type: 'unknown',
34
+ value: value,
35
+ text: text
36
+ });
37
+ }
38
+ export function scalar(value, text) {
39
+ return brand({
40
+ type: 'scalar',
41
+ value: value,
42
+ text: text
43
+ });
44
+ }
45
+ export function circularRef(text) {
46
+ return brand({
47
+ type: 'circular-ref',
48
+ text: text
49
+ });
50
+ }
51
+ /**
52
+ * Given an existing Annotation, set the annotation's text to a new value.
53
+ */
54
+
55
+ export function updateText(annotation, text) {
56
+ if (text !== undefined) {
57
+ return brand(_extends({}, annotation, {
58
+ text: text
59
+ }));
60
+ } else {
61
+ return annotation;
62
+ }
63
+ }
64
+ /**
65
+ * Given an existing ObjectAnnotation, merges new Annotations in there.
66
+ */
67
+
68
+ export function merge(objAnnotation, fields) {
69
+ var newFields = _extends({}, objAnnotation.fields, fields);
70
+
71
+ return object(newFields, objAnnotation.text);
72
+ }
73
+ export function asAnnotation(thing) {
74
+ return typeof thing === 'object' && thing !== null && _register.has(thing) ? thing : undefined;
75
+ }
76
+
77
+ function annotateArray(value, text, seen) {
78
+ seen.add(value);
79
+ var items = value.map(function (v) {
80
+ return annotate(v, undefined, seen);
81
+ });
82
+ return array(items, text);
83
+ }
84
+
85
+ function annotateObject(obj, text, seen) {
86
+ seen.add(obj);
87
+ var fields = {};
88
+ Object.keys(obj).forEach(function (key) {
89
+ var value = obj[key];
90
+ fields[key] = annotate(value, undefined, seen);
91
+ });
92
+ return object(fields, text);
93
+ }
94
+
95
+ function annotate(value, text, seen) {
96
+ if (value === null || value === undefined || typeof value === 'string' || typeof value === 'number' || typeof value === 'boolean' || typeof value === 'symbol' || typeof value.getMonth === 'function') {
97
+ return scalar(value, text);
98
+ }
99
+
100
+ var ann = asAnnotation(value);
101
+
102
+ if (ann) {
103
+ return updateText(ann, text);
104
+ }
105
+
106
+ if (Array.isArray(value)) {
107
+ // "Circular references" can only exist in objects or arrays
108
+ if (seen.has(value)) {
109
+ return circularRef(text);
110
+ } else {
111
+ return annotateArray(value, text, seen);
112
+ }
113
+ }
114
+
115
+ if (typeof value === 'object') {
116
+ // "Circular references" can only exist in objects or arrays
117
+ if (seen.has(value)) {
118
+ return circularRef(text);
119
+ } else {
120
+ return annotateObject(value, text, seen);
121
+ }
122
+ }
123
+
124
+ if (typeof value === 'function') {
125
+ return func(text);
126
+ }
127
+
128
+ return unknown(value, text);
129
+ }
130
+
131
+ function public_annotate(value, text) {
132
+ return annotate(value, text, new WeakSet());
133
+ }
134
+
135
+ function public_annotateObject(obj, text) {
136
+ return annotateObject(obj, text, new WeakSet());
137
+ }
138
+
139
+ export { // This construct just ensures the "seen" weakmap (used for circular
140
+ // reference detection) isn't made part of the public API.
141
+ public_annotate as annotate, public_annotateObject as annotateObject, //
142
+ // NOTE: Don't acces theses private APIs directly. They are only exported here
143
+ // to better enable unit testing.
144
+ annotate as __private_annotate };
@@ -0,0 +1,2 @@
1
+ export { formatInline } from './inline';
2
+ export { formatShort } from './short';