decoders 1.25.5 → 2.0.0-beta2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (151) hide show
  1. package/CHANGELOG.md +23 -3
  2. package/_esm/_guard.js +15 -0
  3. package/_esm/_guard.js.flow +20 -0
  4. package/_esm/_types.js +0 -0
  5. package/_esm/_types.js.flow +20 -0
  6. package/_esm/_utils.js +93 -0
  7. package/_esm/_utils.js.flow +97 -0
  8. package/_esm/annotate.js +144 -0
  9. package/_esm/annotate.js.flow +218 -0
  10. package/_esm/core/array.js +91 -0
  11. package/{array.js.flow → _esm/core/array.js.flow} +22 -25
  12. package/_esm/core/boolean.js +28 -0
  13. package/{boolean.js.flow → _esm/core/boolean.js.flow} +8 -7
  14. package/_esm/core/composition.js +42 -0
  15. package/{utils.js.flow → _esm/core/composition.js.flow} +7 -22
  16. package/_esm/core/constants.js +46 -0
  17. package/{constants.js.flow → _esm/core/constants.js.flow} +13 -12
  18. package/_esm/core/date.js +28 -0
  19. package/{date.js.flow → _esm/core/date.js.flow} +9 -7
  20. package/{describe.js → _esm/core/describe.js} +5 -11
  21. package/{describe.js.flow → _esm/core/describe.js.flow} +4 -4
  22. package/{dispatch.js → _esm/core/dispatch.js} +8 -15
  23. package/{dispatch.js.flow → _esm/core/dispatch.js.flow} +6 -5
  24. package/_esm/core/either.js +90 -0
  25. package/{either.js.flow → _esm/core/either.js.flow} +55 -35
  26. package/_esm/core/fail.js +11 -0
  27. package/_esm/core/fail.js.flow +12 -0
  28. package/_esm/core/instanceOf.js +8 -0
  29. package/{instanceOf.js.flow → _esm/core/instanceOf.js.flow} +7 -8
  30. package/_esm/core/json.js +15 -0
  31. package/{json.js.flow → _esm/core/json.js.flow} +4 -4
  32. package/_esm/core/lazy.js +11 -0
  33. package/{lazy.js.flow → _esm/core/lazy.js.flow} +1 -1
  34. package/_esm/core/mapping.js +54 -0
  35. package/_esm/core/mapping.js.flow +54 -0
  36. package/_esm/core/number.js +25 -0
  37. package/{number.js.flow → _esm/core/number.js.flow} +10 -11
  38. package/_esm/core/object.js +175 -0
  39. package/{object.js.flow → _esm/core/object.js.flow} +44 -52
  40. package/_esm/core/optional.js +38 -0
  41. package/{optional.js.flow → _esm/core/optional.js.flow} +6 -7
  42. package/{string.js → _esm/core/string.js} +18 -35
  43. package/{string.js.flow → _esm/core/string.js.flow} +13 -12
  44. package/_esm/core/tuple.js +155 -0
  45. package/{tuple.js.flow → _esm/core/tuple.js.flow} +62 -63
  46. package/_esm/format/index.js +2 -0
  47. package/_esm/format/index.js.flow +4 -0
  48. package/_esm/format/inline.js +137 -0
  49. package/_esm/format/inline.js.flow +122 -0
  50. package/_esm/format/short.js +4 -0
  51. package/_esm/format/short.js.flow +8 -0
  52. package/_esm/index.js +37 -0
  53. package/_esm/index.js.flow +56 -0
  54. package/_esm/result.js +139 -0
  55. package/_esm/result.js.flow +166 -0
  56. package/_guard.js +26 -0
  57. package/_guard.js.flow +20 -0
  58. package/_types.js +1 -0
  59. package/_types.js.flow +20 -0
  60. package/_utils.js +108 -0
  61. package/_utils.js.flow +97 -0
  62. package/annotate.js +161 -0
  63. package/annotate.js.flow +218 -0
  64. package/core/array.js +108 -0
  65. package/core/array.js.flow +103 -0
  66. package/core/boolean.js +44 -0
  67. package/core/boolean.js.flow +29 -0
  68. package/core/composition.js +56 -0
  69. package/core/composition.js.flow +43 -0
  70. package/core/constants.js +69 -0
  71. package/core/constants.js.flow +46 -0
  72. package/core/date.js +46 -0
  73. package/core/date.js.flow +40 -0
  74. package/core/describe.js +26 -0
  75. package/core/describe.js.flow +17 -0
  76. package/core/dispatch.js +62 -0
  77. package/core/dispatch.js.flow +58 -0
  78. package/core/either.js +117 -0
  79. package/core/either.js.flow +151 -0
  80. package/core/fail.js +21 -0
  81. package/core/fail.js.flow +12 -0
  82. package/core/instanceOf.js +19 -0
  83. package/core/instanceOf.js.flow +20 -0
  84. package/{json.js → core/json.js} +3 -5
  85. package/core/json.js.flow +28 -0
  86. package/{lazy.js → core/lazy.js} +1 -3
  87. package/core/lazy.js.flow +15 -0
  88. package/core/mapping.js +67 -0
  89. package/core/mapping.js.flow +54 -0
  90. package/core/number.js +40 -0
  91. package/core/number.js.flow +34 -0
  92. package/core/object.js +194 -0
  93. package/core/object.js.flow +203 -0
  94. package/core/optional.js +54 -0
  95. package/core/optional.js.flow +41 -0
  96. package/core/string.js +98 -0
  97. package/core/string.js.flow +82 -0
  98. package/core/tuple.js +173 -0
  99. package/core/tuple.js.flow +220 -0
  100. package/format/index.js +12 -0
  101. package/format/index.js.flow +4 -0
  102. package/format/inline.js +146 -0
  103. package/format/inline.js.flow +122 -0
  104. package/format/short.js +10 -0
  105. package/format/short.js.flow +8 -0
  106. package/index.js +118 -395
  107. package/index.js.flow +24 -24
  108. package/package.json +2 -6
  109. package/result.js +172 -0
  110. package/result.js.flow +166 -0
  111. package/array.d.ts +0 -5
  112. package/array.js +0 -133
  113. package/boolean.d.ts +0 -5
  114. package/boolean.js +0 -42
  115. package/constants.d.ts +0 -11
  116. package/constants.js +0 -67
  117. package/date.d.ts +0 -4
  118. package/date.js +0 -42
  119. package/describe.d.ts +0 -3
  120. package/dispatch.d.ts +0 -8
  121. package/either.d.ts +0 -61
  122. package/either.js +0 -85
  123. package/fail.d.ts +0 -3
  124. package/fail.js +0 -19
  125. package/fail.js.flow +0 -13
  126. package/guard.d.ts +0 -7
  127. package/guard.js +0 -30
  128. package/guard.js.flow +0 -36
  129. package/helpers.d.ts +0 -79
  130. package/index.d.ts +0 -38
  131. package/instanceOf.d.ts +0 -3
  132. package/instanceOf.js +0 -17
  133. package/json.d.ts +0 -11
  134. package/lazy.d.ts +0 -3
  135. package/mapping.d.ts +0 -4
  136. package/mapping.js +0 -113
  137. package/mapping.js.flow +0 -71
  138. package/number.d.ts +0 -6
  139. package/number.js +0 -38
  140. package/object.d.ts +0 -33
  141. package/object.js +0 -254
  142. package/optional.d.ts +0 -5
  143. package/optional.js +0 -52
  144. package/string.d.ts +0 -7
  145. package/tuple.d.ts +0 -30
  146. package/tuple.js +0 -199
  147. package/types.d.ts +0 -18
  148. package/types.js +0 -5
  149. package/types.js.flow +0 -26
  150. package/utils.d.ts +0 -13
  151. package/utils.js +0 -70
package/annotate.js ADDED
@@ -0,0 +1,161 @@
1
+ "use strict";
2
+
3
+ exports.__esModule = true;
4
+ exports.__private_annotate = annotate;
5
+ exports.annotate = public_annotate;
6
+ exports.annotateObject = public_annotateObject;
7
+ exports.array = array;
8
+ exports.asAnnotation = asAnnotation;
9
+ exports.circularRef = circularRef;
10
+ exports.func = func;
11
+ exports.merge = merge;
12
+ exports.object = object;
13
+ exports.scalar = scalar;
14
+ exports.unknown = unknown;
15
+ exports.updateText = updateText;
16
+
17
+ 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); }
18
+
19
+ var _register = new WeakSet();
20
+
21
+ function brand(ann) {
22
+ _register.add(ann);
23
+
24
+ return ann;
25
+ }
26
+
27
+ function object(fields, text) {
28
+ return brand({
29
+ type: 'object',
30
+ fields: fields,
31
+ text: text
32
+ });
33
+ }
34
+
35
+ function array(items, text) {
36
+ return brand({
37
+ type: 'array',
38
+ items: items,
39
+ text: text
40
+ });
41
+ }
42
+
43
+ function func(text) {
44
+ return brand({
45
+ type: 'function',
46
+ text: text
47
+ });
48
+ }
49
+
50
+ function unknown(value, text) {
51
+ return brand({
52
+ type: 'unknown',
53
+ value: value,
54
+ text: text
55
+ });
56
+ }
57
+
58
+ function scalar(value, text) {
59
+ return brand({
60
+ type: 'scalar',
61
+ value: value,
62
+ text: text
63
+ });
64
+ }
65
+
66
+ function circularRef(text) {
67
+ return brand({
68
+ type: 'circular-ref',
69
+ text: text
70
+ });
71
+ }
72
+ /**
73
+ * Given an existing Annotation, set the annotation's text to a new value.
74
+ */
75
+
76
+
77
+ function updateText(annotation, text) {
78
+ if (text !== undefined) {
79
+ return brand(_extends({}, annotation, {
80
+ text: text
81
+ }));
82
+ } else {
83
+ return annotation;
84
+ }
85
+ }
86
+ /**
87
+ * Given an existing ObjectAnnotation, merges new Annotations in there.
88
+ */
89
+
90
+
91
+ function merge(objAnnotation, fields) {
92
+ var newFields = _extends({}, objAnnotation.fields, fields);
93
+
94
+ return object(newFields, objAnnotation.text);
95
+ }
96
+
97
+ function asAnnotation(thing) {
98
+ return typeof thing === 'object' && thing !== null && _register.has(thing) ? thing : undefined;
99
+ }
100
+
101
+ function annotateArray(value, text, seen) {
102
+ seen.add(value);
103
+ var items = value.map(function (v) {
104
+ return annotate(v, undefined, seen);
105
+ });
106
+ return array(items, text);
107
+ }
108
+
109
+ function annotateObject(obj, text, seen) {
110
+ seen.add(obj);
111
+ var fields = {};
112
+ Object.keys(obj).forEach(function (key) {
113
+ var value = obj[key];
114
+ fields[key] = annotate(value, undefined, seen);
115
+ });
116
+ return object(fields, text);
117
+ }
118
+
119
+ function annotate(value, text, seen) {
120
+ if (value === null || value === undefined || typeof value === 'string' || typeof value === 'number' || typeof value === 'boolean' || typeof value === 'symbol' || typeof value.getMonth === 'function') {
121
+ return scalar(value, text);
122
+ }
123
+
124
+ var ann = asAnnotation(value);
125
+
126
+ if (ann) {
127
+ return updateText(ann, text);
128
+ }
129
+
130
+ if (Array.isArray(value)) {
131
+ // "Circular references" can only exist in objects or arrays
132
+ if (seen.has(value)) {
133
+ return circularRef(text);
134
+ } else {
135
+ return annotateArray(value, text, seen);
136
+ }
137
+ }
138
+
139
+ if (typeof value === 'object') {
140
+ // "Circular references" can only exist in objects or arrays
141
+ if (seen.has(value)) {
142
+ return circularRef(text);
143
+ } else {
144
+ return annotateObject(value, text, seen);
145
+ }
146
+ }
147
+
148
+ if (typeof value === 'function') {
149
+ return func(text);
150
+ }
151
+
152
+ return unknown(value, text);
153
+ }
154
+
155
+ function public_annotate(value, text) {
156
+ return annotate(value, text, new WeakSet());
157
+ }
158
+
159
+ function public_annotateObject(obj, text) {
160
+ return annotateObject(obj, text, new WeakSet());
161
+ }
@@ -0,0 +1,218 @@
1
+ // @flow strict
2
+
3
+ type cast = $FlowFixMe;
4
+
5
+ const _register: WeakSet<{ ... }> = new WeakSet();
6
+
7
+ export type ObjectAnnotation = {|
8
+ +type: 'object',
9
+ +fields: { +[key: string]: Annotation },
10
+ +text?: string,
11
+ |};
12
+
13
+ export type ArrayAnnotation = {|
14
+ +type: 'array',
15
+ +items: $ReadOnlyArray<Annotation>,
16
+ +text?: string,
17
+ |};
18
+
19
+ export type ScalarAnnotation = {|
20
+ +type: 'scalar',
21
+ +value: mixed,
22
+ +text?: string,
23
+ |};
24
+
25
+ export type FunctionAnnotation = {|
26
+ +type: 'function',
27
+ +text?: string,
28
+ |};
29
+
30
+ export type CircularRefAnnotation = {|
31
+ +type: 'circular-ref',
32
+ +text?: string,
33
+ |};
34
+
35
+ export type UnknownAnnotation = {|
36
+ +type: 'unknown',
37
+ +value: mixed,
38
+ +text?: string,
39
+ |};
40
+
41
+ export type Annotation =
42
+ | ObjectAnnotation
43
+ | ArrayAnnotation
44
+ | ScalarAnnotation
45
+ | FunctionAnnotation
46
+ | CircularRefAnnotation
47
+ | UnknownAnnotation;
48
+
49
+ function brand<A: Annotation>(ann: A): A {
50
+ _register.add(ann);
51
+ return ann;
52
+ }
53
+
54
+ export function object(
55
+ fields: { +[key: string]: Annotation },
56
+ text?: string,
57
+ ): ObjectAnnotation {
58
+ return brand({ type: 'object', fields, text });
59
+ }
60
+
61
+ export function array(items: $ReadOnlyArray<Annotation>, text?: string): ArrayAnnotation {
62
+ return brand({
63
+ type: 'array',
64
+ items,
65
+ text,
66
+ });
67
+ }
68
+
69
+ export function func(text?: string): FunctionAnnotation {
70
+ return brand({
71
+ type: 'function',
72
+ text,
73
+ });
74
+ }
75
+
76
+ export function unknown(value: mixed, text?: string): UnknownAnnotation {
77
+ return brand({
78
+ type: 'unknown',
79
+ value,
80
+ text,
81
+ });
82
+ }
83
+
84
+ export function scalar(value: mixed, text?: string): ScalarAnnotation {
85
+ return brand({
86
+ type: 'scalar',
87
+ value,
88
+ text,
89
+ });
90
+ }
91
+
92
+ export function circularRef(text?: string): CircularRefAnnotation {
93
+ return brand({
94
+ type: 'circular-ref',
95
+ text,
96
+ });
97
+ }
98
+
99
+ /**
100
+ * Given an existing Annotation, set the annotation's text to a new value.
101
+ */
102
+ export function updateText<A: Annotation>(annotation: A, text?: string): A {
103
+ if (text !== undefined) {
104
+ return brand({ ...annotation, text });
105
+ } else {
106
+ return annotation;
107
+ }
108
+ }
109
+
110
+ /**
111
+ * Given an existing ObjectAnnotation, merges new Annotations in there.
112
+ */
113
+ export function merge(
114
+ objAnnotation: ObjectAnnotation,
115
+ fields: { +[key: string]: Annotation },
116
+ ): ObjectAnnotation {
117
+ const newFields = { ...objAnnotation.fields, ...fields };
118
+ return object(newFields, objAnnotation.text);
119
+ }
120
+
121
+ export function asAnnotation(thing: mixed): Annotation | void {
122
+ return typeof thing === 'object' && thing !== null && _register.has(thing)
123
+ ? ((thing: cast): Annotation)
124
+ : undefined;
125
+ }
126
+
127
+ type RefSet = WeakSet<{ ... } | $ReadOnlyArray<mixed>>;
128
+
129
+ function annotateArray(
130
+ value: $ReadOnlyArray<mixed>,
131
+ text?: string,
132
+ seen: RefSet,
133
+ ): ArrayAnnotation | CircularRefAnnotation {
134
+ seen.add(value);
135
+
136
+ const items = value.map((v) => annotate(v, undefined, seen));
137
+ return array(items, text);
138
+ }
139
+
140
+ function annotateObject(
141
+ obj: { +[string]: mixed },
142
+ text?: string,
143
+ seen: RefSet,
144
+ ): ObjectAnnotation {
145
+ seen.add(obj);
146
+
147
+ const fields = {};
148
+ Object.keys(obj).forEach((key) => {
149
+ const value = obj[key];
150
+ fields[key] = annotate(value, undefined, seen);
151
+ });
152
+ return object(fields, text);
153
+ }
154
+
155
+ function annotate(value: mixed, text?: string, seen: RefSet): Annotation {
156
+ if (
157
+ value === null ||
158
+ value === undefined ||
159
+ typeof value === 'string' ||
160
+ typeof value === 'number' ||
161
+ typeof value === 'boolean' ||
162
+ typeof value === 'symbol' ||
163
+ typeof value.getMonth === 'function'
164
+ ) {
165
+ return scalar(value, text);
166
+ }
167
+
168
+ const ann = asAnnotation(value);
169
+ if (ann) {
170
+ return updateText(ann, text);
171
+ }
172
+
173
+ if (Array.isArray(value)) {
174
+ // "Circular references" can only exist in objects or arrays
175
+ if (seen.has(value)) {
176
+ return circularRef(text);
177
+ } else {
178
+ return annotateArray(value, text, seen);
179
+ }
180
+ }
181
+
182
+ if (typeof value === 'object') {
183
+ // "Circular references" can only exist in objects or arrays
184
+ if (seen.has(value)) {
185
+ return circularRef(text);
186
+ } else {
187
+ return annotateObject(value, text, seen);
188
+ }
189
+ }
190
+
191
+ if (typeof value === 'function') {
192
+ return func(text);
193
+ }
194
+
195
+ return unknown(value, text);
196
+ }
197
+
198
+ function public_annotate(value: mixed, text?: string): Annotation {
199
+ return annotate(value, text, new WeakSet());
200
+ }
201
+
202
+ function public_annotateObject(
203
+ obj: { +[string]: mixed },
204
+ text?: string,
205
+ ): ObjectAnnotation {
206
+ return annotateObject(obj, text, new WeakSet());
207
+ }
208
+
209
+ export {
210
+ // This construct just ensures the "seen" weakmap (used for circular
211
+ // reference detection) isn't made part of the public API.
212
+ public_annotate as annotate,
213
+ public_annotateObject as annotateObject,
214
+ //
215
+ // NOTE: Don't acces theses private APIs directly. They are only exported here
216
+ // to better enable unit testing.
217
+ annotate as __private_annotate,
218
+ };
package/core/array.js ADDED
@@ -0,0 +1,108 @@
1
+ "use strict";
2
+
3
+ exports.__esModule = true;
4
+ exports.array = array;
5
+ exports.nonEmptyArray = nonEmptyArray;
6
+ exports.poja = void 0;
7
+
8
+ var Result = _interopRequireWildcard(require("../result"));
9
+
10
+ var _annotate = require("../annotate");
11
+
12
+ var _composition = require("./composition");
13
+
14
+ function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function _getRequireWildcardCache(nodeInterop) { return nodeInterop ? cacheNodeInterop : cacheBabelInterop; })(nodeInterop); }
15
+
16
+ function _interopRequireWildcard(obj, nodeInterop) { if (!nodeInterop && obj && obj.__esModule) { return obj; } if (obj === null || typeof obj !== "object" && typeof obj !== "function") { return { "default": obj }; } var cache = _getRequireWildcardCache(nodeInterop); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj["default"] = obj; if (cache) { cache.set(obj, newObj); } return newObj; }
17
+
18
+ /**
19
+ * Like a "Plain Old JavaScript Object", but for arrays: "Plain Old JavaScript
20
+ * Array" ^_^
21
+ */
22
+ var poja = function poja(blob) {
23
+ if (!Array.isArray(blob)) {
24
+ return Result.err((0, _annotate.annotate)(blob, 'Must be an array'));
25
+ }
26
+
27
+ return Result.ok( // NOTE: Since Flow 0.98, Array.isArray() returns $ReadOnlyArray<mixed>
28
+ // instead of Array<mixed>. For rationale, see
29
+ // https://github.com/facebook/flow/issues/7684. In this case, we
30
+ // don't want to output read-only types because it's up to the user of
31
+ // decoders to determine what they want to do with the decoded output.
32
+ // If they want to write items into the array, that's fine!
33
+ // The fastest way to turn a read-only array into a normal array in
34
+ // Javascript is to use .slice() on it, see this benchmark:
35
+ // http://jsben.ch/lO6C5
36
+ blob.slice());
37
+ };
38
+ /**
39
+ * Given an array of Result instances, loop over them all and return:
40
+ * - An [index, err] tuple, indicating the (index of the) first Err instance
41
+ * encountered; or
42
+ * - a new Ok with an array of all unwrapped Ok'ed values
43
+ */
44
+
45
+
46
+ exports.poja = poja;
47
+
48
+ function all(items, blobs) {
49
+ var results = [];
50
+
51
+ for (var index = 0; index < items.length; ++index) {
52
+ var result = items[index];
53
+
54
+ if (result.type === 'ok') {
55
+ results.push(result.value);
56
+ } else {
57
+ var ann = result.error; // Rewrite the annotation to include the index information, and inject it into the original blob
58
+
59
+ var clone = [].concat(blobs);
60
+ clone.splice(index, 1, (0, _annotate.annotate)(ann, ann.text ? ann.text + " (at index " + index + ")" : "index " + index)); // const errValue = [];
61
+ // if (index > 0) {
62
+ // errValue.push('...'); // TODO: make special mark, not string!
63
+ // }
64
+ // errValue.push(
65
+ // );
66
+ // if (index < iterable.length - 1) {
67
+ // errValue.push('...'); // TODO: make special mark, not string!
68
+ // }
69
+
70
+ return Result.err((0, _annotate.annotate)(clone));
71
+ }
72
+ }
73
+
74
+ return Result.ok(results);
75
+ }
76
+ /**
77
+ * Given a T, builds a decoder that assumes an array input and returns an
78
+ * Array<T>.
79
+ */
80
+
81
+
82
+ function members(decoder) {
83
+ return function (blobs) {
84
+ var results = blobs.map(decoder);
85
+ var result = all(results, blobs);
86
+ return result;
87
+ };
88
+ }
89
+ /**
90
+ * Builds a Decoder that returns Ok for values of `Array<T>`, given a Decoder
91
+ * for `T`. Err otherwise.
92
+ */
93
+
94
+
95
+ function array(decoder) {
96
+ return (0, _composition.compose)(poja, members(decoder));
97
+ }
98
+ /**
99
+ * Builds a Decoder that returns Ok for values of `Array<T>`, but will reject
100
+ * empty arrays.
101
+ */
102
+
103
+
104
+ function nonEmptyArray(decoder) {
105
+ return (0, _composition.compose)(array(decoder), (0, _composition.predicate)(function (arr) {
106
+ return arr.length > 0;
107
+ }, 'Must be non-empty array'));
108
+ }
@@ -0,0 +1,103 @@
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, DecodeResult } from '../_types';
7
+
8
+ /**
9
+ * Like a "Plain Old JavaScript Object", but for arrays: "Plain Old JavaScript
10
+ * Array" ^_^
11
+ */
12
+ export const poja: Decoder<Array<mixed>> = (blob: mixed) => {
13
+ if (!Array.isArray(blob)) {
14
+ return Result.err(annotate(blob, 'Must be an array'));
15
+ }
16
+ return Result.ok(
17
+ // NOTE: Since Flow 0.98, Array.isArray() returns $ReadOnlyArray<mixed>
18
+ // instead of Array<mixed>. For rationale, see
19
+ // https://github.com/facebook/flow/issues/7684. In this case, we
20
+ // don't want to output read-only types because it's up to the user of
21
+ // decoders to determine what they want to do with the decoded output.
22
+ // If they want to write items into the array, that's fine!
23
+ // The fastest way to turn a read-only array into a normal array in
24
+ // Javascript is to use .slice() on it, see this benchmark:
25
+ // http://jsben.ch/lO6C5
26
+ blob.slice(),
27
+ );
28
+ };
29
+
30
+ /**
31
+ * Given an array of Result instances, loop over them all and return:
32
+ * - An [index, err] tuple, indicating the (index of the) first Err instance
33
+ * encountered; or
34
+ * - a new Ok with an array of all unwrapped Ok'ed values
35
+ */
36
+ function all<T>(
37
+ items: $ReadOnlyArray<DecodeResult<T>>,
38
+ blobs: $ReadOnlyArray<mixed>,
39
+ ): DecodeResult<Array<T>> {
40
+ const results: Array<T> = [];
41
+ for (let index = 0; index < items.length; ++index) {
42
+ const result = items[index];
43
+ if (result.type === 'ok') {
44
+ results.push(result.value);
45
+ } else {
46
+ const ann = result.error;
47
+
48
+ // Rewrite the annotation to include the index information, and inject it into the original blob
49
+ const clone = [...blobs];
50
+ clone.splice(
51
+ index,
52
+ 1,
53
+ annotate(
54
+ ann,
55
+ ann.text ? `${ann.text} (at index ${index})` : `index ${index}`,
56
+ ),
57
+ );
58
+
59
+ // const errValue = [];
60
+ // if (index > 0) {
61
+ // errValue.push('...'); // TODO: make special mark, not string!
62
+ // }
63
+ // errValue.push(
64
+ // );
65
+ // if (index < iterable.length - 1) {
66
+ // errValue.push('...'); // TODO: make special mark, not string!
67
+ // }
68
+ return Result.err(annotate(clone));
69
+ }
70
+ }
71
+ return Result.ok(results);
72
+ }
73
+
74
+ /**
75
+ * Given a T, builds a decoder that assumes an array input and returns an
76
+ * Array<T>.
77
+ */
78
+ function members<T>(decoder: Decoder<T>): Decoder<Array<T>, Array<mixed>> {
79
+ return (blobs: $ReadOnlyArray<mixed>) => {
80
+ const results = blobs.map(decoder);
81
+ const result = all(results, blobs);
82
+ return result;
83
+ };
84
+ }
85
+
86
+ /**
87
+ * Builds a Decoder that returns Ok for values of `Array<T>`, given a Decoder
88
+ * for `T`. Err otherwise.
89
+ */
90
+ export function array<T>(decoder: Decoder<T>): Decoder<Array<T>> {
91
+ return compose(poja, members(decoder));
92
+ }
93
+
94
+ /**
95
+ * Builds a Decoder that returns Ok for values of `Array<T>`, but will reject
96
+ * empty arrays.
97
+ */
98
+ export function nonEmptyArray<T>(decoder: Decoder<T>): Decoder<Array<T>> {
99
+ return compose(
100
+ array(decoder),
101
+ predicate((arr) => arr.length > 0, 'Must be non-empty array'),
102
+ );
103
+ }
@@ -0,0 +1,44 @@
1
+ "use strict";
2
+
3
+ exports.__esModule = true;
4
+ exports.truthy = exports.numericBoolean = exports["boolean"] = void 0;
5
+
6
+ var Result = _interopRequireWildcard(require("../result"));
7
+
8
+ var _annotate = require("../annotate");
9
+
10
+ var _composition = require("./composition");
11
+
12
+ var _number = require("./number");
13
+
14
+ function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function _getRequireWildcardCache(nodeInterop) { return nodeInterop ? cacheNodeInterop : cacheBabelInterop; })(nodeInterop); }
15
+
16
+ function _interopRequireWildcard(obj, nodeInterop) { if (!nodeInterop && obj && obj.__esModule) { return obj; } if (obj === null || typeof obj !== "object" && typeof obj !== "function") { return { "default": obj }; } var cache = _getRequireWildcardCache(nodeInterop); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj["default"] = obj; if (cache) { cache.set(obj, newObj); } return newObj; }
17
+
18
+ /**
19
+ * Decoder that only returns Ok for boolean inputs. Err otherwise.
20
+ */
21
+ var _boolean = function _boolean(blob) {
22
+ return typeof blob === 'boolean' ? Result.ok(blob) : Result.err((0, _annotate.annotate)(blob, 'Must be boolean'));
23
+ };
24
+ /**
25
+ * Decoder that returns true for all truthy values, and false otherwise. Never fails.
26
+ */
27
+
28
+
29
+ exports["boolean"] = _boolean;
30
+
31
+ var truthy = function truthy(blob) {
32
+ return Result.ok(!!blob);
33
+ };
34
+ /**
35
+ * Decoder that only returns Ok for numeric input values representing booleans.
36
+ * Returns their boolean representation. Err otherwise.
37
+ */
38
+
39
+
40
+ exports.truthy = truthy;
41
+ var numericBoolean = (0, _composition.map)(_number.number, function (n) {
42
+ return !!n;
43
+ });
44
+ exports.numericBoolean = numericBoolean;
@@ -0,0 +1,29 @@
1
+ // @flow strict
2
+
3
+ import * as Result from '../result';
4
+ import { annotate } from '../annotate';
5
+ import { map } from './composition';
6
+ import { number } from './number';
7
+ import type { Decoder } from '../_types';
8
+
9
+ /**
10
+ * Decoder that only returns Ok for boolean inputs. Err otherwise.
11
+ */
12
+ export const boolean: Decoder<boolean> = (blob: mixed) => {
13
+ return typeof blob === 'boolean'
14
+ ? Result.ok(blob)
15
+ : Result.err(annotate(blob, 'Must be boolean'));
16
+ };
17
+
18
+ /**
19
+ * Decoder that returns true for all truthy values, and false otherwise. Never fails.
20
+ */
21
+ export const truthy: Decoder<boolean> = (blob: mixed) => {
22
+ return Result.ok(!!blob);
23
+ };
24
+
25
+ /**
26
+ * Decoder that only returns Ok for numeric input values representing booleans.
27
+ * Returns their boolean representation. Err otherwise.
28
+ */
29
+ export const numericBoolean: Decoder<boolean> = map(number, (n) => !!n);