xdbc 1.0.93 → 1.0.95
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/.parcel-cache/bf96c58b6061a62a-BundleGraph +0 -0
- package/.parcel-cache/d7c812d65aeeac59-AssetGraph +0 -0
- package/.parcel-cache/data.mdb +0 -0
- package/.parcel-cache/e81759c1f106a17f-RequestGraph +0 -0
- package/.parcel-cache/fe0db3c4eb428be2-AssetGraph +0 -0
- package/.parcel-cache/lock.mdb +0 -0
- package/.parcel-cache/snapshot-e81759c1f106a17f.txt +4609 -0
- package/CODE_OF_CONDUCT.md +128 -0
- package/CONTRIBUTING.md +40 -0
- package/FUNDING.yml +3 -0
- package/LICENSE +21 -0
- package/README.md +96 -0
- package/SECURITY.md +36 -0
- package/Test.html +1 -1
- package/__tests__/DBC/AE.test.ts +62 -0
- package/__tests__/DBC/EQ.test.ts +13 -0
- package/__tests__/DBC/GREATER.test.ts +29 -0
- package/__tests__/DBC/INSTANCE.test.ts +13 -0
- package/__tests__/DBC/JSON.OP.test.ts +47 -0
- package/__tests__/DBC/JSON.Parse.test.ts +17 -0
- package/__tests__/DBC/OR.test.ts +14 -0
- package/__tests__/DBC/REGEX.test.ts +13 -0
- package/__tests__/DBC/TYPE.test.ts +13 -0
- package/babel.rc +12 -0
- package/dist/DBC/AE.js +39 -23
- package/dist/DBC/COMPARISON/GREATER.js +22 -0
- package/dist/DBC/COMPARISON.js +99 -0
- package/dist/DBC/EQ.js +21 -7
- package/dist/DBC/GREATER.js +19 -6
- package/dist/DBC/INSTANCE.js +22 -9
- package/dist/DBC/JSON.OP.js +19 -7
- package/dist/DBC/JSON.Parse.js +19 -7
- package/dist/DBC/OR.js +35 -23
- package/dist/DBC/REGEX.js +21 -9
- package/dist/DBC/TYPE.js +19 -7
- package/dist/DBC.js +112 -39
- package/dist/Demo.js +267 -0
- package/dist/bundle.js +1329 -0
- package/docs/.nojekyll +1 -0
- package/docs/assets/hierarchy.js +1 -0
- package/docs/assets/highlight.css +22 -0
- package/docs/assets/icons.js +18 -0
- package/docs/assets/icons.svg +1 -0
- package/docs/assets/main.js +60 -0
- package/docs/assets/navigation.js +1 -0
- package/docs/assets/search.js +1 -0
- package/docs/assets/style.css +1640 -0
- package/docs/classes/DBC.DBC.html +98 -0
- package/docs/classes/DBC_AE.AE.html +160 -0
- package/docs/classes/DBC_EQ.EQ.html +131 -0
- package/docs/classes/DBC_GREATER.GREATER.html +139 -0
- package/docs/classes/DBC_INSTANCE.INSTANCE.html +130 -0
- package/docs/classes/DBC_JSON.OP.JSON_OP.html +138 -0
- package/docs/classes/DBC_JSON.Parse.JSON_Parse.html +129 -0
- package/docs/classes/DBC_OR.OR.html +137 -0
- package/docs/classes/DBC_REGEX.REGEX.html +136 -0
- package/docs/classes/DBC_TYPE.TYPE.html +130 -0
- package/docs/classes/Demo.Demo.html +14 -0
- package/docs/hierarchy.html +1 -0
- package/docs/index.html +1 -0
- package/docs/modules/DBC.html +1 -0
- package/docs/modules/DBC_AE.html +1 -0
- package/docs/modules/DBC_EQ.html +1 -0
- package/docs/modules/DBC_GREATER.html +1 -0
- package/docs/modules/DBC_INSTANCE.html +1 -0
- package/docs/modules/DBC_JSON.OP.html +1 -0
- package/docs/modules/DBC_JSON.Parse.html +1 -0
- package/docs/modules/DBC_OR.html +1 -0
- package/docs/modules/DBC_REGEX.html +1 -0
- package/docs/modules/DBC_TYPE.html +1 -0
- package/docs/modules/Demo.html +1 -0
- package/jest.config.ts +20 -0
- package/package.json +23 -5
- package/src/DBC/AE.ts +49 -24
- package/src/DBC/COMPARISON/GREATER.ts +38 -0
- package/src/DBC/COMPARISON/GREATER_OR_EQUAL.ts +38 -0
- package/src/DBC/COMPARISON/LESS.ts +38 -0
- package/src/DBC/COMPARISON/LESS_OR_EQUAL.ts +38 -0
- package/src/DBC/COMPARISON.ts +150 -0
- package/src/DBC/EQ.ts +26 -8
- package/src/DBC/GREATER.ts +30 -7
- package/src/DBC/INSTANCE.ts +26 -10
- package/src/DBC/JSON.OP.ts +29 -8
- package/src/DBC/JSON.Parse.ts +24 -8
- package/src/DBC/OR.ts +42 -24
- package/src/DBC/REGEX.ts +26 -10
- package/src/DBC/TYPE.ts +24 -8
- package/src/DBC.ts +138 -46
- package/src/Demo.ts +211 -0
- package/tsconfig.json +3 -1
- package/typedoc.json +16 -0
- package/webpack.config.js +26 -0
- package/dist/DBC.min.js +0 -2
- package/dist/test.js +0 -110
- package/src/test.ts +0 -61
- package/test.js +0 -14
- package/test.ts +0 -11
package/src/DBC/OR.ts
CHANGED
|
@@ -1,18 +1,17 @@
|
|
|
1
|
-
import { DBC } from "../DBC
|
|
1
|
+
import { DBC } from "../DBC";
|
|
2
2
|
/**
|
|
3
3
|
* A {@link DBC } defining that all elements of an {@link object }s have to fulfill
|
|
4
|
-
* one of the given {@link object }s check-methods (( toCheck : any ) => boolean | string ).
|
|
4
|
+
* one of the given {@link object }s check-methods (**( toCheck : any ) => boolean | string** ).
|
|
5
5
|
*
|
|
6
6
|
* @remarks
|
|
7
7
|
* Maintainer: Callari, Salvatore (XDBC@WaXCode.net) */
|
|
8
8
|
export class OR extends DBC {
|
|
9
9
|
// #region Condition checking.
|
|
10
10
|
/**
|
|
11
|
-
* Checks
|
|
12
|
-
* the "value" itself will be checked.
|
|
11
|
+
* Checks the **value** against the given **conditions**
|
|
13
12
|
*
|
|
14
|
-
* @param conditions The { check: (toCheck: any) => boolean | string }
|
|
15
|
-
* @param value Either
|
|
13
|
+
* @param conditions The **{ check: (toCheck: any) => boolean | string }**-{@link object }s to check the **value** against.
|
|
14
|
+
* @param value Either **value**-{@link Array < any >}, which's elements will be checked, or the value to be
|
|
16
15
|
* checked itself.
|
|
17
16
|
* @param index If specified with "idxEnd" being undefined, this {@link Number } will be seen as the index of
|
|
18
17
|
* the value-{@link Array }'s element to check. If value isn't an {@link Array } this parameter
|
|
@@ -21,9 +20,9 @@ export class OR extends DBC {
|
|
|
21
20
|
* check within the value-{@link Array }.
|
|
22
21
|
* @param idxEnd Indicates the last element's index (including) of the span of value-{@link Array } elements to check.
|
|
23
22
|
* Setting this parameter to -1 specifies that all value-{@link Array }'s elements beginning from the
|
|
24
|
-
* specified
|
|
23
|
+
* specified **index** shall be checked.
|
|
25
24
|
*
|
|
26
|
-
* @returns TRUE if at least one of the provided
|
|
25
|
+
* @returns TRUE if at least one of the provided **conditions** is fulfilled, otherwise a {@link string } containing all **conditions** returned {@link string }s separated by " || ". */
|
|
27
26
|
public static checkAlgorithm(
|
|
28
27
|
conditions: Array<{
|
|
29
28
|
check: (toCheck: unknown | undefined | null | object) => boolean | string;
|
|
@@ -35,32 +34,32 @@ export class OR extends DBC {
|
|
|
35
34
|
for (let i = 0; i < conditions.length; i++) {
|
|
36
35
|
const conditionResult = conditions[i].check(value);
|
|
37
36
|
|
|
38
|
-
if (typeof conditionResult
|
|
39
|
-
result += `${conditionResult}${i === conditions.length - 1 ? "" : "
|
|
37
|
+
if (typeof conditionResult === "string") {
|
|
38
|
+
result += `${conditionResult}${i === conditions.length - 1 ? "" : " or "}`;
|
|
40
39
|
} else {
|
|
41
40
|
return true;
|
|
42
41
|
}
|
|
43
42
|
}
|
|
44
43
|
|
|
45
|
-
return
|
|
44
|
+
return result;
|
|
46
45
|
}
|
|
47
46
|
/**
|
|
48
47
|
* A parameter-decorator factory using the {@link OR.checkAlgorithm } with either multiple or a single one
|
|
49
|
-
* of the
|
|
50
|
-
* When specifying an
|
|
51
|
-
* element at the specified
|
|
48
|
+
* of the **realConditions** to check the tagged parameter-value against with.
|
|
49
|
+
* When specifying an **index** and the tagged parameter's **value** is an {@link Array }, the **realConditions** apply to the
|
|
50
|
+
* element at the specified **index**.
|
|
52
51
|
* If the {@link Array } is too short the currently processed { check: (toCheck: any) => boolean | string } of
|
|
53
|
-
*
|
|
54
|
-
* If an
|
|
55
|
-
* If
|
|
56
|
-
* against the
|
|
52
|
+
* **realConditions** will be verified to TRUE automatically, considering optional parameters.
|
|
53
|
+
* If an **index** is specified but the tagged parameter's value isn't an array, the **index** is treated as being undefined.
|
|
54
|
+
* If **index** is undefined and the tagged parameter's value is an {@link Array } each element of it will be checked
|
|
55
|
+
* against the **realConditions**.
|
|
57
56
|
*
|
|
58
|
-
* @param realConditions Either one or more { check: (toCheck: any) => boolean | string } to check the tagged parameter-value
|
|
57
|
+
* @param realConditions Either one or more **{ check: (toCheck: any) => boolean | string }** to check the tagged parameter-value
|
|
59
58
|
* against with.
|
|
60
59
|
* @param path See {@link DBC.decPrecondition }.
|
|
61
60
|
* @param dbc See {@link DBC.decPrecondition }.
|
|
62
61
|
*
|
|
63
|
-
* @returns A {@link string } as soon as one { check: (toCheck: any) => boolean | string } of
|
|
62
|
+
* @returns A {@link string } as soon as one { check: (toCheck: any) => boolean | string } of **realConditions** returns one.
|
|
64
63
|
* Otherwise TRUE. */
|
|
65
64
|
public static PRE(
|
|
66
65
|
conditions: Array<{
|
|
@@ -88,14 +87,14 @@ export class OR extends DBC {
|
|
|
88
87
|
}
|
|
89
88
|
/**
|
|
90
89
|
* A method-decorator factory using the {@link OR.checkAlgorithm } with either multiple or a single one
|
|
91
|
-
* of the
|
|
90
|
+
* of the **realConditions** to check the tagged method's return-value against with.
|
|
92
91
|
*
|
|
93
92
|
* @param realConditions Either one or more { check: (toCheck: any) => boolean | string } to check the tagged parameter-value
|
|
94
93
|
* against with.
|
|
95
94
|
* @param path See {@link DBC.decPrecondition }.
|
|
96
95
|
* @param dbc See {@link DBC.decPrecondition }.
|
|
97
96
|
*
|
|
98
|
-
* @returns A {@link string } as soon as one { check: (toCheck: any) => boolean | string } of
|
|
97
|
+
* @returns A {@link string } as soon as one **{ check: (toCheck: any) => boolean | string }** of **realConditions** return one.
|
|
99
98
|
* Otherwise TRUE. */
|
|
100
99
|
public static POST(
|
|
101
100
|
conditions: Array<{
|
|
@@ -116,18 +115,37 @@ export class OR extends DBC {
|
|
|
116
115
|
path,
|
|
117
116
|
);
|
|
118
117
|
}
|
|
118
|
+
/**
|
|
119
|
+
* A field-decorator factory using the {@link OR.checkAlgorithm } with either multiple or a single one
|
|
120
|
+
* of the **realConditions** to check the tagged field.
|
|
121
|
+
*
|
|
122
|
+
* @param realConditions Either one or more { check: (toCheck: any) => boolean | string } to check the tagged parameter-value
|
|
123
|
+
* against with.
|
|
124
|
+
* @param path See {@link DBC.decInvariant }.
|
|
125
|
+
* @param dbc See {@link DBC.decInvariant }.
|
|
126
|
+
*
|
|
127
|
+
* @returns See {@link DBC.decInvariant }. */
|
|
128
|
+
public static INVARIANT(
|
|
129
|
+
conditions: Array<{
|
|
130
|
+
check: (toCheck: unknown | undefined | null | object) => boolean | string;
|
|
131
|
+
}>,
|
|
132
|
+
path: string | undefined = undefined,
|
|
133
|
+
dbc = "WaXCode.DBC",
|
|
134
|
+
) {
|
|
135
|
+
return DBC.decInvariant([new OR(conditions)], path, dbc);
|
|
136
|
+
}
|
|
119
137
|
// #endregion Condition checking.
|
|
120
138
|
// #region Referenced Condition checking.
|
|
121
139
|
//
|
|
122
140
|
// For usage in dynamic scenarios (like global functions).
|
|
123
141
|
//
|
|
124
142
|
/**
|
|
125
|
-
* Invokes the {@link OR.checkAlgorithm } passing the value
|
|
143
|
+
* Invokes the {@link OR.checkAlgorithm } passing the value **toCheck** and {@link OR.conditions }.
|
|
126
144
|
*
|
|
127
145
|
* @param toCheck See {@link OR.checkAlgorithm }.
|
|
128
146
|
*
|
|
129
147
|
* @returns See {@link OR.checkAlgorithm}. */
|
|
130
|
-
public
|
|
148
|
+
public check(toCheck: unknown | null | undefined) {
|
|
131
149
|
return OR.checkAlgorithm(this.conditions, toCheck);
|
|
132
150
|
}
|
|
133
151
|
/**
|
package/src/DBC/REGEX.ts
CHANGED
|
@@ -20,13 +20,13 @@ export class REGEX extends DBC {
|
|
|
20
20
|
};
|
|
21
21
|
// #region Condition checking.
|
|
22
22
|
/**
|
|
23
|
-
* Checks if the value
|
|
23
|
+
* Checks if the value **toCheck** is complies to the {@link RegExp } **expression**.
|
|
24
24
|
*
|
|
25
|
-
* @param toCheck The value that has comply to the {@link RegExp }
|
|
26
|
-
* @param expression The {@link RegExp } the one
|
|
25
|
+
* @param toCheck The value that has comply to the {@link RegExp } **expression** for this {@link DBC } to be fulfilled.
|
|
26
|
+
* @param expression The {@link RegExp } the one **toCheck** has comply to in order for this {@link DBC } to be
|
|
27
27
|
* fulfilled.
|
|
28
28
|
*
|
|
29
|
-
* @returns TRUE if the value
|
|
29
|
+
* @returns TRUE if the value **toCheck** complies with the {@link RegExp } **expression**, otherwise FALSE. */
|
|
30
30
|
public static checkAlgorithm(
|
|
31
31
|
toCheck: unknown | null | undefined,
|
|
32
32
|
expression: RegExp,
|
|
@@ -94,18 +94,34 @@ export class REGEX extends DBC {
|
|
|
94
94
|
path,
|
|
95
95
|
);
|
|
96
96
|
}
|
|
97
|
+
/**
|
|
98
|
+
* A field-decorator factory using the {@link REGEX.checkAlgorithm } to determine whether this {@link DBC } is fulfilled
|
|
99
|
+
* by the tagged field.
|
|
100
|
+
*
|
|
101
|
+
* @param expression See {@link REGEX.checkAlgorithm }.
|
|
102
|
+
* @param path See {@link DBC.decInvariant }.
|
|
103
|
+
* @param dbc See {@link DBC.decInvariant }.
|
|
104
|
+
*
|
|
105
|
+
* @returns See {@link DBC.decInvariant }. */
|
|
106
|
+
public static INVARIANT(
|
|
107
|
+
expression: RegExp,
|
|
108
|
+
path: string | undefined = undefined,
|
|
109
|
+
dbc = "WaXCode.DBC",
|
|
110
|
+
) {
|
|
111
|
+
return DBC.decInvariant([new REGEX(expression)], path, dbc);
|
|
112
|
+
}
|
|
97
113
|
// #endregion Condition checking.
|
|
98
114
|
// #region Referenced Condition checking.
|
|
99
115
|
//
|
|
100
116
|
// For usage in dynamic scenarios (like with AE-DBC).
|
|
101
117
|
//
|
|
102
118
|
/**
|
|
103
|
-
* Invokes the {@link
|
|
119
|
+
* Invokes the {@link REGEX.checkAlgorithm } passing the value **toCheck** and {@link REGEX.equivalent }.
|
|
104
120
|
*
|
|
105
|
-
* @param toCheck See {@link
|
|
121
|
+
* @param toCheck See {@link REGEX.checkAlgorithm }.
|
|
106
122
|
*
|
|
107
123
|
* @returns See {@link EQ.checkAlgorithm}. */
|
|
108
|
-
public
|
|
124
|
+
public check(toCheck: unknown | null | undefined) {
|
|
109
125
|
return REGEX.checkAlgorithm(toCheck, this.expression);
|
|
110
126
|
}
|
|
111
127
|
/**
|
|
@@ -118,10 +134,10 @@ export class REGEX extends DBC {
|
|
|
118
134
|
// #endregion Referenced Condition checking.
|
|
119
135
|
// #region In-Method checking.
|
|
120
136
|
/**
|
|
121
|
-
* Invokes the {@link
|
|
137
|
+
* Invokes the {@link REGEX.checkAlgorithm } passing the value **toCheck** and {@link REGEX.expression }.
|
|
122
138
|
*
|
|
123
|
-
* @param toCheck See {@link
|
|
124
|
-
* @param expression See {@link
|
|
139
|
+
* @param toCheck See {@link REGEX.checkAlgorithm}.
|
|
140
|
+
* @param expression See {@link REGEX.checkAlgorithm}.
|
|
125
141
|
*/
|
|
126
142
|
public static check(toCheck: unknown | null | undefined, expression: RegExp) {
|
|
127
143
|
const checkResult = REGEX.checkAlgorithm(toCheck, expression);
|
package/src/DBC/TYPE.ts
CHANGED
|
@@ -1,18 +1,18 @@
|
|
|
1
|
-
import { DBC } from "../DBC
|
|
1
|
+
import { DBC } from "../DBC";
|
|
2
2
|
/**
|
|
3
|
-
* A {@link DBC } defining that
|
|
3
|
+
* A {@link DBC } defining that an {@link object }s gotta be of certain {@link TYPE.type }.
|
|
4
4
|
*
|
|
5
5
|
* @remarks
|
|
6
6
|
* Author: Salvatore Callari (Callari@WaXCode.net) / 2025
|
|
7
7
|
* Maintainer: Salvatore Callari (XDBC@WaXCode.net) */
|
|
8
8
|
export class TYPE extends DBC {
|
|
9
9
|
/**
|
|
10
|
-
* Checks if the value
|
|
10
|
+
* Checks if the value **toCheck** is of the **type** specified.
|
|
11
11
|
*
|
|
12
|
-
* @param toCheck The {@link Object } which'
|
|
13
|
-
* @param type The type the {@link object}
|
|
12
|
+
* @param toCheck The {@link Object } which's **type** to check.
|
|
13
|
+
* @param type The type the {@link object} **toCheck** has to be of.
|
|
14
14
|
*
|
|
15
|
-
* @returns TRUE if the value
|
|
15
|
+
* @returns TRUE if the value **toCheck** is of the specified **type**, otherwise FALSE. */
|
|
16
16
|
// biome-ignore lint/suspicious/noExplicitAny: Necessary for dynamic type checking of also UNDEFINED.
|
|
17
17
|
public static checkAlgorithm(toCheck: any, type: string): boolean | string {
|
|
18
18
|
// biome-ignore lint/suspicious/useValidTypeof: Necessary
|
|
@@ -79,19 +79,35 @@ export class TYPE extends DBC {
|
|
|
79
79
|
path,
|
|
80
80
|
);
|
|
81
81
|
}
|
|
82
|
+
/**
|
|
83
|
+
* A field-decorator factory using the {@link TYPE.checkAlgorithm } to determine whether this {@link DBC } is fulfilled
|
|
84
|
+
* by the tagged field.
|
|
85
|
+
*
|
|
86
|
+
* @param type See {@link TYPE.checkAlgorithm }.
|
|
87
|
+
* @param path See {@link DBC.decInvariant }.
|
|
88
|
+
* @param dbc See {@link DBC.decInvariant }.
|
|
89
|
+
*
|
|
90
|
+
* @returns See {@link DBC.decInvariant }. */
|
|
91
|
+
public static INVARIANT(
|
|
92
|
+
type: string,
|
|
93
|
+
path: string | undefined = undefined,
|
|
94
|
+
dbc = "WaXCode.DBC",
|
|
95
|
+
) {
|
|
96
|
+
return DBC.decInvariant([new TYPE(type)], path, dbc);
|
|
97
|
+
}
|
|
82
98
|
// #endregion Condition checking.
|
|
83
99
|
// #region Referenced Condition checking.
|
|
84
100
|
//
|
|
85
101
|
// For usage in dynamic scenarios (like with AE-DBC).
|
|
86
102
|
//
|
|
87
103
|
/**
|
|
88
|
-
* Invokes the {@link TYPE.checkAlgorithm } passing the value
|
|
104
|
+
* Invokes the {@link TYPE.checkAlgorithm } passing the value **toCheck** and the {@link TYPE.type } .
|
|
89
105
|
*
|
|
90
106
|
* @param toCheck See {@link TYPE.checkAlgorithm }.
|
|
91
107
|
*
|
|
92
108
|
* @returns See {@link TYPE.checkAlgorithm}. */
|
|
93
109
|
// biome-ignore lint/suspicious/noExplicitAny: <explanation>
|
|
94
|
-
public
|
|
110
|
+
public check(toCheck: any) {
|
|
95
111
|
return TYPE.checkAlgorithm(toCheck, this.type);
|
|
96
112
|
}
|
|
97
113
|
/**
|
package/src/DBC.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
/**
|
|
2
|
-
* Provides a
|
|
2
|
+
* Provides a **D**esign **B**y **C**ontract Framework using decorators.
|
|
3
3
|
*
|
|
4
4
|
* @remarks
|
|
5
5
|
* Maintainer: Callari, Salvatore (XDBC@WaXCode.net) */
|
|
@@ -76,7 +76,8 @@ export class DBC {
|
|
|
76
76
|
return undefined;
|
|
77
77
|
}
|
|
78
78
|
/**
|
|
79
|
-
* A method-decorator factory checking the {@link paramValueRequests } for value-requests of the method's parameter
|
|
79
|
+
* A method-decorator factory checking the {@link paramValueRequests } for value-requests of the method's parameter thus
|
|
80
|
+
* also usable on setters.
|
|
80
81
|
* When found it will invoke the "receptor" registered there, inter alia by {@link requestParamValue }, with the
|
|
81
82
|
* parameter's value.
|
|
82
83
|
*
|
|
@@ -115,7 +116,7 @@ export class DBC {
|
|
|
115
116
|
}
|
|
116
117
|
// #endregion Check if a value of one of the method's parameter has been requested and pass it to the
|
|
117
118
|
// receptor, if so.
|
|
118
|
-
// biome-ignore lint/complexity/noThisInStatic:
|
|
119
|
+
// biome-ignore lint/complexity/noThisInStatic: Necessary.
|
|
119
120
|
return originalMethod.apply(this, args);
|
|
120
121
|
};
|
|
121
122
|
|
|
@@ -124,39 +125,65 @@ export class DBC {
|
|
|
124
125
|
// #endregion Parameter-value requests.
|
|
125
126
|
// #region Invariant
|
|
126
127
|
/**
|
|
128
|
+
* A property-decorator factory serving as a **D**esign **B**y **C**ontract Invariant.
|
|
129
|
+
* Since the value must be initialized or set according to the specified **contracts** the value will only be checked
|
|
130
|
+
* when assigning it.
|
|
127
131
|
*
|
|
128
|
-
* @param
|
|
129
|
-
* @returns
|
|
130
|
-
*/
|
|
131
|
-
check(toCheck: unknown | null | undefined): boolean | string {
|
|
132
|
-
return false;
|
|
133
|
-
}
|
|
134
|
-
/**
|
|
132
|
+
* @param contracts The {@link DBC }-Contracts the value shall uphold.
|
|
135
133
|
*
|
|
136
|
-
* @
|
|
137
|
-
*
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
134
|
+
* @throws A {@link DBC.Infringement } whenever the property is tried to be set to a value that does not comply to the
|
|
135
|
+
* specified **contracts**, by the returned method.*/
|
|
136
|
+
public static decInvariant(
|
|
137
|
+
contracts: Array<{
|
|
138
|
+
check: (toCheck: unknown | null | undefined) => boolean | string;
|
|
139
|
+
}>,
|
|
140
|
+
path: string | undefined = undefined,
|
|
141
|
+
dbc = "WaXCode.DBC",
|
|
142
|
+
) {
|
|
143
|
+
return (target: unknown, propertyKey: string | symbol) => {
|
|
144
|
+
// biome-ignore lint/suspicious/noExplicitAny: Necessary to intercept UNDEFINED and NULL.
|
|
145
|
+
let value: any;
|
|
146
|
+
// #region Replace original property.
|
|
147
|
+
Object.defineProperty(target, propertyKey, {
|
|
148
|
+
set(newValue) {
|
|
149
|
+
const realValue = path ? DBC.resolve(newValue, path) : newValue;
|
|
150
|
+
// #region Check if all "contracts" are fulfilled.
|
|
151
|
+
for (const contract of contracts) {
|
|
152
|
+
const result = contract.check(realValue);
|
|
153
|
+
|
|
154
|
+
if (typeof result === "string") {
|
|
155
|
+
DBC.resolveDBCPath(window, dbc).reportFieldInfringement(
|
|
156
|
+
result,
|
|
157
|
+
target as object,
|
|
158
|
+
path,
|
|
159
|
+
propertyKey as string,
|
|
160
|
+
realValue,
|
|
161
|
+
);
|
|
162
|
+
}
|
|
163
|
+
}
|
|
164
|
+
// #endregion Check if all "contracts" are fulfilled.
|
|
165
|
+
value = newValue;
|
|
166
|
+
},
|
|
167
|
+
enumerable: true,
|
|
168
|
+
configurable: true,
|
|
169
|
+
});
|
|
170
|
+
// #endregion Replace original property.
|
|
147
171
|
};
|
|
148
172
|
}
|
|
149
173
|
// #endregion Invariant
|
|
150
174
|
// #region Postcondition
|
|
151
175
|
/**
|
|
176
|
+
* A method decorator factory checking the result of a method whenever it is invoked thus also usable on getters.
|
|
152
177
|
*
|
|
153
|
-
* @param check
|
|
154
|
-
* @param dbc
|
|
155
|
-
* @param path
|
|
156
|
-
*
|
|
178
|
+
* @param check The **(toCheck: any, object, string) => boolean | string** to use for checking.
|
|
179
|
+
* @param dbc See {@link DBC.resolveDBCPath }.
|
|
180
|
+
* @param path The dotted path referring to the actual value to check, starting form the specified one.
|
|
181
|
+
*
|
|
182
|
+
* @returns The **( target : object, propertyKey : string, descriptor : PropertyDescriptor ) : PropertyDescriptor**
|
|
183
|
+
* invoked by Typescript.
|
|
157
184
|
*/
|
|
158
185
|
public static decPostcondition(
|
|
159
|
-
// biome-ignore lint/suspicious/noExplicitAny:
|
|
186
|
+
// biome-ignore lint/suspicious/noExplicitAny: Necessary to intercept UNDEFINED and NULL.
|
|
160
187
|
check: (toCheck: any, object, string) => boolean | string,
|
|
161
188
|
dbc: string,
|
|
162
189
|
path: string | undefined = undefined,
|
|
@@ -167,15 +194,11 @@ export class DBC {
|
|
|
167
194
|
descriptor: PropertyDescriptor,
|
|
168
195
|
): PropertyDescriptor => {
|
|
169
196
|
const originalMethod = descriptor.value;
|
|
170
|
-
// biome-ignore lint/suspicious/noExplicitAny:
|
|
197
|
+
// biome-ignore lint/suspicious/noExplicitAny: Necessary to intercept UNDEFINED and NULL.
|
|
171
198
|
descriptor.value = (...args: any[]) => {
|
|
172
199
|
// biome-ignore lint/complexity/noThisInStatic: <explanation>
|
|
173
200
|
const result = originalMethod.apply(this, args);
|
|
174
|
-
const realValue = path
|
|
175
|
-
? path
|
|
176
|
-
?.split(".")
|
|
177
|
-
.reduce((accumulator, current) => accumulator[current], result)
|
|
178
|
-
: result;
|
|
201
|
+
const realValue = path ? DBC.resolve(result, path) : result;
|
|
179
202
|
const checkResult = check(realValue, target, propertyKey);
|
|
180
203
|
|
|
181
204
|
if (typeof checkResult === "string") {
|
|
@@ -196,17 +219,17 @@ export class DBC {
|
|
|
196
219
|
}
|
|
197
220
|
// #endregion Postcondition
|
|
198
221
|
// #region Decorator
|
|
199
|
-
// #region
|
|
222
|
+
// #region Precondition
|
|
200
223
|
/**
|
|
201
224
|
* A parameter-decorator factory that requests the tagged parameter's value passing it to the provided
|
|
202
225
|
* "check"-method when the value becomes available.
|
|
203
226
|
*
|
|
204
227
|
* @param check The "( unknown ) => void" to be invoked along with the tagged parameter's value as soon
|
|
205
228
|
* as it becomes available.
|
|
206
|
-
* @param path The dotted path referring to the actual value to check, starting form the specified one.
|
|
207
229
|
* @param dbc See {@link DBC.resolveDBCPath }.
|
|
230
|
+
* @param path The dotted path referring to the actual value to check, starting form the specified one.
|
|
208
231
|
*
|
|
209
|
-
* @returns The
|
|
232
|
+
* @returns The **(target: object, methodName: string | symbol, parameterIndex: number ) => void** invoked by Typescript- */
|
|
210
233
|
protected static decPrecondition(
|
|
211
234
|
check: (unknown, object, string, number) => boolean | string,
|
|
212
235
|
dbc: string,
|
|
@@ -226,11 +249,7 @@ export class DBC {
|
|
|
226
249
|
methodName,
|
|
227
250
|
parameterIndex,
|
|
228
251
|
(value: unknown) => {
|
|
229
|
-
const realValue = path
|
|
230
|
-
? path
|
|
231
|
-
?.split(".")
|
|
232
|
-
.reduce((accumulator, current) => accumulator[current], value)
|
|
233
|
-
: value;
|
|
252
|
+
const realValue = path ? DBC.resolve(value, path) : value;
|
|
234
253
|
const result = check(realValue, target, methodName, parameterIndex);
|
|
235
254
|
|
|
236
255
|
if (typeof result === "string") {
|
|
@@ -247,7 +266,7 @@ export class DBC {
|
|
|
247
266
|
);
|
|
248
267
|
};
|
|
249
268
|
}
|
|
250
|
-
// #endregion
|
|
269
|
+
// #endregion Precondition
|
|
251
270
|
// #endregion Decorator
|
|
252
271
|
// #region Warning handling.
|
|
253
272
|
/** Stores settings concerning warnings. */
|
|
@@ -265,7 +284,7 @@ export class DBC {
|
|
|
265
284
|
}
|
|
266
285
|
// #endregion Warning handling.
|
|
267
286
|
// #region infringement handling.
|
|
268
|
-
/** Stores the
|
|
287
|
+
/** Stores the settings concerning infringements */
|
|
269
288
|
public infringementSettings: {
|
|
270
289
|
throwException: boolean;
|
|
271
290
|
logToConsole: boolean;
|
|
@@ -293,7 +312,7 @@ export class DBC {
|
|
|
293
312
|
}
|
|
294
313
|
}
|
|
295
314
|
/**
|
|
296
|
-
* Reports a parameter-infringement
|
|
315
|
+
* Reports a parameter-infringement via {@link reportInfringement } also generating a proper {@link string }-wrapper
|
|
297
316
|
* for the given "message","method", parameter-"index" & value.
|
|
298
317
|
*
|
|
299
318
|
* @param message The {@link string } describing the infringement and it's provenience.
|
|
@@ -317,6 +336,29 @@ export class DBC {
|
|
|
317
336
|
path,
|
|
318
337
|
);
|
|
319
338
|
}
|
|
339
|
+
/**
|
|
340
|
+
* Reports a field-infringement via {@link reportInfringement } also generating a proper {@link string }-wrapper
|
|
341
|
+
* for the given **message** & **name**.
|
|
342
|
+
*
|
|
343
|
+
* @param message A {@link string } describing the infringement and it's provenience.
|
|
344
|
+
* @param key The property key.
|
|
345
|
+
* @param path The dotted-path {@link string } that leads to the value not fulfilling the contract starting from
|
|
346
|
+
* the tagged one.
|
|
347
|
+
* @param value The value not fulfilling a contract. */
|
|
348
|
+
public reportFieldInfringement(
|
|
349
|
+
message: string,
|
|
350
|
+
target: object,
|
|
351
|
+
path: string,
|
|
352
|
+
key: string,
|
|
353
|
+
value: unknown,
|
|
354
|
+
): undefined {
|
|
355
|
+
this.reportInfringement(
|
|
356
|
+
`[ New value for "${key}"${path === undefined ? "" : `.${path}`} with value "${value}" did not fulfill one of it's contracts: ${message}]`,
|
|
357
|
+
key,
|
|
358
|
+
target,
|
|
359
|
+
path,
|
|
360
|
+
);
|
|
361
|
+
}
|
|
320
362
|
/**
|
|
321
363
|
* Reports a returnvalue-infringement according via {@link reportInfringement } also generating a proper {@link string }-wrapper
|
|
322
364
|
* for the given "message","method" & value.
|
|
@@ -354,14 +396,23 @@ export class DBC {
|
|
|
354
396
|
// #endregion Errors
|
|
355
397
|
// #endregion Classes
|
|
356
398
|
// #endregion infringement handling.
|
|
399
|
+
/**
|
|
400
|
+
* Resolves the specified dotted {@link string }-path to a {@link DBC }.
|
|
401
|
+
*
|
|
402
|
+
* @param obj The {@link object } to start resolving from.
|
|
403
|
+
* @param path The dotted {@link string }-path leading to the {@link DBC }.
|
|
404
|
+
*
|
|
405
|
+
* @returns The requested {@link DBC }.
|
|
406
|
+
*/
|
|
357
407
|
static resolveDBCPath = (obj, path): DBC =>
|
|
358
408
|
path
|
|
359
409
|
?.split(".")
|
|
360
410
|
.reduce((accumulator, current) => accumulator[current], obj);
|
|
361
411
|
/**
|
|
412
|
+
* Constructs this {@link DBC } by setting the {@link DBC.infringementSettings }, define the **WaXCode** namespace in
|
|
413
|
+
* **window** if not yet available and setting the property **DBC** in there to the instance of this {@link DBC }.
|
|
362
414
|
*
|
|
363
|
-
* @param infringementSettings
|
|
364
|
-
*/
|
|
415
|
+
* @param infringementSettings See {@link DBC.infringementSettings }. */
|
|
365
416
|
constructor(
|
|
366
417
|
infringementSettings: {
|
|
367
418
|
throwException: boolean;
|
|
@@ -375,6 +426,47 @@ export class DBC {
|
|
|
375
426
|
// biome-ignore lint/suspicious/noExplicitAny: <explanation>
|
|
376
427
|
(window as any).WaXCode.DBC = this;
|
|
377
428
|
}
|
|
378
|
-
|
|
429
|
+
/**
|
|
430
|
+
* Resolves the desired {@link object } out a given one **toResolveFrom** using the specified **path**.
|
|
431
|
+
*
|
|
432
|
+
* @param toResolveFrom The {@link object } starting to resolve from.
|
|
433
|
+
* @param path The dotted path-{@link string }.
|
|
434
|
+
* This string uses ., [...], and () to represent accessing nested properties,
|
|
435
|
+
* array elements/object keys, and calling methods, respectively, mimicking JavaScript syntax to navigate
|
|
436
|
+
* an object's structure. Code, e.g. something like a.b( 1 as number ).c, will not be executed and
|
|
437
|
+
* thus make the retrieval fail.
|
|
438
|
+
*
|
|
439
|
+
* @returns The requested {@link object }, NULL or UNDEFINED. */
|
|
440
|
+
public static resolve(toResolveFrom: unknown, path: string) {
|
|
441
|
+
if (!toResolveFrom || typeof path !== "string") {
|
|
442
|
+
return undefined;
|
|
443
|
+
}
|
|
444
|
+
|
|
445
|
+
const parts = path.replace(/\[(['"]?)(.*?)\1\]/g, ".$2").split("."); // Handle indexers
|
|
446
|
+
|
|
447
|
+
let current = toResolveFrom;
|
|
448
|
+
for (const part of parts) {
|
|
449
|
+
if (current === null || typeof current === "undefined") {
|
|
450
|
+
return undefined;
|
|
451
|
+
}
|
|
452
|
+
|
|
453
|
+
const methodMatch = part.match(/(\w+)\((.*)\)/);
|
|
454
|
+
if (methodMatch) {
|
|
455
|
+
const methodName = methodMatch[1];
|
|
456
|
+
const argsStr = methodMatch[2];
|
|
457
|
+
const args = argsStr.split(",").map((arg) => arg.trim()); // Simple argument parsing
|
|
458
|
+
if (typeof current[methodName] === "function") {
|
|
459
|
+
current = current[methodName].apply(current, args);
|
|
460
|
+
} else {
|
|
461
|
+
return undefined; // Method not found or not a function
|
|
462
|
+
}
|
|
463
|
+
} else {
|
|
464
|
+
current = current[part];
|
|
465
|
+
}
|
|
466
|
+
}
|
|
379
467
|
|
|
468
|
+
return current;
|
|
469
|
+
}
|
|
470
|
+
}
|
|
471
|
+
// Set the main instance with standard **DBC.infringementSettings**.
|
|
380
472
|
new DBC();
|