decoders 2.0.0-beta1 → 2.0.0-beta10

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 (160) hide show
  1. package/CHANGELOG.md +30 -0
  2. package/Decoder.d.ts +27 -0
  3. package/Decoder.js +199 -0
  4. package/Decoder.js.flow +224 -0
  5. package/Decoder.mjs +192 -0
  6. package/NotSupportedTSVersion.d.ts +1 -0
  7. package/README.md +105 -961
  8. package/_utils.d.ts +9 -0
  9. package/{cjs/_utils.js → _utils.js} +12 -18
  10. package/{cjs/_utils.js.flow → _utils.js.flow} +15 -19
  11. package/{es/_utils.js → _utils.mjs} +11 -15
  12. package/{ts/annotate.d.ts → annotate.d.ts} +25 -21
  13. package/{cjs/annotate.js → annotate.js} +0 -0
  14. package/{cjs/annotate.js.flow → annotate.js.flow} +0 -0
  15. package/{es/annotate.js → annotate.mjs} +0 -0
  16. package/format.d.ts +4 -0
  17. package/{cjs/format/inline.js → format.js} +7 -2
  18. package/{cjs/format/inline.js.flow → format.js.flow} +7 -3
  19. package/{es/format/inline.js → format.mjs} +5 -2
  20. package/index.d.ts +40 -0
  21. package/index.js +90 -0
  22. package/index.js.flow +44 -0
  23. package/index.mjs +11 -0
  24. package/{ts → lib}/_helpers.d.ts +0 -0
  25. package/lib/arrays.d.ts +37 -0
  26. package/lib/arrays.js +138 -0
  27. package/lib/arrays.js.flow +138 -0
  28. package/lib/arrays.mjs +123 -0
  29. package/lib/basics.d.ts +14 -0
  30. package/lib/basics.js +109 -0
  31. package/lib/basics.js.flow +85 -0
  32. package/lib/basics.mjs +85 -0
  33. package/{ts/boolean.d.ts → lib/booleans.d.ts} +1 -1
  34. package/lib/booleans.js +35 -0
  35. package/lib/booleans.js.flow +22 -0
  36. package/lib/booleans.mjs +25 -0
  37. package/{ts/date.d.ts → lib/dates.d.ts} +1 -1
  38. package/lib/dates.js +44 -0
  39. package/lib/dates.js.flow +40 -0
  40. package/lib/dates.mjs +34 -0
  41. package/{ts → lib}/json.d.ts +1 -1
  42. package/lib/json.js +55 -0
  43. package/lib/json.js.flow +50 -0
  44. package/lib/json.mjs +40 -0
  45. package/{ts/number.d.ts → lib/numbers.d.ts} +2 -1
  46. package/lib/numbers.js +52 -0
  47. package/lib/numbers.js.flow +49 -0
  48. package/lib/numbers.mjs +42 -0
  49. package/{ts/object.d.ts → lib/objects.d.ts} +9 -4
  50. package/lib/objects.js +240 -0
  51. package/lib/objects.js.flow +246 -0
  52. package/lib/objects.mjs +223 -0
  53. package/lib/strings.d.ts +13 -0
  54. package/lib/strings.js +101 -0
  55. package/lib/strings.js.flow +90 -0
  56. package/lib/strings.mjs +82 -0
  57. package/lib/unions.d.ts +78 -0
  58. package/lib/unions.js +161 -0
  59. package/lib/unions.js.flow +158 -0
  60. package/lib/unions.mjs +145 -0
  61. package/lib/utilities.d.ts +10 -0
  62. package/lib/utilities.js +94 -0
  63. package/lib/utilities.js.flow +84 -0
  64. package/lib/utilities.mjs +79 -0
  65. package/package.json +79 -29
  66. package/result.d.ts +16 -0
  67. package/result.js +34 -0
  68. package/result.js.flow +26 -0
  69. package/result.mjs +27 -0
  70. package/cjs/_guard.js +0 -26
  71. package/cjs/_guard.js.flow +0 -20
  72. package/cjs/_types.js +0 -1
  73. package/cjs/_types.js.flow +0 -20
  74. package/cjs/format/index.js +0 -12
  75. package/cjs/format/index.js.flow +0 -4
  76. package/cjs/format/short.js +0 -10
  77. package/cjs/format/short.js.flow +0 -8
  78. package/cjs/index.js +0 -120
  79. package/cjs/index.js.flow +0 -63
  80. package/cjs/result.js +0 -172
  81. package/cjs/result.js.flow +0 -166
  82. package/cjs/stdlib/array.js +0 -108
  83. package/cjs/stdlib/array.js.flow +0 -103
  84. package/cjs/stdlib/boolean.js +0 -44
  85. package/cjs/stdlib/boolean.js.flow +0 -29
  86. package/cjs/stdlib/composition.js +0 -56
  87. package/cjs/stdlib/composition.js.flow +0 -43
  88. package/cjs/stdlib/constants.js +0 -69
  89. package/cjs/stdlib/constants.js.flow +0 -46
  90. package/cjs/stdlib/date.js +0 -46
  91. package/cjs/stdlib/date.js.flow +0 -40
  92. package/cjs/stdlib/describe.js +0 -26
  93. package/cjs/stdlib/describe.js.flow +0 -17
  94. package/cjs/stdlib/dispatch.js +0 -62
  95. package/cjs/stdlib/dispatch.js.flow +0 -58
  96. package/cjs/stdlib/either.js +0 -117
  97. package/cjs/stdlib/either.js.flow +0 -151
  98. package/cjs/stdlib/fail.js +0 -21
  99. package/cjs/stdlib/fail.js.flow +0 -12
  100. package/cjs/stdlib/instanceOf.js +0 -19
  101. package/cjs/stdlib/instanceOf.js.flow +0 -20
  102. package/cjs/stdlib/json.js +0 -31
  103. package/cjs/stdlib/json.js.flow +0 -28
  104. package/cjs/stdlib/lazy.js +0 -16
  105. package/cjs/stdlib/lazy.js.flow +0 -15
  106. package/cjs/stdlib/mapping.js +0 -67
  107. package/cjs/stdlib/mapping.js.flow +0 -54
  108. package/cjs/stdlib/number.js +0 -40
  109. package/cjs/stdlib/number.js.flow +0 -34
  110. package/cjs/stdlib/object.js +0 -194
  111. package/cjs/stdlib/object.js.flow +0 -203
  112. package/cjs/stdlib/optional.js +0 -54
  113. package/cjs/stdlib/optional.js.flow +0 -41
  114. package/cjs/stdlib/string.js +0 -98
  115. package/cjs/stdlib/string.js.flow +0 -82
  116. package/cjs/stdlib/tuple.js +0 -173
  117. package/cjs/stdlib/tuple.js.flow +0 -220
  118. package/es/_guard.js +0 -15
  119. package/es/_types.js +0 -0
  120. package/es/format/index.js +0 -2
  121. package/es/format/short.js +0 -4
  122. package/es/index.js +0 -37
  123. package/es/result.js +0 -139
  124. package/es/stdlib/array.js +0 -91
  125. package/es/stdlib/boolean.js +0 -28
  126. package/es/stdlib/composition.js +0 -42
  127. package/es/stdlib/constants.js +0 -46
  128. package/es/stdlib/date.js +0 -28
  129. package/es/stdlib/describe.js +0 -16
  130. package/es/stdlib/dispatch.js +0 -51
  131. package/es/stdlib/either.js +0 -90
  132. package/es/stdlib/fail.js +0 -11
  133. package/es/stdlib/instanceOf.js +0 -8
  134. package/es/stdlib/json.js +0 -15
  135. package/es/stdlib/lazy.js +0 -11
  136. package/es/stdlib/mapping.js +0 -54
  137. package/es/stdlib/number.js +0 -25
  138. package/es/stdlib/object.js +0 -175
  139. package/es/stdlib/optional.js +0 -38
  140. package/es/stdlib/string.js +0 -76
  141. package/es/stdlib/tuple.js +0 -155
  142. package/ts/_guard.d.ts +0 -7
  143. package/ts/_types.d.ts +0 -16
  144. package/ts/_utils.d.ts +0 -13
  145. package/ts/array.d.ts +0 -5
  146. package/ts/constants.d.ts +0 -11
  147. package/ts/describe.d.ts +0 -3
  148. package/ts/dispatch.d.ts +0 -8
  149. package/ts/either.d.ts +0 -61
  150. package/ts/fail.d.ts +0 -3
  151. package/ts/index.d.ts +0 -42
  152. package/ts/inline.d.ts +0 -3
  153. package/ts/instanceOf.d.ts +0 -3
  154. package/ts/lazy.d.ts +0 -3
  155. package/ts/mapping.d.ts +0 -4
  156. package/ts/optional.d.ts +0 -5
  157. package/ts/result.d.ts +0 -39
  158. package/ts/short.d.ts +0 -3
  159. package/ts/string.d.ts +0 -7
  160. package/ts/tuple.d.ts +0 -30
@@ -1,220 +0,0 @@
1
- // @flow strict
2
-
3
- import { annotate } from '../annotate';
4
- import { compose, predicate } from './composition';
5
- import { err, errValue, isErr, ok, unwrap, value } from '../result';
6
- import { poja } from './array';
7
- import type { Decoder } from '../_types';
8
-
9
- const ntuple = (n: number) =>
10
- compose(
11
- poja,
12
- predicate((arr) => arr.length === n, `Must be a ${n}-tuple`),
13
- );
14
-
15
- /**
16
- * Builds a Decoder that returns Ok for 1-tuple of [T], given a Decoder for T.
17
- * Err otherwise.
18
- */
19
- export function tuple1<T>(decoder1: Decoder<T>): Decoder<[T]> {
20
- return compose(ntuple(1), (blobs: $ReadOnlyArray<mixed>) => {
21
- const [blob1] = blobs;
22
-
23
- const result1 = decoder1(blob1);
24
- try {
25
- return ok([unwrap(result1)]);
26
- } catch (e) {
27
- // If a decoder error has happened while unwrapping all the
28
- // results, try to construct a good error message
29
- return err(annotate(errValue(result1)));
30
- }
31
- });
32
- }
33
-
34
- /**
35
- * Builds a Decoder that returns Ok for 2-tuples of [T1, T2], given Decoders
36
- * for T1 and T2. Err otherwise.
37
- */
38
- export function tuple2<T1, T2>(
39
- decoder1: Decoder<T1>,
40
- decoder2: Decoder<T2>,
41
- ): Decoder<[T1, T2]> {
42
- return compose(ntuple(2), (blobs: $ReadOnlyArray<mixed>) => {
43
- const [blob1, blob2] = blobs;
44
-
45
- const result1 = decoder1(blob1);
46
- const result2 = decoder2(blob2);
47
- try {
48
- return ok([unwrap(result1), unwrap(result2)]);
49
- } catch (e) {
50
- // If a decoder error has happened while unwrapping all the
51
- // results, try to construct a good error message
52
- return err(
53
- annotate([
54
- isErr(result1) ? errValue(result1) : value(result1),
55
- isErr(result2) ? errValue(result2) : value(result2),
56
- ]),
57
- );
58
- }
59
- });
60
- }
61
-
62
- /**
63
- * Builds a Decoder that returns Ok for 3-tuples of [T1, T2, T3], given
64
- * Decoders for T1, T2, and T3. Err otherwise.
65
- */
66
- export function tuple3<T1, T2, T3>(
67
- decoder1: Decoder<T1>,
68
- decoder2: Decoder<T2>,
69
- decoder3: Decoder<T3>,
70
- ): Decoder<[T1, T2, T3]> {
71
- return compose(ntuple(3), (blobs: $ReadOnlyArray<mixed>) => {
72
- const [blob1, blob2, blob3] = blobs;
73
-
74
- const result1 = decoder1(blob1);
75
- const result2 = decoder2(blob2);
76
- const result3 = decoder3(blob3);
77
- try {
78
- return ok([unwrap(result1), unwrap(result2), unwrap(result3)]);
79
- } catch (e) {
80
- // If a decoder error has happened while unwrapping all the
81
- // results, try to construct a good error message
82
- return err(
83
- annotate([
84
- isErr(result1) ? errValue(result1) : value(result1),
85
- isErr(result2) ? errValue(result2) : value(result2),
86
- isErr(result3) ? errValue(result3) : value(result3),
87
- ]),
88
- );
89
- }
90
- });
91
- }
92
-
93
- /**
94
- * Builds a Decoder that returns Ok for 4-tuples of [T1, T2, T3, T4], given
95
- * Decoders for T1, T2, T3, and T4. Err otherwise.
96
- */
97
- export function tuple4<T1, T2, T3, T4>(
98
- decoder1: Decoder<T1>,
99
- decoder2: Decoder<T2>,
100
- decoder3: Decoder<T3>,
101
- decoder4: Decoder<T4>,
102
- ): Decoder<[T1, T2, T3, T4]> {
103
- return compose(ntuple(4), (blobs: $ReadOnlyArray<mixed>) => {
104
- const [blob1, blob2, blob3, blob4] = blobs;
105
-
106
- const result1 = decoder1(blob1);
107
- const result2 = decoder2(blob2);
108
- const result3 = decoder3(blob3);
109
- const result4 = decoder4(blob4);
110
- try {
111
- return ok([
112
- unwrap(result1),
113
- unwrap(result2),
114
- unwrap(result3),
115
- unwrap(result4),
116
- ]);
117
- } catch (e) {
118
- // If a decoder error has happened while unwrapping all the
119
- // results, try to construct a good error message
120
- return err(
121
- annotate([
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
- ]),
127
- );
128
- }
129
- });
130
- }
131
-
132
- /**
133
- * Builds a Decoder that returns Ok for 5-tuples of [T1, T2, T3, T4, T5], given
134
- * Decoders for T1, T2, T3, T4, and T5. Err otherwise.
135
- */
136
- export function tuple5<T1, T2, T3, T4, T5>(
137
- decoder1: Decoder<T1>,
138
- decoder2: Decoder<T2>,
139
- decoder3: Decoder<T3>,
140
- decoder4: Decoder<T4>,
141
- decoder5: Decoder<T5>,
142
- ): Decoder<[T1, T2, T3, T4, T5]> {
143
- return compose(ntuple(5), (blobs: $ReadOnlyArray<mixed>) => {
144
- const [blob1, blob2, blob3, blob4, blob5] = blobs;
145
-
146
- const result1 = decoder1(blob1);
147
- const result2 = decoder2(blob2);
148
- const result3 = decoder3(blob3);
149
- const result4 = decoder4(blob4);
150
- const result5 = decoder5(blob5);
151
- try {
152
- return ok([
153
- unwrap(result1),
154
- unwrap(result2),
155
- unwrap(result3),
156
- unwrap(result4),
157
- unwrap(result5),
158
- ]);
159
- } catch (e) {
160
- // If a decoder error has happened while unwrapping all the
161
- // results, try to construct a good error message
162
- return err(
163
- annotate([
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
- ]),
170
- );
171
- }
172
- });
173
- }
174
-
175
- /**
176
- * Builds a Decoder that returns Ok for 5-tuples of [T1, T2, T3, T4, T5], given
177
- * Decoders for T1, T2, T3, T4, T5, and T6. Err otherwise.
178
- */
179
- export function tuple6<T1, T2, T3, T4, T5, T6>(
180
- decoder1: Decoder<T1>,
181
- decoder2: Decoder<T2>,
182
- decoder3: Decoder<T3>,
183
- decoder4: Decoder<T4>,
184
- decoder5: Decoder<T5>,
185
- decoder6: Decoder<T6>,
186
- ): Decoder<[T1, T2, T3, T4, T5, T6]> {
187
- return compose(ntuple(6), (blobs: $ReadOnlyArray<mixed>) => {
188
- const [blob1, blob2, blob3, blob4, blob5, blob6] = blobs;
189
-
190
- const result1 = decoder1(blob1);
191
- const result2 = decoder2(blob2);
192
- const result3 = decoder3(blob3);
193
- const result4 = decoder4(blob4);
194
- const result5 = decoder5(blob5);
195
- const result6 = decoder6(blob6);
196
- try {
197
- return ok([
198
- unwrap(result1),
199
- unwrap(result2),
200
- unwrap(result3),
201
- unwrap(result4),
202
- unwrap(result5),
203
- unwrap(result6),
204
- ]);
205
- } catch (e) {
206
- // If a decoder error has happened while unwrapping all the
207
- // results, try to construct a good error message
208
- return err(
209
- annotate([
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
- ]),
217
- );
218
- }
219
- });
220
- }
package/es/_guard.js DELETED
@@ -1,15 +0,0 @@
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 DELETED
File without changes
@@ -1,2 +0,0 @@
1
- export { formatInline } from './inline';
2
- export { formatShort } from './short';
@@ -1,4 +0,0 @@
1
- import { summarize as _summarize } from '../_utils';
2
- export function formatShort(ann) {
3
- return _summarize(ann, []).join('\n');
4
- }
package/es/index.js DELETED
@@ -1,37 +0,0 @@
1
- /**
2
- * Elm-like JSON decoders, for use with Flow.
3
- * See http://elmplayground.com/decoding-json-in-elm-1 for an introduction.
4
- *
5
- * Why? All JSON responses coming from our API endpoints are just that: free-form
6
- * JSON data. To Flow, the only type classification possilbe is "any" -- effectively
7
- * turning off all type checks for anything related to JSON. To the receiving end
8
- * (our frontend), the structure of that data is completely opaque to any type
9
- * checkers since JSON values can be anything: an object, an array, null, a string,
10
- * a bool, etc. Our type system is not a runtime type system, so we need a way of
11
- * "converting" an any-type JSON value into a type that we want to work with in our
12
- * frontend code base.
13
- *
14
- * Elm's solution to this problem is to define composable decoders: functions that
15
- * take anything and either fail with an error, or guarantee to return the expected
16
- * type. In our case, it's fine to fail with a runtime error.
17
- *
18
- */
19
- export { guard } from './_guard';
20
- export { compose, map, predicate } from './stdlib/composition';
21
- export { array, nonEmptyArray, poja } from './stdlib/array';
22
- export { boolean, numericBoolean, truthy } from './stdlib/boolean';
23
- export { constant, hardcoded, mixed, null_, undefined_, unknown } from './stdlib/constants';
24
- export { date, iso8601 } from './stdlib/date';
25
- export { describe } from './stdlib/describe';
26
- export { dispatch } from './stdlib/dispatch';
27
- export { either, either3, either4, either5, either6, either7, either8, either9, oneOf } from './stdlib/either';
28
- export { fail } from './stdlib/fail';
29
- export { instanceOf } from './stdlib/instanceOf';
30
- export { json, jsonObject, jsonArray } from './stdlib/json';
31
- export { lazy } from './stdlib/lazy';
32
- export { mapping, dict } from './stdlib/mapping';
33
- export { integer, number, positiveInteger, positiveNumber } from './stdlib/number';
34
- export { exact, inexact, object, pojo } from './stdlib/object';
35
- export { maybe, nullable, optional } from './stdlib/optional';
36
- export { email, nonEmptyString, regex, string, url } from './stdlib/string';
37
- export { tuple1, tuple2, tuple3, tuple4, tuple5, tuple6 } from './stdlib/tuple';
package/es/result.js DELETED
@@ -1,139 +0,0 @@
1
- /**
2
- * Result <value> <error>
3
- * = Ok <value>
4
- * | Err <error>
5
- */
6
-
7
- /**
8
- * Create a new Result instance representing a successful computation.
9
- */
10
- export function ok(value) {
11
- return {
12
- type: 'ok',
13
- value: value
14
- };
15
- }
16
- /**
17
- * Create a new Result instance representing a failed computation.
18
- */
19
-
20
- export function err(error) {
21
- return {
22
- type: 'err',
23
- error: error
24
- };
25
- }
26
- export function toString(result) {
27
- return result.type === 'ok' ? "Ok(" + String(result.value) + ")" : "Err(" + String(result.error) + ")";
28
- }
29
- export function isOk(result) {
30
- return result.type === 'ok';
31
- }
32
- export function isErr(result) {
33
- return result.type === 'err';
34
- }
35
- export function withDefault(result, defaultValue) {
36
- return result.type === 'ok' ? result.value : defaultValue;
37
- }
38
- export function value(result) {
39
- return result.type === 'ok' ? result.value : undefined;
40
- }
41
- export function errValue(result) {
42
- return result.type === 'err' ? result.error : undefined;
43
- }
44
- /**
45
- * Unwrap the value from this Result instance if this is an "Ok" result.
46
- * Otherwise, will throw the "Err" error via a runtime exception.
47
- */
48
-
49
- export function unwrap(result) {
50
- if (result.type === 'ok') {
51
- return result.value;
52
- } else {
53
- throw result.error;
54
- }
55
- }
56
- export function expect(result, message) {
57
- if (result.type === 'ok') {
58
- return result.value;
59
- } else {
60
- throw message instanceof Error ? message : new Error(message);
61
- }
62
- }
63
- export function dispatch(result, okCallback, errCallback) {
64
- return result.type === 'ok' ? okCallback(result.value) : errCallback(result.error);
65
- }
66
- /**
67
- * If the given result is OK, defers to the other result. Otherwise returns the
68
- * error result.
69
- *
70
- * It's like saying A && B, but on Result.
71
- *
72
- * Examples:
73
- *
74
- * Result.ok(42) && Result.ok('hi') // => Ok('hi')
75
- * Result.err('boo') && Result.ok('hi') // => Err('boo')
76
- * Result.ok(42) && Result.err('boo') // => Err('boo')
77
- * Result.err('boo') && Result.err('boo') // => Err('boo')
78
- *
79
- */
80
- // export function and<T, E, T2>(
81
- // result1: Result<T, E>,
82
- // result2: Result<T2, E>,
83
- // ): Result<T2, E> {
84
- // return result1.type === 'ok' ? result2 : result1;
85
- // }
86
-
87
- /**
88
- * If the given result is OK, return that result. Otherwise, defers to the
89
- * other result.
90
- *
91
- * It's like saying A || B, but on Result.
92
- *
93
- * Examples:
94
- *
95
- * Result.ok(42) || Result.ok('hi') // => Ok(42)
96
- * Result.err('boo') || Result.ok('hi') // => Ok('hi')
97
- * Result.ok(42) || Result.err('boo') // => Ok(42)
98
- * Result.err('bleh') || Result.err('boo') // => Err('boo')
99
- *
100
- */
101
- // export function or<T, E, E2>(
102
- // result1: Result<T, E>,
103
- // result2: Result<T, E2>,
104
- // ): Result<T, E2> {
105
- // return result1.type === 'ok' ? result1 : result2;
106
- // }
107
-
108
- /**
109
- * Like .and(), aka &&, but the second argument gets evaluated lazily only if
110
- * the first result is an Ok result. If so, it has access to the Ok value from
111
- * the first argument.
112
- */
113
-
114
- export function andThen(result1, lazyResult2) {
115
- return result1.type === 'ok' ? lazyResult2(result1.value) : result1;
116
- }
117
- /**
118
- * Like .or(), aka ||, but the second argument gets evaluated lazily only if
119
- * the first result is an Err result. If so, it has access to the Err value
120
- * from the first argument.
121
- */
122
-
123
- export function orElse(result1, lazyResult2) {
124
- return result1.type === 'ok' ? result1 : lazyResult2(result1.error);
125
- }
126
- /**
127
- * Transform an Ok result. Will not touch Err results.
128
- */
129
-
130
- export function map(result, mapper) {
131
- return result.type === 'ok' ? ok(mapper(result.value)) : result;
132
- }
133
- /**
134
- * Transform an Err value. Will not touch Ok results.
135
- */
136
-
137
- export function mapError(result, mapper) {
138
- return result.type === 'ok' ? result : err(mapper(result.error));
139
- }
@@ -1,91 +0,0 @@
1
- import * as Result from '../result';
2
- import { annotate } from '../annotate';
3
- import { compose, predicate } from './composition';
4
-
5
- /**
6
- * Like a "Plain Old JavaScript Object", but for arrays: "Plain Old JavaScript
7
- * Array" ^_^
8
- */
9
- export var poja = function poja(blob) {
10
- if (!Array.isArray(blob)) {
11
- return Result.err(annotate(blob, 'Must be an array'));
12
- }
13
-
14
- return Result.ok( // NOTE: Since Flow 0.98, Array.isArray() returns $ReadOnlyArray<mixed>
15
- // instead of Array<mixed>. For rationale, see
16
- // https://github.com/facebook/flow/issues/7684. In this case, we
17
- // don't want to output read-only types because it's up to the user of
18
- // decoders to determine what they want to do with the decoded output.
19
- // If they want to write items into the array, that's fine!
20
- // The fastest way to turn a read-only array into a normal array in
21
- // Javascript is to use .slice() on it, see this benchmark:
22
- // http://jsben.ch/lO6C5
23
- blob.slice());
24
- };
25
- /**
26
- * Given an array of Result instances, loop over them all and return:
27
- * - An [index, err] tuple, indicating the (index of the) first Err instance
28
- * encountered; or
29
- * - a new Ok with an array of all unwrapped Ok'ed values
30
- */
31
-
32
- function all(items, blobs) {
33
- var results = [];
34
-
35
- for (var index = 0; index < items.length; ++index) {
36
- var result = items[index];
37
-
38
- if (result.type === 'ok') {
39
- results.push(result.value);
40
- } else {
41
- var ann = result.error; // Rewrite the annotation to include the index information, and inject it into the original blob
42
-
43
- var clone = [].concat(blobs);
44
- clone.splice(index, 1, annotate(ann, ann.text ? ann.text + " (at index " + index + ")" : "index " + index)); // const errValue = [];
45
- // if (index > 0) {
46
- // errValue.push('...'); // TODO: make special mark, not string!
47
- // }
48
- // errValue.push(
49
- // );
50
- // if (index < iterable.length - 1) {
51
- // errValue.push('...'); // TODO: make special mark, not string!
52
- // }
53
-
54
- return Result.err(annotate(clone));
55
- }
56
- }
57
-
58
- return Result.ok(results);
59
- }
60
- /**
61
- * Given a T, builds a decoder that assumes an array input and returns an
62
- * Array<T>.
63
- */
64
-
65
-
66
- function members(decoder) {
67
- return function (blobs) {
68
- var results = blobs.map(decoder);
69
- var result = all(results, blobs);
70
- return result;
71
- };
72
- }
73
- /**
74
- * Builds a Decoder that returns Ok for values of `Array<T>`, given a Decoder
75
- * for `T`. Err otherwise.
76
- */
77
-
78
-
79
- export function array(decoder) {
80
- return compose(poja, members(decoder));
81
- }
82
- /**
83
- * Builds a Decoder that returns Ok for values of `Array<T>`, but will reject
84
- * empty arrays.
85
- */
86
-
87
- export function nonEmptyArray(decoder) {
88
- return compose(array(decoder), predicate(function (arr) {
89
- return arr.length > 0;
90
- }, 'Must be non-empty array'));
91
- }
@@ -1,28 +0,0 @@
1
- import * as Result from '../result';
2
- import { annotate } from '../annotate';
3
- import { map } from './composition';
4
- import { number } from './number';
5
-
6
- /**
7
- * Decoder that only returns Ok for boolean inputs. Err otherwise.
8
- */
9
- var _boolean = function _boolean(blob) {
10
- return typeof blob === 'boolean' ? Result.ok(blob) : Result.err(annotate(blob, 'Must be boolean'));
11
- };
12
- /**
13
- * Decoder that returns true for all truthy values, and false otherwise. Never fails.
14
- */
15
-
16
-
17
- export { _boolean as boolean };
18
- export var truthy = function truthy(blob) {
19
- return Result.ok(!!blob);
20
- };
21
- /**
22
- * Decoder that only returns Ok for numeric input values representing booleans.
23
- * Returns their boolean representation. Err otherwise.
24
- */
25
-
26
- export var numericBoolean = map(number, function (n) {
27
- return !!n;
28
- });
@@ -1,42 +0,0 @@
1
- import * as Result from '../result';
2
- import { annotate } from '../annotate';
3
-
4
- /**
5
- * Given a decoder T and a mapping function from T's to V's, returns a decoder
6
- * for V's. This is useful to change the original input data.
7
- */
8
- export function map(decoder, mapper) {
9
- return compose(decoder, function (x) {
10
- try {
11
- return Result.ok(mapper(x));
12
- } catch (e) {
13
- return Result.err(annotate(x, e instanceof Error ? e.message : String(e)));
14
- }
15
- });
16
- }
17
- /**
18
- * Compose two decoders by passing the result of the first into the second.
19
- * The second decoder may assume as its input type the output type of the first
20
- * decoder (so it's not necessary to accept the typical "mixed"). This is
21
- * useful for "narrowing down" the checks. For example, if you want to write
22
- * a decoder for positive numbers, you can compose it from an existing decoder
23
- * for any number, and a decoder that, assuming a number, checks if it's
24
- * positive. Very often combined with the predicate() helper as the second
25
- * argument.
26
- */
27
-
28
- export function compose(decoder, next) {
29
- return function (blob) {
30
- return Result.andThen(decoder(blob), next);
31
- };
32
- }
33
- /**
34
- * Factory function returning a Decoder<T>, given a predicate function that
35
- * accepts/rejects the input of type T.
36
- */
37
-
38
- export function predicate(predicate, msg) {
39
- return function (value) {
40
- return predicate(value) ? Result.ok(value) : Result.err(annotate(value, msg));
41
- };
42
- }
@@ -1,46 +0,0 @@
1
- import * as Result from '../result';
2
- import { annotate } from '../annotate';
3
-
4
- /**
5
- * Decoder that only returns Ok for `null` inputs. Err otherwise.
6
- */
7
- export var null_ = function null_(blob) {
8
- return blob === null ? Result.ok(blob) : Result.err(annotate(blob, 'Must be null'));
9
- };
10
- /**
11
- * Decoder that only returns Ok for `undefined` inputs. Err otherwise.
12
- */
13
-
14
- export var undefined_ = function undefined_(blob) {
15
- return blob === undefined ? Result.ok(blob) : Result.err(annotate(blob, 'Must be undefined'));
16
- };
17
- /**
18
- * Decoder that only returns Ok for the given value constant. Err otherwise.
19
- */
20
-
21
- export function constant(value) {
22
- return function (blob) {
23
- return blob === value ? Result.ok(value) : Result.err(annotate(blob, "Must be constant " + String(value)));
24
- };
25
- }
26
- /**
27
- * Decoder that always returns Ok for the given hardcoded value, no matter what the input.
28
- */
29
-
30
- export function hardcoded(value) {
31
- return function () {
32
- return Result.ok(value);
33
- };
34
- }
35
- /**
36
- * Decoder that always returns Ok for the given hardcoded value, no matter what the input.
37
- */
38
-
39
- export var unknown = function unknown(blob) {
40
- return Result.ok(blob);
41
- };
42
- /**
43
- * Alias of unknown.
44
- */
45
-
46
- export var mixed = unknown;
package/es/stdlib/date.js DELETED
@@ -1,28 +0,0 @@
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';
6
- // Only matches the shape. This "over-matches" some values that still aren't
7
- // valid dates (like 9999-99-99), but those will be caught by JS Date's
8
- // internal validations
9
- var iso8601_re = /^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}(?:[.]\d+)?(?:Z|[+-]\d{2}:?\d{2})$/;
10
- export var date = function date(value) {
11
- return isDate(value) ? Result.ok(value) : Result.err(annotate(value, 'Must be a Date'));
12
- };
13
- /**
14
- * Decoder that only returns Ok for strings that are valid ISO8601 date
15
- * strings. Err otherwise.
16
- */
17
-
18
- export var iso8601 = map( // Input itself needs to match the ISO8601 regex...
19
- regex(iso8601_re, 'Must be ISO8601 format'), // Make sure it is a _valid_ date
20
- function (value) {
21
- var date = new Date(value);
22
-
23
- if (isNaN(date.getTime())) {
24
- throw new Error('Must be valid date/time value');
25
- }
26
-
27
- return date;
28
- });
@@ -1,16 +0,0 @@
1
- import * as Result from '../result';
2
- import { annotate } from '../annotate';
3
-
4
- /**
5
- * Wrap another decoder, and override the error message in case it fails. This
6
- * is useful to "simplify" otherwise potentially complex error messages, or to
7
- * use language in those error messages that can be relayed to end users (for
8
- * example to show in form errors).
9
- */
10
- export function describe(decoder, message) {
11
- return function (blob) {
12
- return Result.mapError(decoder(blob), function (err) {
13
- return annotate(err, message);
14
- });
15
- };
16
- }