xjs-common 13.0.0-alpha.1 → 13.0.0-alpha.2
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/README.md +1 -1
- package/build/func/decorator/d-type.d.ts +51 -5
- package/build/func/decorator/d-type.js +23 -8
- package/build/func/u-obj.d.ts +1 -1
- package/build/func/u-obj.js +3 -3
- package/build/func/u-type.js +2 -2
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -1,12 +1,42 @@
|
|
|
1
1
|
import { Ctor, Type } from "../../const/types";
|
|
2
2
|
export declare const smbl_tm: unique symbol;
|
|
3
3
|
export interface TypeDesc {
|
|
4
|
+
/** express primitive type or `object` listed in {@link Type}. */
|
|
4
5
|
t?: Type;
|
|
6
|
+
/** express required. */
|
|
5
7
|
req?: boolean;
|
|
8
|
+
/** type description of each value in an array. */
|
|
6
9
|
ary?: TypeDesc;
|
|
10
|
+
/** express class of the property decorated with {@link DType}. */
|
|
11
|
+
cls?: Ctor;
|
|
12
|
+
/** type description of each value in a record object. */
|
|
7
13
|
rcd?: TypeDesc;
|
|
8
|
-
obj?: Ctor;
|
|
9
14
|
}
|
|
15
|
+
type BasicTypeDesc = TypeDesc & {
|
|
16
|
+
ary?: never;
|
|
17
|
+
obj?: never;
|
|
18
|
+
rec?: never;
|
|
19
|
+
rcd?: never;
|
|
20
|
+
};
|
|
21
|
+
type ArrayTypeDesc = TypeDesc & {
|
|
22
|
+
t?: never;
|
|
23
|
+
obj?: never;
|
|
24
|
+
rec?: never;
|
|
25
|
+
rcd?: never;
|
|
26
|
+
};
|
|
27
|
+
type ClassTypeDesc = TypeDesc & {
|
|
28
|
+
t?: never;
|
|
29
|
+
ary?: never;
|
|
30
|
+
rec?: never;
|
|
31
|
+
rcd?: never;
|
|
32
|
+
};
|
|
33
|
+
type RecordTypeDesc = TypeDesc & {
|
|
34
|
+
t?: never;
|
|
35
|
+
arys?: never;
|
|
36
|
+
obj?: never;
|
|
37
|
+
rec?: never;
|
|
38
|
+
};
|
|
39
|
+
type AnyTypeDesc = BasicTypeDesc | ArrayTypeDesc | ClassTypeDesc | RecordTypeDesc;
|
|
10
40
|
export interface TypeMap {
|
|
11
41
|
[k: string]: TypeDesc;
|
|
12
42
|
}
|
|
@@ -15,13 +45,29 @@ export interface TypeMap {
|
|
|
15
45
|
* and to be cropped by {@link UObj.crop}.
|
|
16
46
|
*/
|
|
17
47
|
export declare namespace DType {
|
|
48
|
+
/** express `string` property. */
|
|
18
49
|
function string(target: Object, propKey: string): void;
|
|
50
|
+
/** express `number` property. */
|
|
19
51
|
function number(target: Object, propKey: string): void;
|
|
52
|
+
/** express `boolean` property. */
|
|
20
53
|
function boolean(target: Object, propKey: string): void;
|
|
54
|
+
/** express required property. */
|
|
21
55
|
function required(target: Object, propKey: string): void;
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
56
|
+
/**
|
|
57
|
+
* express array.
|
|
58
|
+
* @param elmDesc {@link TypeDesc} or class constructor type.
|
|
59
|
+
*/
|
|
60
|
+
function array(elmDesc?: AnyTypeDesc | Ctor): (target: Object, propKey: string) => void;
|
|
61
|
+
/**
|
|
62
|
+
* express record object. note that this may allow array type because array is essentialy object type has properties.
|
|
63
|
+
* @param elmDesc {@link TypeDesc} or class constructor type.
|
|
64
|
+
*/
|
|
65
|
+
function record(elmDesc?: AnyTypeDesc | Ctor): (target: Object, propKey: string) => void;
|
|
66
|
+
/**
|
|
67
|
+
* express an object which has properties that specified class express with {@link DType}.
|
|
68
|
+
* @param ctor class constructor type.
|
|
69
|
+
*/
|
|
70
|
+
function object(ctor: Ctor): (target: Object, propKey: string) => void;
|
|
26
71
|
function keep(target: Object, propKey: string): void;
|
|
27
72
|
}
|
|
73
|
+
export {};
|
|
@@ -9,14 +9,17 @@ export const smbl_tm = Symbol.for("xjs:typeMap");
|
|
|
9
9
|
*/
|
|
10
10
|
export var DType;
|
|
11
11
|
(function (DType) {
|
|
12
|
+
/** express `string` property. */
|
|
12
13
|
function string(target, propKey) {
|
|
13
14
|
setTypeDesc(target, propKey, Type.string);
|
|
14
15
|
}
|
|
15
16
|
DType.string = string;
|
|
17
|
+
/** express `number` property. */
|
|
16
18
|
function number(target, propKey) {
|
|
17
19
|
setTypeDesc(target, propKey, Type.number);
|
|
18
20
|
}
|
|
19
21
|
DType.number = number;
|
|
22
|
+
/** express `boolean` property. */
|
|
20
23
|
function boolean(target, propKey) {
|
|
21
24
|
setTypeDesc(target, propKey, Type.boolean);
|
|
22
25
|
}
|
|
@@ -28,33 +31,45 @@ export var DType;
|
|
|
28
31
|
td.t = t;
|
|
29
32
|
});
|
|
30
33
|
}
|
|
34
|
+
/** express required property. */
|
|
31
35
|
function required(target, propKey) {
|
|
32
36
|
setDesc(target, propKey, (td) => td.req = true);
|
|
33
37
|
}
|
|
34
38
|
DType.required = required;
|
|
39
|
+
/**
|
|
40
|
+
* express array.
|
|
41
|
+
* @param elmDesc {@link TypeDesc} or class constructor type.
|
|
42
|
+
*/
|
|
35
43
|
function array(elmDesc = {}) {
|
|
36
|
-
return (target, propKey) => setDesc(target, propKey, (td) => UType.isFunction(elmDesc) ? td.ary = {
|
|
44
|
+
return (target, propKey) => setDesc(target, propKey, (td) => UType.isFunction(elmDesc) ? td.ary = { cls: elmDesc } : td.ary = elmDesc);
|
|
37
45
|
}
|
|
38
46
|
DType.array = array;
|
|
39
|
-
/**
|
|
47
|
+
/**
|
|
48
|
+
* express record object. note that this may allow array type because array is essentialy object type has properties.
|
|
49
|
+
* @param elmDesc {@link TypeDesc} or class constructor type.
|
|
50
|
+
*/
|
|
40
51
|
function record(elmDesc = {}) {
|
|
41
|
-
return (target, propKey) => setDesc(target, propKey, (td) => UType.isFunction(elmDesc) ? td.rcd = {
|
|
52
|
+
return (target, propKey) => setDesc(target, propKey, (td) => UType.isFunction(elmDesc) ? td.rcd = { cls: elmDesc } : td.rcd = elmDesc);
|
|
42
53
|
}
|
|
43
54
|
DType.record = record;
|
|
44
|
-
|
|
45
|
-
|
|
55
|
+
/**
|
|
56
|
+
* express an object which has properties that specified class express with {@link DType}.
|
|
57
|
+
* @param ctor class constructor type.
|
|
58
|
+
*/
|
|
59
|
+
function object(ctor) {
|
|
60
|
+
return (target, propKey) => setDesc(target, propKey, (td) => td.cls = ctor);
|
|
46
61
|
}
|
|
47
|
-
DType.
|
|
62
|
+
DType.object = object;
|
|
48
63
|
function keep(target, propKey) {
|
|
49
64
|
setDesc(target, propKey, (_) => { });
|
|
50
65
|
}
|
|
51
66
|
DType.keep = keep;
|
|
52
67
|
function setDesc(target, propKey, setter) {
|
|
53
68
|
const map = target[smbl_tm] ? Object.assign({}, target[smbl_tm]) : {};
|
|
54
|
-
map[propKey] ??= { t: null, req: false,
|
|
69
|
+
map[propKey] ??= { t: null, req: false, cls: null, ary: null, rcd: null };
|
|
55
70
|
const td = map[propKey];
|
|
56
71
|
setter(td);
|
|
57
|
-
const structualDescs = [[td.ary, "array"], [td.
|
|
72
|
+
const structualDescs = [[td.ary, "array"], [td.cls, "class"], [td.rcd, "record"]].filter(e => e[0]);
|
|
58
73
|
if (structualDescs.length > 0) {
|
|
59
74
|
let ex1 = null, ex2 = null;
|
|
60
75
|
if (td.t) {
|
package/build/func/u-obj.d.ts
CHANGED
|
@@ -17,7 +17,7 @@ export declare namespace UObj {
|
|
|
17
17
|
function crop<T extends NormalRecord>(o: T, keys: (keyof T)[], removeKeys?: boolean): Partial<T>;
|
|
18
18
|
/**
|
|
19
19
|
* crop properties that is not decorated with {@link DType}. the properties will be removed with `delete` operator.
|
|
20
|
-
* this treats constructual decorator such as {@link DType.
|
|
20
|
+
* this treats constructual decorator such as {@link DType.object} recursively.
|
|
21
21
|
* @param o object whose properties to be removed. if this is class object decorated with {@link DType}, it can omits `ctor` parameter.
|
|
22
22
|
* @param ctor class constructor type whose properties are decorated with {@link DType}. **NOTE** that need to have public constructor without any parameter.
|
|
23
23
|
*/
|
package/build/func/u-obj.js
CHANGED
|
@@ -28,10 +28,10 @@ export var UObj;
|
|
|
28
28
|
return removeKeys ? o : {};
|
|
29
29
|
Object.keys(o).filter(k => {
|
|
30
30
|
if (tm && tm[k] && o[k]) {
|
|
31
|
-
if (tm[k].
|
|
32
|
-
crop(o[k], tm[k]?.
|
|
31
|
+
if (tm[k].cls)
|
|
32
|
+
crop(o[k], tm[k]?.cls);
|
|
33
33
|
else {
|
|
34
|
-
const vCtor = tm[k].ary?.
|
|
34
|
+
const vCtor = tm[k].ary?.cls ?? tm[k].rcd?.cls;
|
|
35
35
|
Object.values(o[k]).forEach(v => crop(v, vCtor));
|
|
36
36
|
}
|
|
37
37
|
}
|
package/build/func/u-type.js
CHANGED
|
@@ -53,8 +53,8 @@ export var UType;
|
|
|
53
53
|
if (td.rcd)
|
|
54
54
|
return UType.isObject(prop)
|
|
55
55
|
? Object.entries(prop).flatMap(e => validateProp(e[0], e[1], td.rcd)).map(joinKey) : [k];
|
|
56
|
-
if (td.
|
|
57
|
-
return validate(prop, td.
|
|
56
|
+
if (td.cls)
|
|
57
|
+
return validate(prop, td.cls).flatMap(joinKey);
|
|
58
58
|
return [];
|
|
59
59
|
}
|
|
60
60
|
function takeAsArray(v) {
|