@gershy/util-codec-parse 0.0.3 → 0.0.5

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 CHANGED
@@ -1,4 +1,3 @@
1
- import '../sideEffects.js';
2
1
  import '@gershy/clearing';
3
2
  export declare namespace Codec {
4
3
  type Base = {
@@ -50,10 +49,14 @@ export declare namespace Codec {
50
49
  map?: (val: Out<Opts[number]>) => any;
51
50
  opts: Opts;
52
51
  };
52
+ type Any = Base & {
53
+ type: 'any';
54
+ map?: (val: any) => any;
55
+ };
53
56
  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
57
  [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>;
58
+ } : C extends Enum<infer Opts> ? Opts[number] : C extends OneOf<infer Opts> ? Out<Opts[number]> : C extends Any ? any : never;
59
+ type Registry = Bln | Num | Str | Arr<any> | Map<any> | Rec<any> | Enum<any> | OneOf<any> | Any;
57
60
  }
58
61
  declare const _default: (codec: Codec.Registry, val: unknown) => any;
59
62
  export default _default;
package/cmp/cjs/main.js CHANGED
@@ -1,113 +1,124 @@
1
1
  "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- require("@gershy/clearing");
2
+ var __defProp = Object.defineProperty;
3
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
+ var __getOwnPropNames = Object.getOwnPropertyNames;
5
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
6
+ var __export = (target, all) => {
7
+ for (var name in all)
8
+ __defProp(target, name, { get: all[name], enumerable: true });
9
+ };
10
+ var __copyProps = (to, from, except, desc) => {
11
+ if (from && typeof from === "object" || typeof from === "function") {
12
+ for (let key of __getOwnPropNames(from))
13
+ if (!__hasOwnProp.call(to, key) && key !== except)
14
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
15
+ }
16
+ return to;
17
+ };
18
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
19
+ var main_exports = {};
20
+ __export(main_exports, {
21
+ default: () => main_default
22
+ });
23
+ module.exports = __toCommonJS(main_exports);
24
+ var import_clearing = require("@gershy/clearing");
4
25
  ;
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) {
26
+ var main_default = (codec, val) => {
27
+ const assert = (args) => {
28
+ try {
29
+ if (!args.fn(args.args)) throw Error("assert failed");
30
+ } catch (err) {
31
+ throw err[cl.mod]({ codecParse: true, ...args });
32
+ }
33
+ };
34
+ const parse = (codec2, val2, chain, ctx) => {
35
+ const checked = (() => {
36
+ if (codec2.type === "bln") {
37
+ assert({ desc: "bln", chain, ctx, args: val2, fn: (val3) => cl.isCls(val3, Boolean) });
38
+ return val2;
39
+ } else if (codec2.type === "num") {
40
+ assert({ desc: "num", chain, ctx, args: val2, fn: (val3) => cl.isCls(val3, Number) });
41
+ return val2;
42
+ } else if (codec2.type === "str") {
43
+ const { minLen = 0, maxLen = Number[cl.int64] } = codec2;
44
+ assert({
45
+ desc: "str",
46
+ chain,
47
+ ctx,
48
+ args: { val: val2, minLen, maxLen },
49
+ fn: ({ val: val3, minLen: minLen2, maxLen: maxLen2 }) => cl.isCls(val3, String) && val3.length >= minLen2 && val3.length <= maxLen2
50
+ });
51
+ return val2;
52
+ } else if (codec2.type === "arr") {
53
+ const { minLen = 0, maxLen = Number[cl.int64], item } = codec2;
54
+ assert({
55
+ desc: "arr",
56
+ chain,
57
+ ctx,
58
+ args: { val: val2, minLen, maxLen },
59
+ fn: ({ val: val3, minLen: minLen2, maxLen: maxLen2 }) => cl.isCls(val3, Array) && val3.length >= minLen2 && val3.length <= maxLen2
60
+ });
61
+ return val2[cl.map]((v, i) => parse(item, v, [...chain, i.toString(10)], ctx));
62
+ } else if (codec2.type === "enum") {
63
+ const { opts } = codec2;
64
+ assert({
65
+ desc: "enum",
66
+ chain,
67
+ ctx,
68
+ args: { val: val2, opts },
69
+ fn: ({ val: val3, opts: opts2 }) => opts2.includes(val3)
70
+ });
71
+ return val2;
72
+ } else if (codec2.type === "map") {
73
+ const { item } = codec2;
74
+ assert({
75
+ desc: "map",
76
+ chain,
77
+ ctx,
78
+ args: val2,
79
+ fn: (val3) => cl.isCls(val3, Object)
80
+ });
81
+ return val2[cl.map]((v, k) => parse(item, v, [...chain, k], ctx));
82
+ } else if (codec2.type === "rec") {
83
+ const { props } = codec2;
84
+ assert({
85
+ desc: "rec",
86
+ chain,
87
+ ctx,
88
+ args: { val: val2, keys: props[cl.toArr]((v, k) => k) },
89
+ fn: ({ val: val3, keys }) => cl.isCls(val3, Object) && val3[cl.count]() === keys[cl.count]() && keys.every((k) => val3[cl.has](k))
90
+ });
91
+ return val2[cl.map]((v, k) => parse(props[k], v, [...chain, k], ctx));
92
+ } else if (codec2.type === "oneOf") {
93
+ const { opts } = codec2;
94
+ const { result, opt: _opt } = (() => {
95
+ const errs = [];
96
+ for (const opt of opts) {
99
97
  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 }));
98
+ const result2 = parse(opt, val2, [...chain, `opt(${opt.type})`], ctx);
99
+ return { result: result2, opt };
100
+ } catch (err) {
101
+ errs.push(err);
106
102
  }
107
- }
108
- else {
109
- return checked;
110
- }
111
- };
112
- return parse(codec, val, [], {});
103
+ }
104
+ throw Error("all options failed")[cl.mod]({ codecParse: true, desc: "oneOf", chain, ctx, args: val2, errs });
105
+ })();
106
+ return result;
107
+ } else if (codec2.type === "any") {
108
+ return val2;
109
+ }
110
+ if (0) /* @__PURE__ */ ((v) => void 0)();
111
+ })();
112
+ if (codec2.map) {
113
+ try {
114
+ return codec2.map(checked);
115
+ } catch (err) {
116
+ if (!err.codecParse) throw err;
117
+ throw err[cl.mod]((msg) => ({ msg: `map failed (${msg})`, chain, ctx, fn: codec2.map }));
118
+ }
119
+ } else {
120
+ return checked;
121
+ }
122
+ };
123
+ return parse(codec, val, [], {});
113
124
  };
@@ -1,4 +1,3 @@
1
- import '../sideEffects.js';
2
1
  import '@gershy/clearing';
3
2
  export declare namespace Codec {
4
3
  type Base = {
@@ -50,10 +49,14 @@ export declare namespace Codec {
50
49
  map?: (val: Out<Opts[number]>) => any;
51
50
  opts: Opts;
52
51
  };
52
+ type Any = Base & {
53
+ type: 'any';
54
+ map?: (val: any) => any;
55
+ };
53
56
  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
57
  [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>;
58
+ } : C extends Enum<infer Opts> ? Opts[number] : C extends OneOf<infer Opts> ? Out<Opts[number]> : C extends Any ? any : never;
59
+ type Registry = Bln | Num | Str | Arr<any> | Map<any> | Rec<any> | Enum<any> | OneOf<any> | Any;
57
60
  }
58
61
  declare const _default: (codec: Codec.Registry, val: unknown) => any;
59
62
  export default _default;
@@ -0,0 +1,104 @@
1
+ import "@gershy/clearing";
2
+ ;
3
+ var main_default = (codec, val) => {
4
+ const assert = (args) => {
5
+ try {
6
+ if (!args.fn(args.args)) throw Error("assert failed");
7
+ } catch (err) {
8
+ throw err[cl.mod]({ codecParse: true, ...args });
9
+ }
10
+ };
11
+ const parse = (codec2, val2, chain, ctx) => {
12
+ const checked = (() => {
13
+ if (codec2.type === "bln") {
14
+ assert({ desc: "bln", chain, ctx, args: val2, fn: (val3) => cl.isCls(val3, Boolean) });
15
+ return val2;
16
+ } else if (codec2.type === "num") {
17
+ assert({ desc: "num", chain, ctx, args: val2, fn: (val3) => cl.isCls(val3, Number) });
18
+ return val2;
19
+ } else if (codec2.type === "str") {
20
+ const { minLen = 0, maxLen = Number[cl.int64] } = codec2;
21
+ assert({
22
+ desc: "str",
23
+ chain,
24
+ ctx,
25
+ args: { val: val2, minLen, maxLen },
26
+ fn: ({ val: val3, minLen: minLen2, maxLen: maxLen2 }) => cl.isCls(val3, String) && val3.length >= minLen2 && val3.length <= maxLen2
27
+ });
28
+ return val2;
29
+ } else if (codec2.type === "arr") {
30
+ const { minLen = 0, maxLen = Number[cl.int64], item } = codec2;
31
+ assert({
32
+ desc: "arr",
33
+ chain,
34
+ ctx,
35
+ args: { val: val2, minLen, maxLen },
36
+ fn: ({ val: val3, minLen: minLen2, maxLen: maxLen2 }) => cl.isCls(val3, Array) && val3.length >= minLen2 && val3.length <= maxLen2
37
+ });
38
+ return val2[cl.map]((v, i) => parse(item, v, [...chain, i.toString(10)], ctx));
39
+ } else if (codec2.type === "enum") {
40
+ const { opts } = codec2;
41
+ assert({
42
+ desc: "enum",
43
+ chain,
44
+ ctx,
45
+ args: { val: val2, opts },
46
+ fn: ({ val: val3, opts: opts2 }) => opts2.includes(val3)
47
+ });
48
+ return val2;
49
+ } else if (codec2.type === "map") {
50
+ const { item } = codec2;
51
+ assert({
52
+ desc: "map",
53
+ chain,
54
+ ctx,
55
+ args: val2,
56
+ fn: (val3) => cl.isCls(val3, Object)
57
+ });
58
+ return val2[cl.map]((v, k) => parse(item, v, [...chain, k], ctx));
59
+ } else if (codec2.type === "rec") {
60
+ const { props } = codec2;
61
+ assert({
62
+ desc: "rec",
63
+ chain,
64
+ ctx,
65
+ args: { val: val2, keys: props[cl.toArr]((v, k) => k) },
66
+ fn: ({ val: val3, keys }) => cl.isCls(val3, Object) && val3[cl.count]() === keys[cl.count]() && keys.every((k) => val3[cl.has](k))
67
+ });
68
+ return val2[cl.map]((v, k) => parse(props[k], v, [...chain, k], ctx));
69
+ } else if (codec2.type === "oneOf") {
70
+ const { opts } = codec2;
71
+ const { result, opt: _opt } = (() => {
72
+ const errs = [];
73
+ for (const opt of opts) {
74
+ try {
75
+ const result2 = parse(opt, val2, [...chain, `opt(${opt.type})`], ctx);
76
+ return { result: result2, opt };
77
+ } catch (err) {
78
+ errs.push(err);
79
+ }
80
+ }
81
+ throw Error("all options failed")[cl.mod]({ codecParse: true, desc: "oneOf", chain, ctx, args: val2, errs });
82
+ })();
83
+ return result;
84
+ } else if (codec2.type === "any") {
85
+ return val2;
86
+ }
87
+ if (0) /* @__PURE__ */ ((v) => void 0)();
88
+ })();
89
+ if (codec2.map) {
90
+ try {
91
+ return codec2.map(checked);
92
+ } catch (err) {
93
+ if (!err.codecParse) throw err;
94
+ throw err[cl.mod]((msg) => ({ msg: `map failed (${msg})`, chain, ctx, fn: codec2.map }));
95
+ }
96
+ } else {
97
+ return checked;
98
+ }
99
+ };
100
+ return parse(codec, val, [], {});
101
+ };
102
+ export {
103
+ main_default as default
104
+ };
@@ -0,0 +1,2 @@
1
+ declare global {}
2
+ export {};
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@gershy/util-codec-parse",
3
- "version": "0.0.3",
3
+ "version": "0.0.5",
4
4
  "description": "TODO",
5
5
  "keywords": [
6
6
  "TODO"
@@ -16,7 +16,7 @@
16
16
  "homepage": "https://github.com/gershyNpm/utilCodecParse#readme",
17
17
  "license": "ISC",
18
18
  "peerDependencies": {
19
- "@gershy/clearing": "^0.0.36"
19
+ "@gershy/clearing": "^0.0.41"
20
20
  },
21
21
  "devDependencies": {
22
22
  "@types/node": "^24.10.1",
@@ -32,7 +32,7 @@
32
32
  "types": "./cmp/mjs/main.d.ts",
33
33
  "exports": {
34
34
  ".": {
35
- "import": "./cmp/mjs/main.js",
35
+ "import": "./cmp/esm/main.js",
36
36
  "require": "./cmp/cjs/main.js"
37
37
  }
38
38
  },
package/cmp/mjs/main.js DELETED
@@ -1,111 +0,0 @@
1
- import '@gershy/clearing';
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
- };
File without changes
File without changes