decoders 2.0.0-beta12 → 2.0.0-beta13
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +53 -35
- package/Decoder.d.ts +71 -5
- package/Decoder.js +41 -42
- package/Decoder.js.flow +102 -66
- package/Decoder.mjs +41 -42
- package/README.md +81 -62
- package/format.d.ts +4 -2
- package/format.js.flow +2 -0
- package/index.d.ts +1 -1
- package/index.js +1 -2
- package/index.js.flow +1 -1
- package/index.mjs +1 -1
- package/lib/arrays.d.ts +22 -0
- package/lib/arrays.js +5 -4
- package/lib/arrays.js.flow +6 -6
- package/lib/arrays.mjs +5 -4
- package/lib/basics.d.ts +64 -9
- package/lib/basics.js +22 -12
- package/lib/basics.js.flow +23 -12
- package/lib/basics.mjs +22 -12
- package/lib/booleans.d.ts +11 -0
- package/lib/booleans.js +1 -1
- package/lib/booleans.js.flow +1 -1
- package/lib/booleans.mjs +1 -1
- package/lib/dates.d.ts +11 -0
- package/lib/dates.js +1 -1
- package/lib/dates.js.flow +1 -1
- package/lib/dates.mjs +1 -1
- package/lib/json.d.ts +24 -0
- package/lib/numbers.d.ts +26 -2
- package/lib/numbers.js +4 -5
- package/lib/numbers.js.flow +4 -5
- package/lib/numbers.mjs +4 -5
- package/lib/objects.d.ts +38 -1
- package/lib/strings.d.ts +43 -0
- package/lib/unions.d.ts +40 -63
- package/lib/unions.js +10 -11
- package/lib/unions.js.flow +10 -13
- package/lib/unions.mjs +9 -8
- package/lib/utilities.d.ts +24 -0
- package/lib/utilities.js +0 -19
- package/lib/utilities.js.flow +0 -19
- package/lib/utilities.mjs +0 -19
- package/package.json +1 -1
package/lib/unions.mjs
CHANGED
|
@@ -83,8 +83,8 @@ function _either() {
|
|
|
83
83
|
|
|
84
84
|
export var either = _either;
|
|
85
85
|
/**
|
|
86
|
-
* Accepts any value that is strictly-equal (using
|
|
87
|
-
* values.
|
|
86
|
+
* Accepts any value that is strictly-equal (using `===`) to one of the
|
|
87
|
+
* specified values.
|
|
88
88
|
*/
|
|
89
89
|
|
|
90
90
|
export function oneOf(constants) {
|
|
@@ -111,11 +111,13 @@ export function oneOf(constants) {
|
|
|
111
111
|
* unions](https://en.wikipedia.org/wiki/Tagged_union), i.e. a union of
|
|
112
112
|
* objects where one field is used as the discriminator.
|
|
113
113
|
*
|
|
114
|
-
*
|
|
115
|
-
*
|
|
114
|
+
* ```ts
|
|
115
|
+
* const A = object({ tag: constant('A'), foo: string });
|
|
116
|
+
* const B = object({ tag: constant('B'), bar: number });
|
|
116
117
|
*
|
|
117
|
-
*
|
|
118
|
-
*
|
|
118
|
+
* const AorB = taggedUnion('tag', { A, B });
|
|
119
|
+
* // ^^^
|
|
120
|
+
* ```
|
|
119
121
|
*
|
|
120
122
|
* Decoding now works in two steps:
|
|
121
123
|
*
|
|
@@ -141,5 +143,4 @@ export function taggedUnion(field, mapping) {
|
|
|
141
143
|
var decoder = mapping[key];
|
|
142
144
|
return decoder.decode(blob);
|
|
143
145
|
});
|
|
144
|
-
}
|
|
145
|
-
export var dispatch = taggedUnion;
|
|
146
|
+
}
|
package/lib/utilities.d.ts
CHANGED
|
@@ -1,10 +1,34 @@
|
|
|
1
1
|
import { Decoder } from '../Decoder';
|
|
2
2
|
|
|
3
|
+
/**
|
|
4
|
+
* Accepts any value that is an ``instanceof`` the given class.
|
|
5
|
+
*/
|
|
3
6
|
export function instanceOf<T>(klass: new (...args: readonly any[]) => T): Decoder<T>;
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* Lazily evaluate the given decoder. This is useful to build self-referential
|
|
10
|
+
* types for recursive data structures.
|
|
11
|
+
*/
|
|
4
12
|
export function lazy<T>(decoderFn: () => Decoder<T>): Decoder<T>;
|
|
13
|
+
|
|
14
|
+
/**
|
|
15
|
+
* Pre-process the data input before passing it into the decoder. This gives
|
|
16
|
+
* you the ability to arbitrarily customize the input on the fly before passing
|
|
17
|
+
* it to the decoder. Of course, the input value at that point is still of
|
|
18
|
+
* ``unknown`` type, so you will have to deal with that accordingly.
|
|
19
|
+
*/
|
|
5
20
|
export function prep<T>(
|
|
6
21
|
mapperFn: (blob: unknown) => unknown,
|
|
7
22
|
decoder: Decoder<T>,
|
|
8
23
|
): Decoder<T>;
|
|
24
|
+
|
|
25
|
+
/**
|
|
26
|
+
* Rejects all inputs, and always fails with the given error message. May be
|
|
27
|
+
* useful for explicitly disallowing keys, or for testing purposes.
|
|
28
|
+
*/
|
|
9
29
|
export function never(msg: string): Decoder<never>;
|
|
30
|
+
|
|
31
|
+
/**
|
|
32
|
+
* Alias of never().
|
|
33
|
+
*/
|
|
10
34
|
export function fail(msg: string): Decoder<never>;
|
package/lib/utilities.js
CHANGED
|
@@ -23,25 +23,6 @@ function instanceOf(klass) {
|
|
|
23
23
|
/**
|
|
24
24
|
* Lazily evaluate the given decoder. This is useful to build self-referential
|
|
25
25
|
* types for recursive data structures.
|
|
26
|
-
*
|
|
27
|
-
* Example:
|
|
28
|
-
*
|
|
29
|
-
* ```ts
|
|
30
|
-
* type Tree = {
|
|
31
|
-
* value: string;
|
|
32
|
-
* children: Array<Tree>;
|
|
33
|
-
* // ^^^^
|
|
34
|
-
* // Self-reference defining a recursive type
|
|
35
|
-
* };
|
|
36
|
-
*
|
|
37
|
-
* const treeDecoder: Decoder<Tree> = object({
|
|
38
|
-
* value: string,
|
|
39
|
-
* children: array(lazy(() => treeDecoder)),
|
|
40
|
-
* // ^^^^^^^^^^^^^^^^^^^^^^^
|
|
41
|
-
* // Use lazy() like this to refer to the treeDecoder which is
|
|
42
|
-
* // getting defined here
|
|
43
|
-
* });
|
|
44
|
-
* ```
|
|
45
26
|
*/
|
|
46
27
|
|
|
47
28
|
|
package/lib/utilities.js.flow
CHANGED
|
@@ -23,25 +23,6 @@ export function instanceOf<T>(klass: Class<T>): Decoder<T> {
|
|
|
23
23
|
/**
|
|
24
24
|
* Lazily evaluate the given decoder. This is useful to build self-referential
|
|
25
25
|
* types for recursive data structures.
|
|
26
|
-
*
|
|
27
|
-
* Example:
|
|
28
|
-
*
|
|
29
|
-
* ```ts
|
|
30
|
-
* type Tree = {
|
|
31
|
-
* value: string;
|
|
32
|
-
* children: Array<Tree>;
|
|
33
|
-
* // ^^^^
|
|
34
|
-
* // Self-reference defining a recursive type
|
|
35
|
-
* };
|
|
36
|
-
*
|
|
37
|
-
* const treeDecoder: Decoder<Tree> = object({
|
|
38
|
-
* value: string,
|
|
39
|
-
* children: array(lazy(() => treeDecoder)),
|
|
40
|
-
* // ^^^^^^^^^^^^^^^^^^^^^^^
|
|
41
|
-
* // Use lazy() like this to refer to the treeDecoder which is
|
|
42
|
-
* // getting defined here
|
|
43
|
-
* });
|
|
44
|
-
* ```
|
|
45
26
|
*/
|
|
46
27
|
export function lazy<T>(decoderFn: () => Decoder<T>): Decoder<T> {
|
|
47
28
|
return define((blob) => decoderFn().decode(blob));
|
package/lib/utilities.mjs
CHANGED
|
@@ -13,25 +13,6 @@ export function instanceOf(klass) {
|
|
|
13
13
|
/**
|
|
14
14
|
* Lazily evaluate the given decoder. This is useful to build self-referential
|
|
15
15
|
* types for recursive data structures.
|
|
16
|
-
*
|
|
17
|
-
* Example:
|
|
18
|
-
*
|
|
19
|
-
* ```ts
|
|
20
|
-
* type Tree = {
|
|
21
|
-
* value: string;
|
|
22
|
-
* children: Array<Tree>;
|
|
23
|
-
* // ^^^^
|
|
24
|
-
* // Self-reference defining a recursive type
|
|
25
|
-
* };
|
|
26
|
-
*
|
|
27
|
-
* const treeDecoder: Decoder<Tree> = object({
|
|
28
|
-
* value: string,
|
|
29
|
-
* children: array(lazy(() => treeDecoder)),
|
|
30
|
-
* // ^^^^^^^^^^^^^^^^^^^^^^^
|
|
31
|
-
* // Use lazy() like this to refer to the treeDecoder which is
|
|
32
|
-
* // getting defined here
|
|
33
|
-
* });
|
|
34
|
-
* ```
|
|
35
16
|
*/
|
|
36
17
|
|
|
37
18
|
export function lazy(decoderFn) {
|