@gershy/util-codec-parse 0.0.1 → 0.0.3
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 +56 -1
- package/cmp/cjs/main.js +110 -1
- package/cmp/mjs/main.d.ts +56 -1
- package/cmp/mjs/main.js +110 -1
- package/package.json +1 -1
package/cmp/cjs/main.d.ts
CHANGED
|
@@ -1,4 +1,59 @@
|
|
|
1
1
|
import '../sideEffects.js';
|
|
2
2
|
import '@gershy/clearing';
|
|
3
|
-
declare
|
|
3
|
+
export declare namespace Codec {
|
|
4
|
+
type Base = {
|
|
5
|
+
type: string;
|
|
6
|
+
map?: (val: any) => any;
|
|
7
|
+
};
|
|
8
|
+
type Bln = Base & {
|
|
9
|
+
type: 'bln';
|
|
10
|
+
map?: (val: boolean) => any;
|
|
11
|
+
};
|
|
12
|
+
type Num = Base & {
|
|
13
|
+
type: 'num';
|
|
14
|
+
map?: (val: number) => any;
|
|
15
|
+
};
|
|
16
|
+
type Str = Base & {
|
|
17
|
+
type: 'str';
|
|
18
|
+
map?: (val: string) => any;
|
|
19
|
+
minLen?: number;
|
|
20
|
+
maxLen?: number;
|
|
21
|
+
};
|
|
22
|
+
type Arr<I extends Base> = Base & {
|
|
23
|
+
type: 'arr';
|
|
24
|
+
map?: (val: Out<I>[]) => any;
|
|
25
|
+
minLen?: number;
|
|
26
|
+
maxLen?: number;
|
|
27
|
+
item: I;
|
|
28
|
+
};
|
|
29
|
+
type Map<I extends Base> = Base & {
|
|
30
|
+
type: 'map';
|
|
31
|
+
map?: (val: Obj<Out<I>>) => any;
|
|
32
|
+
minLen?: number;
|
|
33
|
+
maxLen?: number;
|
|
34
|
+
item: I;
|
|
35
|
+
};
|
|
36
|
+
type Rec<O extends Obj<Base>> = Base & {
|
|
37
|
+
type: 'rec';
|
|
38
|
+
map?: (val: {
|
|
39
|
+
[K in keyof O]: Out<O[K]>;
|
|
40
|
+
}) => any;
|
|
41
|
+
props: O;
|
|
42
|
+
};
|
|
43
|
+
type Enum<Opts extends string[]> = Base & {
|
|
44
|
+
type: 'enum';
|
|
45
|
+
map?: (val: Opts[number]) => any;
|
|
46
|
+
opts: Opts;
|
|
47
|
+
};
|
|
48
|
+
type OneOf<Opts extends Base[]> = Base & {
|
|
49
|
+
type: 'oneOf';
|
|
50
|
+
map?: (val: Out<Opts[number]>) => any;
|
|
51
|
+
opts: Opts;
|
|
52
|
+
};
|
|
53
|
+
type Out<C extends Base> = 0 extends 1 ? never : C extends Bln ? boolean : C extends Num ? number : C extends Str ? string : C extends Arr<infer I> ? Out<I>[] : C extends Map<infer I> ? Obj<Out<I>> : C extends Rec<infer O> ? {
|
|
54
|
+
[K in keyof O]: Out<O[K]>;
|
|
55
|
+
} : C extends Enum<infer Opts> ? Opts[number] : C extends OneOf<infer Opts> ? Out<Opts[number]> : never;
|
|
56
|
+
type Registry = Bln | Num | Str | Arr<any> | Map<any> | Rec<any> | Enum<any> | OneOf<any>;
|
|
57
|
+
}
|
|
58
|
+
declare const _default: (codec: Codec.Registry, val: unknown) => any;
|
|
4
59
|
export default _default;
|
package/cmp/cjs/main.js
CHANGED
|
@@ -1,4 +1,113 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
require("@gershy/clearing");
|
|
4
|
-
|
|
4
|
+
;
|
|
5
|
+
exports.default = (codec, val) => {
|
|
6
|
+
const assert = (args) => {
|
|
7
|
+
try {
|
|
8
|
+
if (!args.fn(args.args))
|
|
9
|
+
throw Error('assert failed');
|
|
10
|
+
}
|
|
11
|
+
catch (err) {
|
|
12
|
+
throw err[cl.mod]({ codecParse: true, ...args });
|
|
13
|
+
}
|
|
14
|
+
};
|
|
15
|
+
const parse = (codec, val, chain, ctx) => {
|
|
16
|
+
const checked = (() => {
|
|
17
|
+
if (codec.type === 'bln') {
|
|
18
|
+
assert({ desc: 'bln', chain, ctx, args: val, fn: val => cl.isCls(val, Boolean) });
|
|
19
|
+
return val;
|
|
20
|
+
}
|
|
21
|
+
else if (codec.type === 'num') {
|
|
22
|
+
assert({ desc: 'num', chain, ctx, args: val, fn: val => cl.isCls(val, Number) });
|
|
23
|
+
return val;
|
|
24
|
+
}
|
|
25
|
+
else if (codec.type === 'str') {
|
|
26
|
+
const { minLen = 0, maxLen = Number[cl.int64] } = codec;
|
|
27
|
+
assert({
|
|
28
|
+
desc: 'str', chain, ctx,
|
|
29
|
+
args: { val, minLen, maxLen },
|
|
30
|
+
fn: ({ val, minLen, maxLen }) => true
|
|
31
|
+
&& cl.isCls(val, String)
|
|
32
|
+
&& val.length >= minLen
|
|
33
|
+
&& val.length <= maxLen,
|
|
34
|
+
});
|
|
35
|
+
return val;
|
|
36
|
+
}
|
|
37
|
+
else if (codec.type === 'arr') {
|
|
38
|
+
const { minLen = 0, maxLen = Number[cl.int64], item } = codec;
|
|
39
|
+
assert({
|
|
40
|
+
desc: 'arr', chain, ctx,
|
|
41
|
+
args: { val, minLen, maxLen },
|
|
42
|
+
fn: ({ val, minLen, maxLen }) => true
|
|
43
|
+
&& cl.isCls(val, Array)
|
|
44
|
+
&& val.length >= minLen
|
|
45
|
+
&& val.length <= maxLen
|
|
46
|
+
});
|
|
47
|
+
return val[cl.map]((v, i) => parse(item, v, [...chain, i.toString(10)], ctx));
|
|
48
|
+
}
|
|
49
|
+
else if (codec.type === 'enum') {
|
|
50
|
+
const { opts } = codec;
|
|
51
|
+
assert({
|
|
52
|
+
desc: 'enum', chain, ctx,
|
|
53
|
+
args: { val, opts },
|
|
54
|
+
fn: ({ val, opts }) => opts.includes(val)
|
|
55
|
+
});
|
|
56
|
+
return val;
|
|
57
|
+
}
|
|
58
|
+
else if (codec.type === 'map') {
|
|
59
|
+
const { item } = codec;
|
|
60
|
+
assert({
|
|
61
|
+
desc: 'map', chain, ctx,
|
|
62
|
+
args: val,
|
|
63
|
+
fn: val => cl.isCls(val, Object)
|
|
64
|
+
});
|
|
65
|
+
return val[cl.map]((v, k) => parse(item, v, [...chain, k], ctx));
|
|
66
|
+
}
|
|
67
|
+
else if (codec.type === 'rec') {
|
|
68
|
+
const { props } = codec;
|
|
69
|
+
assert({
|
|
70
|
+
desc: 'rec', chain, ctx,
|
|
71
|
+
args: { val, keys: props[cl.toArr]((v, k) => k) },
|
|
72
|
+
fn: ({ val, keys }) => true
|
|
73
|
+
&& cl.isCls(val, Object)
|
|
74
|
+
&& val[cl.count]() === keys[cl.count]()
|
|
75
|
+
&& keys.every(k => val[cl.has](k))
|
|
76
|
+
});
|
|
77
|
+
return val[cl.map]((v, k) => parse(props[k], v, [...chain, k], ctx));
|
|
78
|
+
}
|
|
79
|
+
else if (codec.type === 'oneOf') {
|
|
80
|
+
const { opts } = codec;
|
|
81
|
+
const { result, opt } = (() => {
|
|
82
|
+
const errs = [];
|
|
83
|
+
for (const opt of opts) {
|
|
84
|
+
try {
|
|
85
|
+
const result = parse(opt, val, [...chain, `opt(${opt.type})`], ctx);
|
|
86
|
+
return { result, opt };
|
|
87
|
+
}
|
|
88
|
+
catch (err) {
|
|
89
|
+
errs.push(err);
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
throw Error('all options failed')[cl.mod]({ codecParse: true, desc: 'oneOf', chain, ctx, args: val, errs });
|
|
93
|
+
})();
|
|
94
|
+
return result;
|
|
95
|
+
}
|
|
96
|
+
throw Error('codec type unexpected')[cl.mod]({ codec });
|
|
97
|
+
})();
|
|
98
|
+
if (codec.map) {
|
|
99
|
+
try {
|
|
100
|
+
return codec.map(checked);
|
|
101
|
+
}
|
|
102
|
+
catch (err) {
|
|
103
|
+
if (!err.codecParse)
|
|
104
|
+
throw err;
|
|
105
|
+
throw err[cl.mod](msg => ({ msg: `map failed (${msg})`, chain, ctx, fn: codec.map }));
|
|
106
|
+
}
|
|
107
|
+
}
|
|
108
|
+
else {
|
|
109
|
+
return checked;
|
|
110
|
+
}
|
|
111
|
+
};
|
|
112
|
+
return parse(codec, val, [], {});
|
|
113
|
+
};
|
package/cmp/mjs/main.d.ts
CHANGED
|
@@ -1,4 +1,59 @@
|
|
|
1
1
|
import '../sideEffects.js';
|
|
2
2
|
import '@gershy/clearing';
|
|
3
|
-
declare
|
|
3
|
+
export declare namespace Codec {
|
|
4
|
+
type Base = {
|
|
5
|
+
type: string;
|
|
6
|
+
map?: (val: any) => any;
|
|
7
|
+
};
|
|
8
|
+
type Bln = Base & {
|
|
9
|
+
type: 'bln';
|
|
10
|
+
map?: (val: boolean) => any;
|
|
11
|
+
};
|
|
12
|
+
type Num = Base & {
|
|
13
|
+
type: 'num';
|
|
14
|
+
map?: (val: number) => any;
|
|
15
|
+
};
|
|
16
|
+
type Str = Base & {
|
|
17
|
+
type: 'str';
|
|
18
|
+
map?: (val: string) => any;
|
|
19
|
+
minLen?: number;
|
|
20
|
+
maxLen?: number;
|
|
21
|
+
};
|
|
22
|
+
type Arr<I extends Base> = Base & {
|
|
23
|
+
type: 'arr';
|
|
24
|
+
map?: (val: Out<I>[]) => any;
|
|
25
|
+
minLen?: number;
|
|
26
|
+
maxLen?: number;
|
|
27
|
+
item: I;
|
|
28
|
+
};
|
|
29
|
+
type Map<I extends Base> = Base & {
|
|
30
|
+
type: 'map';
|
|
31
|
+
map?: (val: Obj<Out<I>>) => any;
|
|
32
|
+
minLen?: number;
|
|
33
|
+
maxLen?: number;
|
|
34
|
+
item: I;
|
|
35
|
+
};
|
|
36
|
+
type Rec<O extends Obj<Base>> = Base & {
|
|
37
|
+
type: 'rec';
|
|
38
|
+
map?: (val: {
|
|
39
|
+
[K in keyof O]: Out<O[K]>;
|
|
40
|
+
}) => any;
|
|
41
|
+
props: O;
|
|
42
|
+
};
|
|
43
|
+
type Enum<Opts extends string[]> = Base & {
|
|
44
|
+
type: 'enum';
|
|
45
|
+
map?: (val: Opts[number]) => any;
|
|
46
|
+
opts: Opts;
|
|
47
|
+
};
|
|
48
|
+
type OneOf<Opts extends Base[]> = Base & {
|
|
49
|
+
type: 'oneOf';
|
|
50
|
+
map?: (val: Out<Opts[number]>) => any;
|
|
51
|
+
opts: Opts;
|
|
52
|
+
};
|
|
53
|
+
type Out<C extends Base> = 0 extends 1 ? never : C extends Bln ? boolean : C extends Num ? number : C extends Str ? string : C extends Arr<infer I> ? Out<I>[] : C extends Map<infer I> ? Obj<Out<I>> : C extends Rec<infer O> ? {
|
|
54
|
+
[K in keyof O]: Out<O[K]>;
|
|
55
|
+
} : C extends Enum<infer Opts> ? Opts[number] : C extends OneOf<infer Opts> ? Out<Opts[number]> : never;
|
|
56
|
+
type Registry = Bln | Num | Str | Arr<any> | Map<any> | Rec<any> | Enum<any> | OneOf<any>;
|
|
57
|
+
}
|
|
58
|
+
declare const _default: (codec: Codec.Registry, val: unknown) => any;
|
|
4
59
|
export default _default;
|
package/cmp/mjs/main.js
CHANGED
|
@@ -1,2 +1,111 @@
|
|
|
1
1
|
import '@gershy/clearing';
|
|
2
|
-
|
|
2
|
+
;
|
|
3
|
+
export default (codec, val) => {
|
|
4
|
+
const assert = (args) => {
|
|
5
|
+
try {
|
|
6
|
+
if (!args.fn(args.args))
|
|
7
|
+
throw Error('assert failed');
|
|
8
|
+
}
|
|
9
|
+
catch (err) {
|
|
10
|
+
throw err[cl.mod]({ codecParse: true, ...args });
|
|
11
|
+
}
|
|
12
|
+
};
|
|
13
|
+
const parse = (codec, val, chain, ctx) => {
|
|
14
|
+
const checked = (() => {
|
|
15
|
+
if (codec.type === 'bln') {
|
|
16
|
+
assert({ desc: 'bln', chain, ctx, args: val, fn: val => cl.isCls(val, Boolean) });
|
|
17
|
+
return val;
|
|
18
|
+
}
|
|
19
|
+
else if (codec.type === 'num') {
|
|
20
|
+
assert({ desc: 'num', chain, ctx, args: val, fn: val => cl.isCls(val, Number) });
|
|
21
|
+
return val;
|
|
22
|
+
}
|
|
23
|
+
else if (codec.type === 'str') {
|
|
24
|
+
const { minLen = 0, maxLen = Number[cl.int64] } = codec;
|
|
25
|
+
assert({
|
|
26
|
+
desc: 'str', chain, ctx,
|
|
27
|
+
args: { val, minLen, maxLen },
|
|
28
|
+
fn: ({ val, minLen, maxLen }) => true
|
|
29
|
+
&& cl.isCls(val, String)
|
|
30
|
+
&& val.length >= minLen
|
|
31
|
+
&& val.length <= maxLen,
|
|
32
|
+
});
|
|
33
|
+
return val;
|
|
34
|
+
}
|
|
35
|
+
else if (codec.type === 'arr') {
|
|
36
|
+
const { minLen = 0, maxLen = Number[cl.int64], item } = codec;
|
|
37
|
+
assert({
|
|
38
|
+
desc: 'arr', chain, ctx,
|
|
39
|
+
args: { val, minLen, maxLen },
|
|
40
|
+
fn: ({ val, minLen, maxLen }) => true
|
|
41
|
+
&& cl.isCls(val, Array)
|
|
42
|
+
&& val.length >= minLen
|
|
43
|
+
&& val.length <= maxLen
|
|
44
|
+
});
|
|
45
|
+
return val[cl.map]((v, i) => parse(item, v, [...chain, i.toString(10)], ctx));
|
|
46
|
+
}
|
|
47
|
+
else if (codec.type === 'enum') {
|
|
48
|
+
const { opts } = codec;
|
|
49
|
+
assert({
|
|
50
|
+
desc: 'enum', chain, ctx,
|
|
51
|
+
args: { val, opts },
|
|
52
|
+
fn: ({ val, opts }) => opts.includes(val)
|
|
53
|
+
});
|
|
54
|
+
return val;
|
|
55
|
+
}
|
|
56
|
+
else if (codec.type === 'map') {
|
|
57
|
+
const { item } = codec;
|
|
58
|
+
assert({
|
|
59
|
+
desc: 'map', chain, ctx,
|
|
60
|
+
args: val,
|
|
61
|
+
fn: val => cl.isCls(val, Object)
|
|
62
|
+
});
|
|
63
|
+
return val[cl.map]((v, k) => parse(item, v, [...chain, k], ctx));
|
|
64
|
+
}
|
|
65
|
+
else if (codec.type === 'rec') {
|
|
66
|
+
const { props } = codec;
|
|
67
|
+
assert({
|
|
68
|
+
desc: 'rec', chain, ctx,
|
|
69
|
+
args: { val, keys: props[cl.toArr]((v, k) => k) },
|
|
70
|
+
fn: ({ val, keys }) => true
|
|
71
|
+
&& cl.isCls(val, Object)
|
|
72
|
+
&& val[cl.count]() === keys[cl.count]()
|
|
73
|
+
&& keys.every(k => val[cl.has](k))
|
|
74
|
+
});
|
|
75
|
+
return val[cl.map]((v, k) => parse(props[k], v, [...chain, k], ctx));
|
|
76
|
+
}
|
|
77
|
+
else if (codec.type === 'oneOf') {
|
|
78
|
+
const { opts } = codec;
|
|
79
|
+
const { result, opt } = (() => {
|
|
80
|
+
const errs = [];
|
|
81
|
+
for (const opt of opts) {
|
|
82
|
+
try {
|
|
83
|
+
const result = parse(opt, val, [...chain, `opt(${opt.type})`], ctx);
|
|
84
|
+
return { result, opt };
|
|
85
|
+
}
|
|
86
|
+
catch (err) {
|
|
87
|
+
errs.push(err);
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
throw Error('all options failed')[cl.mod]({ codecParse: true, desc: 'oneOf', chain, ctx, args: val, errs });
|
|
91
|
+
})();
|
|
92
|
+
return result;
|
|
93
|
+
}
|
|
94
|
+
throw Error('codec type unexpected')[cl.mod]({ codec });
|
|
95
|
+
})();
|
|
96
|
+
if (codec.map) {
|
|
97
|
+
try {
|
|
98
|
+
return codec.map(checked);
|
|
99
|
+
}
|
|
100
|
+
catch (err) {
|
|
101
|
+
if (!err.codecParse)
|
|
102
|
+
throw err;
|
|
103
|
+
throw err[cl.mod](msg => ({ msg: `map failed (${msg})`, chain, ctx, fn: codec.map }));
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
else {
|
|
107
|
+
return checked;
|
|
108
|
+
}
|
|
109
|
+
};
|
|
110
|
+
return parse(codec, val, [], {});
|
|
111
|
+
};
|