decoders 1.25.2 → 1.26.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 (69) hide show
  1. package/CHANGELOG.md +20 -0
  2. package/{array.js → cjs/array.js} +5 -5
  3. package/{array.js.flow → cjs/array.js.flow} +1 -1
  4. package/{boolean.js → cjs/boolean.js} +4 -4
  5. package/{boolean.js.flow → cjs/boolean.js.flow} +1 -1
  6. package/{constants.js → cjs/constants.js} +7 -7
  7. package/{constants.js.flow → cjs/constants.js.flow} +3 -3
  8. package/{date.js → cjs/date.js} +2 -2
  9. package/{date.js.flow → cjs/date.js.flow} +1 -1
  10. package/{describe.js → cjs/describe.js} +0 -0
  11. package/{describe.js.flow → cjs/describe.js.flow} +0 -0
  12. package/{dispatch.js → cjs/dispatch.js} +0 -0
  13. package/{dispatch.js.flow → cjs/dispatch.js.flow} +0 -0
  14. package/{either.js → cjs/either.js} +6 -6
  15. package/{either.js.flow → cjs/either.js.flow} +2 -4
  16. package/{fail.js → cjs/fail.js} +2 -2
  17. package/{fail.js.flow → cjs/fail.js.flow} +1 -1
  18. package/{guard.js → cjs/guard.js} +0 -0
  19. package/{guard.js.flow → cjs/guard.js.flow} +0 -0
  20. package/{index.js → cjs/index.js} +110 -110
  21. package/{index.js.flow → cjs/index.js.flow} +0 -0
  22. package/{instanceOf.js → cjs/instanceOf.js} +2 -2
  23. package/{instanceOf.js.flow → cjs/instanceOf.js.flow} +1 -1
  24. package/{json.js → cjs/json.js} +1 -1
  25. package/{json.js.flow → cjs/json.js.flow} +0 -0
  26. package/{lazy.js → cjs/lazy.js} +0 -0
  27. package/{lazy.js.flow → cjs/lazy.js.flow} +0 -0
  28. package/{mapping.js → cjs/mapping.js} +4 -4
  29. package/{mapping.js.flow → cjs/mapping.js.flow} +1 -1
  30. package/{number.js → cjs/number.js} +3 -3
  31. package/{number.js.flow → cjs/number.js.flow} +1 -1
  32. package/{object.js → cjs/object.js} +8 -8
  33. package/{object.js.flow → cjs/object.js.flow} +1 -1
  34. package/{optional.js → cjs/optional.js} +5 -5
  35. package/{optional.js.flow → cjs/optional.js.flow} +1 -1
  36. package/{string.js → cjs/string.js} +7 -6
  37. package/{string.js.flow → cjs/string.js.flow} +1 -1
  38. package/{tuple.js → cjs/tuple.js} +13 -13
  39. package/{tuple.js.flow → cjs/tuple.js.flow} +1 -1
  40. package/cjs/types.js +3 -0
  41. package/{types.js.flow → cjs/types.js.flow} +3 -1
  42. package/{utils.js → cjs/utils.js} +6 -6
  43. package/{utils.js.flow → cjs/utils.js.flow} +1 -1
  44. package/es/index.js +1039 -0
  45. package/package.json +9 -5
  46. package/{array.d.ts → ts/array.d.ts} +1 -1
  47. package/{boolean.d.ts → ts/boolean.d.ts} +0 -0
  48. package/{constants.d.ts → ts/constants.d.ts} +1 -2
  49. package/{date.d.ts → ts/date.d.ts} +0 -0
  50. package/{describe.d.ts → ts/describe.d.ts} +0 -0
  51. package/{dispatch.d.ts → ts/dispatch.d.ts} +0 -0
  52. package/{either.d.ts → ts/either.d.ts} +1 -3
  53. package/{fail.d.ts → ts/fail.d.ts} +0 -0
  54. package/{guard.d.ts → ts/guard.d.ts} +0 -0
  55. package/ts/helpers.d.ts +79 -0
  56. package/{index.d.ts → ts/index.d.ts} +1 -1
  57. package/{instanceOf.d.ts → ts/instanceOf.d.ts} +0 -0
  58. package/{json.d.ts → ts/json.d.ts} +0 -0
  59. package/{lazy.d.ts → ts/lazy.d.ts} +0 -0
  60. package/{mapping.d.ts → ts/mapping.d.ts} +0 -0
  61. package/{number.d.ts → ts/number.d.ts} +0 -0
  62. package/{object.d.ts → ts/object.d.ts} +3 -3
  63. package/{optional.d.ts → ts/optional.d.ts} +0 -0
  64. package/{string.d.ts → ts/string.d.ts} +0 -0
  65. package/{tuple.d.ts → ts/tuple.d.ts} +0 -0
  66. package/{types.d.ts → ts/types.d.ts} +3 -1
  67. package/{utils.d.ts → ts/utils.d.ts} +0 -0
  68. package/helpers.d.ts +0 -62
  69. package/types.js +0 -5
package/es/index.js ADDED
@@ -0,0 +1,1039 @@
1
+ import { summarize, serialize, annotate, indent, isAnnotation, annotateFields } from 'debrief';
2
+ import { Ok, Err } from 'lemons';
3
+ import _toConsumableArray from '@babel/runtime/helpers/toConsumableArray';
4
+ import _defineProperty from '@babel/runtime/helpers/defineProperty';
5
+ import _typeof from '@babel/runtime/helpers/typeof';
6
+ import _slicedToArray from '@babel/runtime/helpers/slicedToArray';
7
+
8
+ /**
9
+ * Alternative for serialize() that does not echo back the input value.
10
+ */
11
+ function serializeSimple(annotation) {
12
+ return summarize(annotation).join('\n');
13
+ }
14
+
15
+ function guard(decoder, options) {
16
+ var o = options || {};
17
+ var style = o.style || 'inline';
18
+ var serializer = style === 'inline' ? serialize // Normal serializer, which echoes back inputted value and inlines errors
19
+ : serializeSimple; // Only returns error messages, without echoing back input
20
+
21
+ return function (blob) {
22
+ return decoder(blob).mapError(function (annotation) {
23
+ var err = new Error('\n' + serializer(annotation));
24
+ err.name = 'Decoding error';
25
+ return err;
26
+ }).unwrap();
27
+ };
28
+ }
29
+
30
+ /**
31
+ * `x instanceof Date` checks are unreliable across stack frames (that information
32
+ * might get lost by the JS runtime), so we'll have to reside to more runtime
33
+ * inspection checks.
34
+ *
35
+ * Taken from https://stackoverflow.com/a/44198641
36
+ */
37
+ var isDate = function isDate(value) {
38
+ return value !== undefined && value !== null && // $FlowFixMe[method-unbinding]
39
+ Object.prototype.toString.call(value) === '[object Date]' && !isNaN(value);
40
+ };
41
+ /**
42
+ * Given a decoder T and a mapping function from T's to V's, returns a decoder
43
+ * for V's. This is useful to change the original input data.
44
+ */
45
+
46
+ function map(decoder, mapper) {
47
+ return compose(decoder, function (x) {
48
+ try {
49
+ return Ok(mapper(x));
50
+ } catch (e) {
51
+ return Err(annotate(x, e instanceof Error ? e.message : String(e)));
52
+ }
53
+ });
54
+ }
55
+ /**
56
+ * Compose two decoders by passing the result of the first into the second.
57
+ * The second decoder may assume as its input type the output type of the first
58
+ * decoder (so it's not necessary to accept the typical "mixed"). This is
59
+ * useful for "narrowing down" the checks. For example, if you want to write
60
+ * a decoder for positive numbers, you can compose it from an existing decoder
61
+ * for any number, and a decoder that, assuming a number, checks if it's
62
+ * positive. Very often combined with the predicate() helper as the second
63
+ * argument.
64
+ */
65
+
66
+ function compose(decoder, next) {
67
+ return function (blob) {
68
+ return decoder(blob).andThen(next);
69
+ };
70
+ }
71
+ /**
72
+ * Factory function returning a Decoder<T>, given a predicate function that
73
+ * accepts/rejects the input of type T.
74
+ */
75
+
76
+ function predicate(predicate, msg) {
77
+ return function (value) {
78
+ return predicate(value) ? Ok(value) : Err(annotate(value, msg));
79
+ };
80
+ }
81
+
82
+ function _createForOfIteratorHelper$2(o, allowArrayLike) { var it = typeof Symbol !== "undefined" && o[Symbol.iterator] || o["@@iterator"]; if (!it) { if (Array.isArray(o) || (it = _unsupportedIterableToArray$2(o)) || allowArrayLike && o && typeof o.length === "number") { if (it) o = it; var i = 0; var F = function F() {}; return { s: F, n: function n() { if (i >= o.length) return { done: true }; return { done: false, value: o[i++] }; }, e: function e(_e) { throw _e; }, f: F }; } throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); } var normalCompletion = true, didErr = false, err; return { s: function s() { it = it.call(o); }, n: function n() { var step = it.next(); normalCompletion = step.done; return step; }, e: function e(_e2) { didErr = true; err = _e2; }, f: function f() { try { if (!normalCompletion && it["return"] != null) it["return"](); } finally { if (didErr) throw err; } } }; }
83
+
84
+ function _unsupportedIterableToArray$2(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray$2(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray$2(o, minLen); }
85
+
86
+ function _arrayLikeToArray$2(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) { arr2[i] = arr[i]; } return arr2; }
87
+ /**
88
+ * Like a "Plain Old JavaScript Object", but for arrays: "Plain Old JavaScript
89
+ * Array" ^_^
90
+ */
91
+
92
+ var poja = function poja(blob) {
93
+ if (!Array.isArray(blob)) {
94
+ return Err(annotate(blob, 'Must be an array'));
95
+ }
96
+
97
+ return Ok( // NOTE: Since Flow 0.98, Array.isArray() returns $ReadOnlyArray<mixed>
98
+ // instead of Array<mixed>. For rationale, see
99
+ // https://github.com/facebook/flow/issues/7684. In this case, we
100
+ // don't want to output read-only types because it's up to the user of
101
+ // decoders to determine what they want to do with the decoded output.
102
+ // If they want to write items into the array, that's fine!
103
+ // The fastest way to turn a read-only array into a normal array in
104
+ // Javascript is to use .slice() on it, see this benchmark:
105
+ // http://jsben.ch/lO6C5
106
+ blob.slice());
107
+ };
108
+ /**
109
+ * Given an iterable of Result instances, exhaust them all and return:
110
+ * - An [index, err] tuple, indicating the (index of the) first Err instance
111
+ * encountered; or
112
+ * - a new Ok with an array of all unwrapped Ok'ed values
113
+ */
114
+
115
+ function all(iterable, blobs) {
116
+ var results = [];
117
+ var index = 0;
118
+
119
+ var _iterator = _createForOfIteratorHelper$2(iterable),
120
+ _step;
121
+
122
+ try {
123
+ for (_iterator.s(); !(_step = _iterator.n()).done;) {
124
+ var result = _step.value;
125
+
126
+ try {
127
+ var value = result.unwrap();
128
+ results.push(value);
129
+ } catch (ann) {
130
+ // Rewrite the annotation to include the index information, and inject it into the original blob
131
+ var clone = _toConsumableArray(blobs);
132
+
133
+ clone.splice(index, 1, annotate(ann, ann.annotation !== undefined ? "".concat(ann.annotation, " (at index ").concat(index, ")") : "index ".concat(index))); // const errValue = [];
134
+ // if (index > 0) {
135
+ // errValue.push('...'); // TODO: make special mark, not string!
136
+ // }
137
+ // errValue.push(
138
+ // );
139
+ // if (index < iterable.length - 1) {
140
+ // errValue.push('...'); // TODO: make special mark, not string!
141
+ // }
142
+
143
+ return Err(annotate(clone));
144
+ }
145
+
146
+ index++;
147
+ }
148
+ } catch (err) {
149
+ _iterator.e(err);
150
+ } finally {
151
+ _iterator.f();
152
+ }
153
+
154
+ return Ok(results);
155
+ }
156
+ /**
157
+ * Given a T, builds a decoder that assumes an array input and returns an
158
+ * Array<T>.
159
+ */
160
+
161
+
162
+ function members(decoder) {
163
+ return function (blobs) {
164
+ var results = blobs.map(decoder);
165
+ var result = all(results, blobs);
166
+ return result;
167
+ };
168
+ }
169
+ /**
170
+ * Builds a Decoder that returns Ok for values of `Array<T>`, given a Decoder
171
+ * for `T`. Err otherwise.
172
+ */
173
+
174
+
175
+ function array(decoder) {
176
+ return compose(poja, members(decoder));
177
+ }
178
+ /**
179
+ * Builds a Decoder that returns Ok for values of `Array<T>`, but will reject
180
+ * empty arrays.
181
+ */
182
+
183
+ function nonEmptyArray(decoder) {
184
+ return compose(array(decoder), predicate(function (arr) {
185
+ return arr.length > 0;
186
+ }, 'Must be non-empty array'));
187
+ }
188
+
189
+ var anyNumber = function anyNumber(blob) {
190
+ return typeof blob === 'number' && !Number.isNaN(blob) ? Ok(blob) : Err(annotate(blob, 'Must be number'));
191
+ };
192
+
193
+ var isInteger = function isInteger(n) {
194
+ return Number.isInteger(n);
195
+ };
196
+
197
+ var isFinite = function isFinite(n) {
198
+ return Number.isFinite(n);
199
+ };
200
+
201
+ var number = compose(anyNumber, predicate(isFinite, 'Number must be finite'));
202
+ var positiveNumber = compose(number, predicate(function (n) {
203
+ return n >= 0;
204
+ }, 'Number must be positive')); // Integers
205
+
206
+ var integer = compose(number, predicate(isInteger, 'Number must be an integer'));
207
+ var positiveInteger = compose(integer, predicate(function (n) {
208
+ return n >= 0;
209
+ }, 'Number must be positive'));
210
+
211
+ /**
212
+ * Decoder that only returns Ok for boolean inputs. Err otherwise.
213
+ */
214
+
215
+ var _boolean = function _boolean(blob) {
216
+ return typeof blob === 'boolean' ? Ok(blob) : Err(annotate(blob, 'Must be boolean'));
217
+ };
218
+ var truthy = function truthy(blob) {
219
+ return Ok(!!blob);
220
+ };
221
+ /**
222
+ * Decoder that only returns Ok for numeric input values representing booleans.
223
+ * Returns their boolean representation. Err otherwise.
224
+ */
225
+
226
+ var numericBoolean = map(number, function (n) {
227
+ return !!n;
228
+ });
229
+
230
+ /**
231
+ * Decoder that only returns Ok for `null` inputs. Err otherwise.
232
+ */
233
+ var null_ = function null_(blob) {
234
+ return blob === null ? Ok(blob) : Err(annotate(blob, 'Must be null'));
235
+ };
236
+ /**
237
+ * Decoder that only returns Ok for `undefined` inputs. Err otherwise.
238
+ */
239
+
240
+ var undefined_ = function undefined_(blob) {
241
+ return blob === undefined ? Ok(blob) : Err(annotate(blob, 'Must be undefined'));
242
+ };
243
+ /**
244
+ * Decoder that only returns Ok for the given value constant. Err otherwise.
245
+ */
246
+
247
+ function constant(value) {
248
+ return function (blob) {
249
+ return blob === value ? Ok(value) : Err(annotate(blob, "Must be constant ".concat(String(value))));
250
+ };
251
+ }
252
+ /**
253
+ * Decoder that always returns Ok for the given hardcoded value, no matter what the input.
254
+ */
255
+
256
+ function hardcoded(value) {
257
+ return function (_) {
258
+ return Ok(value);
259
+ };
260
+ }
261
+ /**
262
+ * Decoder that always returns Ok for the given hardcoded value, no matter what the input.
263
+ */
264
+
265
+ var mixed = function mixed(blob) {
266
+ return Ok(blob);
267
+ };
268
+ /**
269
+ * Alias of mixed.
270
+ */
271
+
272
+ var unknown = mixed;
273
+
274
+ /** Match groups in this regex:
275
+ * \1 - the scheme
276
+ * \2 - the username/password (optional)
277
+ * \3 - the host
278
+ * \4 - the port (optional)
279
+ * \5 - the path (optional)
280
+ */
281
+
282
+ 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
283
+
284
+ var DEFAULT_SCHEMES = ['https'];
285
+ /**
286
+ * Decoder that only returns Ok for string inputs. Err otherwise.
287
+ */
288
+
289
+ var string = function string(blob) {
290
+ return typeof blob === 'string' ? Ok(blob) : Err(annotate(blob, 'Must be string'));
291
+ };
292
+ /**
293
+ * Decoder that only returns Ok for non-empty string inputs. Err otherwise.
294
+ */
295
+
296
+ var nonEmptyString = regex(/\S/, 'Must be non-empty string');
297
+ /**
298
+ * Decoder that only returns Ok for string inputs that match the regular
299
+ * expression. Err otherwise. Will always validate that the input is a string
300
+ * before testing the regex.
301
+ */
302
+
303
+ function regex(regex, msg) {
304
+ return compose(string, predicate(function (s) {
305
+ return regex.test(s);
306
+ }, msg));
307
+ }
308
+ /**
309
+ * Decoder that only returns Ok for string inputs that match the almost perfect
310
+ * email regex, taken from http://emailregex.com. Err otherwise.
311
+ */
312
+
313
+ 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');
314
+ /**
315
+ * Decoder that only returns Ok for string inputs that match URLs of the
316
+ * expected scheme. Defaults to only accept HTTPS URLs. Err otherwise.
317
+ *
318
+ * Variants that can be used:
319
+ *
320
+ * - url() accepts only https:// URLs
321
+ * - url([]) accepts any URL scheme
322
+ * - url(['http']) accepts only HTTP
323
+ * - url(['https', 'git+ssh']) accepts both https:// and git+ssh:// URLs
324
+ */
325
+
326
+ var url = function url() {
327
+ var schemes = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : DEFAULT_SCHEMES;
328
+ return compose(string, function (value) {
329
+ var matches = value.match(url_re);
330
+
331
+ if (!matches) {
332
+ return Err(annotate(value, 'Must be URL'));
333
+ } else {
334
+ var scheme = matches[1];
335
+
336
+ if (schemes.length === 0 || schemes.includes(scheme.toLowerCase())) {
337
+ return Ok(value);
338
+ } else {
339
+ return Err(annotate(value, "URL scheme must be any of: ".concat(schemes.join(', '))));
340
+ }
341
+ }
342
+ });
343
+ };
344
+
345
+ // Only matches the shape. This "over-matches" some values that still aren't
346
+ // valid dates (like 9999-99-99), but those will be caught by JS Date's
347
+ // internal validations
348
+ var iso8601_re = /^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}(?:[.]\d+)?(?:Z|[+-]\d{2}:?\d{2})$/;
349
+ var date = function date(value) {
350
+ return isDate(value) ? Ok(value) : Err(annotate(value, 'Must be a Date'));
351
+ };
352
+ /**
353
+ * Decoder that only returns Ok for strings that are valid ISO8601 date
354
+ * strings. Err otherwise.
355
+ */
356
+
357
+ var iso8601 = map( // Input itself needs to match the ISO8601 regex...
358
+ regex(iso8601_re, 'Must be ISO8601 format'), // Make sure it is a _valid_ date
359
+ function (value) {
360
+ var date = new Date(value);
361
+
362
+ if (isNaN(date.getTime())) {
363
+ throw new Error('Must be valid date/time value');
364
+ }
365
+
366
+ return date;
367
+ });
368
+
369
+ /**
370
+ * Wrap another decoder, and override the error message in case it fails. This
371
+ * is useful to "simplify" otherwise potentially complex error messages, or to
372
+ * use language in those error messages that can be relayed to end users (for
373
+ * example to show in form errors).
374
+ */
375
+ function describe(decoder, message) {
376
+ return function (blob) {
377
+ return decoder(blob).mapError(function (err) {
378
+ return annotate(err, message);
379
+ });
380
+ };
381
+ }
382
+
383
+ /**
384
+ * Indents and adds a dash in front of this (potentially multiline) string.
385
+ */
386
+ // istanbul ignore next
387
+ function itemize() {
388
+ var s = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : '';
389
+ return '-' + indent(s).substring(1);
390
+ }
391
+
392
+ function either(d1, d2) {
393
+ return function (blob) {
394
+ return d1(blob).dispatch(function (value1) {
395
+ return Ok(value1);
396
+ }, function (err1) {
397
+ return d2(blob).dispatch(function (value2) {
398
+ return Ok(value2);
399
+ }, function (err2) {
400
+ return Err(annotate(blob, ['Either:', itemize(summarize(err1).join('\n')), itemize(summarize(err2).join('\n'))].join('\n')));
401
+ });
402
+ });
403
+ };
404
+ }
405
+ function either3(d1, d2, d3) {
406
+ return either(d1, either(d2, d3));
407
+ }
408
+ function either4(d1, d2, d3, d4) {
409
+ return either(d1, either3(d2, d3, d4));
410
+ }
411
+ function either5(d1, d2, d3, d4, d5) {
412
+ return either(d1, either4(d2, d3, d4, d5));
413
+ }
414
+ function either6(d1, d2, d3, d4, d5, d6) {
415
+ return either(d1, either5(d2, d3, d4, d5, d6));
416
+ }
417
+ function either7(d1, d2, d3, d4, d5, d6, d7) {
418
+ return either(d1, either6(d2, d3, d4, d5, d6, d7));
419
+ }
420
+ function either8(d1, d2, d3, d4, d5, d6, d7, d8) {
421
+ return either(d1, either7(d2, d3, d4, d5, d6, d7, d8));
422
+ }
423
+ function either9(d1, d2, d3, d4, d5, d6, d7, d8, d9) {
424
+ return either(d1, either8(d2, d3, d4, d5, d6, d7, d8, d9));
425
+ }
426
+ function oneOf(constants) {
427
+ return function (blob) {
428
+ var winner = constants.find(function (c) {
429
+ return c === blob;
430
+ });
431
+
432
+ if (winner !== undefined) {
433
+ return Ok(winner);
434
+ }
435
+
436
+ return Err(annotate(blob, "Must be one of ".concat(constants.map(function (value) {
437
+ return JSON.stringify(value);
438
+ }).join(', '))));
439
+ };
440
+ }
441
+
442
+ function ownKeys$1(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); if (enumerableOnly) { symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; }); } keys.push.apply(keys, symbols); } return keys; }
443
+
444
+ function _objectSpread$1(target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i] != null ? arguments[i] : {}; if (i % 2) { ownKeys$1(Object(source), true).forEach(function (key) { _defineProperty(target, key, source[key]); }); } else if (Object.getOwnPropertyDescriptors) { Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)); } else { ownKeys$1(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } } return target; }
445
+
446
+ function _createForOfIteratorHelper$1(o, allowArrayLike) { var it = typeof Symbol !== "undefined" && o[Symbol.iterator] || o["@@iterator"]; if (!it) { if (Array.isArray(o) || (it = _unsupportedIterableToArray$1(o)) || allowArrayLike && o && typeof o.length === "number") { if (it) o = it; var i = 0; var F = function F() {}; return { s: F, n: function n() { if (i >= o.length) return { done: true }; return { done: false, value: o[i++] }; }, e: function e(_e) { throw _e; }, f: F }; } throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); } var normalCompletion = true, didErr = false, err; return { s: function s() { it = it.call(o); }, n: function n() { var step = it.next(); normalCompletion = step.done; return step; }, e: function e(_e2) { didErr = true; err = _e2; }, f: function f() { try { if (!normalCompletion && it["return"] != null) it["return"](); } finally { if (didErr) throw err; } } }; }
447
+
448
+ function _unsupportedIterableToArray$1(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray$1(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray$1(o, minLen); }
449
+
450
+ function _arrayLikeToArray$1(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) { arr2[i] = arr[i]; } return arr2; }
451
+
452
+ function isPojo(o) {
453
+ return o !== null && o !== undefined && _typeof(o) === 'object' && // This still seems to be the only reliable way to determine whether
454
+ // something is a pojo... ¯\_(ツ)_/¯
455
+ // $FlowFixMe[method-unbinding]
456
+ Object.prototype.toString.call(o) === '[object Object]';
457
+ }
458
+
459
+ function subtract(xs, ys) {
460
+ var result = new Set();
461
+
462
+ var _iterator = _createForOfIteratorHelper$1(xs),
463
+ _step;
464
+
465
+ try {
466
+ for (_iterator.s(); !(_step = _iterator.n()).done;) {
467
+ var x = _step.value;
468
+
469
+ if (!ys.has(x)) {
470
+ result.add(x);
471
+ }
472
+ }
473
+ } catch (err) {
474
+ _iterator.e(err);
475
+ } finally {
476
+ _iterator.f();
477
+ }
478
+
479
+ return result;
480
+ }
481
+
482
+ var pojo = function pojo(blob) {
483
+ return isPojo(blob) ? Ok( // NOTE:
484
+ // Since Flow 0.98, typeof o === 'object' refines to
485
+ // {| +[string]: mixed |}
486
+ // instead of
487
+ // {| [string]: mixed |}
488
+ //
489
+ // For rationale, see https://github.com/facebook/flow/issues/7685.
490
+ // In this case, we don't want to output a read-only version of
491
+ // the object because it's up to the user of decoders to
492
+ // determine what they want to do with the decoded output. If they
493
+ // want to write items into the array, that's fine! The fastest
494
+ // way to turn a read-only Object to a writeable one in ES6 seems
495
+ // to be to use object-spread. (Going off this benchmark:
496
+ // https://thecodebarbarian.com/object-assign-vs-object-spread.html)
497
+ _objectSpread$1({}, blob)) : Err(annotate(blob, 'Must be an object'));
498
+ };
499
+ /**
500
+ * Given a mapping of fields-to-decoders, builds a decoder for an object type.
501
+ *
502
+ * For example, given decoders for a number and a string, we can construct an
503
+ * "object description" like so:
504
+ *
505
+ * { id: number, name: string }
506
+ *
507
+ * Which is of type:
508
+ *
509
+ * { id: Decoder<number>, name: Decoder<string> }
510
+ *
511
+ * Passing this to object() will produce the following return type:
512
+ *
513
+ * Decoder<{ id: number, name: string }>
514
+ *
515
+ * Put simply: it'll "peel off" all of the nested Decoders, puts them together
516
+ * in an object, and wraps it in a Decoder<...>.
517
+ */
518
+
519
+ function object(mapping) {
520
+ var known = new Set(Object.keys(mapping));
521
+ return compose(pojo, function (blob) {
522
+ var actual = new Set(Object.keys(blob)); // At this point, "missing" will also include all fields that may
523
+ // validly be optional. We'll let the underlying decoder decide and
524
+ // remove the key from this missing set if the decoder accepts the
525
+ // value.
526
+
527
+ var missing = subtract(known, actual);
528
+ var record = {};
529
+
530
+ var fieldErrors = _objectSpread$1({}, null); // NOTE: We're using .keys() here over .entries(), since .entries()
531
+ // will type the value part as "mixed"
532
+
533
+
534
+ for (var _i = 0, _Object$keys = Object.keys(mapping); _i < _Object$keys.length; _i++) {
535
+ var _key = _Object$keys[_i];
536
+ var decoder = mapping[_key];
537
+ var rawValue = blob[_key];
538
+ var result = decoder(rawValue);
539
+
540
+ try {
541
+ var value = result.unwrap();
542
+
543
+ if (value !== undefined) {
544
+ record[_key] = value;
545
+ } // If this succeeded, remove the key from the missing keys
546
+ // tracker
547
+
548
+
549
+ missing["delete"](_key);
550
+ } catch (ann) {
551
+ /* istanbul ignore next */
552
+ if (!isAnnotation(ann)) {
553
+ throw ann;
554
+ } // Keep track of the annotation, but don't return just yet. We
555
+ // want to collect more error information.
556
+
557
+
558
+ if (rawValue === undefined) {
559
+ // Explicitly add it to the missing set if the value is
560
+ // undefined. This covers explicit undefineds to be
561
+ // treated the same as implicit undefineds (aka missing
562
+ // keys).
563
+ missing.add(_key);
564
+ } else {
565
+ fieldErrors[_key] = ann;
566
+ }
567
+ }
568
+ } // Deal with errors now. There are two classes of errors we want to
569
+ // report. First of all, we want to report any inline errors in this
570
+ // object. Lastly, any fields that are missing should be annotated on
571
+ // the outer object itself.
572
+
573
+
574
+ var fieldsWithErrors = Object.keys(fieldErrors);
575
+
576
+ if (fieldsWithErrors.length > 0 || missing.size > 0) {
577
+ var err;
578
+
579
+ if (fieldsWithErrors.length > 0) {
580
+ var errorlist = fieldsWithErrors.map(function (k) {
581
+ return [k, fieldErrors[k]];
582
+ });
583
+ err = annotateFields(blob, errorlist);
584
+ } else {
585
+ err = annotate(blob);
586
+ }
587
+
588
+ if (missing.size > 0) {
589
+ var errMsg = Array.from(missing).map(function (key) {
590
+ return "\"".concat(key, "\"");
591
+ }).join(', ');
592
+ var pluralized = missing.size > 1 ? 'keys' : 'key';
593
+ err = annotate(err, "Missing ".concat(pluralized, ": ").concat(errMsg));
594
+ }
595
+
596
+ return Err(err);
597
+ }
598
+
599
+ return Ok(record);
600
+ });
601
+ }
602
+ function exact(mapping) {
603
+ // Check the inputted object for any superfluous keys
604
+ var allowed = new Set(Object.keys(mapping));
605
+ var checked = compose(pojo, function (blob) {
606
+ var actual = new Set(Object.keys(blob));
607
+ var superfluous = subtract(actual, allowed);
608
+
609
+ if (superfluous.size > 0) {
610
+ return Err(annotate(blob, "Superfluous keys: ".concat(Array.from(superfluous).join(', '))));
611
+ }
612
+
613
+ return Ok(blob);
614
+ }); // Defer to the "object" decoder for doing the real decoding work. Since
615
+ // we made sure there are no superfluous keys in this structure, it's now
616
+ // safe to force-cast it to an $Exact<> type.
617
+
618
+ var decoder = object(mapping);
619
+ return compose(checked, decoder);
620
+ }
621
+ function inexact(mapping) {
622
+ return compose(pojo, function (blob) {
623
+ var allkeys = new Set(Object.keys(blob));
624
+ var decoder = map(object(mapping), function (safepart) {
625
+ var safekeys = new Set(Object.keys(mapping)); // To account for hard-coded keys that aren't part of the input
626
+
627
+ var _iterator2 = _createForOfIteratorHelper$1(safekeys),
628
+ _step2;
629
+
630
+ try {
631
+ for (_iterator2.s(); !(_step2 = _iterator2.n()).done;) {
632
+ var k = _step2.value;
633
+ allkeys.add(k);
634
+ }
635
+ } catch (err) {
636
+ _iterator2.e(err);
637
+ } finally {
638
+ _iterator2.f();
639
+ }
640
+
641
+ var rv = {};
642
+
643
+ var _iterator3 = _createForOfIteratorHelper$1(allkeys),
644
+ _step3;
645
+
646
+ try {
647
+ for (_iterator3.s(); !(_step3 = _iterator3.n()).done;) {
648
+ var _k = _step3.value;
649
+
650
+ if (safekeys.has(_k)) {
651
+ var value = safepart[_k];
652
+
653
+ if (value !== undefined) {
654
+ rv[_k] = value;
655
+ }
656
+ } else {
657
+ rv[_k] = blob[_k];
658
+ }
659
+ }
660
+ } catch (err) {
661
+ _iterator3.e(err);
662
+ } finally {
663
+ _iterator3.f();
664
+ }
665
+
666
+ return rv;
667
+ });
668
+ return decoder(blob);
669
+ });
670
+ }
671
+
672
+ /**
673
+ * Dispatches to one of several given decoders, based on the value found at
674
+ * runtime in the given field. For example, suppose you have these decoders:
675
+ *
676
+ * const rectangle = object({
677
+ * type: constant('rect'),
678
+ * x: number,
679
+ * y: number,
680
+ * width: number,
681
+ * height: number,
682
+ * });
683
+ *
684
+ * const circle = object({
685
+ * type: constant('circle'),
686
+ * cx: number,
687
+ * cy: number,
688
+ * r: number,
689
+ * });
690
+ *
691
+ * Then these two decoders are equivalent:
692
+ *
693
+ * const shape = either(rectangle, circle)
694
+ * const shape = dispatch('type', { rectangle, circle })
695
+ *
696
+ * Will be of type Decoder<Rectangle | Circle>.
697
+ *
698
+ * But the dispatch version will typically be more runtime-efficient. The
699
+ * reason is that it will first do minimal work to "look ahead" into the `type`
700
+ * field here, and based on that value, pick the decoder to invoke.
701
+ *
702
+ * The `either` version will simply try to invoke each decoder, until it finds
703
+ * one that matches.
704
+ *
705
+ * Also, the error messages will be less ambiguous using `dispatch()`.
706
+ */
707
+ function dispatch(field, mapping) {
708
+ var base = object(_defineProperty({}, field, oneOf(Object.keys(mapping))));
709
+ return function (blob) {
710
+ return base(blob).andThen(function (baseObj) {
711
+ var decoderName = baseObj[field];
712
+ var decoder = mapping[decoderName];
713
+ return decoder(blob);
714
+ });
715
+ };
716
+ }
717
+
718
+ /**
719
+ * Decoder that always fails with the given error message, no matter what the input.
720
+ */
721
+ function fail(msg) {
722
+ return function (blob) {
723
+ return Err(annotate(blob, msg));
724
+ };
725
+ }
726
+
727
+ function instanceOf(klass) {
728
+ return function (blob) {
729
+ return blob instanceof klass ? Ok(blob) : Err(annotate(blob, "Must be ".concat( // $FlowFixMe[incompatible-use] - klass.name is fine?
730
+ klass.name, " instance")));
731
+ };
732
+ }
733
+
734
+ /**
735
+ * Given an function returning a Decoder, will use that decoder to decode the
736
+ * value. This is typically used to build decoders for recursive or
737
+ * self-referential types.
738
+ */
739
+ function lazy(decoderFn) {
740
+ return function (blob) {
741
+ var decoder = decoderFn();
742
+ return decoder(blob);
743
+ };
744
+ }
745
+
746
+ function _createForOfIteratorHelper(o, allowArrayLike) { var it = typeof Symbol !== "undefined" && o[Symbol.iterator] || o["@@iterator"]; if (!it) { if (Array.isArray(o) || (it = _unsupportedIterableToArray(o)) || allowArrayLike && o && typeof o.length === "number") { if (it) o = it; var i = 0; var F = function F() {}; return { s: F, n: function n() { if (i >= o.length) return { done: true }; return { done: false, value: o[i++] }; }, e: function e(_e) { throw _e; }, f: F }; } throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); } var normalCompletion = true, didErr = false, err; return { s: function s() { it = it.call(o); }, n: function n() { var step = it.next(); normalCompletion = step.done; return step; }, e: function e(_e2) { didErr = true; err = _e2; }, f: function f() { try { if (!normalCompletion && it["return"] != null) it["return"](); } finally { if (didErr) throw err; } } }; }
747
+
748
+ function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); }
749
+
750
+ function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) { arr2[i] = arr[i]; } return arr2; }
751
+
752
+ function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); if (enumerableOnly) { symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; }); } keys.push.apply(keys, symbols); } return keys; }
753
+
754
+ function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i] != null ? arguments[i] : {}; if (i % 2) { ownKeys(Object(source), true).forEach(function (key) { _defineProperty(target, key, source[key]); }); } else if (Object.getOwnPropertyDescriptors) { Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)); } else { ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } } return target; }
755
+ /**
756
+ * Given an object, will decode a Map of string keys to whatever values.
757
+ *
758
+ * For example, given a decoder for a Person, we can verify a Person lookup
759
+ * table structure (of type Map<string, Person>) like so:
760
+ *
761
+ * mapping(person)
762
+ *
763
+ */
764
+
765
+ function mapping(decoder) {
766
+ return compose(pojo, // $FlowFixMe[unclear-type] (not really an issue) - deliberate use of Object here
767
+ function (blob) {
768
+ var tuples = [];
769
+ var errors = [];
770
+ Object.keys(blob).forEach(function (key) {
771
+ var value = blob[key];
772
+ var result = decoder(value);
773
+
774
+ try {
775
+ var okValue = result.unwrap();
776
+
777
+ if (errors.length === 0) {
778
+ tuples.push([key, okValue]);
779
+ }
780
+ } catch (e) {
781
+ /* istanbul ignore else */
782
+ if (isAnnotation(e)) {
783
+ tuples.length = 0; // Clear the tuples array
784
+
785
+ errors.push([key, e]);
786
+ } else {
787
+ // Otherwise, simply rethrow it
788
+
789
+ /* istanbul ignore next */
790
+ throw e;
791
+ }
792
+ }
793
+ });
794
+
795
+ if (errors.length > 0) {
796
+ return Err(annotateFields(blob, errors));
797
+ } else {
798
+ return Ok(new Map(tuples));
799
+ }
800
+ });
801
+ }
802
+
803
+ function mapToObject(mapping) {
804
+ var result = _objectSpread({}, null);
805
+
806
+ var _iterator = _createForOfIteratorHelper(mapping.entries()),
807
+ _step;
808
+
809
+ try {
810
+ for (_iterator.s(); !(_step = _iterator.n()).done;) {
811
+ var _step$value = _slicedToArray(_step.value, 2),
812
+ k = _step$value[0],
813
+ v = _step$value[1];
814
+
815
+ result[k] = v;
816
+ }
817
+ } catch (err) {
818
+ _iterator.e(err);
819
+ } finally {
820
+ _iterator.f();
821
+ }
822
+
823
+ return result;
824
+ }
825
+ /**
826
+ * Like mapping(), but returns an object rather than a Map instance.
827
+ */
828
+
829
+
830
+ function dict(decoder) {
831
+ return map(mapping(decoder), mapToObject);
832
+ }
833
+
834
+ var jsonObject = lazy(function () {
835
+ return dict(json);
836
+ });
837
+ var jsonArray = lazy(function () {
838
+ return array(json);
839
+ });
840
+ var json = either6(null_, string, number, _boolean, jsonObject, jsonArray);
841
+
842
+ /**
843
+ * Builds a Decoder that returns Ok for either `undefined` or `T` values,
844
+ * given a Decoder for `T`. Err otherwise.
845
+ */
846
+ function optional(decoder) {
847
+ return either(undefined_, decoder);
848
+ }
849
+ /**
850
+ * Builds a Decoder that returns Ok for either `null` or `T` values,
851
+ * given a Decoder for `T`. Err otherwise.
852
+ */
853
+
854
+ function nullable(decoder) {
855
+ return either(null_, decoder);
856
+ }
857
+ /**
858
+ * Decoder that only returns Ok for `null` or `undefined` inputs.
859
+ * This is effectively equivalent to either(null_, undefined_), but combines
860
+ * their error message output into a single line for convenience.
861
+ */
862
+
863
+ var undefined_or_null = function undefined_or_null(blob) {
864
+ return blob === undefined || blob === null ? Ok(blob) : // Combine error message into a single line
865
+ Err(annotate(blob, 'Must be undefined or null'));
866
+ };
867
+ /**
868
+ * Decoder that only returns Ok for `null` or `undefined` inputs.
869
+ */
870
+
871
+
872
+ function maybe(decoder) {
873
+ return either(undefined_or_null, decoder);
874
+ }
875
+
876
+ var ntuple = function ntuple(n) {
877
+ return compose(poja, predicate(function (arr) {
878
+ return arr.length === n;
879
+ }, "Must be a ".concat(n, "-tuple")));
880
+ };
881
+ /**
882
+ * Builds a Decoder that returns Ok for 1-tuple of [T], given a Decoder for T.
883
+ * Err otherwise.
884
+ */
885
+
886
+
887
+ function tuple1(decoder1) {
888
+ return compose(ntuple(1), function (blobs) {
889
+ var _blobs = _slicedToArray(blobs, 1),
890
+ blob1 = _blobs[0];
891
+
892
+ var result1 = decoder1(blob1);
893
+
894
+ try {
895
+ return Ok([result1.unwrap()]);
896
+ } catch (e) {
897
+ // If a decoder error has happened while unwrapping all the
898
+ // results, try to construct a good error message
899
+ return Err(annotate(result1.errValue()));
900
+ }
901
+ });
902
+ }
903
+ /**
904
+ * Builds a Decoder that returns Ok for 2-tuples of [T1, T2], given Decoders
905
+ * for T1 and T2. Err otherwise.
906
+ */
907
+
908
+ function tuple2(decoder1, decoder2) {
909
+ return compose(ntuple(2), function (blobs) {
910
+ var _blobs2 = _slicedToArray(blobs, 2),
911
+ blob1 = _blobs2[0],
912
+ blob2 = _blobs2[1];
913
+
914
+ var result1 = decoder1(blob1);
915
+ var result2 = decoder2(blob2);
916
+
917
+ try {
918
+ return Ok([result1.unwrap(), result2.unwrap()]);
919
+ } catch (e) {
920
+ // If a decoder error has happened while unwrapping all the
921
+ // results, try to construct a good error message
922
+ return Err(annotate([result1.isErr() ? result1.errValue() : result1.value(), result2.isErr() ? result2.errValue() : result2.value()]));
923
+ }
924
+ });
925
+ }
926
+ /**
927
+ * Builds a Decoder that returns Ok for 3-tuples of [T1, T2, T3], given
928
+ * Decoders for T1, T2, and T3. Err otherwise.
929
+ */
930
+
931
+ function tuple3(decoder1, decoder2, decoder3) {
932
+ return compose(ntuple(3), function (blobs) {
933
+ var _blobs3 = _slicedToArray(blobs, 3),
934
+ blob1 = _blobs3[0],
935
+ blob2 = _blobs3[1],
936
+ blob3 = _blobs3[2];
937
+
938
+ var result1 = decoder1(blob1);
939
+ var result2 = decoder2(blob2);
940
+ var result3 = decoder3(blob3);
941
+
942
+ try {
943
+ return Ok([result1.unwrap(), result2.unwrap(), result3.unwrap()]);
944
+ } catch (e) {
945
+ // If a decoder error has happened while unwrapping all the
946
+ // results, try to construct a good error message
947
+ return Err(annotate([result1.isErr() ? result1.errValue() : result1.value(), result2.isErr() ? result2.errValue() : result2.value(), result3.isErr() ? result3.errValue() : result3.value()]));
948
+ }
949
+ });
950
+ }
951
+ /**
952
+ * Builds a Decoder that returns Ok for 4-tuples of [T1, T2, T3, T4], given
953
+ * Decoders for T1, T2, T3, and T4. Err otherwise.
954
+ */
955
+
956
+ function tuple4(decoder1, decoder2, decoder3, decoder4) {
957
+ return compose(ntuple(4), function (blobs) {
958
+ var _blobs4 = _slicedToArray(blobs, 4),
959
+ blob1 = _blobs4[0],
960
+ blob2 = _blobs4[1],
961
+ blob3 = _blobs4[2],
962
+ blob4 = _blobs4[3];
963
+
964
+ var result1 = decoder1(blob1);
965
+ var result2 = decoder2(blob2);
966
+ var result3 = decoder3(blob3);
967
+ var result4 = decoder4(blob4);
968
+
969
+ try {
970
+ return Ok([result1.unwrap(), result2.unwrap(), result3.unwrap(), result4.unwrap()]);
971
+ } catch (e) {
972
+ // If a decoder error has happened while unwrapping all the
973
+ // results, try to construct a good error message
974
+ return Err(annotate([result1.isErr() ? result1.errValue() : result1.value(), result2.isErr() ? result2.errValue() : result2.value(), result3.isErr() ? result3.errValue() : result3.value(), result4.isErr() ? result4.errValue() : result4.value()]));
975
+ }
976
+ });
977
+ }
978
+ /**
979
+ * Builds a Decoder that returns Ok for 5-tuples of [T1, T2, T3, T4, T5], given
980
+ * Decoders for T1, T2, T3, T4, and T5. Err otherwise.
981
+ */
982
+
983
+ function tuple5(decoder1, decoder2, decoder3, decoder4, decoder5) {
984
+ return compose(ntuple(5), function (blobs) {
985
+ var _blobs5 = _slicedToArray(blobs, 5),
986
+ blob1 = _blobs5[0],
987
+ blob2 = _blobs5[1],
988
+ blob3 = _blobs5[2],
989
+ blob4 = _blobs5[3],
990
+ blob5 = _blobs5[4];
991
+
992
+ var result1 = decoder1(blob1);
993
+ var result2 = decoder2(blob2);
994
+ var result3 = decoder3(blob3);
995
+ var result4 = decoder4(blob4);
996
+ var result5 = decoder5(blob5);
997
+
998
+ try {
999
+ return Ok([result1.unwrap(), result2.unwrap(), result3.unwrap(), result4.unwrap(), result5.unwrap()]);
1000
+ } catch (e) {
1001
+ // If a decoder error has happened while unwrapping all the
1002
+ // results, try to construct a good error message
1003
+ return Err(annotate([result1.isErr() ? result1.errValue() : result1.value(), result2.isErr() ? result2.errValue() : result2.value(), result3.isErr() ? result3.errValue() : result3.value(), result4.isErr() ? result4.errValue() : result4.value(), result5.isErr() ? result5.errValue() : result5.value()]));
1004
+ }
1005
+ });
1006
+ }
1007
+ /**
1008
+ * Builds a Decoder that returns Ok for 5-tuples of [T1, T2, T3, T4, T5], given
1009
+ * Decoders for T1, T2, T3, T4, T5, and T6. Err otherwise.
1010
+ */
1011
+
1012
+ function tuple6(decoder1, decoder2, decoder3, decoder4, decoder5, decoder6) {
1013
+ return compose(ntuple(6), function (blobs) {
1014
+ var _blobs6 = _slicedToArray(blobs, 6),
1015
+ blob1 = _blobs6[0],
1016
+ blob2 = _blobs6[1],
1017
+ blob3 = _blobs6[2],
1018
+ blob4 = _blobs6[3],
1019
+ blob5 = _blobs6[4],
1020
+ blob6 = _blobs6[5];
1021
+
1022
+ var result1 = decoder1(blob1);
1023
+ var result2 = decoder2(blob2);
1024
+ var result3 = decoder3(blob3);
1025
+ var result4 = decoder4(blob4);
1026
+ var result5 = decoder5(blob5);
1027
+ var result6 = decoder6(blob6);
1028
+
1029
+ try {
1030
+ return Ok([result1.unwrap(), result2.unwrap(), result3.unwrap(), result4.unwrap(), result5.unwrap(), result6.unwrap()]);
1031
+ } catch (e) {
1032
+ // If a decoder error has happened while unwrapping all the
1033
+ // results, try to construct a good error message
1034
+ return Err(annotate([result1.isErr() ? result1.errValue() : result1.value(), result2.isErr() ? result2.errValue() : result2.value(), result3.isErr() ? result3.errValue() : result3.value(), result4.isErr() ? result4.errValue() : result4.value(), result5.isErr() ? result5.errValue() : result5.value(), result6.isErr() ? result6.errValue() : result6.value()]));
1035
+ }
1036
+ });
1037
+ }
1038
+
1039
+ export { array, _boolean as boolean, compose, constant, date, describe, dict, dispatch, either, either3, either4, either5, either6, either7, either8, either9, email, exact, fail, guard, hardcoded, inexact, instanceOf, integer, iso8601, json, jsonArray, jsonObject, lazy, map, mapping, maybe, mixed, nonEmptyArray, nonEmptyString, null_, nullable, number, numericBoolean, object, oneOf, optional, poja, pojo, positiveInteger, positiveNumber, predicate, regex, string, truthy, tuple1, tuple2, tuple3, tuple4, tuple5, tuple6, undefined_, unknown, url };