@gershy/util-codec-parse 0.0.14 → 0.0.16
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/cmp/cjs/main.d.ts +48 -41
- package/cmp/cjs/main.js +13 -8
- package/cmp/esm/main.d.ts +48 -41
- package/cmp/esm/main.js +13 -8
- package/package.json +2 -2
package/cmp/cjs/main.d.ts
CHANGED
|
@@ -1,38 +1,40 @@
|
|
|
1
1
|
import '@gershy/clearing';
|
|
2
2
|
export declare namespace Codec {
|
|
3
|
-
type
|
|
4
|
-
type:
|
|
5
|
-
map?: (val:
|
|
3
|
+
export type Nul = {
|
|
4
|
+
type: 'nul';
|
|
5
|
+
map?: (val: null) => any;
|
|
6
6
|
};
|
|
7
|
-
type Bln =
|
|
7
|
+
export type Bln = {
|
|
8
8
|
type: 'bln';
|
|
9
9
|
map?: (val: boolean) => any;
|
|
10
10
|
};
|
|
11
|
-
type Num =
|
|
11
|
+
export type Num = {
|
|
12
12
|
type: 'num';
|
|
13
13
|
map?: (val: number) => any;
|
|
14
14
|
};
|
|
15
|
-
type Str =
|
|
15
|
+
export type Str = {
|
|
16
16
|
type: 'str';
|
|
17
17
|
map?: (val: string) => any;
|
|
18
18
|
minLen?: number;
|
|
19
19
|
maxLen?: number;
|
|
20
20
|
};
|
|
21
|
-
type Arr<I extends
|
|
21
|
+
export type Arr<I extends Reg> = {
|
|
22
22
|
type: 'arr';
|
|
23
23
|
map?: (val: Out<I>[]) => any;
|
|
24
24
|
minLen?: number;
|
|
25
25
|
maxLen?: number;
|
|
26
26
|
item: I;
|
|
27
27
|
};
|
|
28
|
-
type Map<I extends
|
|
28
|
+
export type Map<I extends Reg> = {
|
|
29
29
|
type: 'map';
|
|
30
30
|
map?: (val: Obj<Out<I>>) => any;
|
|
31
31
|
minLen?: number;
|
|
32
32
|
maxLen?: number;
|
|
33
33
|
item: I;
|
|
34
34
|
};
|
|
35
|
-
type Rec<O extends Obj<
|
|
35
|
+
export type Rec<O extends Obj<Reg & {
|
|
36
|
+
req?: boolean;
|
|
37
|
+
}>> = {
|
|
36
38
|
type: 'rec';
|
|
37
39
|
map?: (val: {
|
|
38
40
|
[K in keyof O]: Out<O[K]>;
|
|
@@ -40,50 +42,55 @@ export declare namespace Codec {
|
|
|
40
42
|
props: O;
|
|
41
43
|
loose?: boolean;
|
|
42
44
|
};
|
|
43
|
-
type Enum<Opts extends
|
|
45
|
+
export type Enum<Opts extends readonly Json[]> = {
|
|
44
46
|
type: 'enum';
|
|
45
47
|
map?: (val: Opts[number]) => any;
|
|
46
48
|
opts: Opts;
|
|
47
49
|
};
|
|
48
|
-
type OneOf<Opts extends
|
|
50
|
+
export type OneOf<Opts extends Reg[]> = {
|
|
49
51
|
type: 'oneOf';
|
|
50
52
|
map?: (val: Out<Opts[number]>) => any;
|
|
51
53
|
opts: Opts;
|
|
52
54
|
};
|
|
53
|
-
type Any =
|
|
55
|
+
export type Any = {
|
|
54
56
|
type: 'any';
|
|
55
57
|
map?: (val: any) => any;
|
|
56
58
|
};
|
|
57
|
-
type
|
|
59
|
+
export type Reg = Nul | Bln | Num | Str | Arr<any> | Map<any> | Rec<any> | Enum<any> | OneOf<any> | Any;
|
|
60
|
+
type RecOut<P extends Obj<Reg>> = P extends Obj<Reg & {
|
|
61
|
+
req?: boolean;
|
|
62
|
+
}> ? {
|
|
63
|
+
[K in keyof P]: never | Out<P[K]> | (P[K] extends Reg & {
|
|
64
|
+
req: false;
|
|
65
|
+
} ? undefined : never);
|
|
66
|
+
} : never;
|
|
67
|
+
export type Out<C extends Reg> = 0 extends 1 ? never : C extends {
|
|
58
68
|
map: (val: any) => infer Mapped;
|
|
59
69
|
} ? Mapped : string extends C['type'] ? any : C extends {
|
|
60
|
-
type:
|
|
61
|
-
} ?
|
|
62
|
-
|
|
63
|
-
}
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
any: any;
|
|
85
|
-
})[T] : never : never;
|
|
86
|
-
type Registry = Bln | Num | Str | Arr<any> | Map<any> | Rec<any> | Enum<any> | OneOf<any> | Any;
|
|
70
|
+
type: 'nul';
|
|
71
|
+
} ? null : C extends {
|
|
72
|
+
type: 'bln';
|
|
73
|
+
} ? boolean : C extends {
|
|
74
|
+
type: 'num';
|
|
75
|
+
} ? number : C extends {
|
|
76
|
+
type: 'str';
|
|
77
|
+
} ? string : C extends {
|
|
78
|
+
type: 'enum';
|
|
79
|
+
opts: readonly (infer O)[];
|
|
80
|
+
} ? O : C extends {
|
|
81
|
+
type: 'arr';
|
|
82
|
+
item: infer I;
|
|
83
|
+
} ? I extends Reg ? Out<I>[] : never : C extends {
|
|
84
|
+
type: 'map';
|
|
85
|
+
item: infer I;
|
|
86
|
+
} ? I extends Reg ? Obj<Out<I>> : never : C extends {
|
|
87
|
+
type: 'rec';
|
|
88
|
+
props: infer P;
|
|
89
|
+
} ? P extends Obj<Reg> ? RecOut<P> : never : C extends {
|
|
90
|
+
type: 'oneOf';
|
|
91
|
+
opts: (infer O)[];
|
|
92
|
+
} ? O extends Reg ? Out<O> : never : never;
|
|
93
|
+
export {};
|
|
87
94
|
}
|
|
88
|
-
declare const _default: <C extends Codec.
|
|
95
|
+
declare const _default: <C extends Codec.Reg>(codec: C, val: unknown) => Codec.Out<C>;
|
|
89
96
|
export default _default;
|
package/cmp/cjs/main.js
CHANGED
|
@@ -33,7 +33,10 @@ var main_default = (codec, val) => {
|
|
|
33
33
|
};
|
|
34
34
|
const parse = (codec2, val2, chain, ctx) => {
|
|
35
35
|
const checked = (() => {
|
|
36
|
-
if (codec2.type === "
|
|
36
|
+
if (codec2.type === "nul") {
|
|
37
|
+
assert({ desc: "nul", chain, ctx, args: val2, fn: (val3) => val3 === null });
|
|
38
|
+
return val2;
|
|
39
|
+
} else if (codec2.type === "bln") {
|
|
37
40
|
assert({ desc: "bln", chain, ctx, args: val2, fn: (val3) => cl.isCls(val3, Boolean) });
|
|
38
41
|
return val2;
|
|
39
42
|
} else if (codec2.type === "num") {
|
|
@@ -82,13 +85,15 @@ var main_default = (codec, val) => {
|
|
|
82
85
|
} else if (codec2.type === "rec") {
|
|
83
86
|
const { props, loose = false } = codec2;
|
|
84
87
|
const keys = props[cl.toArr]((v, k) => k);
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
88
|
+
const reqKeys = props[cl.toArr]((v, k) => v.req ?? true ? k : cl.skip);
|
|
89
|
+
assert({ desc: "rec.obj", chain, ctx, args: { val: val2 }, fn: (args) => cl.isCls(args.val, Object) });
|
|
90
|
+
assert({ desc: "rec.req", chain, ctx, args: { val: val2, reqKeys }, fn: ({ val: val3, reqKeys: reqKeys2 }) => {
|
|
91
|
+
return reqKeys2.every((rk) => val3[cl.has](rk));
|
|
92
|
+
} });
|
|
93
|
+
if (!loose)
|
|
94
|
+
assert({ desc: "rec.tight", chain, ctx, args: { val: val2, keys }, fn: ({ val: val3, keys: keys2 }) => {
|
|
95
|
+
return val3[cl.toArr]((v, k) => k).every((k) => keys2.includes(k));
|
|
96
|
+
} });
|
|
92
97
|
return val2[cl.map]((v, k) => {
|
|
93
98
|
return props[cl.has](k) ? parse(props[k], v, [...chain, k], ctx) : v;
|
|
94
99
|
});
|
package/cmp/esm/main.d.ts
CHANGED
|
@@ -1,38 +1,40 @@
|
|
|
1
1
|
import '@gershy/clearing';
|
|
2
2
|
export declare namespace Codec {
|
|
3
|
-
type
|
|
4
|
-
type:
|
|
5
|
-
map?: (val:
|
|
3
|
+
export type Nul = {
|
|
4
|
+
type: 'nul';
|
|
5
|
+
map?: (val: null) => any;
|
|
6
6
|
};
|
|
7
|
-
type Bln =
|
|
7
|
+
export type Bln = {
|
|
8
8
|
type: 'bln';
|
|
9
9
|
map?: (val: boolean) => any;
|
|
10
10
|
};
|
|
11
|
-
type Num =
|
|
11
|
+
export type Num = {
|
|
12
12
|
type: 'num';
|
|
13
13
|
map?: (val: number) => any;
|
|
14
14
|
};
|
|
15
|
-
type Str =
|
|
15
|
+
export type Str = {
|
|
16
16
|
type: 'str';
|
|
17
17
|
map?: (val: string) => any;
|
|
18
18
|
minLen?: number;
|
|
19
19
|
maxLen?: number;
|
|
20
20
|
};
|
|
21
|
-
type Arr<I extends
|
|
21
|
+
export type Arr<I extends Reg> = {
|
|
22
22
|
type: 'arr';
|
|
23
23
|
map?: (val: Out<I>[]) => any;
|
|
24
24
|
minLen?: number;
|
|
25
25
|
maxLen?: number;
|
|
26
26
|
item: I;
|
|
27
27
|
};
|
|
28
|
-
type Map<I extends
|
|
28
|
+
export type Map<I extends Reg> = {
|
|
29
29
|
type: 'map';
|
|
30
30
|
map?: (val: Obj<Out<I>>) => any;
|
|
31
31
|
minLen?: number;
|
|
32
32
|
maxLen?: number;
|
|
33
33
|
item: I;
|
|
34
34
|
};
|
|
35
|
-
type Rec<O extends Obj<
|
|
35
|
+
export type Rec<O extends Obj<Reg & {
|
|
36
|
+
req?: boolean;
|
|
37
|
+
}>> = {
|
|
36
38
|
type: 'rec';
|
|
37
39
|
map?: (val: {
|
|
38
40
|
[K in keyof O]: Out<O[K]>;
|
|
@@ -40,50 +42,55 @@ export declare namespace Codec {
|
|
|
40
42
|
props: O;
|
|
41
43
|
loose?: boolean;
|
|
42
44
|
};
|
|
43
|
-
type Enum<Opts extends
|
|
45
|
+
export type Enum<Opts extends readonly Json[]> = {
|
|
44
46
|
type: 'enum';
|
|
45
47
|
map?: (val: Opts[number]) => any;
|
|
46
48
|
opts: Opts;
|
|
47
49
|
};
|
|
48
|
-
type OneOf<Opts extends
|
|
50
|
+
export type OneOf<Opts extends Reg[]> = {
|
|
49
51
|
type: 'oneOf';
|
|
50
52
|
map?: (val: Out<Opts[number]>) => any;
|
|
51
53
|
opts: Opts;
|
|
52
54
|
};
|
|
53
|
-
type Any =
|
|
55
|
+
export type Any = {
|
|
54
56
|
type: 'any';
|
|
55
57
|
map?: (val: any) => any;
|
|
56
58
|
};
|
|
57
|
-
type
|
|
59
|
+
export type Reg = Nul | Bln | Num | Str | Arr<any> | Map<any> | Rec<any> | Enum<any> | OneOf<any> | Any;
|
|
60
|
+
type RecOut<P extends Obj<Reg>> = P extends Obj<Reg & {
|
|
61
|
+
req?: boolean;
|
|
62
|
+
}> ? {
|
|
63
|
+
[K in keyof P]: never | Out<P[K]> | (P[K] extends Reg & {
|
|
64
|
+
req: false;
|
|
65
|
+
} ? undefined : never);
|
|
66
|
+
} : never;
|
|
67
|
+
export type Out<C extends Reg> = 0 extends 1 ? never : C extends {
|
|
58
68
|
map: (val: any) => infer Mapped;
|
|
59
69
|
} ? Mapped : string extends C['type'] ? any : C extends {
|
|
60
|
-
type:
|
|
61
|
-
} ?
|
|
62
|
-
|
|
63
|
-
}
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
any: any;
|
|
85
|
-
})[T] : never : never;
|
|
86
|
-
type Registry = Bln | Num | Str | Arr<any> | Map<any> | Rec<any> | Enum<any> | OneOf<any> | Any;
|
|
70
|
+
type: 'nul';
|
|
71
|
+
} ? null : C extends {
|
|
72
|
+
type: 'bln';
|
|
73
|
+
} ? boolean : C extends {
|
|
74
|
+
type: 'num';
|
|
75
|
+
} ? number : C extends {
|
|
76
|
+
type: 'str';
|
|
77
|
+
} ? string : C extends {
|
|
78
|
+
type: 'enum';
|
|
79
|
+
opts: readonly (infer O)[];
|
|
80
|
+
} ? O : C extends {
|
|
81
|
+
type: 'arr';
|
|
82
|
+
item: infer I;
|
|
83
|
+
} ? I extends Reg ? Out<I>[] : never : C extends {
|
|
84
|
+
type: 'map';
|
|
85
|
+
item: infer I;
|
|
86
|
+
} ? I extends Reg ? Obj<Out<I>> : never : C extends {
|
|
87
|
+
type: 'rec';
|
|
88
|
+
props: infer P;
|
|
89
|
+
} ? P extends Obj<Reg> ? RecOut<P> : never : C extends {
|
|
90
|
+
type: 'oneOf';
|
|
91
|
+
opts: (infer O)[];
|
|
92
|
+
} ? O extends Reg ? Out<O> : never : never;
|
|
93
|
+
export {};
|
|
87
94
|
}
|
|
88
|
-
declare const _default: <C extends Codec.
|
|
95
|
+
declare const _default: <C extends Codec.Reg>(codec: C, val: unknown) => Codec.Out<C>;
|
|
89
96
|
export default _default;
|
package/cmp/esm/main.js
CHANGED
|
@@ -10,7 +10,10 @@ var main_default = (codec, val) => {
|
|
|
10
10
|
};
|
|
11
11
|
const parse = (codec2, val2, chain, ctx) => {
|
|
12
12
|
const checked = (() => {
|
|
13
|
-
if (codec2.type === "
|
|
13
|
+
if (codec2.type === "nul") {
|
|
14
|
+
assert({ desc: "nul", chain, ctx, args: val2, fn: (val3) => val3 === null });
|
|
15
|
+
return val2;
|
|
16
|
+
} else if (codec2.type === "bln") {
|
|
14
17
|
assert({ desc: "bln", chain, ctx, args: val2, fn: (val3) => cl.isCls(val3, Boolean) });
|
|
15
18
|
return val2;
|
|
16
19
|
} else if (codec2.type === "num") {
|
|
@@ -59,13 +62,15 @@ var main_default = (codec, val) => {
|
|
|
59
62
|
} else if (codec2.type === "rec") {
|
|
60
63
|
const { props, loose = false } = codec2;
|
|
61
64
|
const keys = props[cl.toArr]((v, k) => k);
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
65
|
+
const reqKeys = props[cl.toArr]((v, k) => v.req ?? true ? k : cl.skip);
|
|
66
|
+
assert({ desc: "rec.obj", chain, ctx, args: { val: val2 }, fn: (args) => cl.isCls(args.val, Object) });
|
|
67
|
+
assert({ desc: "rec.req", chain, ctx, args: { val: val2, reqKeys }, fn: ({ val: val3, reqKeys: reqKeys2 }) => {
|
|
68
|
+
return reqKeys2.every((rk) => val3[cl.has](rk));
|
|
69
|
+
} });
|
|
70
|
+
if (!loose)
|
|
71
|
+
assert({ desc: "rec.tight", chain, ctx, args: { val: val2, keys }, fn: ({ val: val3, keys: keys2 }) => {
|
|
72
|
+
return val3[cl.toArr]((v, k) => k).every((k) => keys2.includes(k));
|
|
73
|
+
} });
|
|
69
74
|
return val2[cl.map]((v, k) => {
|
|
70
75
|
return props[cl.has](k) ? parse(props[k], v, [...chain, k], ctx) : v;
|
|
71
76
|
});
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@gershy/util-codec-parse",
|
|
3
|
-
"version": "0.0.
|
|
3
|
+
"version": "0.0.16",
|
|
4
4
|
"author": "Gershom Maes",
|
|
5
5
|
"description": "TODO",
|
|
6
6
|
"keywords": [
|
|
@@ -20,7 +20,7 @@
|
|
|
20
20
|
],
|
|
21
21
|
"license": "ISC",
|
|
22
22
|
"peerDependencies": {
|
|
23
|
-
"@gershy/clearing": "^0.0.
|
|
23
|
+
"@gershy/clearing": "^0.0.48"
|
|
24
24
|
},
|
|
25
25
|
"devDependencies": {
|
|
26
26
|
"@types/node": "^24.10.1"
|