decoders 1.26.0-test1 → 2.0.0-beta12

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 (128) hide show
  1. package/CHANGELOG.md +53 -3
  2. package/Decoder.d.ts +28 -0
  3. package/Decoder.js +223 -0
  4. package/Decoder.js.flow +250 -0
  5. package/Decoder.mjs +216 -0
  6. package/NotSupportedTSVersion.d.ts +1 -0
  7. package/README.md +105 -961
  8. package/_utils.d.ts +9 -0
  9. package/_utils.js +102 -0
  10. package/_utils.js.flow +93 -0
  11. package/_utils.mjs +89 -0
  12. package/annotate.d.ts +62 -0
  13. package/annotate.js +161 -0
  14. package/annotate.js.flow +218 -0
  15. package/annotate.mjs +144 -0
  16. package/format.d.ts +4 -0
  17. package/format.js +151 -0
  18. package/format.js.flow +126 -0
  19. package/format.mjs +140 -0
  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/helpers.d.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 +38 -0
  30. package/lib/basics.js +134 -0
  31. package/lib/basics.js.flow +113 -0
  32. package/lib/basics.mjs +110 -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} +11 -6
  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 -25
  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/array.js +0 -133
  71. package/cjs/array.js.flow +0 -106
  72. package/cjs/boolean.js +0 -42
  73. package/cjs/boolean.js.flow +0 -28
  74. package/cjs/constants.js +0 -67
  75. package/cjs/constants.js.flow +0 -45
  76. package/cjs/date.js +0 -42
  77. package/cjs/date.js.flow +0 -38
  78. package/cjs/describe.js +0 -22
  79. package/cjs/describe.js.flow +0 -17
  80. package/cjs/dispatch.js +0 -58
  81. package/cjs/dispatch.js.flow +0 -57
  82. package/cjs/either.js +0 -85
  83. package/cjs/either.js.flow +0 -131
  84. package/cjs/fail.js +0 -19
  85. package/cjs/fail.js.flow +0 -13
  86. package/cjs/guard.js +0 -30
  87. package/cjs/guard.js.flow +0 -36
  88. package/cjs/index.js +0 -397
  89. package/cjs/index.js.flow +0 -56
  90. package/cjs/instanceOf.js +0 -17
  91. package/cjs/instanceOf.js.flow +0 -21
  92. package/cjs/json.js +0 -33
  93. package/cjs/json.js.flow +0 -28
  94. package/cjs/lazy.js +0 -18
  95. package/cjs/lazy.js.flow +0 -15
  96. package/cjs/mapping.js +0 -113
  97. package/cjs/mapping.js.flow +0 -71
  98. package/cjs/number.js +0 -38
  99. package/cjs/number.js.flow +0 -35
  100. package/cjs/object.js +0 -254
  101. package/cjs/object.js.flow +0 -211
  102. package/cjs/optional.js +0 -52
  103. package/cjs/optional.js.flow +0 -42
  104. package/cjs/string.js +0 -93
  105. package/cjs/string.js.flow +0 -81
  106. package/cjs/tuple.js +0 -199
  107. package/cjs/tuple.js.flow +0 -221
  108. package/cjs/types.js +0 -5
  109. package/cjs/types.js.flow +0 -26
  110. package/cjs/utils.js +0 -70
  111. package/cjs/utils.js.flow +0 -58
  112. package/es/index.js +0 -1212
  113. package/ts/array.d.ts +0 -5
  114. package/ts/constants.d.ts +0 -11
  115. package/ts/describe.d.ts +0 -3
  116. package/ts/dispatch.d.ts +0 -8
  117. package/ts/either.d.ts +0 -61
  118. package/ts/fail.d.ts +0 -3
  119. package/ts/guard.d.ts +0 -7
  120. package/ts/index.d.ts +0 -38
  121. package/ts/instanceOf.d.ts +0 -3
  122. package/ts/lazy.d.ts +0 -3
  123. package/ts/mapping.d.ts +0 -4
  124. package/ts/optional.d.ts +0 -5
  125. package/ts/string.d.ts +0 -7
  126. package/ts/tuple.d.ts +0 -30
  127. package/ts/types.d.ts +0 -18
  128. package/ts/utils.d.ts +0 -13
package/lib/arrays.mjs ADDED
@@ -0,0 +1,123 @@
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
+ /**
91
+ * Accepts a tuple (an array with exactly _n_ items) of values accepted by the
92
+ * _n_ given decoders.
93
+ */
94
+ function _tuple() {
95
+ for (var _len = arguments.length, decoders = new Array(_len), _key = 0; _key < _len; _key++) {
96
+ decoders[_key] = arguments[_key];
97
+ }
98
+
99
+ return ntuple(decoders.length).then(function (blobs, ok, err) {
100
+ var allOk = true;
101
+ var rvs = decoders.map(function (decoder, i) {
102
+ var blob = blobs[i];
103
+ var result = decoder.decode(blob);
104
+
105
+ if (result.ok) {
106
+ return result.value;
107
+ } else {
108
+ allOk = false;
109
+ return result.error;
110
+ }
111
+ });
112
+
113
+ if (allOk) {
114
+ return ok(rvs);
115
+ } else {
116
+ // If a decoder error has happened while unwrapping all the
117
+ // results, try to construct a good error message
118
+ return err(annotate(rvs));
119
+ }
120
+ });
121
+ }
122
+
123
+ export var tuple = _tuple;
@@ -0,0 +1,38 @@
1
+ import { Decoder, Scalar } from '../Decoder';
2
+
3
+ export const null_: Decoder<null>;
4
+ export const undefined_: Decoder<undefined>;
5
+ export function optional<T>(decoder: Decoder<T>): Decoder<T | undefined>;
6
+ export function optional<T, V extends Scalar>(
7
+ decoder: Decoder<T>,
8
+ defaultValue: V,
9
+ ): Decoder<NonNullable<T> | V>;
10
+ export function optional<T, V>(
11
+ decoder: Decoder<T>,
12
+ defaultValue: V,
13
+ ): Decoder<NonNullable<T> | V>;
14
+ export function nullable<T>(decoder: Decoder<T>): Decoder<T | null>;
15
+ export function nullable<T, V extends Scalar>(
16
+ decoder: Decoder<T>,
17
+ defaultValue: V,
18
+ ): Decoder<NonNullable<T> | V>;
19
+ export function nullable<T, V>(
20
+ decoder: Decoder<T>,
21
+ defaultValue: V,
22
+ ): Decoder<NonNullable<T> | V>;
23
+ export function maybe<T>(decoder: Decoder<T>): Decoder<T | null | undefined>;
24
+ export function maybe<T, V extends Scalar>(
25
+ decoder: Decoder<T>,
26
+ defaultValue: V,
27
+ ): Decoder<NonNullable<T> | V>;
28
+ export function maybe<T, V>(
29
+ decoder: Decoder<T>,
30
+ defaultValue: V,
31
+ ): Decoder<NonNullable<T> | V>;
32
+ export function constant<T extends Scalar>(value: T): Decoder<T>;
33
+ export function always<T extends Scalar>(value: T): Decoder<T>;
34
+ export function always<T>(value: T): Decoder<T>;
35
+ export function hardcoded<T extends Scalar>(value: T): Decoder<T>;
36
+ export function hardcoded<T>(value: T): Decoder<T>;
37
+ export const mixed: Decoder<unknown>;
38
+ export const unknown: Decoder<unknown>;
package/lib/basics.js ADDED
@@ -0,0 +1,134 @@
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
+ /**
33
+ * Accepts whatever the given decoder accepts, or `null`, or `undefined`.
34
+ */
35
+ function _maybeish(emptyCase) {
36
+ function _inner(decoder
37
+ /* defaultValue */
38
+ ) {
39
+ var _arguments = arguments;
40
+ var rv = (0, _unions.either)(emptyCase, decoder);
41
+ return (// If a default value is provided...
42
+ arguments.length >= 2 ? // ...then return the default value
43
+ rv.transform(function (value) {
44
+ return value != null ? value : _arguments[1];
45
+ }) : // Otherwise the "normal" empty case
46
+ rv
47
+ );
48
+ }
49
+
50
+ return _inner;
51
+ }
52
+ /**
53
+ * Accepts whatever the given decoder accepts, or `null`.
54
+ *
55
+ * If a default value is explicitly provided, return that instead in the `null`
56
+ * case.
57
+ */
58
+
59
+
60
+ var nullable = _maybeish(null_);
61
+ /**
62
+ * Accepts whatever the given decoder accepts, or `undefined`.
63
+ *
64
+ * If a default value is explicitly provided, return that instead in the
65
+ * `undefined` case.
66
+ */
67
+
68
+
69
+ exports.nullable = nullable;
70
+
71
+ var optional = _maybeish(undefined_);
72
+ /**
73
+ * Accepts whatever the given decoder accepts, or `null`, or `undefined`.
74
+ *
75
+ * If a default value is explicitly provided, return that instead in the
76
+ * `null`/`undefined` case.
77
+ */
78
+
79
+
80
+ exports.optional = optional;
81
+
82
+ var maybe = _maybeish(undefined_or_null);
83
+ /**
84
+ * Accepts only the given constant value.
85
+ */
86
+
87
+
88
+ exports.maybe = maybe;
89
+
90
+ function constant(value) {
91
+ return (0, _Decoder.define)(function (blob, ok, err) {
92
+ return blob === value ? ok(value) : err("Must be constant " + String(value));
93
+ });
94
+ }
95
+ /**
96
+ * Accepts anything, completely ignores it, and always returns the provided
97
+ * value instead.
98
+ *
99
+ * This is useful to manually add extra fields to object decoders.
100
+ */
101
+
102
+
103
+ function always(value) {
104
+ return (0, _Decoder.define)(function (blob
105
+ /* ignored */
106
+ , ok, _) {
107
+ return ok(value);
108
+ });
109
+ }
110
+ /**
111
+ * Alias of always.
112
+ */
113
+
114
+
115
+ var hardcoded = always;
116
+ /**
117
+ * Accepts anything and returns it unchanged.
118
+ *
119
+ * Useful for situation in which you don't know or expect a specific type. Of
120
+ * course, the downside is that you won't know the type of the value statically
121
+ * and you'll have to further refine it yourself.
122
+ */
123
+
124
+ exports.hardcoded = hardcoded;
125
+ var unknown = (0, _Decoder.define)(function (blob, ok, _) {
126
+ return ok(blob);
127
+ });
128
+ /**
129
+ * Alias of unknown.
130
+ */
131
+
132
+ exports.unknown = unknown;
133
+ var mixed = unknown;
134
+ exports.mixed = mixed;
@@ -0,0 +1,113 @@
1
+ // @flow strict
2
+
3
+ import { define } from '../Decoder';
4
+ import { either } from './unions';
5
+ import type { _Any } from '../_utils';
6
+ import type { Decoder, Scalar } from '../Decoder';
7
+
8
+ /**
9
+ * Accepts and returns only the literal `null` value.
10
+ */
11
+ export const null_: Decoder<null> = define((blob, ok, err) =>
12
+ blob === null ? ok(blob) : err('Must be null'),
13
+ );
14
+
15
+ /**
16
+ * Accepts and returns only the literal `undefined` value.
17
+ */
18
+ export const undefined_: Decoder<void> = define((blob, ok, err) =>
19
+ blob === undefined ? ok(blob) : err('Must be undefined'),
20
+ );
21
+
22
+ const undefined_or_null: Decoder<null | void> = define((blob, ok, err) =>
23
+ blob === undefined || blob === null
24
+ ? ok(blob)
25
+ : // Combine error message into a single line for readability
26
+ err('Must be undefined or null'),
27
+ );
28
+
29
+ interface Maybeish<E> {
30
+ <T>(decoder: Decoder<T>): Decoder<E | T>;
31
+ <T, V>(decoder: Decoder<T>, defaultValue: V): Decoder<$NonMaybeType<T> | V>;
32
+ }
33
+
34
+ /**
35
+ * Accepts whatever the given decoder accepts, or `null`, or `undefined`.
36
+ */
37
+ function _maybeish<E>(emptyCase: Decoder<E>): Maybeish<E> {
38
+ function _inner(decoder /* defaultValue */) {
39
+ const rv = either(emptyCase, decoder);
40
+ return (
41
+ // If a default value is provided...
42
+ arguments.length >= 2
43
+ ? // ...then return the default value
44
+ rv.transform((value) => value ?? arguments[1])
45
+ : // Otherwise the "normal" empty case
46
+ rv
47
+ );
48
+ }
49
+
50
+ return (_inner: _Any);
51
+ }
52
+
53
+ /**
54
+ * Accepts whatever the given decoder accepts, or `null`.
55
+ *
56
+ * If a default value is explicitly provided, return that instead in the `null`
57
+ * case.
58
+ */
59
+ export const nullable: Maybeish<null> = _maybeish(null_);
60
+
61
+ /**
62
+ * Accepts whatever the given decoder accepts, or `undefined`.
63
+ *
64
+ * If a default value is explicitly provided, return that instead in the
65
+ * `undefined` case.
66
+ */
67
+ export const optional: Maybeish<void> = _maybeish(undefined_);
68
+
69
+ /**
70
+ * Accepts whatever the given decoder accepts, or `null`, or `undefined`.
71
+ *
72
+ * If a default value is explicitly provided, return that instead in the
73
+ * `null`/`undefined` case.
74
+ */
75
+ export const maybe: Maybeish<null | void> = _maybeish(undefined_or_null);
76
+
77
+ /**
78
+ * Accepts only the given constant value.
79
+ */
80
+ export function constant<T: Scalar>(value: T): Decoder<T> {
81
+ return define((blob, ok, err) =>
82
+ blob === value ? ok(value) : err(`Must be constant ${String(value)}`),
83
+ );
84
+ }
85
+
86
+ /**
87
+ * Accepts anything, completely ignores it, and always returns the provided
88
+ * value instead.
89
+ *
90
+ * This is useful to manually add extra fields to object decoders.
91
+ */
92
+ export function always<T>(value: T): Decoder<T> {
93
+ return define((blob /* ignored */, ok, _) => ok(value));
94
+ }
95
+
96
+ /**
97
+ * Alias of always.
98
+ */
99
+ export const hardcoded: <T>(T) => Decoder<T> = always;
100
+
101
+ /**
102
+ * Accepts anything and returns it unchanged.
103
+ *
104
+ * Useful for situation in which you don't know or expect a specific type. Of
105
+ * course, the downside is that you won't know the type of the value statically
106
+ * and you'll have to further refine it yourself.
107
+ */
108
+ export const unknown: Decoder<mixed> = define((blob, ok, _) => ok(blob));
109
+
110
+ /**
111
+ * Alias of unknown.
112
+ */
113
+ export const mixed: Decoder<mixed> = unknown;
package/lib/basics.mjs ADDED
@@ -0,0 +1,110 @@
1
+ import { define } from '../Decoder.mjs';
2
+ import { either } from './unions.mjs';
3
+
4
+ /**
5
+ * Accepts and returns only the literal `null` value.
6
+ */
7
+ export var null_ = define(function (blob, ok, err) {
8
+ return blob === null ? ok(blob) : err('Must be null');
9
+ });
10
+ /**
11
+ * Accepts and returns only the literal `undefined` value.
12
+ */
13
+
14
+ export var undefined_ = define(function (blob, ok, err) {
15
+ return blob === undefined ? ok(blob) : err('Must be undefined');
16
+ });
17
+ var undefined_or_null = define(function (blob, ok, err) {
18
+ return blob === undefined || blob === null ? ok(blob) : // Combine error message into a single line for readability
19
+ err('Must be undefined or null');
20
+ });
21
+
22
+ /**
23
+ * Accepts whatever the given decoder accepts, or `null`, or `undefined`.
24
+ */
25
+ function _maybeish(emptyCase) {
26
+ function _inner(decoder
27
+ /* defaultValue */
28
+ ) {
29
+ var _arguments = arguments;
30
+ var rv = either(emptyCase, decoder);
31
+ return (// If a default value is provided...
32
+ arguments.length >= 2 ? // ...then return the default value
33
+ rv.transform(function (value) {
34
+ return value != null ? value : _arguments[1];
35
+ }) : // Otherwise the "normal" empty case
36
+ rv
37
+ );
38
+ }
39
+
40
+ return _inner;
41
+ }
42
+ /**
43
+ * Accepts whatever the given decoder accepts, or `null`.
44
+ *
45
+ * If a default value is explicitly provided, return that instead in the `null`
46
+ * case.
47
+ */
48
+
49
+
50
+ export var nullable = _maybeish(null_);
51
+ /**
52
+ * Accepts whatever the given decoder accepts, or `undefined`.
53
+ *
54
+ * If a default value is explicitly provided, return that instead in the
55
+ * `undefined` case.
56
+ */
57
+
58
+ export var optional = _maybeish(undefined_);
59
+ /**
60
+ * Accepts whatever the given decoder accepts, or `null`, or `undefined`.
61
+ *
62
+ * If a default value is explicitly provided, return that instead in the
63
+ * `null`/`undefined` case.
64
+ */
65
+
66
+ export var maybe = _maybeish(undefined_or_null);
67
+ /**
68
+ * Accepts only the given constant value.
69
+ */
70
+
71
+ export function constant(value) {
72
+ return define(function (blob, ok, err) {
73
+ return blob === value ? ok(value) : err("Must be constant " + String(value));
74
+ });
75
+ }
76
+ /**
77
+ * Accepts anything, completely ignores it, and always returns the provided
78
+ * value instead.
79
+ *
80
+ * This is useful to manually add extra fields to object decoders.
81
+ */
82
+
83
+ export function always(value) {
84
+ return define(function (blob
85
+ /* ignored */
86
+ , ok, _) {
87
+ return ok(value);
88
+ });
89
+ }
90
+ /**
91
+ * Alias of always.
92
+ */
93
+
94
+ export var hardcoded = always;
95
+ /**
96
+ * Accepts anything and returns it unchanged.
97
+ *
98
+ * Useful for situation in which you don't know or expect a specific type. Of
99
+ * course, the downside is that you won't know the type of the value statically
100
+ * and you'll have to further refine it yourself.
101
+ */
102
+
103
+ export var unknown = define(function (blob, ok, _) {
104
+ return ok(blob);
105
+ });
106
+ /**
107
+ * Alias of unknown.
108
+ */
109
+
110
+ export var mixed = unknown;
@@ -1,4 +1,4 @@
1
- import { Decoder } from './types';
1
+ import { Decoder } from '../Decoder';
2
2
 
3
3
  export const boolean: Decoder<boolean>;
4
4
  export const truthy: Decoder<boolean>;
@@ -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;