decoders 1.25.4 → 2.0.0-beta1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (132) hide show
  1. package/CHANGELOG.md +30 -3
  2. package/cjs/_guard.js +26 -0
  3. package/cjs/_guard.js.flow +20 -0
  4. package/cjs/_types.js +1 -0
  5. package/cjs/_types.js.flow +20 -0
  6. package/cjs/_utils.js +108 -0
  7. package/cjs/_utils.js.flow +97 -0
  8. package/cjs/annotate.js +161 -0
  9. package/cjs/annotate.js.flow +218 -0
  10. package/cjs/format/index.js +12 -0
  11. package/cjs/format/index.js.flow +4 -0
  12. package/cjs/format/inline.js +146 -0
  13. package/cjs/format/inline.js.flow +122 -0
  14. package/cjs/format/short.js +10 -0
  15. package/cjs/format/short.js.flow +8 -0
  16. package/cjs/index.js +120 -0
  17. package/{index.js.flow → cjs/index.js.flow} +31 -24
  18. package/cjs/result.js +172 -0
  19. package/cjs/result.js.flow +166 -0
  20. package/cjs/stdlib/array.js +108 -0
  21. package/{array.js.flow → cjs/stdlib/array.js.flow} +22 -25
  22. package/cjs/stdlib/boolean.js +44 -0
  23. package/{boolean.js.flow → cjs/stdlib/boolean.js.flow} +8 -7
  24. package/cjs/stdlib/composition.js +56 -0
  25. package/{utils.js.flow → cjs/stdlib/composition.js.flow} +7 -22
  26. package/cjs/stdlib/constants.js +69 -0
  27. package/{constants.js.flow → cjs/stdlib/constants.js.flow} +13 -12
  28. package/cjs/stdlib/date.js +46 -0
  29. package/{date.js.flow → cjs/stdlib/date.js.flow} +9 -7
  30. package/cjs/stdlib/describe.js +26 -0
  31. package/{describe.js.flow → cjs/stdlib/describe.js.flow} +4 -4
  32. package/cjs/stdlib/dispatch.js +62 -0
  33. package/{dispatch.js.flow → cjs/stdlib/dispatch.js.flow} +6 -5
  34. package/cjs/stdlib/either.js +117 -0
  35. package/{either.js.flow → cjs/stdlib/either.js.flow} +55 -35
  36. package/cjs/stdlib/fail.js +21 -0
  37. package/cjs/stdlib/fail.js.flow +12 -0
  38. package/cjs/stdlib/instanceOf.js +19 -0
  39. package/{instanceOf.js.flow → cjs/stdlib/instanceOf.js.flow} +7 -8
  40. package/{json.js → cjs/stdlib/json.js} +3 -5
  41. package/{json.js.flow → cjs/stdlib/json.js.flow} +4 -4
  42. package/{lazy.js → cjs/stdlib/lazy.js} +1 -3
  43. package/{lazy.js.flow → cjs/stdlib/lazy.js.flow} +1 -1
  44. package/cjs/stdlib/mapping.js +67 -0
  45. package/cjs/stdlib/mapping.js.flow +54 -0
  46. package/cjs/stdlib/number.js +40 -0
  47. package/{number.js.flow → cjs/stdlib/number.js.flow} +10 -11
  48. package/cjs/stdlib/object.js +194 -0
  49. package/{object.js.flow → cjs/stdlib/object.js.flow} +44 -52
  50. package/cjs/stdlib/optional.js +54 -0
  51. package/{optional.js.flow → cjs/stdlib/optional.js.flow} +6 -7
  52. package/cjs/stdlib/string.js +98 -0
  53. package/{string.js.flow → cjs/stdlib/string.js.flow} +13 -12
  54. package/cjs/stdlib/tuple.js +173 -0
  55. package/{tuple.js.flow → cjs/stdlib/tuple.js.flow} +62 -63
  56. package/es/_guard.js +15 -0
  57. package/es/_types.js +0 -0
  58. package/es/_utils.js +93 -0
  59. package/es/annotate.js +144 -0
  60. package/es/format/index.js +2 -0
  61. package/es/format/inline.js +137 -0
  62. package/es/format/short.js +4 -0
  63. package/es/index.js +37 -0
  64. package/es/result.js +139 -0
  65. package/es/stdlib/array.js +91 -0
  66. package/es/stdlib/boolean.js +28 -0
  67. package/es/stdlib/composition.js +42 -0
  68. package/es/stdlib/constants.js +46 -0
  69. package/es/stdlib/date.js +28 -0
  70. package/{describe.js → es/stdlib/describe.js} +5 -11
  71. package/{dispatch.js → es/stdlib/dispatch.js} +8 -15
  72. package/es/stdlib/either.js +90 -0
  73. package/es/stdlib/fail.js +11 -0
  74. package/es/stdlib/instanceOf.js +8 -0
  75. package/es/stdlib/json.js +15 -0
  76. package/es/stdlib/lazy.js +11 -0
  77. package/es/stdlib/mapping.js +54 -0
  78. package/es/stdlib/number.js +25 -0
  79. package/es/stdlib/object.js +175 -0
  80. package/es/stdlib/optional.js +38 -0
  81. package/{string.js → es/stdlib/string.js} +18 -35
  82. package/es/stdlib/tuple.js +155 -0
  83. package/package.json +12 -7
  84. package/ts/_guard.d.ts +7 -0
  85. package/ts/_helpers.d.ts +79 -0
  86. package/{types.d.ts → ts/_types.d.ts} +3 -5
  87. package/{utils.d.ts → ts/_utils.d.ts} +3 -3
  88. package/ts/annotate.d.ts +58 -0
  89. package/{array.d.ts → ts/array.d.ts} +1 -1
  90. package/{boolean.d.ts → ts/boolean.d.ts} +1 -1
  91. package/{constants.d.ts → ts/constants.d.ts} +1 -1
  92. package/{date.d.ts → ts/date.d.ts} +1 -1
  93. package/{describe.d.ts → ts/describe.d.ts} +1 -1
  94. package/{dispatch.d.ts → ts/dispatch.d.ts} +3 -3
  95. package/{either.d.ts → ts/either.d.ts} +8 -8
  96. package/{fail.d.ts → ts/fail.d.ts} +1 -1
  97. package/ts/index.d.ts +42 -0
  98. package/ts/inline.d.ts +3 -0
  99. package/{instanceOf.d.ts → ts/instanceOf.d.ts} +1 -1
  100. package/{json.d.ts → ts/json.d.ts} +1 -1
  101. package/{lazy.d.ts → ts/lazy.d.ts} +1 -1
  102. package/{mapping.d.ts → ts/mapping.d.ts} +1 -1
  103. package/{number.d.ts → ts/number.d.ts} +1 -1
  104. package/{object.d.ts → ts/object.d.ts} +8 -8
  105. package/{optional.d.ts → ts/optional.d.ts} +1 -1
  106. package/ts/result.d.ts +39 -0
  107. package/ts/short.d.ts +3 -0
  108. package/{string.d.ts → ts/string.d.ts} +1 -1
  109. package/{tuple.d.ts → ts/tuple.d.ts} +5 -5
  110. package/array.js +0 -133
  111. package/boolean.js +0 -42
  112. package/constants.js +0 -67
  113. package/date.js +0 -42
  114. package/either.js +0 -85
  115. package/fail.js +0 -19
  116. package/fail.js.flow +0 -13
  117. package/guard.d.ts +0 -7
  118. package/guard.js +0 -30
  119. package/guard.js.flow +0 -36
  120. package/helpers.d.ts +0 -62
  121. package/index.d.ts +0 -38
  122. package/index.js +0 -397
  123. package/instanceOf.js +0 -17
  124. package/mapping.js +0 -113
  125. package/mapping.js.flow +0 -71
  126. package/number.js +0 -38
  127. package/object.js +0 -254
  128. package/optional.js +0 -52
  129. package/tuple.js +0 -199
  130. package/types.js +0 -5
  131. package/types.js.flow +0 -26
  132. package/utils.js +0 -70
@@ -1,21 +1,19 @@
1
1
  "use strict";
2
2
 
3
- Object.defineProperty(exports, "__esModule", {
4
- value: true
5
- });
3
+ exports.__esModule = true;
6
4
  exports.jsonObject = exports.jsonArray = exports.json = void 0;
7
5
 
8
6
  var _array = require("./array");
9
7
 
10
8
  var _boolean2 = require("./boolean");
11
9
 
12
- var _constants = require("./constants");
10
+ var _mapping = require("./mapping");
13
11
 
14
12
  var _either = require("./either");
15
13
 
16
14
  var _lazy = require("./lazy");
17
15
 
18
- var _mapping = require("./mapping");
16
+ var _constants = require("./constants");
19
17
 
20
18
  var _number = require("./number");
21
19
 
@@ -2,13 +2,13 @@
2
2
 
3
3
  import { array } from './array';
4
4
  import { boolean } from './boolean';
5
- import { null_ } from './constants';
5
+ import { dict } from './mapping';
6
6
  import { either6 } from './either';
7
7
  import { lazy } from './lazy';
8
- import { dict } from './mapping';
8
+ import { null_ } from './constants';
9
9
  import { number } from './number';
10
10
  import { string } from './string';
11
- import type { Decoder } from './types';
11
+ import type { Decoder } from '../_types';
12
12
 
13
13
  export type JSONValue = null | string | number | boolean | JSONObject | JSONArray;
14
14
  export type JSONObject = { [string]: JSONValue };
@@ -24,5 +24,5 @@ export const json: Decoder<JSONValue> = either6(
24
24
  number,
25
25
  boolean,
26
26
  jsonObject,
27
- jsonArray
27
+ jsonArray,
28
28
  );
@@ -1,8 +1,6 @@
1
1
  "use strict";
2
2
 
3
- Object.defineProperty(exports, "__esModule", {
4
- value: true
5
- });
3
+ exports.__esModule = true;
6
4
  exports.lazy = lazy;
7
5
 
8
6
  /**
@@ -1,6 +1,6 @@
1
1
  // @flow strict
2
2
 
3
- import type { Decoder } from './types';
3
+ import type { Decoder } from '../_types';
4
4
 
5
5
  /**
6
6
  * Given an function returning a Decoder, will use that decoder to decode the
@@ -0,0 +1,67 @@
1
+ "use strict";
2
+
3
+ exports.__esModule = true;
4
+ exports.dict = dict;
5
+ exports.mapping = mapping;
6
+
7
+ var Result = _interopRequireWildcard(require("../result"));
8
+
9
+ var _annotate = require("../annotate");
10
+
11
+ var _composition = require("./composition");
12
+
13
+ var _object = require("./object");
14
+
15
+ 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); }
16
+
17
+ 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; }
18
+
19
+ /**
20
+ * Given an object, will decode a Map of string keys to whatever values.
21
+ *
22
+ * For example, given a decoder for a Person, we can verify a Person lookup
23
+ * table structure (of type Map<string, Person>) like so:
24
+ *
25
+ * mapping(person)
26
+ *
27
+ */
28
+ function mapping(decoder) {
29
+ return (0, _composition.compose)(_object.pojo, function (blob) {
30
+ var tuples = [];
31
+ var errors = null;
32
+ Object.keys(blob).forEach(function (key) {
33
+ var value = blob[key];
34
+ var result = decoder(value);
35
+
36
+ if (result.type === 'ok') {
37
+ if (errors === null) {
38
+ tuples.push([key, result.value]);
39
+ }
40
+ } else {
41
+ tuples.length = 0; // Clear the tuples array
42
+
43
+ if (errors === null) {
44
+ errors = {};
45
+ }
46
+
47
+ errors[key] = result.error;
48
+ }
49
+ });
50
+
51
+ if (errors !== null) {
52
+ return Result.err((0, _annotate.merge)((0, _annotate.annotateObject)(blob), errors));
53
+ } else {
54
+ return Result.ok(new Map(tuples));
55
+ }
56
+ });
57
+ }
58
+ /**
59
+ * Like mapping(), but returns an object rather than a Map instance.
60
+ */
61
+
62
+
63
+ function dict(decoder) {
64
+ return (0, _composition.map)(mapping(decoder), function (m) {
65
+ return Object.fromEntries(m);
66
+ });
67
+ }
@@ -0,0 +1,54 @@
1
+ // @flow strict
2
+
3
+ import * as Result from '../result';
4
+ import { annotateObject } from '../annotate';
5
+ import { compose, map } from './composition';
6
+ import { merge } from '../annotate';
7
+ import { pojo } from './object';
8
+ import type { Annotation } from '../annotate';
9
+ import type { Decoder } from '../_types';
10
+
11
+ /**
12
+ * Given an object, will decode a Map of string keys to whatever values.
13
+ *
14
+ * For example, given a decoder for a Person, we can verify a Person lookup
15
+ * table structure (of type Map<string, Person>) like so:
16
+ *
17
+ * mapping(person)
18
+ *
19
+ */
20
+ export function mapping<T>(decoder: Decoder<T>): Decoder<Map<string, T>> {
21
+ return compose(pojo, (blob: { +[key: string]: mixed }) => {
22
+ let tuples: Array<[string, T]> = [];
23
+ let errors: { [key: string]: Annotation } | null = null;
24
+
25
+ Object.keys(blob).forEach((key: string) => {
26
+ const value = blob[key];
27
+ const result = decoder(value);
28
+ if (result.type === 'ok') {
29
+ if (errors === null) {
30
+ tuples.push([key, result.value]);
31
+ }
32
+ } else {
33
+ tuples.length = 0; // Clear the tuples array
34
+ if (errors === null) {
35
+ errors = {};
36
+ }
37
+ errors[key] = result.error;
38
+ }
39
+ });
40
+
41
+ if (errors !== null) {
42
+ return Result.err(merge(annotateObject(blob), errors));
43
+ } else {
44
+ return Result.ok(new Map(tuples));
45
+ }
46
+ });
47
+ }
48
+
49
+ /**
50
+ * Like mapping(), but returns an object rather than a Map instance.
51
+ */
52
+ export function dict<T>(decoder: Decoder<T>): Decoder<{ [string]: T }> {
53
+ return map(mapping(decoder), (m) => Object.fromEntries(m));
54
+ }
@@ -0,0 +1,40 @@
1
+ "use strict";
2
+
3
+ exports.__esModule = true;
4
+ exports.positiveNumber = exports.positiveInteger = exports.number = exports.integer = void 0;
5
+
6
+ var Result = _interopRequireWildcard(require("../result"));
7
+
8
+ var _annotate = require("../annotate");
9
+
10
+ var _composition = require("./composition");
11
+
12
+ 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); }
13
+
14
+ 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; }
15
+
16
+ var anyNumber = function anyNumber(blob) {
17
+ return typeof blob === 'number' && !Number.isNaN(blob) ? Result.ok(blob) : Result.err((0, _annotate.annotate)(blob, 'Must be number'));
18
+ };
19
+
20
+ var isInteger = function isInteger(n) {
21
+ return Number.isInteger(n);
22
+ };
23
+
24
+ var isFinite = function isFinite(n) {
25
+ return Number.isFinite(n);
26
+ };
27
+
28
+ var number = (0, _composition.compose)(anyNumber, (0, _composition.predicate)(isFinite, 'Number must be finite'));
29
+ exports.number = number;
30
+ var positiveNumber = (0, _composition.compose)(number, (0, _composition.predicate)(function (n) {
31
+ return n >= 0;
32
+ }, 'Number must be positive')); // Integers
33
+
34
+ exports.positiveNumber = positiveNumber;
35
+ var integer = (0, _composition.compose)(number, (0, _composition.predicate)(isInteger, 'Number must be an integer'));
36
+ exports.integer = integer;
37
+ var positiveInteger = (0, _composition.compose)(integer, (0, _composition.predicate)(function (n) {
38
+ return n >= 0;
39
+ }, 'Number must be positive'));
40
+ exports.positiveInteger = positiveInteger;
@@ -1,15 +1,14 @@
1
1
  // @flow strict
2
2
 
3
- import { annotate } from 'debrief';
4
- import { Err, Ok } from 'lemons/Result';
5
-
6
- import type { Decoder } from './types';
7
- import { compose, predicate } from './utils';
3
+ import * as Result from '../result';
4
+ import { annotate } from '../annotate';
5
+ import { compose, predicate } from './composition';
6
+ import type { Decoder } from '../_types';
8
7
 
9
8
  const anyNumber: Decoder<number> = (blob: mixed) => {
10
9
  return typeof blob === 'number' && !Number.isNaN(blob)
11
- ? Ok(blob)
12
- : Err(annotate(blob, 'Must be number'));
10
+ ? Result.ok(blob)
11
+ : Result.err(annotate(blob, 'Must be number'));
13
12
  };
14
13
 
15
14
  const isInteger = (n: number) => Number.isInteger(n);
@@ -17,19 +16,19 @@ const isFinite = (n: number) => Number.isFinite(n);
17
16
 
18
17
  export const number: Decoder<number> = compose(
19
18
  anyNumber,
20
- predicate(isFinite, 'Number must be finite')
19
+ predicate(isFinite, 'Number must be finite'),
21
20
  );
22
21
  export const positiveNumber: Decoder<number> = compose(
23
22
  number,
24
- predicate((n) => n >= 0, 'Number must be positive')
23
+ predicate((n) => n >= 0, 'Number must be positive'),
25
24
  );
26
25
 
27
26
  // Integers
28
27
  export const integer: Decoder<number> = compose(
29
28
  number,
30
- predicate(isInteger, 'Number must be an integer')
29
+ predicate(isInteger, 'Number must be an integer'),
31
30
  );
32
31
  export const positiveInteger: Decoder<number> = compose(
33
32
  integer,
34
- predicate((n) => n >= 0, 'Number must be positive')
33
+ predicate((n) => n >= 0, 'Number must be positive'),
35
34
  );
@@ -0,0 +1,194 @@
1
+ "use strict";
2
+
3
+ exports.__esModule = true;
4
+ exports.exact = exact;
5
+ exports.inexact = inexact;
6
+ exports.object = object;
7
+ exports.pojo = void 0;
8
+
9
+ var Result = _interopRequireWildcard(require("../result"));
10
+
11
+ var _annotate = require("../annotate");
12
+
13
+ var _composition = require("./composition");
14
+
15
+ 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); }
16
+
17
+ 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; }
18
+
19
+ 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); }
20
+
21
+ function isPojo(o) {
22
+ return o !== null && o !== undefined && typeof o === 'object' && // This still seems to be the only reliable way to determine whether
23
+ // something is a pojo... ¯\_(ツ)_/¯
24
+ // $FlowFixMe[method-unbinding]
25
+ Object.prototype.toString.call(o) === '[object Object]';
26
+ }
27
+
28
+ function subtract(xs, ys) {
29
+ var result = new Set();
30
+ xs.forEach(function (x) {
31
+ if (!ys.has(x)) {
32
+ result.add(x);
33
+ }
34
+ });
35
+ return result;
36
+ }
37
+
38
+ var pojo = function pojo(blob) {
39
+ return isPojo(blob) ? Result.ok( // NOTE:
40
+ // Since Flow 0.98, typeof o === 'object' refines to
41
+ // {| +[string]: mixed |}
42
+ // instead of
43
+ // {| [string]: mixed |}
44
+ //
45
+ // For rationale, see https://github.com/facebook/flow/issues/7685.
46
+ // In this case, we don't want to output a read-only version of
47
+ // the object because it's up to the user of decoders to
48
+ // determine what they want to do with the decoded output. If they
49
+ // want to write items into the array, that's fine! The fastest
50
+ // way to turn a read-only Object to a writeable one in ES6 seems
51
+ // to be to use object-spread. (Going off this benchmark:
52
+ // https://thecodebarbarian.com/object-assign-vs-object-spread.html)
53
+ _extends({}, blob)) : Result.err((0, _annotate.annotate)(blob, 'Must be an object'));
54
+ };
55
+ /**
56
+ * Given a mapping of fields-to-decoders, builds a decoder for an object type.
57
+ *
58
+ * For example, given decoders for a number and a string, we can construct an
59
+ * "object description" like so:
60
+ *
61
+ * { id: number, name: string }
62
+ *
63
+ * Which is of type:
64
+ *
65
+ * { id: Decoder<number>, name: Decoder<string> }
66
+ *
67
+ * Passing this to object() will produce the following return type:
68
+ *
69
+ * Decoder<{ id: number, name: string }>
70
+ *
71
+ * Put simply: it'll "peel off" all of the nested Decoders, puts them together
72
+ * in an object, and wraps it in a Decoder<...>.
73
+ */
74
+
75
+
76
+ exports.pojo = pojo;
77
+
78
+ function object(mapping) {
79
+ var known = new Set(Object.keys(mapping));
80
+ return (0, _composition.compose)(pojo, function (blob) {
81
+ var actual = new Set(Object.keys(blob)); // At this point, "missing" will also include all fields that may
82
+ // validly be optional. We'll let the underlying decoder decide and
83
+ // remove the key from this missing set if the decoder accepts the
84
+ // value.
85
+
86
+ var missing = subtract(known, actual);
87
+ var record = {};
88
+ var errors = null;
89
+ Object.keys(mapping).forEach(function (key) {
90
+ var decoder = mapping[key];
91
+ var rawValue = blob[key];
92
+ var result = decoder(rawValue);
93
+
94
+ if (result.type === 'ok') {
95
+ var value = result.value;
96
+
97
+ if (value !== undefined) {
98
+ record[key] = value;
99
+ } // If this succeeded, remove the key from the missing keys
100
+ // tracker
101
+
102
+
103
+ missing["delete"](key);
104
+ } else {
105
+ var ann = result.error; // Keep track of the annotation, but don't return just yet. We
106
+ // want to collect more error information.
107
+
108
+ if (rawValue === undefined) {
109
+ // Explicitly add it to the missing set if the value is
110
+ // undefined. This covers explicit undefineds to be
111
+ // treated the same as implicit undefineds (aka missing
112
+ // keys).
113
+ missing.add(key);
114
+ } else {
115
+ if (errors === null) {
116
+ errors = {};
117
+ }
118
+
119
+ errors[key] = ann;
120
+ }
121
+ }
122
+ }); // Deal with errors now. There are two classes of errors we want to
123
+ // report. First of all, we want to report any inline errors in this
124
+ // object. Lastly, any fields that are missing should be annotated on
125
+ // the outer object itself.
126
+
127
+ if (errors || missing.size > 0) {
128
+ var objAnn = (0, _annotate.annotateObject)(blob);
129
+
130
+ if (errors) {
131
+ objAnn = (0, _annotate.merge)(objAnn, errors);
132
+ }
133
+
134
+ if (missing.size > 0) {
135
+ var errMsg = Array.from(missing).map(function (key) {
136
+ return "\"" + key + "\"";
137
+ }).join(', ');
138
+ var pluralized = missing.size > 1 ? 'keys' : 'key';
139
+ objAnn = (0, _annotate.updateText)(objAnn, "Missing " + pluralized + ": " + errMsg);
140
+ }
141
+
142
+ return Result.err(objAnn);
143
+ }
144
+
145
+ return Result.ok(record);
146
+ });
147
+ }
148
+
149
+ function exact(mapping) {
150
+ // Check the inputted object for any superfluous keys
151
+ var allowed = new Set(Object.keys(mapping));
152
+ var checked = (0, _composition.compose)(pojo, function (blob) {
153
+ var actual = new Set(Object.keys(blob));
154
+ var superfluous = subtract(actual, allowed);
155
+
156
+ if (superfluous.size > 0) {
157
+ return Result.err((0, _annotate.annotate)(blob, "Superfluous keys: " + Array.from(superfluous).join(', ')));
158
+ }
159
+
160
+ return Result.ok(blob);
161
+ }); // Defer to the "object" decoder for doing the real decoding work. Since
162
+ // we made sure there are no superfluous keys in this structure, it's now
163
+ // safe to force-cast it to an $Exact<> type.
164
+
165
+ var decoder = object(mapping);
166
+ return (0, _composition.compose)(checked, decoder);
167
+ }
168
+
169
+ function inexact(mapping) {
170
+ return (0, _composition.compose)(pojo, function (blob) {
171
+ var allkeys = new Set(Object.keys(blob));
172
+ var decoder = (0, _composition.map)(object(mapping), function (safepart) {
173
+ var safekeys = new Set(Object.keys(mapping)); // To account for hard-coded keys that aren't part of the input
174
+
175
+ safekeys.forEach(function (k) {
176
+ return allkeys.add(k);
177
+ });
178
+ var rv = {};
179
+ allkeys.forEach(function (k) {
180
+ if (safekeys.has(k)) {
181
+ var value = safepart[k];
182
+
183
+ if (value !== undefined) {
184
+ rv[k] = value;
185
+ }
186
+ } else {
187
+ rv[k] = blob[k];
188
+ }
189
+ });
190
+ return rv;
191
+ });
192
+ return decoder(blob);
193
+ });
194
+ }