decoders 2.0.0-beta8 → 2.0.1

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 (146) hide show
  1. package/CHANGELOG.md +67 -20
  2. package/Decoder.d.ts +94 -0
  3. package/Decoder.js +115 -0
  4. package/Decoder.js.flow +286 -0
  5. package/Decoder.mjs +108 -0
  6. package/README.md +122 -1345
  7. package/_utils.d.ts +0 -1
  8. package/_utils.js +51 -68
  9. package/_utils.js.flow +30 -20
  10. package/_utils.mjs +39 -54
  11. package/annotate.js +79 -72
  12. package/annotate.mjs +62 -58
  13. package/format.d.ts +4 -2
  14. package/format.js +64 -74
  15. package/format.js.flow +14 -12
  16. package/format.mjs +58 -68
  17. package/index.d.ts +38 -41
  18. package/index.js +66 -98
  19. package/index.js.flow +39 -58
  20. package/index.mjs +11 -37
  21. package/{core → lib}/_helpers.d.ts +0 -0
  22. package/lib/arrays.d.ts +59 -0
  23. package/lib/arrays.js +97 -0
  24. package/lib/arrays.js.flow +138 -0
  25. package/lib/arrays.mjs +84 -0
  26. package/lib/basics.d.ts +93 -0
  27. package/lib/basics.js +94 -0
  28. package/lib/basics.js.flow +124 -0
  29. package/lib/basics.mjs +75 -0
  30. package/lib/booleans.d.ts +16 -0
  31. package/lib/booleans.js +25 -0
  32. package/lib/booleans.js.flow +22 -0
  33. package/lib/booleans.mjs +15 -0
  34. package/lib/dates.d.ts +15 -0
  35. package/lib/dates.js +28 -0
  36. package/lib/dates.js.flow +40 -0
  37. package/lib/dates.mjs +19 -0
  38. package/lib/json.d.ts +35 -0
  39. package/lib/json.js +33 -0
  40. package/lib/json.js.flow +50 -0
  41. package/lib/json.mjs +18 -0
  42. package/lib/numbers.d.ts +31 -0
  43. package/lib/numbers.js +31 -0
  44. package/lib/numbers.js.flow +48 -0
  45. package/lib/numbers.mjs +21 -0
  46. package/lib/objects.d.ts +76 -0
  47. package/lib/objects.js +161 -0
  48. package/lib/objects.js.flow +238 -0
  49. package/lib/objects.mjs +147 -0
  50. package/lib/strings.d.ts +56 -0
  51. package/lib/strings.js +58 -0
  52. package/lib/strings.js.flow +90 -0
  53. package/lib/strings.mjs +40 -0
  54. package/lib/unions.d.ts +55 -0
  55. package/lib/unions.js +96 -0
  56. package/lib/unions.js.flow +155 -0
  57. package/lib/unions.mjs +83 -0
  58. package/lib/utilities.d.ts +40 -0
  59. package/lib/utilities.js +48 -0
  60. package/lib/utilities.js.flow +65 -0
  61. package/lib/utilities.mjs +37 -0
  62. package/package.json +64 -17
  63. package/result.d.ts +2 -25
  64. package/result.js +9 -90
  65. package/result.js.flow +4 -76
  66. package/result.mjs +5 -71
  67. package/_guard.d.ts +0 -7
  68. package/_guard.js +0 -22
  69. package/_guard.js.flow +0 -20
  70. package/_guard.mjs +0 -15
  71. package/_types.d.ts +0 -13
  72. package/_types.js +0 -1
  73. package/_types.js.flow +0 -20
  74. package/_types.mjs +0 -0
  75. package/core/array.d.ts +0 -5
  76. package/core/array.js +0 -104
  77. package/core/array.js.flow +0 -100
  78. package/core/array.mjs +0 -91
  79. package/core/boolean.d.ts +0 -5
  80. package/core/boolean.js +0 -40
  81. package/core/boolean.js.flow +0 -27
  82. package/core/boolean.mjs +0 -28
  83. package/core/composition.d.ts +0 -14
  84. package/core/composition.js +0 -54
  85. package/core/composition.js.flow +0 -48
  86. package/core/composition.mjs +0 -44
  87. package/core/constants.d.ts +0 -11
  88. package/core/constants.js +0 -65
  89. package/core/constants.js.flow +0 -44
  90. package/core/constants.mjs +0 -46
  91. package/core/date.d.ts +0 -4
  92. package/core/date.js +0 -42
  93. package/core/date.js.flow +0 -38
  94. package/core/date.mjs +0 -28
  95. package/core/describe.d.ts +0 -3
  96. package/core/describe.js +0 -22
  97. package/core/describe.js.flow +0 -17
  98. package/core/describe.mjs +0 -16
  99. package/core/dispatch.d.ts +0 -8
  100. package/core/dispatch.js +0 -58
  101. package/core/dispatch.js.flow +0 -58
  102. package/core/dispatch.mjs +0 -51
  103. package/core/either.d.ts +0 -61
  104. package/core/either.js +0 -113
  105. package/core/either.js.flow +0 -151
  106. package/core/either.mjs +0 -90
  107. package/core/fail.d.ts +0 -3
  108. package/core/fail.js +0 -17
  109. package/core/fail.js.flow +0 -12
  110. package/core/fail.mjs +0 -11
  111. package/core/instanceOf.d.ts +0 -3
  112. package/core/instanceOf.js +0 -15
  113. package/core/instanceOf.js.flow +0 -20
  114. package/core/instanceOf.mjs +0 -8
  115. package/core/json.d.ts +0 -11
  116. package/core/json.js +0 -31
  117. package/core/json.js.flow +0 -28
  118. package/core/json.mjs +0 -15
  119. package/core/lazy.d.ts +0 -3
  120. package/core/lazy.js +0 -16
  121. package/core/lazy.js.flow +0 -15
  122. package/core/lazy.mjs +0 -11
  123. package/core/mapping.d.ts +0 -6
  124. package/core/mapping.js +0 -67
  125. package/core/mapping.js.flow +0 -62
  126. package/core/mapping.mjs +0 -58
  127. package/core/number.d.ts +0 -6
  128. package/core/number.js +0 -36
  129. package/core/number.js.flow +0 -40
  130. package/core/number.mjs +0 -25
  131. package/core/object.d.ts +0 -33
  132. package/core/object.js +0 -190
  133. package/core/object.js.flow +0 -203
  134. package/core/object.mjs +0 -175
  135. package/core/optional.d.ts +0 -5
  136. package/core/optional.js +0 -50
  137. package/core/optional.js.flow +0 -41
  138. package/core/optional.mjs +0 -38
  139. package/core/string.d.ts +0 -10
  140. package/core/string.js +0 -68
  141. package/core/string.js.flow +0 -59
  142. package/core/string.mjs +0 -49
  143. package/core/tuple.d.ts +0 -30
  144. package/core/tuple.js +0 -177
  145. package/core/tuple.js.flow +0 -211
  146. package/core/tuple.mjs +0 -159
package/core/number.js DELETED
@@ -1,36 +0,0 @@
1
- "use strict";
2
-
3
- exports.__esModule = true;
4
- exports.positiveNumber = exports.positiveInteger = exports.number = exports.integer = void 0;
5
-
6
- var _annotate = require("../annotate");
7
-
8
- var _result = require("../result");
9
-
10
- var _composition = require("./composition");
11
-
12
- var anyNumber = function anyNumber(blob) {
13
- return typeof blob === 'number' && !Number.isNaN(blob) ? (0, _result.ok)(blob) : (0, _result.err)((0, _annotate.annotate)(blob, 'Must be number'));
14
- };
15
-
16
- var isInteger = function isInteger(n) {
17
- return Number.isInteger(n);
18
- };
19
-
20
- var isFinite = function isFinite(n) {
21
- return Number.isFinite(n);
22
- };
23
-
24
- var number = (0, _composition.predicate)(anyNumber, isFinite, 'Number must be finite');
25
- exports.number = number;
26
- var positiveNumber = (0, _composition.predicate)(number, function (n) {
27
- return n >= 0;
28
- }, 'Number must be positive'); // Integers
29
-
30
- exports.positiveNumber = positiveNumber;
31
- var integer = (0, _composition.predicate)(number, isInteger, 'Number must be an integer');
32
- exports.integer = integer;
33
- var positiveInteger = (0, _composition.predicate)(integer, function (n) {
34
- return n >= 0;
35
- }, 'Number must be positive');
36
- exports.positiveInteger = positiveInteger;
@@ -1,40 +0,0 @@
1
- // @flow strict
2
-
3
- import { annotate } from '../annotate';
4
- import { err, ok } from '../result';
5
- import { predicate } from './composition';
6
- import type { Decoder } from '../_types';
7
-
8
- const anyNumber: Decoder<number> = (blob: mixed) => {
9
- return typeof blob === 'number' && !Number.isNaN(blob)
10
- ? ok(blob)
11
- : err(annotate(blob, 'Must be number'));
12
- };
13
-
14
- const isInteger = (n: number) => Number.isInteger(n);
15
- const isFinite = (n: number) => Number.isFinite(n);
16
-
17
- export const number: Decoder<number> = predicate(
18
- anyNumber,
19
- isFinite,
20
- 'Number must be finite',
21
- );
22
-
23
- export const positiveNumber: Decoder<number> = predicate(
24
- number,
25
- (n) => n >= 0,
26
- 'Number must be positive',
27
- );
28
-
29
- // Integers
30
- export const integer: Decoder<number> = predicate(
31
- number,
32
- isInteger,
33
- 'Number must be an integer',
34
- );
35
-
36
- export const positiveInteger: Decoder<number> = predicate(
37
- integer,
38
- (n) => n >= 0,
39
- 'Number must be positive',
40
- );
package/core/number.mjs DELETED
@@ -1,25 +0,0 @@
1
- import { annotate } from '../annotate.mjs';
2
- import { err, ok } from '../result.mjs';
3
- import { predicate } from './composition.mjs';
4
-
5
- var anyNumber = function anyNumber(blob) {
6
- return typeof blob === 'number' && !Number.isNaN(blob) ? ok(blob) : err(annotate(blob, 'Must be number'));
7
- };
8
-
9
- var isInteger = function isInteger(n) {
10
- return Number.isInteger(n);
11
- };
12
-
13
- var isFinite = function isFinite(n) {
14
- return Number.isFinite(n);
15
- };
16
-
17
- export var number = predicate(anyNumber, isFinite, 'Number must be finite');
18
- export var positiveNumber = predicate(number, function (n) {
19
- return n >= 0;
20
- }, 'Number must be positive'); // Integers
21
-
22
- export var integer = predicate(number, isInteger, 'Number must be an integer');
23
- export var positiveInteger = predicate(integer, function (n) {
24
- return n >= 0;
25
- }, 'Number must be positive');
package/core/object.d.ts DELETED
@@ -1,33 +0,0 @@
1
- import { Decoder, DecoderType } from '../_types';
2
- import { AllowImplicit } from './_helpers';
3
-
4
- export type ObjectDecoderType<T> = AllowImplicit<{
5
- [key in keyof T]: DecoderType<T[key]>;
6
- }>;
7
-
8
- export const pojo: Decoder<{ [key: string]: unknown }>;
9
-
10
- export function object<O extends { [key: string]: Decoder<any> }>(
11
- mapping: O,
12
- ): Decoder<{ [K in keyof ObjectDecoderType<O>]: ObjectDecoderType<O>[K] }>;
13
- // ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
14
- // This is basically just equivalent to:
15
- // ObjectDecoderType<O>
16
- //
17
- // But by "resolving" this with a mapped type, we remove the helper
18
- // type names from the inferred type here, making this much easier to
19
- // work with while developing.
20
-
21
- export function exact<O extends { [key: string]: Decoder<any> }>(
22
- mapping: O,
23
- ): Decoder<{ [K in keyof ObjectDecoderType<O>]: ObjectDecoderType<O>[K] }>;
24
- // ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
25
- // Ditto (see above)
26
-
27
- export function inexact<O extends { [key: string]: Decoder<any> }>(
28
- mapping: O,
29
- ): Decoder<
30
- { [K in keyof ObjectDecoderType<O>]: ObjectDecoderType<O>[K] } & {
31
- [extra: string]: unknown;
32
- }
33
- >;
package/core/object.js DELETED
@@ -1,190 +0,0 @@
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 _annotate = require("../annotate");
10
-
11
- var _composition = require("./composition");
12
-
13
- var _result = require("../result");
14
-
15
- 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); }
16
-
17
- function isPojo(o) {
18
- return o !== null && o !== undefined && typeof o === 'object' && // This still seems to be the only reliable way to determine whether
19
- // something is a pojo... ¯\_(ツ)_/¯
20
- // $FlowFixMe[method-unbinding]
21
- Object.prototype.toString.call(o) === '[object Object]';
22
- }
23
-
24
- function subtract(xs, ys) {
25
- var result = new Set();
26
- xs.forEach(function (x) {
27
- if (!ys.has(x)) {
28
- result.add(x);
29
- }
30
- });
31
- return result;
32
- }
33
-
34
- var pojo = function pojo(blob) {
35
- return isPojo(blob) ? (0, _result.ok)( // NOTE:
36
- // Since Flow 0.98, typeof o === 'object' refines to
37
- // {| +[string]: mixed |}
38
- // instead of
39
- // {| [string]: mixed |}
40
- //
41
- // For rationale, see https://github.com/facebook/flow/issues/7685.
42
- // In this case, we don't want to output a read-only version of
43
- // the object because it's up to the user of decoders to
44
- // determine what they want to do with the decoded output. If they
45
- // want to write items into the array, that's fine! The fastest
46
- // way to turn a read-only Object to a writeable one in ES6 seems
47
- // to be to use object-spread. (Going off this benchmark:
48
- // https://thecodebarbarian.com/object-assign-vs-object-spread.html)
49
- _extends({}, blob)) : (0, _result.err)((0, _annotate.annotate)(blob, 'Must be an object'));
50
- };
51
- /**
52
- * Given a mapping of fields-to-decoders, builds a decoder for an object type.
53
- *
54
- * For example, given decoders for a number and a string, we can construct an
55
- * "object description" like so:
56
- *
57
- * { id: number, name: string }
58
- *
59
- * Which is of type:
60
- *
61
- * { id: Decoder<number>, name: Decoder<string> }
62
- *
63
- * Passing this to object() will produce the following return type:
64
- *
65
- * Decoder<{ id: number, name: string }>
66
- *
67
- * Put simply: it'll "peel off" all of the nested Decoders, puts them together
68
- * in an object, and wraps it in a Decoder<...>.
69
- */
70
-
71
-
72
- exports.pojo = pojo;
73
-
74
- function object(mapping) {
75
- var known = new Set(Object.keys(mapping));
76
- return (0, _composition.compose)(pojo, function (blob) {
77
- var actual = new Set(Object.keys(blob)); // At this point, "missing" will also include all fields that may
78
- // validly be optional. We'll let the underlying decoder decide and
79
- // remove the key from this missing set if the decoder accepts the
80
- // value.
81
-
82
- var missing = subtract(known, actual);
83
- var record = {};
84
- var errors = null;
85
- Object.keys(mapping).forEach(function (key) {
86
- var decoder = mapping[key];
87
- var rawValue = blob[key];
88
- var result = decoder(rawValue);
89
-
90
- if (result.type === 'ok') {
91
- var value = result.value;
92
-
93
- if (value !== undefined) {
94
- record[key] = value;
95
- } // If this succeeded, remove the key from the missing keys
96
- // tracker
97
-
98
-
99
- missing["delete"](key);
100
- } else {
101
- var ann = result.error; // Keep track of the annotation, but don't return just yet. We
102
- // want to collect more error information.
103
-
104
- if (rawValue === undefined) {
105
- // Explicitly add it to the missing set if the value is
106
- // undefined. This covers explicit undefineds to be
107
- // treated the same as implicit undefineds (aka missing
108
- // keys).
109
- missing.add(key);
110
- } else {
111
- if (errors === null) {
112
- errors = {};
113
- }
114
-
115
- errors[key] = ann;
116
- }
117
- }
118
- }); // Deal with errors now. There are two classes of errors we want to
119
- // report. First of all, we want to report any inline errors in this
120
- // object. Lastly, any fields that are missing should be annotated on
121
- // the outer object itself.
122
-
123
- if (errors || missing.size > 0) {
124
- var objAnn = (0, _annotate.annotateObject)(blob);
125
-
126
- if (errors) {
127
- objAnn = (0, _annotate.merge)(objAnn, errors);
128
- }
129
-
130
- if (missing.size > 0) {
131
- var errMsg = Array.from(missing).map(function (key) {
132
- return "\"" + key + "\"";
133
- }).join(', ');
134
- var pluralized = missing.size > 1 ? 'keys' : 'key';
135
- objAnn = (0, _annotate.updateText)(objAnn, "Missing " + pluralized + ": " + errMsg);
136
- }
137
-
138
- return (0, _result.err)(objAnn);
139
- }
140
-
141
- return (0, _result.ok)(record);
142
- });
143
- }
144
-
145
- function exact(mapping) {
146
- // Check the inputted object for any superfluous keys
147
- var allowed = new Set(Object.keys(mapping));
148
- var checked = (0, _composition.compose)(pojo, function (blob) {
149
- var actual = new Set(Object.keys(blob));
150
- var superfluous = subtract(actual, allowed);
151
-
152
- if (superfluous.size > 0) {
153
- return (0, _result.err)((0, _annotate.annotate)(blob, "Superfluous keys: " + Array.from(superfluous).join(', ')));
154
- }
155
-
156
- return (0, _result.ok)(blob);
157
- }); // Defer to the "object" decoder for doing the real decoding work. Since
158
- // we made sure there are no superfluous keys in this structure, it's now
159
- // safe to force-cast it to an $Exact<> type.
160
-
161
- var decoder = object(mapping);
162
- return (0, _composition.compose)(checked, decoder);
163
- }
164
-
165
- function inexact(mapping) {
166
- return (0, _composition.compose)(pojo, function (blob) {
167
- var allkeys = new Set(Object.keys(blob));
168
- var decoder = (0, _composition.map)(object(mapping), function (safepart) {
169
- var safekeys = new Set(Object.keys(mapping)); // To account for hard-coded keys that aren't part of the input
170
-
171
- safekeys.forEach(function (k) {
172
- return allkeys.add(k);
173
- });
174
- var rv = {};
175
- allkeys.forEach(function (k) {
176
- if (safekeys.has(k)) {
177
- var value = safepart[k];
178
-
179
- if (value !== undefined) {
180
- rv[k] = value;
181
- }
182
- } else {
183
- rv[k] = blob[k];
184
- }
185
- });
186
- return rv;
187
- });
188
- return decoder(blob);
189
- });
190
- }
@@ -1,203 +0,0 @@
1
- // @flow strict
2
-
3
- import { annotate, annotateObject, merge, updateText } from '../annotate';
4
- import { compose, map } from './composition';
5
- import { err, ok } from '../result';
6
- import type { Annotation } from '../annotate';
7
- import type { Decoder, DecoderType } from '../_types';
8
-
9
- // $FlowFixMe[unclear-type] (not really an issue) - deliberate use of `any` - not sure how we should get rid of this
10
- type AnyDecoder = any;
11
-
12
- // $FlowFixMe[unclear-type] (not really an issue) - deliberately casting
13
- type cast = any;
14
-
15
- function isPojo(o: mixed): boolean %checks {
16
- return (
17
- o !== null &&
18
- o !== undefined &&
19
- typeof o === 'object' &&
20
- // This still seems to be the only reliable way to determine whether
21
- // something is a pojo... ¯\_(ツ)_/¯
22
- // $FlowFixMe[method-unbinding]
23
- Object.prototype.toString.call(o) === '[object Object]'
24
- );
25
- }
26
-
27
- function subtract(xs: Set<string>, ys: Set<string>): Set<string> {
28
- const result = new Set();
29
- xs.forEach((x) => {
30
- if (!ys.has(x)) {
31
- result.add(x);
32
- }
33
- });
34
- return result;
35
- }
36
-
37
- export const pojo: Decoder<{| [string]: mixed |}> = (blob: mixed) => {
38
- return isPojo(blob)
39
- ? ok(
40
- // NOTE:
41
- // Since Flow 0.98, typeof o === 'object' refines to
42
- // {| +[string]: mixed |}
43
- // instead of
44
- // {| [string]: mixed |}
45
- //
46
- // For rationale, see https://github.com/facebook/flow/issues/7685.
47
- // In this case, we don't want to output a read-only version of
48
- // the object because it's up to the user of decoders to
49
- // determine what they want to do with the decoded output. If they
50
- // want to write items into the array, that's fine! The fastest
51
- // way to turn a read-only Object to a writeable one in ES6 seems
52
- // to be to use object-spread. (Going off this benchmark:
53
- // https://thecodebarbarian.com/object-assign-vs-object-spread.html)
54
- { ...blob },
55
- )
56
- : err(annotate(blob, 'Must be an object'));
57
- };
58
-
59
- /**
60
- * Given a mapping of fields-to-decoders, builds a decoder for an object type.
61
- *
62
- * For example, given decoders for a number and a string, we can construct an
63
- * "object description" like so:
64
- *
65
- * { id: number, name: string }
66
- *
67
- * Which is of type:
68
- *
69
- * { id: Decoder<number>, name: Decoder<string> }
70
- *
71
- * Passing this to object() will produce the following return type:
72
- *
73
- * Decoder<{ id: number, name: string }>
74
- *
75
- * Put simply: it'll "peel off" all of the nested Decoders, puts them together
76
- * in an object, and wraps it in a Decoder<...>.
77
- */
78
- export function object<O: { +[field: string]: AnyDecoder, ... }>(
79
- mapping: O,
80
- ): Decoder<$ObjMap<O, DecoderType>> {
81
- const known = new Set(Object.keys(mapping));
82
- return compose(pojo, (blob: { +[string]: mixed }) => {
83
- const actual = new Set(Object.keys(blob));
84
-
85
- // At this point, "missing" will also include all fields that may
86
- // validly be optional. We'll let the underlying decoder decide and
87
- // remove the key from this missing set if the decoder accepts the
88
- // value.
89
- const missing = subtract(known, actual);
90
-
91
- let record = {};
92
- let errors: { [key: string]: Annotation } | null = null;
93
-
94
- Object.keys(mapping).forEach((key) => {
95
- const decoder = mapping[key];
96
- const rawValue = blob[key];
97
- const result = decoder(rawValue);
98
-
99
- if (result.type === 'ok') {
100
- const value = result.value;
101
- if (value !== undefined) {
102
- record[key] = value;
103
- }
104
-
105
- // If this succeeded, remove the key from the missing keys
106
- // tracker
107
- missing.delete(key);
108
- } else {
109
- const ann = result.error;
110
-
111
- // Keep track of the annotation, but don't return just yet. We
112
- // want to collect more error information.
113
- if (rawValue === undefined) {
114
- // Explicitly add it to the missing set if the value is
115
- // undefined. This covers explicit undefineds to be
116
- // treated the same as implicit undefineds (aka missing
117
- // keys).
118
- missing.add(key);
119
- } else {
120
- if (errors === null) {
121
- errors = {};
122
- }
123
- errors[key] = ann;
124
- }
125
- }
126
- });
127
-
128
- // Deal with errors now. There are two classes of errors we want to
129
- // report. First of all, we want to report any inline errors in this
130
- // object. Lastly, any fields that are missing should be annotated on
131
- // the outer object itself.
132
- if (errors || missing.size > 0) {
133
- let objAnn = annotateObject(blob);
134
-
135
- if (errors) {
136
- objAnn = merge(objAnn, errors);
137
- }
138
-
139
- if (missing.size > 0) {
140
- const errMsg = Array.from(missing)
141
- .map((key) => `"${key}"`)
142
- .join(', ');
143
- const pluralized = missing.size > 1 ? 'keys' : 'key';
144
- objAnn = updateText(objAnn, `Missing ${pluralized}: ${errMsg}`);
145
- }
146
-
147
- return err(objAnn);
148
- }
149
-
150
- return ok(record);
151
- });
152
- }
153
-
154
- export function exact<O: { +[field: string]: AnyDecoder, ... }>(
155
- mapping: O,
156
- ): Decoder<$ObjMap<$Exact<O>, DecoderType>> {
157
- // Check the inputted object for any superfluous keys
158
- const allowed = new Set(Object.keys(mapping));
159
- const checked = compose(pojo, (blob: {| [string]: mixed |}) => {
160
- const actual = new Set(Object.keys(blob));
161
- const superfluous = subtract(actual, allowed);
162
- if (superfluous.size > 0) {
163
- return err(
164
- annotate(blob, `Superfluous keys: ${Array.from(superfluous).join(', ')}`),
165
- );
166
- }
167
- return ok(blob);
168
- });
169
-
170
- // Defer to the "object" decoder for doing the real decoding work. Since
171
- // we made sure there are no superfluous keys in this structure, it's now
172
- // safe to force-cast it to an $Exact<> type.
173
- const decoder = ((object(mapping): cast): Decoder<$ObjMap<$Exact<O>, DecoderType>>);
174
- return compose(checked, decoder);
175
- }
176
-
177
- export function inexact<O: { +[field: string]: AnyDecoder }>(
178
- mapping: O,
179
- ): Decoder<$ObjMap<O, DecoderType> & { +[string]: mixed }> {
180
- return compose(pojo, (blob: {| [string]: mixed |}) => {
181
- const allkeys = new Set(Object.keys(blob));
182
- const decoder = map(object(mapping), (safepart: $ObjMap<O, DecoderType>) => {
183
- const safekeys = new Set(Object.keys(mapping));
184
-
185
- // To account for hard-coded keys that aren't part of the input
186
- safekeys.forEach((k) => allkeys.add(k));
187
-
188
- const rv = {};
189
- allkeys.forEach((k) => {
190
- if (safekeys.has(k)) {
191
- const value = safepart[k];
192
- if (value !== undefined) {
193
- rv[k] = value;
194
- }
195
- } else {
196
- rv[k] = blob[k];
197
- }
198
- });
199
- return rv;
200
- });
201
- return decoder(blob);
202
- });
203
- }