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/lib/basics.mjs ADDED
@@ -0,0 +1,75 @@
1
+ import { define } from '../Decoder.mjs'
2
+ import { either } from './unions.mjs'
3
+
4
+ export var null_ = define(function (blob, ok, err) {
5
+ return blob === null ? ok(blob) : err('Must be null')
6
+ })
7
+
8
+ export var undefined_ = define(function (blob, ok, err) {
9
+ return blob === undefined ? ok(blob) : err('Must be undefined')
10
+ })
11
+ var undefined_or_null = define(function (blob, ok, err) {
12
+ return blob === undefined || blob === null ? ok(blob) : err('Must be undefined or null')
13
+ })
14
+
15
+ function _maybeish(emptyCase) {
16
+ function _inner(decoder) {
17
+ var rv = either(emptyCase, decoder)
18
+
19
+ if (arguments.length >= 2) {
20
+ var _defaultValue = arguments[1]
21
+
22
+ var _defaultValue2 = typeof _defaultValue === 'function' ? _defaultValue() : _defaultValue
23
+
24
+ return rv.transform(function (value) {
25
+ return value != null ? value : _defaultValue2
26
+ })
27
+ } else {
28
+ return rv
29
+ }
30
+ }
31
+
32
+ return _inner
33
+ }
34
+
35
+ export var nullable = _maybeish(null_)
36
+
37
+ export var optional = _maybeish(undefined_)
38
+
39
+ export var maybe = _maybeish(undefined_or_null)
40
+
41
+ export function constant(value) {
42
+ return define(function (blob, ok, err) {
43
+ return blob === value ? ok(value) : err('Must be constant ' + String(value))
44
+ })
45
+ }
46
+
47
+ export function always(value) {
48
+ return define(
49
+ typeof value === 'function'
50
+ ? function (
51
+ blob,
52
+
53
+ ok,
54
+ _
55
+ ) {
56
+ return ok(value())
57
+ }
58
+ : function (
59
+ blob,
60
+
61
+ ok,
62
+ _
63
+ ) {
64
+ return ok(value)
65
+ }
66
+ )
67
+ }
68
+
69
+ export var hardcoded = always
70
+
71
+ export var unknown = define(function (blob, ok, _) {
72
+ return ok(blob)
73
+ })
74
+
75
+ export var mixed = unknown
@@ -0,0 +1,16 @@
1
+ import { Decoder } from '../Decoder';
2
+
3
+ /**
4
+ * Accepts and returns booleans.
5
+ */
6
+ export const boolean: Decoder<boolean>;
7
+
8
+ /**
9
+ * Accepts anything and will return its "truth" value. Will never reject.
10
+ */
11
+ export const truthy: Decoder<boolean>;
12
+
13
+ /**
14
+ * Accepts numbers, but return their boolean representation.
15
+ */
16
+ export const numericBoolean: Decoder<boolean>;
@@ -0,0 +1,25 @@
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
+ var _boolean = (0, _Decoder.define)(function (blob, ok, err) {
11
+ return typeof blob === 'boolean' ? ok(blob) : err('Must be boolean')
12
+ })
13
+
14
+ exports['boolean'] = _boolean
15
+ var truthy = (0, _Decoder.define)(function (blob, ok, _) {
16
+ return ok(!!blob)
17
+ })
18
+
19
+ exports.truthy = truthy
20
+
21
+ var numericBoolean = _numbers.number.transform(function (n) {
22
+ return !!n
23
+ })
24
+
25
+ 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,15 @@
1
+ import { define } from '../Decoder.mjs'
2
+ import { number } from './numbers.mjs'
3
+
4
+ var _boolean = define(function (blob, ok, err) {
5
+ return typeof blob === 'boolean' ? ok(blob) : err('Must be boolean')
6
+ })
7
+
8
+ export { _boolean as boolean }
9
+ export var truthy = define(function (blob, ok, _) {
10
+ return ok(!!blob)
11
+ })
12
+
13
+ export var numericBoolean = number.transform(function (n) {
14
+ return !!n
15
+ })
package/lib/dates.d.ts ADDED
@@ -0,0 +1,15 @@
1
+ import { Decoder } from '../Decoder';
2
+
3
+ /**
4
+ * Accepts and returns `Date` instances.
5
+ */
6
+ export const date: Decoder<Date>;
7
+
8
+ /**
9
+ * Accepts [ISO8601](https://en.wikipedia.org/wiki/ISO_8601)-formatted strings,
10
+ * returns them as `Date` instances.
11
+ *
12
+ * This is very useful for working with dates in APIs: serialize them as
13
+ * `.toISOString()` when sending, decode them with `iso8601` when receiving.
14
+ */
15
+ export const iso8601: Decoder<Date>;
package/lib/dates.js ADDED
@@ -0,0 +1,28 @@
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
+ var iso8601_re = /^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}(?:[.]\d+)?(?:Z|[+-]\d{2}:?\d{2})$/
12
+
13
+ var date = (0, _Decoder.define)(function (blob, ok, err) {
14
+ var date = (0, _utils.asDate)(blob)
15
+ return date !== null ? ok(date) : err('Must be a Date')
16
+ })
17
+
18
+ exports.date = date
19
+ var iso8601 = (0, _strings.regex)(iso8601_re, 'Must be ISO8601 format').transform(function (value) {
20
+ var date = new Date(value)
21
+
22
+ if (isNaN(date.getTime())) {
23
+ throw new Error('Must be valid date/time value')
24
+ }
25
+
26
+ return date
27
+ })
28
+ 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 them 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,19 @@
1
+ import { asDate } from '../_utils.mjs'
2
+ import { define } from '../Decoder.mjs'
3
+ import { regex } from './strings.mjs'
4
+ var iso8601_re = /^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}(?:[.]\d+)?(?:Z|[+-]\d{2}:?\d{2})$/
5
+
6
+ export var date = define(function (blob, ok, err) {
7
+ var date = asDate(blob)
8
+ return date !== null ? ok(date) : err('Must be a Date')
9
+ })
10
+
11
+ export var iso8601 = regex(iso8601_re, 'Must be ISO8601 format').transform(function (value) {
12
+ var date = new Date(value)
13
+
14
+ if (isNaN(date.getTime())) {
15
+ throw new Error('Must be valid date/time value')
16
+ }
17
+
18
+ return date
19
+ })
package/lib/json.d.ts ADDED
@@ -0,0 +1,35 @@
1
+ import { Decoder } from '../Decoder';
2
+
3
+ export type JSONValue = null | string | number | boolean | JSONObject | JSONArray;
4
+ export interface JSONObject {
5
+ [key: string]: JSONValue | undefined;
6
+ }
7
+ export type JSONArray = JSONValue[];
8
+
9
+ /**
10
+ * Accepts any value that's a valid JSON value.
11
+ *
12
+ * In other words: any value returned by `JSON.parse()` should decode without
13
+ * failure.
14
+ *
15
+ * ```typescript
16
+ * type JSONValue =
17
+ * | null
18
+ * | string
19
+ * | number
20
+ * | boolean
21
+ * | { [string]: JSONValue }
22
+ * | JSONValue[]
23
+ * ```
24
+ */
25
+ export const json: Decoder<JSONValue>;
26
+
27
+ /**
28
+ * Accepts arrays that contain only valid JSON values.
29
+ */
30
+ export const jsonArray: Decoder<JSONArray>;
31
+
32
+ /**
33
+ * Accepts objects that contain only valid JSON values.
34
+ */
35
+ export const jsonObject: Decoder<JSONObject>;
package/lib/json.js ADDED
@@ -0,0 +1,33 @@
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
+ var jsonObject = (0, _utilities.lazy)(function () {
23
+ return (0, _objects.dict)(json)
24
+ })
25
+
26
+ exports.jsonObject = jsonObject
27
+ var jsonArray = (0, _utilities.lazy)(function () {
28
+ return (0, _arrays.array)(json)
29
+ })
30
+
31
+ exports.jsonArray = jsonArray
32
+ var json = (0, _unions.either)(_basics.null_, _strings.string, _numbers.number, _booleans['boolean'], jsonObject, jsonArray).describe('Must be valid JSON value')
33
+ 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
+ * Accepts objects that contain only valid JSON values.
19
+ */
20
+ export const jsonObject: Decoder<JSONObject> = lazy(() => dict(json));
21
+
22
+ /**
23
+ * Accepts arrays that contain only valid JSON values.
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,18 @@
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
+ export var jsonObject = lazy(function () {
11
+ return dict(json)
12
+ })
13
+
14
+ export var jsonArray = lazy(function () {
15
+ return array(json)
16
+ })
17
+
18
+ export var json = either(null_, string, number, _boolean, jsonObject, jsonArray).describe('Must be valid JSON value')
@@ -0,0 +1,31 @@
1
+ import { Decoder } from '../Decoder';
2
+
3
+ /**
4
+ * Accepts any valid ``number`` value.
5
+ *
6
+ * This also accepts special values like `NaN` and `Infinity`. Unless you
7
+ * want to deliberately accept those, you'll likely want to use the
8
+ * `number` decoder instead.
9
+ */
10
+ export const anyNumber: Decoder<number>;
11
+
12
+ /**
13
+ * Accepts finite numbers (can be integer or float values). Values `NaN`,
14
+ * or positive and negative `Infinity` will get rejected.
15
+ */
16
+ export const number: Decoder<number>;
17
+
18
+ /**
19
+ * Accepts only finite whole numbers.
20
+ */
21
+ export const integer: Decoder<number>;
22
+
23
+ /**
24
+ * Accepts only positive finite numbers.
25
+ */
26
+ export const positiveNumber: Decoder<number>;
27
+
28
+ /**
29
+ * Accepts only positive finite whole numbers.
30
+ */
31
+ export const positiveInteger: Decoder<number>;
package/lib/numbers.js ADDED
@@ -0,0 +1,31 @@
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
+ var anyNumber = (0, _Decoder.define)(function (blob, ok, err) {
9
+ return typeof blob === 'number' ? ok(blob) : err('Must be number')
10
+ })
11
+
12
+ exports.anyNumber = anyNumber
13
+ var number = anyNumber.refine(function (n) {
14
+ return Number.isFinite(n)
15
+ }, 'Number must be finite')
16
+
17
+ exports.number = number
18
+ var integer = number.refine(function (n) {
19
+ return Number.isInteger(n)
20
+ }, 'Number must be an integer')
21
+
22
+ exports.integer = integer
23
+ var positiveNumber = number.refine(function (n) {
24
+ return n >= 0
25
+ }, 'Number must be positive')
26
+
27
+ exports.positiveNumber = positiveNumber
28
+ var positiveInteger = integer.refine(function (n) {
29
+ return n >= 0
30
+ }, 'Number must be positive')
31
+ exports.positiveInteger = positiveInteger
@@ -0,0 +1,48 @@
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
10
+ * want to deliberately accept those, you'll likely want to use the
11
+ * `number` decoder 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 `NaN`,
19
+ * or positive and negative `Infinity` will get rejected.
20
+ */
21
+ export const number: Decoder<number> = anyNumber.refine(
22
+ (n) => Number.isFinite(n),
23
+ 'Number must be finite',
24
+ );
25
+
26
+ /**
27
+ * Accepts only finite whole numbers.
28
+ */
29
+ export const integer: Decoder<number> = number.refine(
30
+ (n) => Number.isInteger(n),
31
+ 'Number must be an integer',
32
+ );
33
+
34
+ /**
35
+ * Accepts only positive finite numbers.
36
+ */
37
+ export const positiveNumber: Decoder<number> = number.refine(
38
+ (n) => n >= 0,
39
+ 'Number must be positive',
40
+ );
41
+
42
+ /**
43
+ * Accepts only positive finite whole numbers.
44
+ */
45
+ export const positiveInteger: Decoder<number> = integer.refine(
46
+ (n) => n >= 0,
47
+ 'Number must be positive',
48
+ );
@@ -0,0 +1,21 @@
1
+ import { define } from '../Decoder.mjs'
2
+
3
+ export var anyNumber = define(function (blob, ok, err) {
4
+ return typeof blob === 'number' ? ok(blob) : err('Must be number')
5
+ })
6
+
7
+ export var number = anyNumber.refine(function (n) {
8
+ return Number.isFinite(n)
9
+ }, 'Number must be finite')
10
+
11
+ export var integer = number.refine(function (n) {
12
+ return Number.isInteger(n)
13
+ }, 'Number must be an integer')
14
+
15
+ export var positiveNumber = number.refine(function (n) {
16
+ return n >= 0
17
+ }, 'Number must be positive')
18
+
19
+ export var positiveInteger = integer.refine(function (n) {
20
+ return n >= 0
21
+ }, 'Number must be positive')
@@ -0,0 +1,76 @@
1
+ /// <reference lib="es6" />
2
+
3
+ import { Decoder, DecoderType } from '../Decoder';
4
+ import { AllowImplicit } from './_helpers';
5
+
6
+ export type ObjectDecoderType<T> = AllowImplicit<{
7
+ [key in keyof T]: DecoderType<T[key]>;
8
+ }>;
9
+
10
+ /**
11
+ * Accepts any "plain old JavaScript object", but doesn't validate its keys or
12
+ * values further.
13
+ */
14
+ export const pojo: Decoder<Record<string, unknown>>;
15
+
16
+ /**
17
+ * Accepts objects with fields matching the given decoders. Extra fields that
18
+ * exist on the input object are ignored and will not be returned.
19
+ */
20
+ export function object(decodersByKey: Record<any, never>): Decoder<Record<string, never>>;
21
+ export function object<O extends Record<string, Decoder<any>>>(
22
+ decodersByKey: O,
23
+ ): Decoder<{ [K in keyof ObjectDecoderType<O>]: ObjectDecoderType<O>[K] }>;
24
+ // ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
25
+ // This is basically just equivalent to:
26
+ // ObjectDecoderType<O>
27
+ //
28
+ // But by "resolving" this with a mapped type, we remove the helper
29
+ // type names from the inferred type here, making this much easier to
30
+ // work with while developing.
31
+
32
+ /**
33
+ * Like `object()`, but will reject inputs that contain extra fields that are
34
+ * not specified explicitly.
35
+ */
36
+ export function exact(decodersByKey: Record<any, never>): Decoder<Record<string, never>>;
37
+ export function exact<O extends Record<string, Decoder<any>>>(
38
+ decodersByKey: O,
39
+ ): Decoder<{ [K in keyof ObjectDecoderType<O>]: ObjectDecoderType<O>[K] }>;
40
+ // ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
41
+ // Ditto (see above)
42
+
43
+ /**
44
+ * Like `object()`, but will pass through any extra fields on the input object
45
+ * unvalidated that will thus be of `unknown` type statically.
46
+ */
47
+ export function inexact(
48
+ decodersByKey: Record<any, never>,
49
+ ): Decoder<Record<string, unknown>>;
50
+ export function inexact<O extends Record<string, Decoder<any>>>(
51
+ decodersByKey: O,
52
+ ): Decoder<
53
+ { [K in keyof ObjectDecoderType<O>]: ObjectDecoderType<O>[K] } & Record<
54
+ string,
55
+ unknown
56
+ >
57
+ >;
58
+
59
+ /**
60
+ * Accepts objects where all values match the given decoder, and returns the
61
+ * result as a `Record<string, T>`.
62
+ *
63
+ * The main difference between `object()` and `dict()` is that you'd typically
64
+ * use `object()` if this is a record-like object, where all field names are
65
+ * known and the values are heterogeneous. Whereas with `dict()` the keys are
66
+ * typically dynamic and the values homogeneous, like in a dictionary,
67
+ * a lookup table, or a cache.
68
+ */
69
+ export function dict<T>(decoder: Decoder<T>): Decoder<Record<string, T>>;
70
+
71
+ /**
72
+ * Similar to `dict()`, but returns the result as a `Map<string, T>` (an [ES6
73
+ * Map](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Map))
74
+ * instead.
75
+ */
76
+ export function mapping<T>(decoder: Decoder<T>): Decoder<Map<string, T>>;