xdbc 1.0.96 → 1.0.97
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/package.json +1 -1
- package/src/DBC/IF.ts +147 -0
package/package.json
CHANGED
package/src/DBC/IF.ts
ADDED
|
@@ -0,0 +1,147 @@
|
|
|
1
|
+
import { DBC } from "../DBC";
|
|
2
|
+
/**
|
|
3
|
+
* A {@link DBC } defining that an {@link object } has also to comply to a certain {@link DBC } if it complies to
|
|
4
|
+
* another specified one.
|
|
5
|
+
*
|
|
6
|
+
* @remarks
|
|
7
|
+
* Maintainer: Callari, Salvatore (XDBC@WaXCode.net) */
|
|
8
|
+
export class IF extends DBC {
|
|
9
|
+
// #region Condition checking.
|
|
10
|
+
/**
|
|
11
|
+
* Checks if the value **toCheck** complies to the specified **condition** and if so does also comply to the one **inCase**.
|
|
12
|
+
*
|
|
13
|
+
* @param toCheck The value that has to be equal to it's possible **equivalent** for this {@link DBC } to be fulfilled.
|
|
14
|
+
* @param condition The contract **toCheck** has to comply to in order to also have to comply to the one **inCase**.
|
|
15
|
+
* @param inCase The contract **toCheck** has to also comply to if it complies to **condition**.
|
|
16
|
+
*
|
|
17
|
+
* @returns TRUE if the value **toCheck** and the **equivalent** are equal to each other, otherwise FALSE. */
|
|
18
|
+
public static checkAlgorithm(
|
|
19
|
+
// biome-ignore lint/suspicious/noExplicitAny: <explanation>
|
|
20
|
+
toCheck: any,
|
|
21
|
+
condition: {check: (toCheck: unknown | undefined | null | object) => boolean | string;},
|
|
22
|
+
inCase:{
|
|
23
|
+
check: (toCheck: unknown | undefined | null | object) => boolean | string;
|
|
24
|
+
},
|
|
25
|
+
invert,
|
|
26
|
+
): boolean | string {
|
|
27
|
+
if (!invert && condition.check(toCheck)&&!inCase.check(toCheck)) {
|
|
28
|
+
return `In case that the value complies to "${condition}" it also has to comply to "${inCase}"`;
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
if (!invert && !condition.check(toCheck)&&!inCase.check(toCheck)) {
|
|
32
|
+
return `In case that the value does not comply to "${condition}" it has to comply to "${inCase}"`;
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
return true;
|
|
36
|
+
}
|
|
37
|
+
/**
|
|
38
|
+
* A parameter-decorator factory using the {@link EQ.checkAlgorithm } to determine whether this {@link DBC } is fulfilled
|
|
39
|
+
* by the tagged parameter.
|
|
40
|
+
*
|
|
41
|
+
* @param condition See {@link IF.checkAlgorithm }.
|
|
42
|
+
* @param inCase See {@link IF.checkAlgorithm }.
|
|
43
|
+
* @param path See {@link DBC.decPrecondition }.
|
|
44
|
+
* @param dbc See {@link DBC.decPrecondition }.
|
|
45
|
+
*
|
|
46
|
+
* @returns See {@link DBC.decPrecondition }. */
|
|
47
|
+
public static PRE(
|
|
48
|
+
condition: {check: (toCheck: unknown | undefined | null | object) => boolean | string;},
|
|
49
|
+
inCase: {check: (toCheck: unknown | undefined | null | object) => boolean | string;},
|
|
50
|
+
invert = false,
|
|
51
|
+
path: string | undefined = undefined,
|
|
52
|
+
dbc = "WaXCode.DBC",
|
|
53
|
+
): (
|
|
54
|
+
target: object,
|
|
55
|
+
methodName: string | symbol,
|
|
56
|
+
parameterIndex: number,
|
|
57
|
+
) => void {
|
|
58
|
+
return DBC.decPrecondition(
|
|
59
|
+
(
|
|
60
|
+
value: object,
|
|
61
|
+
target: object,
|
|
62
|
+
methodName: string,
|
|
63
|
+
parameterIndex: number,
|
|
64
|
+
) => {
|
|
65
|
+
return IF.checkAlgorithm(value, condition, inCase, invert);
|
|
66
|
+
},
|
|
67
|
+
dbc,
|
|
68
|
+
path,
|
|
69
|
+
);
|
|
70
|
+
}
|
|
71
|
+
/**
|
|
72
|
+
* A method-decorator factory using the {@link IF.checkAlgorithm } to determine whether this {@link DBC } is fulfilled
|
|
73
|
+
* by the tagged method's returnvalue.
|
|
74
|
+
*
|
|
75
|
+
* @param condition See {@link IF.checkAlgorithm }.
|
|
76
|
+
* @param inCase See {@link IF.checkAlgorithm }.
|
|
77
|
+
* @param path See {@link DBC.Postcondition }.
|
|
78
|
+
* @param dbc See {@link DBC.decPostcondition }.
|
|
79
|
+
*
|
|
80
|
+
* @returns See {@link DBC.decPostcondition }. */
|
|
81
|
+
public static POST(
|
|
82
|
+
condition: {check: (toCheck: unknown | undefined | null | object) => boolean | string;},
|
|
83
|
+
inCase: {check: (toCheck: unknown | undefined | null | object) => boolean | string;},
|
|
84
|
+
invert = false,
|
|
85
|
+
path: string | undefined = undefined,
|
|
86
|
+
dbc = "WaXCode.DBC",
|
|
87
|
+
): (
|
|
88
|
+
target: object,
|
|
89
|
+
propertyKey: string,
|
|
90
|
+
descriptor: PropertyDescriptor,
|
|
91
|
+
) => PropertyDescriptor {
|
|
92
|
+
return DBC.decPostcondition(
|
|
93
|
+
(value: object, target: object, propertyKey: string) => {
|
|
94
|
+
return IF.checkAlgorithm(value, condition, inCase, invert);
|
|
95
|
+
},
|
|
96
|
+
dbc,
|
|
97
|
+
path,
|
|
98
|
+
);
|
|
99
|
+
}
|
|
100
|
+
/**
|
|
101
|
+
* A field-decorator factory using the {@link IF.checkAlgorithm } to determine whether this {@link DBC } is fulfilled
|
|
102
|
+
* by the tagged field.
|
|
103
|
+
*
|
|
104
|
+
* @param condition See {@link IF.checkAlgorithm }.
|
|
105
|
+
* @param inCase See {@link IF.checkAlgorithm }.
|
|
106
|
+
* @param path See {@link DBC.decInvariant }.
|
|
107
|
+
* @param dbc See {@link DBC.decInvariant }.
|
|
108
|
+
*
|
|
109
|
+
* @returns See {@link DBC.decInvariant }. */
|
|
110
|
+
public static INVARIANT(
|
|
111
|
+
condition: {check: (toCheck: unknown | undefined | null | object) => boolean | string;},
|
|
112
|
+
inCase: {check: (toCheck: unknown | undefined | null | object) => boolean | string;},
|
|
113
|
+
invert = false,
|
|
114
|
+
path: string | undefined = undefined,
|
|
115
|
+
dbc = "WaXCode.DBC",
|
|
116
|
+
) {
|
|
117
|
+
return DBC.decInvariant([new IF(condition, inCase, invert)], path, dbc);
|
|
118
|
+
}
|
|
119
|
+
// #endregion Condition checking.
|
|
120
|
+
// #region Referenced Condition checking.
|
|
121
|
+
//
|
|
122
|
+
// For usage in dynamic scenarios (like with AE-DBC).
|
|
123
|
+
//
|
|
124
|
+
/**
|
|
125
|
+
* Invokes the {@link IF.checkAlgorithm } passing the value **toCheck**, {@link IF.equivalent } and {@link IF.invert }.
|
|
126
|
+
*
|
|
127
|
+
* @param toCheck See {@link IF.checkAlgorithm }.
|
|
128
|
+
*
|
|
129
|
+
* @returns See {@link IF.checkAlgorithm}. */
|
|
130
|
+
// biome-ignore lint/suspicious/noExplicitAny: Necessary to check against NULL & UNDEFINED.
|
|
131
|
+
public check(toCheck: any) {
|
|
132
|
+
return IF.checkAlgorithm(toCheck, this.condition, this.inCase, this.invert);
|
|
133
|
+
}
|
|
134
|
+
/**
|
|
135
|
+
* Creates this {@link IF } by setting the protected property {@link IF.equivalent } used by {@link IF.check }.
|
|
136
|
+
*
|
|
137
|
+
* @param equivalent See {@link IF.check }. */
|
|
138
|
+
public constructor(
|
|
139
|
+
// biome-ignore lint/suspicious/noExplicitAny: To be able to match UNDEFINED and NULL.
|
|
140
|
+
protected condition: {check: (toCheck: unknown | undefined | null | object) => boolean | string;},
|
|
141
|
+
protected inCase: {check: (toCheck: unknown | undefined | null | object) => boolean | string;},
|
|
142
|
+
protected invert = false,
|
|
143
|
+
) {
|
|
144
|
+
super();
|
|
145
|
+
}
|
|
146
|
+
// #endregion Referenced Condition checking.
|
|
147
|
+
}
|