decoders 2.0.0-beta9 → 2.0.0

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 (136) hide show
  1. package/CHANGELOG.md +56 -29
  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/README.md +122 -1507
  7. package/_utils.d.ts +0 -1
  8. package/_utils.js +11 -17
  9. package/_utils.js.flow +13 -17
  10. package/_utils.mjs +10 -14
  11. package/format.d.ts +4 -2
  12. package/format.js +1 -1
  13. package/format.js.flow +3 -1
  14. package/format.mjs +1 -1
  15. package/index.d.ts +29 -31
  16. package/index.js +62 -84
  17. package/index.js.flow +30 -48
  18. package/index.mjs +11 -36
  19. package/{core → lib}/_helpers.d.ts +0 -0
  20. package/lib/arrays.d.ts +59 -0
  21. package/lib/arrays.js +139 -0
  22. package/lib/arrays.js.flow +138 -0
  23. package/lib/arrays.mjs +124 -0
  24. package/lib/basics.d.ts +93 -0
  25. package/lib/basics.js +144 -0
  26. package/lib/basics.js.flow +124 -0
  27. package/lib/basics.mjs +120 -0
  28. package/lib/booleans.d.ts +16 -0
  29. package/lib/booleans.js +35 -0
  30. package/lib/booleans.js.flow +22 -0
  31. package/lib/booleans.mjs +25 -0
  32. package/lib/dates.d.ts +15 -0
  33. package/lib/dates.js +44 -0
  34. package/lib/dates.js.flow +40 -0
  35. package/lib/dates.mjs +34 -0
  36. package/lib/json.d.ts +35 -0
  37. package/lib/json.js +55 -0
  38. package/lib/json.js.flow +50 -0
  39. package/lib/json.mjs +40 -0
  40. package/lib/numbers.d.ts +31 -0
  41. package/lib/numbers.js +51 -0
  42. package/lib/numbers.js.flow +48 -0
  43. package/lib/numbers.mjs +41 -0
  44. package/lib/objects.d.ts +75 -0
  45. package/{core/object.js → lib/objects.js} +78 -85
  46. package/{core/object.js.flow → lib/objects.js.flow} +89 -102
  47. package/{core/object.mjs → lib/objects.mjs} +77 -82
  48. package/lib/strings.d.ts +56 -0
  49. package/lib/strings.js +101 -0
  50. package/lib/strings.js.flow +90 -0
  51. package/lib/strings.mjs +82 -0
  52. package/lib/unions.d.ts +55 -0
  53. package/lib/unions.js +160 -0
  54. package/{core/either.js.flow → lib/unions.js.flow} +67 -17
  55. package/lib/unions.mjs +146 -0
  56. package/lib/utilities.d.ts +34 -0
  57. package/lib/utilities.js +75 -0
  58. package/lib/utilities.js.flow +65 -0
  59. package/lib/utilities.mjs +60 -0
  60. package/package.json +64 -17
  61. package/result.d.ts +0 -23
  62. package/result.js +0 -68
  63. package/result.js.flow +0 -72
  64. package/result.mjs +0 -54
  65. package/_guard.d.ts +0 -7
  66. package/_guard.js +0 -22
  67. package/_guard.js.flow +0 -20
  68. package/_guard.mjs +0 -15
  69. package/_types.d.ts +0 -13
  70. package/_types.js +0 -1
  71. package/_types.js.flow +0 -20
  72. package/_types.mjs +0 -0
  73. package/core/array.d.ts +0 -8
  74. package/core/array.js +0 -115
  75. package/core/array.js.flow +0 -107
  76. package/core/array.mjs +0 -100
  77. package/core/boolean.d.ts +0 -5
  78. package/core/boolean.js +0 -40
  79. package/core/boolean.js.flow +0 -27
  80. package/core/boolean.mjs +0 -28
  81. package/core/composition.d.ts +0 -18
  82. package/core/composition.js +0 -82
  83. package/core/composition.js.flow +0 -74
  84. package/core/composition.mjs +0 -70
  85. package/core/constants.d.ts +0 -11
  86. package/core/constants.js +0 -65
  87. package/core/constants.js.flow +0 -44
  88. package/core/constants.mjs +0 -46
  89. package/core/date.d.ts +0 -4
  90. package/core/date.js +0 -42
  91. package/core/date.js.flow +0 -38
  92. package/core/date.mjs +0 -28
  93. package/core/describe.d.ts +0 -3
  94. package/core/describe.js +0 -22
  95. package/core/describe.js.flow +0 -17
  96. package/core/describe.mjs +0 -16
  97. package/core/dispatch.d.ts +0 -8
  98. package/core/dispatch.js +0 -60
  99. package/core/dispatch.js.flow +0 -59
  100. package/core/dispatch.mjs +0 -52
  101. package/core/either.d.ts +0 -66
  102. package/core/either.js +0 -101
  103. package/core/either.mjs +0 -90
  104. package/core/fail.d.ts +0 -3
  105. package/core/fail.js +0 -17
  106. package/core/fail.js.flow +0 -12
  107. package/core/fail.mjs +0 -11
  108. package/core/instanceOf.d.ts +0 -3
  109. package/core/instanceOf.js +0 -15
  110. package/core/instanceOf.js.flow +0 -20
  111. package/core/instanceOf.mjs +0 -8
  112. package/core/json.d.ts +0 -11
  113. package/core/json.js +0 -31
  114. package/core/json.js.flow +0 -28
  115. package/core/json.mjs +0 -15
  116. package/core/lazy.d.ts +0 -3
  117. package/core/lazy.js +0 -16
  118. package/core/lazy.js.flow +0 -15
  119. package/core/lazy.mjs +0 -11
  120. package/core/number.d.ts +0 -6
  121. package/core/number.js +0 -36
  122. package/core/number.js.flow +0 -40
  123. package/core/number.mjs +0 -25
  124. package/core/object.d.ts +0 -38
  125. package/core/optional.d.ts +0 -5
  126. package/core/optional.js +0 -50
  127. package/core/optional.js.flow +0 -41
  128. package/core/optional.mjs +0 -38
  129. package/core/string.d.ts +0 -13
  130. package/core/string.js +0 -80
  131. package/core/string.js.flow +0 -72
  132. package/core/string.mjs +0 -58
  133. package/core/tuple.d.ts +0 -30
  134. package/core/tuple.js +0 -54
  135. package/core/tuple.js.flow +0 -51
  136. package/core/tuple.mjs +0 -45
@@ -1,59 +0,0 @@
1
- // @flow strict
2
-
3
- import { andThen } from '../result';
4
- import { object } from './object';
5
- import { oneOf } from './either';
6
- import { prep } from './composition';
7
- import type { _Any } from '../_utils';
8
- import type { Decoder, DecoderType } from '../_types';
9
-
10
- /**
11
- * Dispatches to one of several given decoders, based on the value found at
12
- * runtime in the given field. For example, suppose you have these decoders:
13
- *
14
- * const rectangle = object({
15
- * type: constant('rect'),
16
- * x: number,
17
- * y: number,
18
- * width: number,
19
- * height: number,
20
- * });
21
- *
22
- * const circle = object({
23
- * type: constant('circle'),
24
- * cx: number,
25
- * cy: number,
26
- * r: number,
27
- * });
28
- *
29
- * Then these two decoders are equivalent:
30
- *
31
- * const shape = either(rectangle, circle)
32
- * const shape = taggedUnion('type', { rectangle, circle })
33
- *
34
- * Will be of type Decoder<Rectangle | Circle>.
35
- *
36
- * But `taggedUnion` will typically be more runtime-efficient. The reason is
37
- * that it will first do minimal work to "look ahead" into the `type` field
38
- * here, and based on that value, pick the decoder to invoke.
39
- *
40
- * The `either` version will simply try to invoke each decoder, until it finds
41
- * one that matches.
42
- *
43
- * Also, the error messages will be less ambiguous using `taggedUnion()`.
44
- */
45
- export function taggedUnion<O: { +[field: string]: Decoder<_Any>, ... }>(
46
- field: string,
47
- mapping: O,
48
- ): Decoder<$Values<$ObjMap<O, DecoderType>>> {
49
- const base = object({
50
- [field]: prep(String, oneOf(Object.keys(mapping))),
51
- });
52
- return (blob: mixed) => {
53
- return andThen(base(blob), (baseObj) => {
54
- const decoderName = baseObj[field];
55
- const decoder = mapping[decoderName];
56
- return decoder(blob);
57
- });
58
- };
59
- }
package/core/dispatch.mjs DELETED
@@ -1,52 +0,0 @@
1
- import { andThen } from '../result.mjs';
2
- import { object } from './object.mjs';
3
- import { oneOf } from './either.mjs';
4
- import { prep } from './composition.mjs';
5
-
6
- /**
7
- * Dispatches to one of several given decoders, based on the value found at
8
- * runtime in the given field. For example, suppose you have these decoders:
9
- *
10
- * const rectangle = object({
11
- * type: constant('rect'),
12
- * x: number,
13
- * y: number,
14
- * width: number,
15
- * height: number,
16
- * });
17
- *
18
- * const circle = object({
19
- * type: constant('circle'),
20
- * cx: number,
21
- * cy: number,
22
- * r: number,
23
- * });
24
- *
25
- * Then these two decoders are equivalent:
26
- *
27
- * const shape = either(rectangle, circle)
28
- * const shape = taggedUnion('type', { rectangle, circle })
29
- *
30
- * Will be of type Decoder<Rectangle | Circle>.
31
- *
32
- * But `taggedUnion` will typically be more runtime-efficient. The reason is
33
- * that it will first do minimal work to "look ahead" into the `type` field
34
- * here, and based on that value, pick the decoder to invoke.
35
- *
36
- * The `either` version will simply try to invoke each decoder, until it finds
37
- * one that matches.
38
- *
39
- * Also, the error messages will be less ambiguous using `taggedUnion()`.
40
- */
41
- export function taggedUnion(field, mapping) {
42
- var _object;
43
-
44
- var base = object((_object = {}, _object[field] = prep(String, oneOf(Object.keys(mapping))), _object));
45
- return function (blob) {
46
- return andThen(base(blob), function (baseObj) {
47
- var decoderName = baseObj[field];
48
- var decoder = mapping[decoderName];
49
- return decoder(blob);
50
- });
51
- };
52
- }
package/core/either.d.ts DELETED
@@ -1,66 +0,0 @@
1
- import { Decoder, Scalar } from '../_types';
2
-
3
- export type DecoderTypes<T> = T extends ReadonlyArray<Decoder<infer U>> ? U : never;
4
-
5
- export function either<T extends ReadonlyArray<Decoder<any>>>(
6
- ...args: T
7
- ): Decoder<DecoderTypes<T>>;
8
- // export function either<T1, T2>(d1: Decoder<T1>, d2: Decoder<T2>): Decoder<T1 | T2>;
9
- // export function either2<T1, T2>(d1: Decoder<T1>, d2: Decoder<T2>): Decoder<T1 | T2>;
10
- // export function either3<T1, T2, T3>(
11
- // d1: Decoder<T1>,
12
- // d2: Decoder<T2>,
13
- // d3: Decoder<T3>,
14
- // ): Decoder<T1 | T2 | T3>;
15
- // export function either4<T1, T2, T3, T4>(
16
- // d1: Decoder<T1>,
17
- // d2: Decoder<T2>,
18
- // d3: Decoder<T3>,
19
- // d4: Decoder<T4>,
20
- // ): Decoder<T1 | T2 | T3 | T4>;
21
- // export function either5<T1, T2, T3, T4, T5>(
22
- // d1: Decoder<T1>,
23
- // d2: Decoder<T2>,
24
- // d3: Decoder<T3>,
25
- // d4: Decoder<T4>,
26
- // d5: Decoder<T5>,
27
- // ): Decoder<T1 | T2 | T3 | T4 | T5>;
28
- // export function either6<T1, T2, T3, T4, T5, T6>(
29
- // d1: Decoder<T1>,
30
- // d2: Decoder<T2>,
31
- // d3: Decoder<T3>,
32
- // d4: Decoder<T4>,
33
- // d5: Decoder<T5>,
34
- // d6: Decoder<T6>,
35
- // ): Decoder<T1 | T2 | T3 | T4 | T5 | T6>;
36
- // export function either7<T1, T2, T3, T4, T5, T6, T7>(
37
- // d1: Decoder<T1>,
38
- // d2: Decoder<T2>,
39
- // d3: Decoder<T3>,
40
- // d4: Decoder<T4>,
41
- // d5: Decoder<T5>,
42
- // d6: Decoder<T6>,
43
- // d7: Decoder<T7>,
44
- // ): Decoder<T1 | T2 | T3 | T4 | T5 | T6 | T7>;
45
- // export function either8<T1, T2, T3, T4, T5, T6, T7, T8>(
46
- // d1: Decoder<T1>,
47
- // d2: Decoder<T2>,
48
- // d3: Decoder<T3>,
49
- // d4: Decoder<T4>,
50
- // d5: Decoder<T5>,
51
- // d6: Decoder<T6>,
52
- // d7: Decoder<T7>,
53
- // d8: Decoder<T8>,
54
- // ): Decoder<T1 | T2 | T3 | T4 | T5 | T6 | T7 | T8>;
55
- // export function either9<T1, T2, T3, T4, T5, T6, T7, T8, T9>(
56
- // d1: Decoder<T1>,
57
- // d2: Decoder<T2>,
58
- // d3: Decoder<T3>,
59
- // d4: Decoder<T4>,
60
- // d5: Decoder<T5>,
61
- // d6: Decoder<T6>,
62
- // d7: Decoder<T7>,
63
- // d8: Decoder<T8>,
64
- // d9: Decoder<T9>,
65
- // ): Decoder<T1 | T2 | T3 | T4 | T5 | T6 | T7 | T8 | T9>;
66
- export function oneOf<T extends Scalar>(constants: readonly T[]): Decoder<T>;
package/core/either.js DELETED
@@ -1,101 +0,0 @@
1
- "use strict";
2
-
3
- exports.__esModule = true;
4
- exports.either = void 0;
5
- exports.oneOf = oneOf;
6
-
7
- var _annotate = require("../annotate");
8
-
9
- var _result = require("../result");
10
-
11
- var _utils = require("../_utils");
12
-
13
- var EITHER_PREFIX = 'Either:\n';
14
- /**
15
- * Indents and adds a dash in front of this (potentially multiline) string.
16
- */
17
-
18
- function itemize(s) {
19
- return '-' + (0, _utils.indent)(s).substring(1);
20
- }
21
- /**
22
- * Nests another error as an item under a new-to-be-created "Either error". If
23
- * the given subitem already is an "Either error" of itself, don't indent, but
24
- * just "inject" its items at the same error level, for nicely flattened either
25
- * expressions.
26
- *
27
- * Avoids:
28
- *
29
- * Either:
30
- * - Either:
31
- * - Must be P
32
- * - Either:
33
- * - Must be Q
34
- * - Must be R
35
- * - Must be S
36
- *
37
- * And "flattens" these to:
38
- *
39
- * Either:
40
- * - Must be P
41
- * - Must be Q
42
- * - Must be R
43
- * - Must be S
44
- *
45
- */
46
-
47
-
48
- function nest(errText) {
49
- return errText.startsWith(EITHER_PREFIX) ? errText.substr(EITHER_PREFIX.length) : itemize(errText);
50
- } // prettier-ignore
51
-
52
-
53
- function _either() {
54
- for (var _len = arguments.length, decoders = new Array(_len), _key = 0; _key < _len; _key++) {
55
- decoders[_key] = arguments[_key];
56
- }
57
-
58
- if (decoders.length === 0) {
59
- throw new Error('Pass at least one decoder to either()');
60
- }
61
-
62
- return function (blob) {
63
- // Collect errors here along the way
64
- var errors = [];
65
-
66
- for (var _i = 0; _i < decoders.length; _i++) {
67
- var result = decoders[_i](blob);
68
-
69
- if (result.ok) {
70
- return result;
71
- } else {
72
- errors.push(result.error);
73
- }
74
- } // Decoding all alternatives failed, return the combined error message
75
-
76
-
77
- var text = EITHER_PREFIX + errors.map(function (err) {
78
- return nest((0, _utils.summarize)(err).join('\n'));
79
- }).join('\n');
80
- return (0, _result.err)((0, _annotate.annotate)(blob, text));
81
- };
82
- }
83
-
84
- var either = _either;
85
- exports.either = either;
86
-
87
- function oneOf(constants) {
88
- return function (blob) {
89
- var winner = constants.find(function (c) {
90
- return c === blob;
91
- });
92
-
93
- if (winner !== undefined) {
94
- return (0, _result.ok)(winner);
95
- }
96
-
97
- return (0, _result.err)((0, _annotate.annotate)(blob, "Must be one of " + constants.map(function (value) {
98
- return JSON.stringify(value);
99
- }).join(', ')));
100
- };
101
- }
package/core/either.mjs DELETED
@@ -1,90 +0,0 @@
1
- import { annotate } from '../annotate.mjs';
2
- import { err, ok } from '../result.mjs';
3
- import { indent, summarize } from '../_utils.mjs';
4
- var EITHER_PREFIX = 'Either:\n';
5
- /**
6
- * Indents and adds a dash in front of this (potentially multiline) string.
7
- */
8
-
9
- function itemize(s) {
10
- return '-' + indent(s).substring(1);
11
- }
12
- /**
13
- * Nests another error as an item under a new-to-be-created "Either error". If
14
- * the given subitem already is an "Either error" of itself, don't indent, but
15
- * just "inject" its items at the same error level, for nicely flattened either
16
- * expressions.
17
- *
18
- * Avoids:
19
- *
20
- * Either:
21
- * - Either:
22
- * - Must be P
23
- * - Either:
24
- * - Must be Q
25
- * - Must be R
26
- * - Must be S
27
- *
28
- * And "flattens" these to:
29
- *
30
- * Either:
31
- * - Must be P
32
- * - Must be Q
33
- * - Must be R
34
- * - Must be S
35
- *
36
- */
37
-
38
-
39
- function nest(errText) {
40
- return errText.startsWith(EITHER_PREFIX) ? errText.substr(EITHER_PREFIX.length) : itemize(errText);
41
- } // prettier-ignore
42
-
43
-
44
- function _either() {
45
- for (var _len = arguments.length, decoders = new Array(_len), _key = 0; _key < _len; _key++) {
46
- decoders[_key] = arguments[_key];
47
- }
48
-
49
- if (decoders.length === 0) {
50
- throw new Error('Pass at least one decoder to either()');
51
- }
52
-
53
- return function (blob) {
54
- // Collect errors here along the way
55
- var errors = [];
56
-
57
- for (var _i = 0; _i < decoders.length; _i++) {
58
- var result = decoders[_i](blob);
59
-
60
- if (result.ok) {
61
- return result;
62
- } else {
63
- errors.push(result.error);
64
- }
65
- } // Decoding all alternatives failed, return the combined error message
66
-
67
-
68
- var text = EITHER_PREFIX + errors.map(function (err) {
69
- return nest(summarize(err).join('\n'));
70
- }).join('\n');
71
- return err(annotate(blob, text));
72
- };
73
- }
74
-
75
- export var either = _either;
76
- export function oneOf(constants) {
77
- return function (blob) {
78
- var winner = constants.find(function (c) {
79
- return c === blob;
80
- });
81
-
82
- if (winner !== undefined) {
83
- return ok(winner);
84
- }
85
-
86
- return err(annotate(blob, "Must be one of " + constants.map(function (value) {
87
- return JSON.stringify(value);
88
- }).join(', ')));
89
- };
90
- }
package/core/fail.d.ts DELETED
@@ -1,3 +0,0 @@
1
- import { Decoder } from '../_types';
2
-
3
- export function fail(msg: string): Decoder<never>;
package/core/fail.js DELETED
@@ -1,17 +0,0 @@
1
- "use strict";
2
-
3
- exports.__esModule = true;
4
- exports.fail = fail;
5
-
6
- var _annotate = require("../annotate");
7
-
8
- var _result = require("../result");
9
-
10
- /**
11
- * Decoder that always fails with the given error message, no matter what the input.
12
- */
13
- function fail(msg) {
14
- return function (blob) {
15
- return (0, _result.err)((0, _annotate.annotate)(blob, msg));
16
- };
17
- }
package/core/fail.js.flow DELETED
@@ -1,12 +0,0 @@
1
- // @flow strict
2
-
3
- import { annotate } from '../annotate';
4
- import { err } from '../result';
5
- import type { Decoder } from '../_types';
6
-
7
- /**
8
- * Decoder that always fails with the given error message, no matter what the input.
9
- */
10
- export function fail(msg: string): Decoder<empty> {
11
- return (blob: mixed) => err(annotate(blob, msg));
12
- }
package/core/fail.mjs DELETED
@@ -1,11 +0,0 @@
1
- import { annotate } from '../annotate.mjs';
2
- import { err } from '../result.mjs';
3
-
4
- /**
5
- * Decoder that always fails with the given error message, no matter what the input.
6
- */
7
- export function fail(msg) {
8
- return function (blob) {
9
- return err(annotate(blob, msg));
10
- };
11
- }
@@ -1,3 +0,0 @@
1
- import { Decoder } from '../_types';
2
-
3
- export function instanceOf<T>(klass: new (...args: readonly any[]) => T): Decoder<T>;
@@ -1,15 +0,0 @@
1
- "use strict";
2
-
3
- exports.__esModule = true;
4
- exports.instanceOf = instanceOf;
5
-
6
- var _annotate = require("../annotate");
7
-
8
- var _result = require("../result");
9
-
10
- function instanceOf(klass) {
11
- return function (blob) {
12
- return blob instanceof klass ? (0, _result.ok)(blob) : (0, _result.err)((0, _annotate.annotate)(blob, "Must be " + // $FlowFixMe[incompatible-use] - klass.name is fine?
13
- klass.name + " instance"));
14
- };
15
- }
@@ -1,20 +0,0 @@
1
- // @flow strict
2
-
3
- import { annotate } from '../annotate';
4
- import { err, ok } from '../result';
5
- import type { Decoder } from '../_types';
6
-
7
- export function instanceOf<T>(klass: Class<T>): Decoder<T> {
8
- return (blob: mixed) =>
9
- blob instanceof klass
10
- ? ok(blob)
11
- : err(
12
- annotate(
13
- blob,
14
- `Must be ${
15
- // $FlowFixMe[incompatible-use] - klass.name is fine?
16
- klass.name
17
- } instance`,
18
- ),
19
- );
20
- }
@@ -1,8 +0,0 @@
1
- import { annotate } from '../annotate.mjs';
2
- import { err, ok } from '../result.mjs';
3
- export function instanceOf(klass) {
4
- return function (blob) {
5
- return blob instanceof klass ? ok(blob) : err(annotate(blob, "Must be " + // $FlowFixMe[incompatible-use] - klass.name is fine?
6
- klass.name + " instance"));
7
- };
8
- }
package/core/json.d.ts DELETED
@@ -1,11 +0,0 @@
1
- import { Decoder } from '../_types';
2
-
3
- export type JSONValue = null | string | number | boolean | JSONObject | JSONArray;
4
- export interface JSONObject {
5
- [key: string]: JSONValue;
6
- }
7
- export type JSONArray = JSONValue[];
8
-
9
- export const json: Decoder<JSONValue>;
10
- export const jsonArray: Decoder<JSONArray>;
11
- export const jsonObject: Decoder<JSONObject>;
package/core/json.js DELETED
@@ -1,31 +0,0 @@
1
- "use strict";
2
-
3
- exports.__esModule = true;
4
- exports.jsonObject = exports.jsonArray = exports.json = void 0;
5
-
6
- var _array = require("./array");
7
-
8
- var _boolean2 = require("./boolean");
9
-
10
- var _object = require("./object");
11
-
12
- var _either = require("./either");
13
-
14
- var _lazy = require("./lazy");
15
-
16
- var _constants = require("./constants");
17
-
18
- var _number = require("./number");
19
-
20
- var _string = require("./string");
21
-
22
- var jsonObject = (0, _lazy.lazy)(function () {
23
- return (0, _object.dict)(json);
24
- });
25
- exports.jsonObject = jsonObject;
26
- var jsonArray = (0, _lazy.lazy)(function () {
27
- return (0, _array.array)(json);
28
- });
29
- exports.jsonArray = jsonArray;
30
- var json = (0, _either.either)(_constants.null_, _string.string, _number.number, _boolean2["boolean"], jsonObject, jsonArray);
31
- exports.json = json;
package/core/json.js.flow DELETED
@@ -1,28 +0,0 @@
1
- // @flow strict
2
-
3
- import { array } from './array';
4
- import { boolean } from './boolean';
5
- import { dict } from './object';
6
- import { either } from './either';
7
- import { lazy } from './lazy';
8
- import { null_ } from './constants';
9
- import { number } from './number';
10
- import { string } from './string';
11
- import type { Decoder } from '../_types';
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
- export const jsonObject: Decoder<JSONObject> = lazy(() => dict(json));
18
-
19
- export const jsonArray: Decoder<JSONArray> = lazy(() => array(json));
20
-
21
- export const json: Decoder<JSONValue> = either(
22
- null_,
23
- string,
24
- number,
25
- boolean,
26
- jsonObject,
27
- jsonArray,
28
- );
package/core/json.mjs DELETED
@@ -1,15 +0,0 @@
1
- import { array } from './array.mjs';
2
- import { boolean as _boolean } from './boolean.mjs';
3
- import { dict } from './object.mjs';
4
- import { either } from './either.mjs';
5
- import { lazy } from './lazy.mjs';
6
- import { null_ } from './constants.mjs';
7
- import { number } from './number.mjs';
8
- import { string } from './string.mjs';
9
- export var jsonObject = lazy(function () {
10
- return dict(json);
11
- });
12
- export var jsonArray = lazy(function () {
13
- return array(json);
14
- });
15
- export var json = either(null_, string, number, _boolean, jsonObject, jsonArray);
package/core/lazy.d.ts DELETED
@@ -1,3 +0,0 @@
1
- import { Decoder } from '../_types';
2
-
3
- export function lazy<T>(decoderFn: () => Decoder<T>): Decoder<T>;
package/core/lazy.js DELETED
@@ -1,16 +0,0 @@
1
- "use strict";
2
-
3
- exports.__esModule = true;
4
- exports.lazy = lazy;
5
-
6
- /**
7
- * Given an function returning a Decoder, will use that decoder to decode the
8
- * value. This is typically used to build decoders for recursive or
9
- * self-referential types.
10
- */
11
- function lazy(decoderFn) {
12
- return function (blob) {
13
- var decoder = decoderFn();
14
- return decoder(blob);
15
- };
16
- }
package/core/lazy.js.flow DELETED
@@ -1,15 +0,0 @@
1
- // @flow strict
2
-
3
- import type { Decoder } from '../_types';
4
-
5
- /**
6
- * Given an function returning a Decoder, will use that decoder to decode the
7
- * value. This is typically used to build decoders for recursive or
8
- * self-referential types.
9
- */
10
- export function lazy<T>(decoderFn: () => Decoder<T>): Decoder<T> {
11
- return (blob: mixed) => {
12
- const decoder = decoderFn();
13
- return decoder(blob);
14
- };
15
- }
package/core/lazy.mjs DELETED
@@ -1,11 +0,0 @@
1
- /**
2
- * Given an function returning a Decoder, will use that decoder to decode the
3
- * value. This is typically used to build decoders for recursive or
4
- * self-referential types.
5
- */
6
- export function lazy(decoderFn) {
7
- return function (blob) {
8
- var decoder = decoderFn();
9
- return decoder(blob);
10
- };
11
- }
package/core/number.d.ts DELETED
@@ -1,6 +0,0 @@
1
- import { Decoder } from '../_types';
2
-
3
- export const integer: Decoder<number>;
4
- export const number: Decoder<number>;
5
- export const positiveInteger: Decoder<number>;
6
- export const positiveNumber: Decoder<number>;
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;