decoders 2.0.0-beta1 → 2.0.0-beta13

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 (165) hide show
  1. package/CHANGELOG.md +53 -5
  2. package/Decoder.d.ts +94 -0
  3. package/Decoder.js +222 -0
  4. package/Decoder.js.flow +286 -0
  5. package/Decoder.mjs +215 -0
  6. package/NotSupportedTSVersion.d.ts +1 -0
  7. package/README.md +124 -961
  8. package/_utils.d.ts +9 -0
  9. package/{cjs/_utils.js → _utils.js} +12 -18
  10. package/{cjs/_utils.js.flow → _utils.js.flow} +15 -19
  11. package/{es/_utils.js → _utils.mjs} +11 -15
  12. package/{ts/annotate.d.ts → annotate.d.ts} +25 -21
  13. package/{cjs/annotate.js → annotate.js} +0 -0
  14. package/{cjs/annotate.js.flow → annotate.js.flow} +0 -0
  15. package/{es/annotate.js → annotate.mjs} +0 -0
  16. package/format.d.ts +6 -0
  17. package/{cjs/format/inline.js → format.js} +7 -2
  18. package/{cjs/format/inline.js.flow → format.js.flow} +9 -3
  19. package/{es/format/inline.js → format.mjs} +5 -2
  20. package/index.d.ts +40 -0
  21. package/index.js +89 -0
  22. package/index.js.flow +44 -0
  23. package/index.mjs +11 -0
  24. package/{ts → lib}/_helpers.d.ts +0 -0
  25. package/lib/arrays.d.ts +59 -0
  26. package/lib/arrays.js +139 -0
  27. package/lib/arrays.js.flow +138 -0
  28. package/lib/arrays.mjs +124 -0
  29. package/lib/basics.d.ts +93 -0
  30. package/lib/basics.js +144 -0
  31. package/lib/basics.js.flow +124 -0
  32. package/lib/basics.mjs +120 -0
  33. package/lib/booleans.d.ts +16 -0
  34. package/lib/booleans.js +35 -0
  35. package/lib/booleans.js.flow +22 -0
  36. package/lib/booleans.mjs +25 -0
  37. package/lib/dates.d.ts +15 -0
  38. package/lib/dates.js +44 -0
  39. package/lib/dates.js.flow +40 -0
  40. package/lib/dates.mjs +34 -0
  41. package/lib/json.d.ts +35 -0
  42. package/lib/json.js +55 -0
  43. package/lib/json.js.flow +50 -0
  44. package/lib/json.mjs +40 -0
  45. package/lib/numbers.d.ts +31 -0
  46. package/lib/numbers.js +51 -0
  47. package/lib/numbers.js.flow +48 -0
  48. package/lib/numbers.mjs +41 -0
  49. package/lib/objects.d.ts +75 -0
  50. package/lib/objects.js +240 -0
  51. package/lib/objects.js.flow +246 -0
  52. package/lib/objects.mjs +223 -0
  53. package/lib/strings.d.ts +56 -0
  54. package/lib/strings.js +101 -0
  55. package/lib/strings.js.flow +90 -0
  56. package/lib/strings.mjs +82 -0
  57. package/lib/unions.d.ts +55 -0
  58. package/lib/unions.js +160 -0
  59. package/lib/unions.js.flow +155 -0
  60. package/lib/unions.mjs +146 -0
  61. package/lib/utilities.d.ts +34 -0
  62. package/lib/utilities.js +75 -0
  63. package/lib/utilities.js.flow +65 -0
  64. package/lib/utilities.mjs +60 -0
  65. package/package.json +79 -29
  66. package/result.d.ts +16 -0
  67. package/result.js +34 -0
  68. package/result.js.flow +26 -0
  69. package/result.mjs +27 -0
  70. package/cjs/_guard.js +0 -26
  71. package/cjs/_guard.js.flow +0 -20
  72. package/cjs/_types.js +0 -1
  73. package/cjs/_types.js.flow +0 -20
  74. package/cjs/format/index.js +0 -12
  75. package/cjs/format/index.js.flow +0 -4
  76. package/cjs/format/short.js +0 -10
  77. package/cjs/format/short.js.flow +0 -8
  78. package/cjs/index.js +0 -120
  79. package/cjs/index.js.flow +0 -63
  80. package/cjs/result.js +0 -172
  81. package/cjs/result.js.flow +0 -166
  82. package/cjs/stdlib/array.js +0 -108
  83. package/cjs/stdlib/array.js.flow +0 -103
  84. package/cjs/stdlib/boolean.js +0 -44
  85. package/cjs/stdlib/boolean.js.flow +0 -29
  86. package/cjs/stdlib/composition.js +0 -56
  87. package/cjs/stdlib/composition.js.flow +0 -43
  88. package/cjs/stdlib/constants.js +0 -69
  89. package/cjs/stdlib/constants.js.flow +0 -46
  90. package/cjs/stdlib/date.js +0 -46
  91. package/cjs/stdlib/date.js.flow +0 -40
  92. package/cjs/stdlib/describe.js +0 -26
  93. package/cjs/stdlib/describe.js.flow +0 -17
  94. package/cjs/stdlib/dispatch.js +0 -62
  95. package/cjs/stdlib/dispatch.js.flow +0 -58
  96. package/cjs/stdlib/either.js +0 -117
  97. package/cjs/stdlib/either.js.flow +0 -151
  98. package/cjs/stdlib/fail.js +0 -21
  99. package/cjs/stdlib/fail.js.flow +0 -12
  100. package/cjs/stdlib/instanceOf.js +0 -19
  101. package/cjs/stdlib/instanceOf.js.flow +0 -20
  102. package/cjs/stdlib/json.js +0 -31
  103. package/cjs/stdlib/json.js.flow +0 -28
  104. package/cjs/stdlib/lazy.js +0 -16
  105. package/cjs/stdlib/lazy.js.flow +0 -15
  106. package/cjs/stdlib/mapping.js +0 -67
  107. package/cjs/stdlib/mapping.js.flow +0 -54
  108. package/cjs/stdlib/number.js +0 -40
  109. package/cjs/stdlib/number.js.flow +0 -34
  110. package/cjs/stdlib/object.js +0 -194
  111. package/cjs/stdlib/object.js.flow +0 -203
  112. package/cjs/stdlib/optional.js +0 -54
  113. package/cjs/stdlib/optional.js.flow +0 -41
  114. package/cjs/stdlib/string.js +0 -98
  115. package/cjs/stdlib/string.js.flow +0 -82
  116. package/cjs/stdlib/tuple.js +0 -173
  117. package/cjs/stdlib/tuple.js.flow +0 -220
  118. package/es/_guard.js +0 -15
  119. package/es/_types.js +0 -0
  120. package/es/format/index.js +0 -2
  121. package/es/format/short.js +0 -4
  122. package/es/index.js +0 -37
  123. package/es/result.js +0 -139
  124. package/es/stdlib/array.js +0 -91
  125. package/es/stdlib/boolean.js +0 -28
  126. package/es/stdlib/composition.js +0 -42
  127. package/es/stdlib/constants.js +0 -46
  128. package/es/stdlib/date.js +0 -28
  129. package/es/stdlib/describe.js +0 -16
  130. package/es/stdlib/dispatch.js +0 -51
  131. package/es/stdlib/either.js +0 -90
  132. package/es/stdlib/fail.js +0 -11
  133. package/es/stdlib/instanceOf.js +0 -8
  134. package/es/stdlib/json.js +0 -15
  135. package/es/stdlib/lazy.js +0 -11
  136. package/es/stdlib/mapping.js +0 -54
  137. package/es/stdlib/number.js +0 -25
  138. package/es/stdlib/object.js +0 -175
  139. package/es/stdlib/optional.js +0 -38
  140. package/es/stdlib/string.js +0 -76
  141. package/es/stdlib/tuple.js +0 -155
  142. package/ts/_guard.d.ts +0 -7
  143. package/ts/_types.d.ts +0 -16
  144. package/ts/_utils.d.ts +0 -13
  145. package/ts/array.d.ts +0 -5
  146. package/ts/boolean.d.ts +0 -5
  147. package/ts/constants.d.ts +0 -11
  148. package/ts/date.d.ts +0 -4
  149. package/ts/describe.d.ts +0 -3
  150. package/ts/dispatch.d.ts +0 -8
  151. package/ts/either.d.ts +0 -61
  152. package/ts/fail.d.ts +0 -3
  153. package/ts/index.d.ts +0 -42
  154. package/ts/inline.d.ts +0 -3
  155. package/ts/instanceOf.d.ts +0 -3
  156. package/ts/json.d.ts +0 -11
  157. package/ts/lazy.d.ts +0 -3
  158. package/ts/mapping.d.ts +0 -4
  159. package/ts/number.d.ts +0 -6
  160. package/ts/object.d.ts +0 -33
  161. package/ts/optional.d.ts +0 -5
  162. package/ts/result.d.ts +0 -39
  163. package/ts/short.d.ts +0 -3
  164. package/ts/string.d.ts +0 -7
  165. package/ts/tuple.d.ts +0 -30
package/lib/arrays.js ADDED
@@ -0,0 +1,139 @@
1
+ "use strict";
2
+
3
+ exports.__esModule = true;
4
+ exports.array = array;
5
+ exports.nonEmptyArray = nonEmptyArray;
6
+ exports.poja = void 0;
7
+ exports.set = set;
8
+ exports.tuple = void 0;
9
+
10
+ var _annotate = require("../annotate");
11
+
12
+ var _Decoder = require("../Decoder");
13
+
14
+ /**
15
+ * Accepts any array, but doesn't validate its items further.
16
+ *
17
+ * "poja" means "plain old JavaScript array", a play on `pojo()`.
18
+ */
19
+ var poja = (0, _Decoder.define)(function (blob, ok, err) {
20
+ if (!Array.isArray(blob)) {
21
+ return err('Must be an array');
22
+ }
23
+
24
+ return ok( // NOTE: Since Flow 0.98, Array.isArray() returns $ReadOnlyArray<mixed>
25
+ // instead of Array<mixed>. For rationale, see
26
+ // https://github.com/facebook/flow/issues/7684. In this case, we
27
+ // don't want to output read-only types because it's up to the user of
28
+ // decoders to determine what they want to do with the decoded output.
29
+ // If they want to write items into the array, that's fine!
30
+ // The fastest way to turn a read-only array into a normal array in
31
+ // Javascript is to use .slice() on it, see this benchmark:
32
+ // http://jsben.ch/lO6C5
33
+ blob.slice());
34
+ });
35
+ /**
36
+ * Given an array of Result instances, loop over them all and return:
37
+ * - An [index, err] tuple, indicating the (index of the) first Err instance
38
+ * encountered; or
39
+ * - a new Ok with an array of all unwrapped Ok'ed values
40
+ */
41
+
42
+ exports.poja = poja;
43
+
44
+ function all(items, blobs, // TODO: Make this less ugly
45
+ ok, err) {
46
+ var results = [];
47
+
48
+ for (var index = 0; index < items.length; ++index) {
49
+ var result = items[index];
50
+
51
+ if (result.ok) {
52
+ results.push(result.value);
53
+ } else {
54
+ var ann = result.error; // Rewrite the annotation to include the index information, and inject it into the original blob
55
+
56
+ var clone = [].concat(blobs);
57
+ clone.splice(index, 1, (0, _annotate.annotate)(ann, ann.text ? ann.text + " (at index " + index + ")" : "index " + index));
58
+ return err((0, _annotate.annotate)(clone));
59
+ }
60
+ }
61
+
62
+ return ok(results);
63
+ }
64
+ /**
65
+ * Accepts arrays of whatever the given decoder accepts.
66
+ */
67
+
68
+
69
+ function array(decoder) {
70
+ return poja.then(function (blobs, ok, err) {
71
+ var results = blobs.map(decoder.decode);
72
+ return all(results, blobs, ok, err);
73
+ });
74
+ }
75
+ /**
76
+ * Like `array()`, but will reject arrays with 0 elements.
77
+ */
78
+
79
+
80
+ function nonEmptyArray(decoder) {
81
+ return array(decoder).refine(function (arr) {
82
+ return arr.length > 0;
83
+ }, 'Must be non-empty array');
84
+ }
85
+ /**
86
+ * Similar to `array()`, but returns the result as an [ES6
87
+ * Set](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Set).
88
+ */
89
+
90
+
91
+ function set(decoder) {
92
+ return array(decoder).transform(function (items) {
93
+ return new Set(items);
94
+ });
95
+ }
96
+
97
+ var ntuple = function ntuple(n) {
98
+ return poja.refine(function (arr) {
99
+ return arr.length === n;
100
+ }, "Must be a " + n + "-tuple");
101
+ }; // prettier-ignore
102
+
103
+
104
+ function _tuple() {
105
+ for (var _len = arguments.length, decoders = new Array(_len), _key = 0; _key < _len; _key++) {
106
+ decoders[_key] = arguments[_key];
107
+ }
108
+
109
+ return ntuple(decoders.length).then(function (blobs, ok, err) {
110
+ var allOk = true;
111
+ var rvs = decoders.map(function (decoder, i) {
112
+ var blob = blobs[i];
113
+ var result = decoder.decode(blob);
114
+
115
+ if (result.ok) {
116
+ return result.value;
117
+ } else {
118
+ allOk = false;
119
+ return result.error;
120
+ }
121
+ });
122
+
123
+ if (allOk) {
124
+ return ok(rvs);
125
+ } else {
126
+ // If a decoder error has happened while unwrapping all the
127
+ // results, try to construct a good error message
128
+ return err((0, _annotate.annotate)(rvs));
129
+ }
130
+ });
131
+ }
132
+ /**
133
+ * Accepts a tuple (an array with exactly _n_ items) of values accepted by the
134
+ * _n_ given decoders.
135
+ */
136
+
137
+
138
+ var tuple = _tuple;
139
+ exports.tuple = tuple;
@@ -0,0 +1,138 @@
1
+ // @flow strict
2
+
3
+ import { annotate } from '../annotate';
4
+ import { define } from '../Decoder';
5
+ import type { _Any } from '../_utils';
6
+ import type { Annotation } from '../annotate';
7
+ import type { Decoder, DecodeResult } from '../Decoder';
8
+
9
+ /**
10
+ * Accepts any array, but doesn't validate its items further.
11
+ *
12
+ * "poja" means "plain old JavaScript array", a play on `pojo()`.
13
+ */
14
+ export const poja: Decoder<Array<mixed>> = define((blob, ok, err) => {
15
+ if (!Array.isArray(blob)) {
16
+ return err('Must be an array');
17
+ }
18
+ return ok(
19
+ // NOTE: Since Flow 0.98, Array.isArray() returns $ReadOnlyArray<mixed>
20
+ // instead of Array<mixed>. For rationale, see
21
+ // https://github.com/facebook/flow/issues/7684. In this case, we
22
+ // don't want to output read-only types because it's up to the user of
23
+ // decoders to determine what they want to do with the decoded output.
24
+ // If they want to write items into the array, that's fine!
25
+ // The fastest way to turn a read-only array into a normal array in
26
+ // Javascript is to use .slice() on it, see this benchmark:
27
+ // http://jsben.ch/lO6C5
28
+ blob.slice(),
29
+ );
30
+ });
31
+
32
+ /**
33
+ * Given an array of Result instances, loop over them all and return:
34
+ * - An [index, err] tuple, indicating the (index of the) first Err instance
35
+ * encountered; or
36
+ * - a new Ok with an array of all unwrapped Ok'ed values
37
+ */
38
+ function all<T>(
39
+ items: $ReadOnlyArray<DecodeResult<T>>,
40
+ blobs: $ReadOnlyArray<mixed>,
41
+
42
+ // TODO: Make this less ugly
43
+ ok: (Array<T>) => DecodeResult<Array<T>>,
44
+ err: (Annotation) => DecodeResult<Array<T>>,
45
+ ): DecodeResult<Array<T>> {
46
+ const results: Array<T> = [];
47
+ for (let index = 0; index < items.length; ++index) {
48
+ const result = items[index];
49
+ if (result.ok) {
50
+ results.push(result.value);
51
+ } else {
52
+ const ann = result.error;
53
+
54
+ // Rewrite the annotation to include the index information, and inject it into the original blob
55
+ const clone = [...blobs];
56
+ clone.splice(
57
+ index,
58
+ 1,
59
+ annotate(
60
+ ann,
61
+ ann.text ? `${ann.text} (at index ${index})` : `index ${index}`,
62
+ ),
63
+ );
64
+
65
+ return err(annotate(clone));
66
+ }
67
+ }
68
+ return ok(results);
69
+ }
70
+
71
+ /**
72
+ * Accepts arrays of whatever the given decoder accepts.
73
+ */
74
+ export function array<T>(decoder: Decoder<T>): Decoder<Array<T>> {
75
+ return poja.then((blobs: $ReadOnlyArray<mixed>, ok, err) => {
76
+ const results = blobs.map(decoder.decode);
77
+ return all(results, blobs, ok, err);
78
+ });
79
+ }
80
+
81
+ /**
82
+ * Like `array()`, but will reject arrays with 0 elements.
83
+ */
84
+ export function nonEmptyArray<T>(decoder: Decoder<T>): Decoder<Array<T>> {
85
+ return array(decoder).refine((arr) => arr.length > 0, 'Must be non-empty array');
86
+ }
87
+
88
+ /**
89
+ * Similar to `array()`, but returns the result as an [ES6
90
+ * Set](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Set).
91
+ */
92
+ export function set<T>(decoder: Decoder<T>): Decoder<Set<T>> {
93
+ return array(decoder).transform((items) => new Set(items));
94
+ }
95
+
96
+ const ntuple = (n: number) =>
97
+ poja.refine((arr) => arr.length === n, `Must be a ${n}-tuple`);
98
+
99
+ // prettier-ignore
100
+ interface TupleT {
101
+ <A>(a: Decoder<A>): Decoder<[A]>;
102
+ <A, B>(a: Decoder<A>, b: Decoder<B>): Decoder<[A, B]>;
103
+ <A, B, C>(a: Decoder<A>, b: Decoder<B>, c: Decoder<C>): Decoder<[A, B, C]>;
104
+ <A, B, C, D>(a: Decoder<A>, b: Decoder<B>, c: Decoder<C>, d: Decoder<D>): Decoder<[A, B, C, D]>;
105
+ <A, B, C, D, E>(a: Decoder<A>, b: Decoder<B>, c: Decoder<C>, d: Decoder<D>, e: Decoder<E>): Decoder<[A, B, C, D, E]>;
106
+ <A, B, C, D, E, F>(a: Decoder<A>, b: Decoder<B>, c: Decoder<C>, d: Decoder<D>, e: Decoder<E>, f: Decoder<F>): Decoder<[A, B, C, D, E, F]>;
107
+ }
108
+
109
+ function _tuple(...decoders: $ReadOnlyArray<Decoder<mixed>>): Decoder<Array<mixed>> {
110
+ return ntuple(decoders.length).then((blobs, ok, err) => {
111
+ let allOk = true;
112
+
113
+ const rvs = decoders.map((decoder, i) => {
114
+ const blob = blobs[i];
115
+ const result = decoder.decode(blob);
116
+ if (result.ok) {
117
+ return result.value;
118
+ } else {
119
+ allOk = false;
120
+ return result.error;
121
+ }
122
+ });
123
+
124
+ if (allOk) {
125
+ return ok(rvs);
126
+ } else {
127
+ // If a decoder error has happened while unwrapping all the
128
+ // results, try to construct a good error message
129
+ return err(annotate(rvs));
130
+ }
131
+ });
132
+ }
133
+
134
+ /**
135
+ * Accepts a tuple (an array with exactly _n_ items) of values accepted by the
136
+ * _n_ given decoders.
137
+ */
138
+ export const tuple: TupleT = (_tuple: _Any);
package/lib/arrays.mjs ADDED
@@ -0,0 +1,124 @@
1
+ import { annotate } from '../annotate.mjs';
2
+ import { define } from '../Decoder.mjs';
3
+
4
+ /**
5
+ * Accepts any array, but doesn't validate its items further.
6
+ *
7
+ * "poja" means "plain old JavaScript array", a play on `pojo()`.
8
+ */
9
+ export var poja = define(function (blob, ok, err) {
10
+ if (!Array.isArray(blob)) {
11
+ return err('Must be an array');
12
+ }
13
+
14
+ return ok( // NOTE: Since Flow 0.98, Array.isArray() returns $ReadOnlyArray<mixed>
15
+ // instead of Array<mixed>. For rationale, see
16
+ // https://github.com/facebook/flow/issues/7684. In this case, we
17
+ // don't want to output read-only types because it's up to the user of
18
+ // decoders to determine what they want to do with the decoded output.
19
+ // If they want to write items into the array, that's fine!
20
+ // The fastest way to turn a read-only array into a normal array in
21
+ // Javascript is to use .slice() on it, see this benchmark:
22
+ // http://jsben.ch/lO6C5
23
+ blob.slice());
24
+ });
25
+ /**
26
+ * Given an array of Result instances, loop over them all and return:
27
+ * - An [index, err] tuple, indicating the (index of the) first Err instance
28
+ * encountered; or
29
+ * - a new Ok with an array of all unwrapped Ok'ed values
30
+ */
31
+
32
+ function all(items, blobs, // TODO: Make this less ugly
33
+ ok, err) {
34
+ var results = [];
35
+
36
+ for (var index = 0; index < items.length; ++index) {
37
+ var result = items[index];
38
+
39
+ if (result.ok) {
40
+ results.push(result.value);
41
+ } else {
42
+ var ann = result.error; // Rewrite the annotation to include the index information, and inject it into the original blob
43
+
44
+ var clone = [].concat(blobs);
45
+ clone.splice(index, 1, annotate(ann, ann.text ? ann.text + " (at index " + index + ")" : "index " + index));
46
+ return err(annotate(clone));
47
+ }
48
+ }
49
+
50
+ return ok(results);
51
+ }
52
+ /**
53
+ * Accepts arrays of whatever the given decoder accepts.
54
+ */
55
+
56
+
57
+ export function array(decoder) {
58
+ return poja.then(function (blobs, ok, err) {
59
+ var results = blobs.map(decoder.decode);
60
+ return all(results, blobs, ok, err);
61
+ });
62
+ }
63
+ /**
64
+ * Like `array()`, but will reject arrays with 0 elements.
65
+ */
66
+
67
+ export function nonEmptyArray(decoder) {
68
+ return array(decoder).refine(function (arr) {
69
+ return arr.length > 0;
70
+ }, 'Must be non-empty array');
71
+ }
72
+ /**
73
+ * Similar to `array()`, but returns the result as an [ES6
74
+ * Set](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Set).
75
+ */
76
+
77
+ export function set(decoder) {
78
+ return array(decoder).transform(function (items) {
79
+ return new Set(items);
80
+ });
81
+ }
82
+
83
+ var ntuple = function ntuple(n) {
84
+ return poja.refine(function (arr) {
85
+ return arr.length === n;
86
+ }, "Must be a " + n + "-tuple");
87
+ }; // prettier-ignore
88
+
89
+
90
+ function _tuple() {
91
+ for (var _len = arguments.length, decoders = new Array(_len), _key = 0; _key < _len; _key++) {
92
+ decoders[_key] = arguments[_key];
93
+ }
94
+
95
+ return ntuple(decoders.length).then(function (blobs, ok, err) {
96
+ var allOk = true;
97
+ var rvs = decoders.map(function (decoder, i) {
98
+ var blob = blobs[i];
99
+ var result = decoder.decode(blob);
100
+
101
+ if (result.ok) {
102
+ return result.value;
103
+ } else {
104
+ allOk = false;
105
+ return result.error;
106
+ }
107
+ });
108
+
109
+ if (allOk) {
110
+ return ok(rvs);
111
+ } else {
112
+ // If a decoder error has happened while unwrapping all the
113
+ // results, try to construct a good error message
114
+ return err(annotate(rvs));
115
+ }
116
+ });
117
+ }
118
+ /**
119
+ * Accepts a tuple (an array with exactly _n_ items) of values accepted by the
120
+ * _n_ given decoders.
121
+ */
122
+
123
+
124
+ export var tuple = _tuple;
@@ -0,0 +1,93 @@
1
+ import { Decoder, Scalar } from '../Decoder';
2
+
3
+ /**
4
+ * Accepts and returns only the literal `null` value.
5
+ */
6
+ export const null_: Decoder<null>;
7
+
8
+ /**
9
+ * Accepts and returns only the literal `undefined` value.
10
+ */
11
+ export const undefined_: Decoder<undefined>;
12
+
13
+ /**
14
+ * Accepts whatever the given decoder accepts, or `undefined`.
15
+ *
16
+ * If a default value is explicitly provided, return that instead in the
17
+ * `undefined` case.
18
+ */
19
+ export function optional<T>(decoder: Decoder<T>): Decoder<T | undefined>;
20
+ export function optional<T, V extends Scalar>(
21
+ decoder: Decoder<T>,
22
+ defaultValue: (() => V) | V,
23
+ ): Decoder<NonNullable<T> | V>;
24
+ export function optional<T, V>(
25
+ decoder: Decoder<T>,
26
+ defaultValue: (() => V) | V,
27
+ ): Decoder<NonNullable<T> | V>;
28
+
29
+ /**
30
+ * Accepts whatever the given decoder accepts, or `null`.
31
+ *
32
+ * If a default value is explicitly provided, return that instead in the `null`
33
+ * case.
34
+ */
35
+ export function nullable<T>(decoder: Decoder<T>): Decoder<T | null>;
36
+ export function nullable<T, V extends Scalar>(
37
+ decoder: Decoder<T>,
38
+ defaultValue: (() => V) | V,
39
+ ): Decoder<NonNullable<T> | V>;
40
+ export function nullable<T, V>(
41
+ decoder: Decoder<T>,
42
+ defaultValue: (() => V) | V,
43
+ ): Decoder<NonNullable<T> | V>;
44
+
45
+ /**
46
+ * Accepts whatever the given decoder accepts, or `null`, or `undefined`.
47
+ *
48
+ * If a default value is explicitly provided, return that instead in the
49
+ * `null`/`undefined` case.
50
+ */
51
+ export function maybe<T>(decoder: Decoder<T>): Decoder<T | null | undefined>;
52
+ export function maybe<T, V extends Scalar>(
53
+ decoder: Decoder<T>,
54
+ defaultValue: (() => V) | V,
55
+ ): Decoder<NonNullable<T> | V>;
56
+ export function maybe<T, V>(
57
+ decoder: Decoder<T>,
58
+ defaultValue: (() => V) | V,
59
+ ): Decoder<NonNullable<T> | V>;
60
+
61
+ /**
62
+ * Accepts only the given constant value.
63
+ */
64
+ export function constant<T extends Scalar>(value: T): Decoder<T>;
65
+
66
+ /**
67
+ * Accepts anything, completely ignores it, and always returns the provided
68
+ * value instead.
69
+ *
70
+ * This is useful to manually add extra fields to object decoders.
71
+ */
72
+ export function always<T extends Scalar>(value: T): Decoder<T>;
73
+ export function always<T>(value: (() => T) | T): Decoder<T>;
74
+
75
+ /**
76
+ * Alias of always.
77
+ */
78
+ export function hardcoded<T extends Scalar>(value: T): Decoder<T>;
79
+ export function hardcoded<T>(value: (() => T) | T): Decoder<T>;
80
+
81
+ /**
82
+ * Accepts anything and returns it unchanged.
83
+ *
84
+ * Useful for situation in which you don't know or expect a specific type. Of
85
+ * course, the downside is that you won't know the type of the value statically
86
+ * and you'll have to further refine it yourself.
87
+ */
88
+ export const unknown: Decoder<unknown>;
89
+
90
+ /**
91
+ * Alias of unknown.
92
+ */
93
+ export const mixed: Decoder<unknown>;
package/lib/basics.js ADDED
@@ -0,0 +1,144 @@
1
+ "use strict";
2
+
3
+ exports.__esModule = true;
4
+ exports.always = always;
5
+ exports.constant = constant;
6
+ exports.unknown = exports.undefined_ = exports.optional = exports.nullable = exports.null_ = exports.mixed = exports.maybe = exports.hardcoded = void 0;
7
+
8
+ var _Decoder = require("../Decoder");
9
+
10
+ var _unions = require("./unions");
11
+
12
+ /**
13
+ * Accepts and returns only the literal `null` value.
14
+ */
15
+ var null_ = (0, _Decoder.define)(function (blob, ok, err) {
16
+ return blob === null ? ok(blob) : err('Must be null');
17
+ });
18
+ /**
19
+ * Accepts and returns only the literal `undefined` value.
20
+ */
21
+
22
+ exports.null_ = null_;
23
+ var undefined_ = (0, _Decoder.define)(function (blob, ok, err) {
24
+ return blob === undefined ? ok(blob) : err('Must be undefined');
25
+ });
26
+ exports.undefined_ = undefined_;
27
+ var undefined_or_null = (0, _Decoder.define)(function (blob, ok, err) {
28
+ return blob === undefined || blob === null ? ok(blob) : // Combine error message into a single line for readability
29
+ err('Must be undefined or null');
30
+ });
31
+
32
+ function _maybeish(emptyCase) {
33
+ function _inner(decoder
34
+ /* defaultValue */
35
+ ) {
36
+ var rv = (0, _unions.either)(emptyCase, decoder);
37
+
38
+ if ( // If a default value is provided...
39
+ arguments.length >= 2) {
40
+ // ...then return the default value
41
+ var _defaultValue = arguments[1];
42
+
43
+ var _defaultValue2 = typeof _defaultValue === 'function' ? _defaultValue() : _defaultValue;
44
+
45
+ return rv.transform(function (value) {
46
+ return value != null ? value : _defaultValue2;
47
+ });
48
+ } else {
49
+ // Otherwise the "normal" empty case
50
+ return rv;
51
+ }
52
+ }
53
+
54
+ return _inner;
55
+ }
56
+ /**
57
+ * Accepts whatever the given decoder accepts, or `null`.
58
+ *
59
+ * If a default value is explicitly provided, return that instead in the `null`
60
+ * case.
61
+ */
62
+
63
+
64
+ var nullable = _maybeish(null_);
65
+ /**
66
+ * Accepts whatever the given decoder accepts, or `undefined`.
67
+ *
68
+ * If a default value is explicitly provided, return that instead in the
69
+ * `undefined` case.
70
+ */
71
+
72
+
73
+ exports.nullable = nullable;
74
+
75
+ var optional = _maybeish(undefined_);
76
+ /**
77
+ * Accepts whatever the given decoder accepts, or `null`, or `undefined`.
78
+ *
79
+ * If a default value is explicitly provided, return that instead in the
80
+ * `null`/`undefined` case.
81
+ */
82
+
83
+
84
+ exports.optional = optional;
85
+
86
+ var maybe = _maybeish(undefined_or_null);
87
+ /**
88
+ * Accepts only the given constant value.
89
+ */
90
+
91
+
92
+ exports.maybe = maybe;
93
+
94
+ function constant(value) {
95
+ return (0, _Decoder.define)(function (blob, ok, err) {
96
+ return blob === value ? ok(value) : err("Must be constant " + String(value));
97
+ });
98
+ }
99
+ /**
100
+ * Accepts anything, completely ignores it, and always returns the provided
101
+ * value instead.
102
+ *
103
+ * This is useful to manually add extra fields to object decoders.
104
+ */
105
+
106
+
107
+ function always(value) {
108
+ return (0, _Decoder.define)(typeof value === 'function' ? function (blob
109
+ /* ignored */
110
+ , ok, _) {
111
+ return (// $FlowFixMe[incompatible-use]
112
+ ok(value())
113
+ );
114
+ } : function (blob
115
+ /* ignored */
116
+ , ok, _) {
117
+ return ok(value);
118
+ });
119
+ }
120
+ /**
121
+ * Alias of always.
122
+ */
123
+
124
+
125
+ var hardcoded = always;
126
+ /**
127
+ * Accepts anything and returns it unchanged.
128
+ *
129
+ * Useful for situation in which you don't know or expect a specific type. Of
130
+ * course, the downside is that you won't know the type of the value statically
131
+ * and you'll have to further refine it yourself.
132
+ */
133
+
134
+ exports.hardcoded = hardcoded;
135
+ var unknown = (0, _Decoder.define)(function (blob, ok, _) {
136
+ return ok(blob);
137
+ });
138
+ /**
139
+ * Alias of unknown.
140
+ */
141
+
142
+ exports.unknown = unknown;
143
+ var mixed = unknown;
144
+ exports.mixed = mixed;