decoders 2.0.0-beta4 → 2.0.0-beta8

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 (132) hide show
  1. package/CHANGELOG.md +10 -0
  2. package/NotSupportedTSVersion.d.ts +1 -0
  3. package/README.md +728 -343
  4. package/_guard.d.ts +7 -0
  5. package/_guard.js +2 -6
  6. package/_guard.js.flow +3 -3
  7. package/{_esm/_guard.js → _guard.mjs} +3 -3
  8. package/_types.d.ts +13 -0
  9. package/{_esm/_types.js → _types.mjs} +0 -0
  10. package/_utils.d.ts +10 -0
  11. package/{_esm/_utils.js → _utils.mjs} +0 -0
  12. package/annotate.d.ts +62 -0
  13. package/{_esm/annotate.js → annotate.mjs} +0 -0
  14. package/core/_helpers.d.ts +79 -0
  15. package/core/array.d.ts +5 -0
  16. package/core/array.js +7 -11
  17. package/core/array.js.flow +6 -9
  18. package/{_esm/core/array.js → core/array.mjs} +9 -9
  19. package/core/boolean.d.ts +5 -0
  20. package/core/boolean.js +4 -8
  21. package/core/boolean.js.flow +3 -5
  22. package/{_esm/core/boolean.js → core/boolean.mjs} +6 -6
  23. package/core/composition.d.ts +14 -0
  24. package/core/composition.js +9 -11
  25. package/core/composition.js.flow +13 -8
  26. package/{_esm/core/composition.js → core/composition.mjs} +10 -8
  27. package/core/constants.d.ts +11 -0
  28. package/core/constants.js +6 -10
  29. package/core/constants.js.flow +7 -9
  30. package/{_esm/core/constants.js → core/constants.mjs} +7 -7
  31. package/core/date.d.ts +4 -0
  32. package/core/date.js +3 -7
  33. package/core/date.js.flow +2 -4
  34. package/{_esm/core/date.js → core/date.mjs} +6 -6
  35. package/core/describe.d.ts +3 -0
  36. package/core/describe.js +2 -6
  37. package/core/describe.js.flow +2 -2
  38. package/{_esm/core/describe.js → core/describe.mjs} +3 -3
  39. package/core/dispatch.d.ts +8 -0
  40. package/core/dispatch.js +9 -13
  41. package/core/dispatch.js.flow +8 -8
  42. package/{_esm/core/dispatch.js → core/dispatch.mjs} +10 -10
  43. package/core/either.d.ts +61 -0
  44. package/core/either.js +7 -11
  45. package/core/either.js.flow +6 -6
  46. package/{_esm/core/either.js → core/either.mjs} +8 -8
  47. package/core/fail.d.ts +3 -0
  48. package/core/fail.js +2 -6
  49. package/core/fail.js.flow +2 -2
  50. package/{_esm/core/fail.js → core/fail.mjs} +3 -3
  51. package/core/instanceOf.d.ts +3 -0
  52. package/core/instanceOf.js +2 -6
  53. package/core/instanceOf.js.flow +3 -3
  54. package/core/instanceOf.mjs +8 -0
  55. package/core/json.d.ts +11 -0
  56. package/core/json.mjs +15 -0
  57. package/core/lazy.d.ts +3 -0
  58. package/{_esm/core/lazy.js → core/lazy.mjs} +0 -0
  59. package/core/mapping.d.ts +6 -0
  60. package/core/mapping.js +23 -23
  61. package/core/mapping.js.flow +25 -17
  62. package/{_esm/core/mapping.js → core/mapping.mjs} +26 -22
  63. package/core/number.d.ts +6 -0
  64. package/core/number.js +9 -13
  65. package/core/number.js.flow +18 -12
  66. package/core/number.mjs +25 -0
  67. package/core/object.d.ts +33 -0
  68. package/core/object.js +7 -11
  69. package/core/object.js.flow +7 -7
  70. package/{_esm/core/object.js → core/object.mjs} +9 -9
  71. package/core/optional.d.ts +5 -0
  72. package/core/optional.js +4 -8
  73. package/core/optional.js.flow +3 -3
  74. package/{_esm/core/optional.js → core/optional.mjs} +6 -6
  75. package/core/string.d.ts +10 -0
  76. package/core/string.js +18 -48
  77. package/core/string.js.flow +16 -39
  78. package/core/string.mjs +49 -0
  79. package/core/tuple.d.ts +30 -0
  80. package/core/tuple.js +12 -8
  81. package/core/tuple.js.flow +25 -29
  82. package/{_esm/core/tuple.js → core/tuple.mjs} +16 -12
  83. package/format.d.ts +4 -0
  84. package/{format/inline.js → format.js} +6 -1
  85. package/{_esm/format/inline.js.flow → format.js.flow} +6 -2
  86. package/{_esm/format/inline.js → format.mjs} +4 -1
  87. package/index.d.ts +43 -0
  88. package/index.js +3 -2
  89. package/index.js.flow +2 -2
  90. package/{_esm/index.js → index.mjs} +19 -19
  91. package/package.json +15 -3
  92. package/result.d.ts +39 -0
  93. package/result.js +3 -78
  94. package/result.js.flow +4 -76
  95. package/{_esm/result.js → result.mjs} +3 -64
  96. package/_esm/_guard.js.flow +0 -20
  97. package/_esm/_types.js.flow +0 -20
  98. package/_esm/_utils.js.flow +0 -97
  99. package/_esm/annotate.js.flow +0 -218
  100. package/_esm/core/array.js.flow +0 -103
  101. package/_esm/core/boolean.js.flow +0 -29
  102. package/_esm/core/composition.js.flow +0 -43
  103. package/_esm/core/constants.js.flow +0 -46
  104. package/_esm/core/date.js.flow +0 -40
  105. package/_esm/core/describe.js.flow +0 -17
  106. package/_esm/core/dispatch.js.flow +0 -58
  107. package/_esm/core/either.js.flow +0 -151
  108. package/_esm/core/fail.js.flow +0 -12
  109. package/_esm/core/instanceOf.js +0 -8
  110. package/_esm/core/instanceOf.js.flow +0 -20
  111. package/_esm/core/json.js +0 -15
  112. package/_esm/core/json.js.flow +0 -28
  113. package/_esm/core/lazy.js.flow +0 -15
  114. package/_esm/core/mapping.js.flow +0 -54
  115. package/_esm/core/number.js +0 -25
  116. package/_esm/core/number.js.flow +0 -34
  117. package/_esm/core/object.js.flow +0 -203
  118. package/_esm/core/optional.js.flow +0 -41
  119. package/_esm/core/string.js +0 -76
  120. package/_esm/core/string.js.flow +0 -82
  121. package/_esm/core/tuple.js.flow +0 -215
  122. package/_esm/format/index.js +0 -2
  123. package/_esm/format/index.js.flow +0 -4
  124. package/_esm/format/short.js +0 -4
  125. package/_esm/format/short.js.flow +0 -8
  126. package/_esm/index.js.flow +0 -63
  127. package/_esm/result.js.flow +0 -170
  128. package/format/index.js +0 -12
  129. package/format/index.js.flow +0 -4
  130. package/format/inline.js.flow +0 -122
  131. package/format/short.js +0 -10
  132. package/format/short.js.flow +0 -8
@@ -1,203 +0,0 @@
1
- // @flow strict
2
-
3
- import * as Result from '../result';
4
- import { annotate, annotateObject, merge, updateText } from '../annotate';
5
- import { compose, map } from './composition';
6
- import type { Annotation } from '../annotate';
7
- import type { Decoder, DecoderType } from '../_types';
8
-
9
- // $FlowFixMe[unclear-type] (not really an issue) - deliberate use of `any` - not sure how we should get rid of this
10
- type AnyDecoder = any;
11
-
12
- // $FlowFixMe[unclear-type] (not really an issue) - deliberately casting
13
- type cast = any;
14
-
15
- function isPojo(o: mixed): boolean %checks {
16
- return (
17
- o !== null &&
18
- o !== undefined &&
19
- typeof o === 'object' &&
20
- // This still seems to be the only reliable way to determine whether
21
- // something is a pojo... ¯\_(ツ)_/¯
22
- // $FlowFixMe[method-unbinding]
23
- Object.prototype.toString.call(o) === '[object Object]'
24
- );
25
- }
26
-
27
- function subtract(xs: Set<string>, ys: Set<string>): Set<string> {
28
- const result = new Set();
29
- xs.forEach((x) => {
30
- if (!ys.has(x)) {
31
- result.add(x);
32
- }
33
- });
34
- return result;
35
- }
36
-
37
- export const pojo: Decoder<{| [string]: mixed |}> = (blob: mixed) => {
38
- return isPojo(blob)
39
- ? Result.ok(
40
- // NOTE:
41
- // Since Flow 0.98, typeof o === 'object' refines to
42
- // {| +[string]: mixed |}
43
- // instead of
44
- // {| [string]: mixed |}
45
- //
46
- // For rationale, see https://github.com/facebook/flow/issues/7685.
47
- // In this case, we don't want to output a read-only version of
48
- // the object because it's up to the user of decoders to
49
- // determine what they want to do with the decoded output. If they
50
- // want to write items into the array, that's fine! The fastest
51
- // way to turn a read-only Object to a writeable one in ES6 seems
52
- // to be to use object-spread. (Going off this benchmark:
53
- // https://thecodebarbarian.com/object-assign-vs-object-spread.html)
54
- { ...blob },
55
- )
56
- : Result.err(annotate(blob, 'Must be an object'));
57
- };
58
-
59
- /**
60
- * Given a mapping of fields-to-decoders, builds a decoder for an object type.
61
- *
62
- * For example, given decoders for a number and a string, we can construct an
63
- * "object description" like so:
64
- *
65
- * { id: number, name: string }
66
- *
67
- * Which is of type:
68
- *
69
- * { id: Decoder<number>, name: Decoder<string> }
70
- *
71
- * Passing this to object() will produce the following return type:
72
- *
73
- * Decoder<{ id: number, name: string }>
74
- *
75
- * Put simply: it'll "peel off" all of the nested Decoders, puts them together
76
- * in an object, and wraps it in a Decoder<...>.
77
- */
78
- export function object<O: { +[field: string]: AnyDecoder, ... }>(
79
- mapping: O,
80
- ): Decoder<$ObjMap<O, DecoderType>> {
81
- const known = new Set(Object.keys(mapping));
82
- return compose(pojo, (blob: { +[string]: mixed }) => {
83
- const actual = new Set(Object.keys(blob));
84
-
85
- // At this point, "missing" will also include all fields that may
86
- // validly be optional. We'll let the underlying decoder decide and
87
- // remove the key from this missing set if the decoder accepts the
88
- // value.
89
- const missing = subtract(known, actual);
90
-
91
- let record = {};
92
- let errors: { [key: string]: Annotation } | null = null;
93
-
94
- Object.keys(mapping).forEach((key) => {
95
- const decoder = mapping[key];
96
- const rawValue = blob[key];
97
- const result = decoder(rawValue);
98
-
99
- if (result.type === 'ok') {
100
- const value = result.value;
101
- if (value !== undefined) {
102
- record[key] = value;
103
- }
104
-
105
- // If this succeeded, remove the key from the missing keys
106
- // tracker
107
- missing.delete(key);
108
- } else {
109
- const ann = result.error;
110
-
111
- // Keep track of the annotation, but don't return just yet. We
112
- // want to collect more error information.
113
- if (rawValue === undefined) {
114
- // Explicitly add it to the missing set if the value is
115
- // undefined. This covers explicit undefineds to be
116
- // treated the same as implicit undefineds (aka missing
117
- // keys).
118
- missing.add(key);
119
- } else {
120
- if (errors === null) {
121
- errors = {};
122
- }
123
- errors[key] = ann;
124
- }
125
- }
126
- });
127
-
128
- // Deal with errors now. There are two classes of errors we want to
129
- // report. First of all, we want to report any inline errors in this
130
- // object. Lastly, any fields that are missing should be annotated on
131
- // the outer object itself.
132
- if (errors || missing.size > 0) {
133
- let objAnn = annotateObject(blob);
134
-
135
- if (errors) {
136
- objAnn = merge(objAnn, errors);
137
- }
138
-
139
- if (missing.size > 0) {
140
- const errMsg = Array.from(missing)
141
- .map((key) => `"${key}"`)
142
- .join(', ');
143
- const pluralized = missing.size > 1 ? 'keys' : 'key';
144
- objAnn = updateText(objAnn, `Missing ${pluralized}: ${errMsg}`);
145
- }
146
-
147
- return Result.err(objAnn);
148
- }
149
-
150
- return Result.ok(record);
151
- });
152
- }
153
-
154
- export function exact<O: { +[field: string]: AnyDecoder, ... }>(
155
- mapping: O,
156
- ): Decoder<$ObjMap<$Exact<O>, DecoderType>> {
157
- // Check the inputted object for any superfluous keys
158
- const allowed = new Set(Object.keys(mapping));
159
- const checked = compose(pojo, (blob: {| [string]: mixed |}) => {
160
- const actual = new Set(Object.keys(blob));
161
- const superfluous = subtract(actual, allowed);
162
- if (superfluous.size > 0) {
163
- return Result.err(
164
- annotate(blob, `Superfluous keys: ${Array.from(superfluous).join(', ')}`),
165
- );
166
- }
167
- return Result.ok(blob);
168
- });
169
-
170
- // Defer to the "object" decoder for doing the real decoding work. Since
171
- // we made sure there are no superfluous keys in this structure, it's now
172
- // safe to force-cast it to an $Exact<> type.
173
- const decoder = ((object(mapping): cast): Decoder<$ObjMap<$Exact<O>, DecoderType>>);
174
- return compose(checked, decoder);
175
- }
176
-
177
- export function inexact<O: { +[field: string]: AnyDecoder }>(
178
- mapping: O,
179
- ): Decoder<$ObjMap<O, DecoderType> & { +[string]: mixed }> {
180
- return compose(pojo, (blob: {| [string]: mixed |}) => {
181
- const allkeys = new Set(Object.keys(blob));
182
- const decoder = map(object(mapping), (safepart: $ObjMap<O, DecoderType>) => {
183
- const safekeys = new Set(Object.keys(mapping));
184
-
185
- // To account for hard-coded keys that aren't part of the input
186
- safekeys.forEach((k) => allkeys.add(k));
187
-
188
- const rv = {};
189
- allkeys.forEach((k) => {
190
- if (safekeys.has(k)) {
191
- const value = safepart[k];
192
- if (value !== undefined) {
193
- rv[k] = value;
194
- }
195
- } else {
196
- rv[k] = blob[k];
197
- }
198
- });
199
- return rv;
200
- });
201
- return decoder(blob);
202
- });
203
- }
@@ -1,41 +0,0 @@
1
- // @flow strict
2
-
3
- import * as Result from '../result';
4
- import { annotate } from '../annotate';
5
- import { either } from './either';
6
- import { null_, undefined_ } from './constants';
7
- import type { Decoder } from '../_types';
8
-
9
- /**
10
- * Builds a Decoder that returns Ok for either `undefined` or `T` values,
11
- * given a Decoder for `T`. Err otherwise.
12
- */
13
- export function optional<T>(decoder: Decoder<T>): Decoder<void | T> {
14
- return either(undefined_, decoder);
15
- }
16
-
17
- /**
18
- * Builds a Decoder that returns Ok for either `null` or `T` values,
19
- * given a Decoder for `T`. Err otherwise.
20
- */
21
- export function nullable<T>(decoder: Decoder<T>): Decoder<null | T> {
22
- return either(null_, decoder);
23
- }
24
-
25
- /**
26
- * Decoder that only returns Ok for `null` or `undefined` inputs.
27
- * This is effectively equivalent to either(null_, undefined_), but combines
28
- * their error message output into a single line for convenience.
29
- */
30
- const undefined_or_null: Decoder<null | void> = (blob: mixed) =>
31
- blob === undefined || blob === null
32
- ? Result.ok(blob)
33
- : // Combine error message into a single line
34
- Result.err(annotate(blob, 'Must be undefined or null'));
35
-
36
- /**
37
- * Decoder that only returns Ok for `null` or `undefined` inputs.
38
- */
39
- export function maybe<T>(decoder: Decoder<T>): Decoder<?T> {
40
- return either(undefined_or_null, decoder);
41
- }
@@ -1,76 +0,0 @@
1
- import * as Result from '../result';
2
- import { annotate } from '../annotate';
3
- import { compose, predicate } from './composition';
4
-
5
- /** Match groups in this regex:
6
- * \1 - the scheme
7
- * \2 - the username/password (optional)
8
- * \3 - the host
9
- * \4 - the port (optional)
10
- * \5 - the path (optional)
11
- */
12
- var url_re = /^([A-Za-z]{3,9}(?:[+][A-Za-z]{3,9})?):\/\/(?:([-;:&=+$,\w]+)@)?(?:([A-Za-z0-9.-]+)(?::([0-9]{2,5}))?)(\/(?:[-+~%/.,\w]*)?(?:\?[-+=&;%@.,\w]*)?(?:#[.,!/\w]*)?)?$/; // The URL schemes the url() decoder accepts by default
13
-
14
- var DEFAULT_SCHEMES = ['https'];
15
- /**
16
- * Decoder that only returns Ok for string inputs. Err otherwise.
17
- */
18
-
19
- export var string = function string(blob) {
20
- return typeof blob === 'string' ? Result.ok(blob) : Result.err(annotate(blob, 'Must be string'));
21
- };
22
- /**
23
- * Decoder that only returns Ok for non-empty string inputs. Err otherwise.
24
- */
25
-
26
- export var nonEmptyString = regex(/\S/, 'Must be non-empty string');
27
- /**
28
- * Decoder that only returns Ok for string inputs that match the regular
29
- * expression. Err otherwise. Will always validate that the input is a string
30
- * before testing the regex.
31
- */
32
-
33
- export function regex(regex, msg) {
34
- return compose(string, predicate(function (s) {
35
- return regex.test(s);
36
- }, msg));
37
- }
38
- /**
39
- * Decoder that only returns Ok for string inputs that match the almost perfect
40
- * email regex, taken from http://emailregex.com. Err otherwise.
41
- */
42
-
43
- export var email = regex(/^(([^<>()[\]\\.,;:\s@"]+(\.[^<>()[\]\\.,;:\s@"]+)*)|(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/, 'Must be email');
44
- /**
45
- * Decoder that only returns Ok for string inputs that match URLs of the
46
- * expected scheme. Defaults to only accept HTTPS URLs. Err otherwise.
47
- *
48
- * Variants that can be used:
49
- *
50
- * - url() accepts only https:// URLs
51
- * - url([]) accepts any URL scheme
52
- * - url(['http']) accepts only HTTP
53
- * - url(['https', 'git+ssh']) accepts both https:// and git+ssh:// URLs
54
- */
55
-
56
- export var url = function url(schemes) {
57
- if (schemes === void 0) {
58
- schemes = DEFAULT_SCHEMES;
59
- }
60
-
61
- return compose(string, function (value) {
62
- var matches = value.match(url_re);
63
-
64
- if (!matches) {
65
- return Result.err(annotate(value, 'Must be URL'));
66
- } else {
67
- var scheme = matches[1];
68
-
69
- if (schemes.length === 0 || schemes.includes(scheme.toLowerCase())) {
70
- return Result.ok(value);
71
- } else {
72
- return Result.err(annotate(value, "URL scheme must be any of: " + schemes.join(', ')));
73
- }
74
- }
75
- });
76
- };
@@ -1,82 +0,0 @@
1
- // @flow strict
2
-
3
- import * as Result from '../result';
4
- import { annotate } from '../annotate';
5
- import { compose, predicate } from './composition';
6
- import type { Decoder } from '../_types';
7
-
8
- /** Match groups in this regex:
9
- * \1 - the scheme
10
- * \2 - the username/password (optional)
11
- * \3 - the host
12
- * \4 - the port (optional)
13
- * \5 - the path (optional)
14
- */
15
- const url_re =
16
- /^([A-Za-z]{3,9}(?:[+][A-Za-z]{3,9})?):\/\/(?:([-;:&=+$,\w]+)@)?(?:([A-Za-z0-9.-]+)(?::([0-9]{2,5}))?)(\/(?:[-+~%/.,\w]*)?(?:\?[-+=&;%@.,\w]*)?(?:#[.,!/\w]*)?)?$/;
17
-
18
- // The URL schemes the url() decoder accepts by default
19
- const DEFAULT_SCHEMES = ['https'];
20
-
21
- /**
22
- * Decoder that only returns Ok for string inputs. Err otherwise.
23
- */
24
- export const string: Decoder<string> = (blob: mixed) => {
25
- return typeof blob === 'string'
26
- ? Result.ok(blob)
27
- : Result.err(annotate(blob, 'Must be string'));
28
- };
29
-
30
- /**
31
- * Decoder that only returns Ok for non-empty string inputs. Err otherwise.
32
- */
33
- export const nonEmptyString: Decoder<string> = regex(/\S/, 'Must be non-empty string');
34
-
35
- /**
36
- * Decoder that only returns Ok for string inputs that match the regular
37
- * expression. Err otherwise. Will always validate that the input is a string
38
- * before testing the regex.
39
- */
40
- export function regex(regex: RegExp, msg: string): Decoder<string> {
41
- return compose(
42
- string,
43
- predicate((s) => regex.test(s), msg),
44
- );
45
- }
46
-
47
- /**
48
- * Decoder that only returns Ok for string inputs that match the almost perfect
49
- * email regex, taken from http://emailregex.com. Err otherwise.
50
- */
51
- export const email: Decoder<string> = regex(
52
- /^(([^<>()[\]\\.,;:\s@"]+(\.[^<>()[\]\\.,;:\s@"]+)*)|(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/,
53
- 'Must be email',
54
- );
55
-
56
- /**
57
- * Decoder that only returns Ok for string inputs that match URLs of the
58
- * expected scheme. Defaults to only accept HTTPS URLs. Err otherwise.
59
- *
60
- * Variants that can be used:
61
- *
62
- * - url() accepts only https:// URLs
63
- * - url([]) accepts any URL scheme
64
- * - url(['http']) accepts only HTTP
65
- * - url(['https', 'git+ssh']) accepts both https:// and git+ssh:// URLs
66
- */
67
- export const url = (schemes: $ReadOnlyArray<string> = DEFAULT_SCHEMES): Decoder<string> =>
68
- compose(string, (value: string) => {
69
- const matches = value.match(url_re);
70
- if (!matches) {
71
- return Result.err(annotate(value, 'Must be URL'));
72
- } else {
73
- const scheme = matches[1];
74
- if (schemes.length === 0 || schemes.includes(scheme.toLowerCase())) {
75
- return Result.ok(value);
76
- } else {
77
- return Result.err(
78
- annotate(value, `URL scheme must be any of: ${schemes.join(', ')}`),
79
- );
80
- }
81
- }
82
- });
@@ -1,215 +0,0 @@
1
- // @flow strict
2
-
3
- import { annotate } from '../annotate';
4
- import { compose, predicate } from './composition';
5
- import { err, ok, okOrErrValue, unwrap } 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([okOrErrValue(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(annotate([okOrErrValue(result1), okOrErrValue(result2)]));
53
- }
54
- });
55
- }
56
-
57
- /**
58
- * Builds a Decoder that returns Ok for 3-tuples of [T1, T2, T3], given
59
- * Decoders for T1, T2, and T3. Err otherwise.
60
- */
61
- export function tuple3<T1, T2, T3>(
62
- decoder1: Decoder<T1>,
63
- decoder2: Decoder<T2>,
64
- decoder3: Decoder<T3>,
65
- ): Decoder<[T1, T2, T3]> {
66
- return compose(ntuple(3), (blobs: $ReadOnlyArray<mixed>) => {
67
- const [blob1, blob2, blob3] = blobs;
68
-
69
- const result1 = decoder1(blob1);
70
- const result2 = decoder2(blob2);
71
- const result3 = decoder3(blob3);
72
- try {
73
- return ok([unwrap(result1), unwrap(result2), unwrap(result3)]);
74
- } catch (e) {
75
- // If a decoder error has happened while unwrapping all the
76
- // results, try to construct a good error message
77
- return err(
78
- annotate([
79
- okOrErrValue(result1),
80
- okOrErrValue(result2),
81
- okOrErrValue(result3),
82
- ]),
83
- );
84
- }
85
- });
86
- }
87
-
88
- /**
89
- * Builds a Decoder that returns Ok for 4-tuples of [T1, T2, T3, T4], given
90
- * Decoders for T1, T2, T3, and T4. Err otherwise.
91
- */
92
- export function tuple4<T1, T2, T3, T4>(
93
- decoder1: Decoder<T1>,
94
- decoder2: Decoder<T2>,
95
- decoder3: Decoder<T3>,
96
- decoder4: Decoder<T4>,
97
- ): Decoder<[T1, T2, T3, T4]> {
98
- return compose(ntuple(4), (blobs: $ReadOnlyArray<mixed>) => {
99
- const [blob1, blob2, blob3, blob4] = blobs;
100
-
101
- const result1 = decoder1(blob1);
102
- const result2 = decoder2(blob2);
103
- const result3 = decoder3(blob3);
104
- const result4 = decoder4(blob4);
105
- try {
106
- return ok([
107
- unwrap(result1),
108
- unwrap(result2),
109
- unwrap(result3),
110
- unwrap(result4),
111
- ]);
112
- } catch (e) {
113
- // If a decoder error has happened while unwrapping all the
114
- // results, try to construct a good error message
115
- return err(
116
- annotate([
117
- okOrErrValue(result1),
118
- okOrErrValue(result2),
119
- okOrErrValue(result3),
120
- okOrErrValue(result4),
121
- ]),
122
- );
123
- }
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, and T5. Err otherwise.
130
- */
131
- export function tuple5<T1, T2, T3, T4, T5>(
132
- decoder1: Decoder<T1>,
133
- decoder2: Decoder<T2>,
134
- decoder3: Decoder<T3>,
135
- decoder4: Decoder<T4>,
136
- decoder5: Decoder<T5>,
137
- ): Decoder<[T1, T2, T3, T4, T5]> {
138
- return compose(ntuple(5), (blobs: $ReadOnlyArray<mixed>) => {
139
- const [blob1, blob2, blob3, blob4, blob5] = blobs;
140
-
141
- const result1 = decoder1(blob1);
142
- const result2 = decoder2(blob2);
143
- const result3 = decoder3(blob3);
144
- const result4 = decoder4(blob4);
145
- const result5 = decoder5(blob5);
146
- try {
147
- return ok([
148
- unwrap(result1),
149
- unwrap(result2),
150
- unwrap(result3),
151
- unwrap(result4),
152
- unwrap(result5),
153
- ]);
154
- } catch (e) {
155
- // If a decoder error has happened while unwrapping all the
156
- // results, try to construct a good error message
157
- return err(
158
- annotate([
159
- okOrErrValue(result1),
160
- okOrErrValue(result2),
161
- okOrErrValue(result3),
162
- okOrErrValue(result4),
163
- okOrErrValue(result5),
164
- ]),
165
- );
166
- }
167
- });
168
- }
169
-
170
- /**
171
- * Builds a Decoder that returns Ok for 5-tuples of [T1, T2, T3, T4, T5], given
172
- * Decoders for T1, T2, T3, T4, T5, and T6. Err otherwise.
173
- */
174
- export function tuple6<T1, T2, T3, T4, T5, T6>(
175
- decoder1: Decoder<T1>,
176
- decoder2: Decoder<T2>,
177
- decoder3: Decoder<T3>,
178
- decoder4: Decoder<T4>,
179
- decoder5: Decoder<T5>,
180
- decoder6: Decoder<T6>,
181
- ): Decoder<[T1, T2, T3, T4, T5, T6]> {
182
- return compose(ntuple(6), (blobs: $ReadOnlyArray<mixed>) => {
183
- const [blob1, blob2, blob3, blob4, blob5, blob6] = blobs;
184
-
185
- const result1 = decoder1(blob1);
186
- const result2 = decoder2(blob2);
187
- const result3 = decoder3(blob3);
188
- const result4 = decoder4(blob4);
189
- const result5 = decoder5(blob5);
190
- const result6 = decoder6(blob6);
191
- try {
192
- return ok([
193
- unwrap(result1),
194
- unwrap(result2),
195
- unwrap(result3),
196
- unwrap(result4),
197
- unwrap(result5),
198
- unwrap(result6),
199
- ]);
200
- } catch (e) {
201
- // If a decoder error has happened while unwrapping all the
202
- // results, try to construct a good error message
203
- return err(
204
- annotate([
205
- okOrErrValue(result1),
206
- okOrErrValue(result2),
207
- okOrErrValue(result3),
208
- okOrErrValue(result4),
209
- okOrErrValue(result5),
210
- okOrErrValue(result6),
211
- ]),
212
- );
213
- }
214
- });
215
- }
@@ -1,2 +0,0 @@
1
- export { formatInline } from './inline';
2
- export { formatShort } from './short';
@@ -1,4 +0,0 @@
1
- // @flow strict
2
-
3
- export { formatInline } from './inline';
4
- 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
- }
@@ -1,8 +0,0 @@
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
- }