decoders 2.0.0-beta1 → 2.0.0-beta10

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 (160) hide show
  1. package/CHANGELOG.md +30 -0
  2. package/Decoder.d.ts +27 -0
  3. package/Decoder.js +199 -0
  4. package/Decoder.js.flow +224 -0
  5. package/Decoder.mjs +192 -0
  6. package/NotSupportedTSVersion.d.ts +1 -0
  7. package/README.md +105 -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 +4 -0
  17. package/{cjs/format/inline.js → format.js} +7 -2
  18. package/{cjs/format/inline.js.flow → format.js.flow} +7 -3
  19. package/{es/format/inline.js → format.mjs} +5 -2
  20. package/index.d.ts +40 -0
  21. package/index.js +90 -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 +37 -0
  26. package/lib/arrays.js +138 -0
  27. package/lib/arrays.js.flow +138 -0
  28. package/lib/arrays.mjs +123 -0
  29. package/lib/basics.d.ts +14 -0
  30. package/lib/basics.js +109 -0
  31. package/lib/basics.js.flow +85 -0
  32. package/lib/basics.mjs +85 -0
  33. package/{ts/boolean.d.ts → lib/booleans.d.ts} +1 -1
  34. package/lib/booleans.js +35 -0
  35. package/lib/booleans.js.flow +22 -0
  36. package/lib/booleans.mjs +25 -0
  37. package/{ts/date.d.ts → lib/dates.d.ts} +1 -1
  38. package/lib/dates.js +44 -0
  39. package/lib/dates.js.flow +40 -0
  40. package/lib/dates.mjs +34 -0
  41. package/{ts → lib}/json.d.ts +1 -1
  42. package/lib/json.js +55 -0
  43. package/lib/json.js.flow +50 -0
  44. package/lib/json.mjs +40 -0
  45. package/{ts/number.d.ts → lib/numbers.d.ts} +2 -1
  46. package/lib/numbers.js +52 -0
  47. package/lib/numbers.js.flow +49 -0
  48. package/lib/numbers.mjs +42 -0
  49. package/{ts/object.d.ts → lib/objects.d.ts} +9 -4
  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 +13 -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 +78 -0
  58. package/lib/unions.js +161 -0
  59. package/lib/unions.js.flow +158 -0
  60. package/lib/unions.mjs +145 -0
  61. package/lib/utilities.d.ts +10 -0
  62. package/lib/utilities.js +94 -0
  63. package/lib/utilities.js.flow +84 -0
  64. package/lib/utilities.mjs +79 -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/constants.d.ts +0 -11
  147. package/ts/describe.d.ts +0 -3
  148. package/ts/dispatch.d.ts +0 -8
  149. package/ts/either.d.ts +0 -61
  150. package/ts/fail.d.ts +0 -3
  151. package/ts/index.d.ts +0 -42
  152. package/ts/inline.d.ts +0 -3
  153. package/ts/instanceOf.d.ts +0 -3
  154. package/ts/lazy.d.ts +0 -3
  155. package/ts/mapping.d.ts +0 -4
  156. package/ts/optional.d.ts +0 -5
  157. package/ts/result.d.ts +0 -39
  158. package/ts/short.d.ts +0 -3
  159. package/ts/string.d.ts +0 -7
  160. package/ts/tuple.d.ts +0 -30
@@ -0,0 +1,35 @@
1
+ "use strict";
2
+
3
+ exports.__esModule = true;
4
+ exports.truthy = exports.numericBoolean = exports["boolean"] = void 0;
5
+
6
+ var _Decoder = require("../Decoder");
7
+
8
+ var _numbers = require("./numbers");
9
+
10
+ /**
11
+ * Accepts and returns booleans.
12
+ */
13
+ var _boolean = (0, _Decoder.define)(function (blob, ok, err) {
14
+ return typeof blob === 'boolean' ? ok(blob) : err('Must be boolean');
15
+ });
16
+ /**
17
+ * Accepts anything and will return its “truth” value. Will never reject.
18
+ */
19
+
20
+
21
+ exports["boolean"] = _boolean;
22
+ var truthy = (0, _Decoder.define)(function (blob, ok, _) {
23
+ return ok(!!blob);
24
+ });
25
+ /**
26
+ * Accepts numbers, but return their boolean representation.
27
+ */
28
+
29
+ exports.truthy = truthy;
30
+
31
+ var numericBoolean = _numbers.number.transform(function (n) {
32
+ return !!n;
33
+ });
34
+
35
+ exports.numericBoolean = numericBoolean;
@@ -0,0 +1,22 @@
1
+ // @flow strict
2
+
3
+ import { define } from '../Decoder';
4
+ import { number } from './numbers';
5
+ import type { Decoder } from '../Decoder';
6
+
7
+ /**
8
+ * Accepts and returns booleans.
9
+ */
10
+ export const boolean: Decoder<boolean> = define((blob, ok, err) => {
11
+ return typeof blob === 'boolean' ? ok(blob) : err('Must be boolean');
12
+ });
13
+
14
+ /**
15
+ * Accepts anything and will return its “truth” value. Will never reject.
16
+ */
17
+ export const truthy: Decoder<boolean> = define((blob, ok, _) => ok(!!blob));
18
+
19
+ /**
20
+ * Accepts numbers, but return their boolean representation.
21
+ */
22
+ export const numericBoolean: Decoder<boolean> = number.transform((n) => !!n);
@@ -0,0 +1,25 @@
1
+ import { define } from '../Decoder.mjs';
2
+ import { number } from './numbers.mjs';
3
+
4
+ /**
5
+ * Accepts and returns booleans.
6
+ */
7
+ var _boolean = define(function (blob, ok, err) {
8
+ return typeof blob === 'boolean' ? ok(blob) : err('Must be boolean');
9
+ });
10
+ /**
11
+ * Accepts anything and will return its “truth” value. Will never reject.
12
+ */
13
+
14
+
15
+ export { _boolean as boolean };
16
+ export var truthy = define(function (blob, ok, _) {
17
+ return ok(!!blob);
18
+ });
19
+ /**
20
+ * Accepts numbers, but return their boolean representation.
21
+ */
22
+
23
+ export var numericBoolean = number.transform(function (n) {
24
+ return !!n;
25
+ });
@@ -1,4 +1,4 @@
1
- import { Decoder } from '../_types';
1
+ import { Decoder } from '../Decoder';
2
2
 
3
3
  export const date: Decoder<Date>;
4
4
  export const iso8601: Decoder<Date>;
package/lib/dates.js ADDED
@@ -0,0 +1,44 @@
1
+ "use strict";
2
+
3
+ exports.__esModule = true;
4
+ exports.iso8601 = exports.date = void 0;
5
+
6
+ var _utils = require("../_utils");
7
+
8
+ var _Decoder = require("../Decoder");
9
+
10
+ var _strings = require("./strings");
11
+
12
+ // Only matches the shape. This "over-matches" some values that still aren't
13
+ // valid dates (like 9999-99-99), but those will be caught by JS Date's
14
+ // internal validations
15
+ var iso8601_re = /^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}(?:[.]\d+)?(?:Z|[+-]\d{2}:?\d{2})$/;
16
+ /**
17
+ * Accepts and returns `Date` instances.
18
+ */
19
+
20
+ var date = (0, _Decoder.define)(function (blob, ok, err) {
21
+ var date = (0, _utils.asDate)(blob);
22
+ return date !== null ? ok(date) : err('Must be a Date');
23
+ });
24
+ /**
25
+ * Accepts [ISO8601](https://en.wikipedia.org/wiki/ISO_8601)-formatted strings,
26
+ * returns then as `Date` instances.
27
+ *
28
+ * This is very useful for working with dates in APIs: serialize them as
29
+ * `.toISOString()` when sending, decode them with `iso8601` when receiving.
30
+ */
31
+
32
+ exports.date = date;
33
+ var iso8601 = // Input itself needs to match the ISO8601 regex...
34
+ (0, _strings.regex)(iso8601_re, 'Must be ISO8601 format').transform( // Make sure it is a _valid_ date
35
+ function (value) {
36
+ var date = new Date(value);
37
+
38
+ if (isNaN(date.getTime())) {
39
+ throw new Error('Must be valid date/time value');
40
+ }
41
+
42
+ return date;
43
+ });
44
+ exports.iso8601 = iso8601;
@@ -0,0 +1,40 @@
1
+ // @flow strict
2
+
3
+ import { asDate } from '../_utils';
4
+ import { define } from '../Decoder';
5
+ import { regex } from './strings';
6
+ import type { Decoder } from '../Decoder';
7
+
8
+ // Only matches the shape. This "over-matches" some values that still aren't
9
+ // valid dates (like 9999-99-99), but those will be caught by JS Date's
10
+ // internal validations
11
+ const iso8601_re =
12
+ /^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}(?:[.]\d+)?(?:Z|[+-]\d{2}:?\d{2})$/;
13
+
14
+ /**
15
+ * Accepts and returns `Date` instances.
16
+ */
17
+ export const date: Decoder<Date> = define((blob, ok, err) => {
18
+ const date = asDate(blob);
19
+ return date !== null ? ok(date) : err('Must be a Date');
20
+ });
21
+
22
+ /**
23
+ * Accepts [ISO8601](https://en.wikipedia.org/wiki/ISO_8601)-formatted strings,
24
+ * returns then as `Date` instances.
25
+ *
26
+ * This is very useful for working with dates in APIs: serialize them as
27
+ * `.toISOString()` when sending, decode them with `iso8601` when receiving.
28
+ */
29
+ export const iso8601: Decoder<Date> =
30
+ // Input itself needs to match the ISO8601 regex...
31
+ regex(iso8601_re, 'Must be ISO8601 format').transform(
32
+ // Make sure it is a _valid_ date
33
+ (value: string) => {
34
+ const date = new Date(value);
35
+ if (isNaN(date.getTime())) {
36
+ throw new Error('Must be valid date/time value');
37
+ }
38
+ return date;
39
+ },
40
+ );
package/lib/dates.mjs ADDED
@@ -0,0 +1,34 @@
1
+ import { asDate } from '../_utils.mjs';
2
+ import { define } from '../Decoder.mjs';
3
+ import { regex } from './strings.mjs';
4
+ // Only matches the shape. This "over-matches" some values that still aren't
5
+ // valid dates (like 9999-99-99), but those will be caught by JS Date's
6
+ // internal validations
7
+ var iso8601_re = /^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}(?:[.]\d+)?(?:Z|[+-]\d{2}:?\d{2})$/;
8
+ /**
9
+ * Accepts and returns `Date` instances.
10
+ */
11
+
12
+ export var date = define(function (blob, ok, err) {
13
+ var date = asDate(blob);
14
+ return date !== null ? ok(date) : err('Must be a Date');
15
+ });
16
+ /**
17
+ * Accepts [ISO8601](https://en.wikipedia.org/wiki/ISO_8601)-formatted strings,
18
+ * returns then as `Date` instances.
19
+ *
20
+ * This is very useful for working with dates in APIs: serialize them as
21
+ * `.toISOString()` when sending, decode them with `iso8601` when receiving.
22
+ */
23
+
24
+ export var iso8601 = // Input itself needs to match the ISO8601 regex...
25
+ regex(iso8601_re, 'Must be ISO8601 format').transform( // Make sure it is a _valid_ date
26
+ function (value) {
27
+ var date = new Date(value);
28
+
29
+ if (isNaN(date.getTime())) {
30
+ throw new Error('Must be valid date/time value');
31
+ }
32
+
33
+ return date;
34
+ });
@@ -1,4 +1,4 @@
1
- import { Decoder } from '../_types';
1
+ import { Decoder } from '../Decoder';
2
2
 
3
3
  export type JSONValue = null | string | number | boolean | JSONObject | JSONArray;
4
4
  export interface JSONObject {
package/lib/json.js ADDED
@@ -0,0 +1,55 @@
1
+ "use strict";
2
+
3
+ exports.__esModule = true;
4
+ exports.jsonObject = exports.jsonArray = exports.json = void 0;
5
+
6
+ var _arrays = require("./arrays");
7
+
8
+ var _booleans = require("./booleans");
9
+
10
+ var _objects = require("./objects");
11
+
12
+ var _unions = require("./unions");
13
+
14
+ var _utilities = require("./utilities");
15
+
16
+ var _basics = require("./basics");
17
+
18
+ var _numbers = require("./numbers");
19
+
20
+ var _strings = require("./strings");
21
+
22
+ /**
23
+ * Like `json`, but will only decode when the JSON value is an object.
24
+ */
25
+ var jsonObject = (0, _utilities.lazy)(function () {
26
+ return (0, _objects.dict)(json);
27
+ });
28
+ /**
29
+ * Like `json`, but will only decode when the JSON value is an array.
30
+ */
31
+
32
+ exports.jsonObject = jsonObject;
33
+ var jsonArray = (0, _utilities.lazy)(function () {
34
+ return (0, _arrays.array)(json);
35
+ });
36
+ /**
37
+ * Accepts any value that's a valid JSON value.
38
+ *
39
+ * In other words: any value returned by `JSON.parse()` should decode without
40
+ * failure.
41
+ *
42
+ * ```typescript
43
+ * type JSONValue =
44
+ * | null
45
+ * | string
46
+ * | number
47
+ * | boolean
48
+ * | { [string]: JSONValue }
49
+ * | JSONValue[]
50
+ * ```
51
+ */
52
+
53
+ exports.jsonArray = jsonArray;
54
+ var json = (0, _unions.either)(_basics.null_, _strings.string, _numbers.number, _booleans["boolean"], jsonObject, jsonArray).describe('Must be valid JSON value');
55
+ exports.json = json;
@@ -0,0 +1,50 @@
1
+ // @flow strict
2
+
3
+ import { array } from './arrays';
4
+ import { boolean } from './booleans';
5
+ import { dict } from './objects';
6
+ import { either } from './unions';
7
+ import { lazy } from './utilities';
8
+ import { null_ } from './basics';
9
+ import { number } from './numbers';
10
+ import { string } from './strings';
11
+ import type { Decoder } from '../Decoder';
12
+
13
+ export type JSONValue = null | string | number | boolean | JSONObject | JSONArray;
14
+ export type JSONObject = { [string]: JSONValue };
15
+ export type JSONArray = Array<JSONValue>;
16
+
17
+ /**
18
+ * Like `json`, but will only decode when the JSON value is an object.
19
+ */
20
+ export const jsonObject: Decoder<JSONObject> = lazy(() => dict(json));
21
+
22
+ /**
23
+ * Like `json`, but will only decode when the JSON value is an array.
24
+ */
25
+ export const jsonArray: Decoder<JSONArray> = lazy(() => array(json));
26
+
27
+ /**
28
+ * Accepts any value that's a valid JSON value.
29
+ *
30
+ * In other words: any value returned by `JSON.parse()` should decode without
31
+ * failure.
32
+ *
33
+ * ```typescript
34
+ * type JSONValue =
35
+ * | null
36
+ * | string
37
+ * | number
38
+ * | boolean
39
+ * | { [string]: JSONValue }
40
+ * | JSONValue[]
41
+ * ```
42
+ */
43
+ export const json: Decoder<JSONValue> = either(
44
+ null_,
45
+ string,
46
+ number,
47
+ boolean,
48
+ jsonObject,
49
+ jsonArray,
50
+ ).describe('Must be valid JSON value');
package/lib/json.mjs ADDED
@@ -0,0 +1,40 @@
1
+ import { array } from './arrays.mjs';
2
+ import { boolean as _boolean } from './booleans.mjs';
3
+ import { dict } from './objects.mjs';
4
+ import { either } from './unions.mjs';
5
+ import { lazy } from './utilities.mjs';
6
+ import { null_ } from './basics.mjs';
7
+ import { number } from './numbers.mjs';
8
+ import { string } from './strings.mjs';
9
+
10
+ /**
11
+ * Like `json`, but will only decode when the JSON value is an object.
12
+ */
13
+ export var jsonObject = lazy(function () {
14
+ return dict(json);
15
+ });
16
+ /**
17
+ * Like `json`, but will only decode when the JSON value is an array.
18
+ */
19
+
20
+ export var jsonArray = lazy(function () {
21
+ return array(json);
22
+ });
23
+ /**
24
+ * Accepts any value that's a valid JSON value.
25
+ *
26
+ * In other words: any value returned by `JSON.parse()` should decode without
27
+ * failure.
28
+ *
29
+ * ```typescript
30
+ * type JSONValue =
31
+ * | null
32
+ * | string
33
+ * | number
34
+ * | boolean
35
+ * | { [string]: JSONValue }
36
+ * | JSONValue[]
37
+ * ```
38
+ */
39
+
40
+ export var json = either(null_, string, number, _boolean, jsonObject, jsonArray).describe('Must be valid JSON value');
@@ -1,5 +1,6 @@
1
- import { Decoder } from '../_types';
1
+ import { Decoder } from '../Decoder';
2
2
 
3
+ export const anyNumber: Decoder<number>;
3
4
  export const integer: Decoder<number>;
4
5
  export const number: Decoder<number>;
5
6
  export const positiveInteger: Decoder<number>;
package/lib/numbers.js ADDED
@@ -0,0 +1,52 @@
1
+ "use strict";
2
+
3
+ exports.__esModule = true;
4
+ exports.positiveNumber = exports.positiveInteger = exports.number = exports.integer = exports.anyNumber = void 0;
5
+
6
+ var _Decoder = require("../Decoder");
7
+
8
+ /**
9
+ * Accepts any valid ``number`` value.
10
+ *
11
+ * This also accepts special values like `NaN` and `Infinity`. Unless you want
12
+ * to deliberately accept those, you'll likely want to use the `number` decoder
13
+ * instead.
14
+ */
15
+ var anyNumber = (0, _Decoder.define)(function (blob, ok, err) {
16
+ return typeof blob === 'number' ? ok(blob) : err('Must be number');
17
+ });
18
+ /**
19
+ * Accepts finite numbers (can be integer or float values). Values
20
+ * `NaN`,
21
+ * or positive and negative `Infinity` will get rejected.
22
+ */
23
+
24
+ exports.anyNumber = anyNumber;
25
+ var number = anyNumber.refine(function (n) {
26
+ return Number.isFinite(n);
27
+ }, 'Number must be finite');
28
+ /**
29
+ * Accepts only finite whole numbers.
30
+ */
31
+
32
+ exports.number = number;
33
+ var integer = number.refine(function (n) {
34
+ return Number.isInteger(n);
35
+ }, 'Number must be an integer');
36
+ /**
37
+ * Accepts only positive finite numbers.
38
+ */
39
+
40
+ exports.integer = integer;
41
+ var positiveNumber = number.refine(function (n) {
42
+ return n >= 0;
43
+ }, 'Number must be positive');
44
+ /**
45
+ * Accepts only positive finite whole numbers.
46
+ */
47
+
48
+ exports.positiveNumber = positiveNumber;
49
+ var positiveInteger = integer.refine(function (n) {
50
+ return n >= 0;
51
+ }, 'Number must be positive');
52
+ exports.positiveInteger = positiveInteger;
@@ -0,0 +1,49 @@
1
+ // @flow strict
2
+
3
+ import { define } from '../Decoder';
4
+ import type { Decoder } from '../Decoder';
5
+
6
+ /**
7
+ * Accepts any valid ``number`` value.
8
+ *
9
+ * This also accepts special values like `NaN` and `Infinity`. Unless you want
10
+ * to deliberately accept those, you'll likely want to use the `number` decoder
11
+ * instead.
12
+ */
13
+ export const anyNumber: Decoder<number> = define((blob, ok, err) =>
14
+ typeof blob === 'number' ? ok(blob) : err('Must be number'),
15
+ );
16
+
17
+ /**
18
+ * Accepts finite numbers (can be integer or float values). Values
19
+ * `NaN`,
20
+ * or positive and negative `Infinity` will get rejected.
21
+ */
22
+ export const number: Decoder<number> = anyNumber.refine(
23
+ (n) => Number.isFinite(n),
24
+ 'Number must be finite',
25
+ );
26
+
27
+ /**
28
+ * Accepts only finite whole numbers.
29
+ */
30
+ export const integer: Decoder<number> = number.refine(
31
+ (n) => Number.isInteger(n),
32
+ 'Number must be an integer',
33
+ );
34
+
35
+ /**
36
+ * Accepts only positive finite numbers.
37
+ */
38
+ export const positiveNumber: Decoder<number> = number.refine(
39
+ (n) => n >= 0,
40
+ 'Number must be positive',
41
+ );
42
+
43
+ /**
44
+ * Accepts only positive finite whole numbers.
45
+ */
46
+ export const positiveInteger: Decoder<number> = integer.refine(
47
+ (n) => n >= 0,
48
+ 'Number must be positive',
49
+ );
@@ -0,0 +1,42 @@
1
+ import { define } from '../Decoder.mjs';
2
+
3
+ /**
4
+ * Accepts any valid ``number`` value.
5
+ *
6
+ * This also accepts special values like `NaN` and `Infinity`. Unless you want
7
+ * to deliberately accept those, you'll likely want to use the `number` decoder
8
+ * instead.
9
+ */
10
+ export var anyNumber = define(function (blob, ok, err) {
11
+ return typeof blob === 'number' ? ok(blob) : err('Must be number');
12
+ });
13
+ /**
14
+ * Accepts finite numbers (can be integer or float values). Values
15
+ * `NaN`,
16
+ * or positive and negative `Infinity` will get rejected.
17
+ */
18
+
19
+ export var number = anyNumber.refine(function (n) {
20
+ return Number.isFinite(n);
21
+ }, 'Number must be finite');
22
+ /**
23
+ * Accepts only finite whole numbers.
24
+ */
25
+
26
+ export var integer = number.refine(function (n) {
27
+ return Number.isInteger(n);
28
+ }, 'Number must be an integer');
29
+ /**
30
+ * Accepts only positive finite numbers.
31
+ */
32
+
33
+ export var positiveNumber = number.refine(function (n) {
34
+ return n >= 0;
35
+ }, 'Number must be positive');
36
+ /**
37
+ * Accepts only positive finite whole numbers.
38
+ */
39
+
40
+ export var positiveInteger = integer.refine(function (n) {
41
+ return n >= 0;
42
+ }, 'Number must be positive');
@@ -1,4 +1,6 @@
1
- import { Decoder, DecoderType } from '../_types';
1
+ /// <reference lib="es6" />
2
+
3
+ import { Decoder, DecoderType } from '../Decoder';
2
4
  import { AllowImplicit } from './_helpers';
3
5
 
4
6
  export type ObjectDecoderType<T> = AllowImplicit<{
@@ -8,7 +10,7 @@ export type ObjectDecoderType<T> = AllowImplicit<{
8
10
  export const pojo: Decoder<{ [key: string]: unknown }>;
9
11
 
10
12
  export function object<O extends { [key: string]: Decoder<any> }>(
11
- mapping: O,
13
+ decodersByKey: O,
12
14
  ): Decoder<{ [K in keyof ObjectDecoderType<O>]: ObjectDecoderType<O>[K] }>;
13
15
  // ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
14
16
  // This is basically just equivalent to:
@@ -19,15 +21,18 @@ export function object<O extends { [key: string]: Decoder<any> }>(
19
21
  // work with while developing.
20
22
 
21
23
  export function exact<O extends { [key: string]: Decoder<any> }>(
22
- mapping: O,
24
+ decodersByKey: O,
23
25
  ): Decoder<{ [K in keyof ObjectDecoderType<O>]: ObjectDecoderType<O>[K] }>;
24
26
  // ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
25
27
  // Ditto (see above)
26
28
 
27
29
  export function inexact<O extends { [key: string]: Decoder<any> }>(
28
- mapping: O,
30
+ decodersByKey: O,
29
31
  ): Decoder<
30
32
  { [K in keyof ObjectDecoderType<O>]: ObjectDecoderType<O>[K] } & {
31
33
  [extra: string]: unknown;
32
34
  }
33
35
  >;
36
+
37
+ export function mapping<T>(decoder: Decoder<T>): Decoder<Map<string, T>>;
38
+ export function dict<T>(decoder: Decoder<T>): Decoder<{ [key: string]: T }>;