xdbc 1.0.211 → 1.0.213

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.
Files changed (88) hide show
  1. package/.gitattributes +8 -0
  2. package/.github/workflows/ci.yml +51 -0
  3. package/.vscode/tasks.json +23 -23
  4. package/CODE_OF_CONDUCT.md +1 -1
  5. package/README.md +1 -0
  6. package/__tests__/DBC/AE.test.ts +1 -1
  7. package/__tests__/DBC/DEFINED.test.ts +49 -49
  8. package/__tests__/DBC/Decorators.test.ts +334 -332
  9. package/__tests__/DBC/HasAttribute.test.ts +56 -52
  10. package/__tests__/DBC/IF.test.ts +57 -57
  11. package/__tests__/DBC/OR.test.ts +1 -1
  12. package/__tests__/DBC/UNDEFINED.test.ts +41 -41
  13. package/__tests__/DBC/ZOD.test.ts +50 -50
  14. package/biome.json +40 -33
  15. package/dist/DBC/AE.d.ts +117 -0
  16. package/dist/DBC/COMPARISON/GREATER.d.ts +13 -0
  17. package/dist/DBC/COMPARISON/GREATER_OR_EQUAL.d.ts +13 -0
  18. package/dist/DBC/COMPARISON/LESS.d.ts +13 -0
  19. package/dist/DBC/COMPARISON/LESS_OR_EQUAL.d.ts +13 -0
  20. package/dist/DBC/COMPARISON.d.ts +70 -0
  21. package/dist/DBC/DEFINED.d.ts +62 -0
  22. package/dist/DBC/EQ/DIFFERENT.d.ts +26 -0
  23. package/dist/DBC/EQ.d.ts +70 -0
  24. package/dist/DBC/HasAttribute.d.ts +72 -0
  25. package/dist/DBC/IF.d.ts +90 -0
  26. package/dist/DBC/INSTANCE.d.ts +84 -0
  27. package/dist/DBC/JSON.OP.d.ts +94 -0
  28. package/dist/DBC/JSON.Parse.d.ts +69 -0
  29. package/dist/DBC/OR.d.ts +104 -0
  30. package/dist/DBC/REGEX.d.ts +96 -0
  31. package/dist/DBC/TYPE.d.ts +75 -0
  32. package/dist/DBC/UNDEFINED.d.ts +62 -0
  33. package/dist/DBC/ZOD.d.ts +72 -0
  34. package/dist/DBC.d.ts +244 -0
  35. package/dist/Demo.d.ts +20 -0
  36. package/dist/bundle.js +2297 -0
  37. package/docs/assets/highlight.css +22 -22
  38. package/docs/assets/icons.js +17 -17
  39. package/docs/assets/main.js +60 -60
  40. package/docs/assets/style.css +1640 -1640
  41. package/docs/classes/DBC.DBC.html +98 -98
  42. package/docs/classes/DBC_AE.AE.html +160 -160
  43. package/docs/classes/DBC_EQ.EQ.html +131 -131
  44. package/docs/classes/DBC_GREATER.GREATER.html +139 -139
  45. package/docs/classes/DBC_INSTANCE.INSTANCE.html +130 -130
  46. package/docs/classes/DBC_JSON.OP.JSON_OP.html +138 -138
  47. package/docs/classes/DBC_JSON.Parse.JSON_Parse.html +129 -129
  48. package/docs/classes/DBC_OR.OR.html +137 -137
  49. package/docs/classes/DBC_REGEX.REGEX.html +136 -136
  50. package/docs/classes/DBC_TYPE.TYPE.html +130 -130
  51. package/docs/classes/Demo.Demo.html +14 -14
  52. package/docs/hierarchy.html +1 -1
  53. package/docs/index.html +1 -1
  54. package/docs/modules/DBC.html +1 -1
  55. package/docs/modules/DBC_AE.html +1 -1
  56. package/docs/modules/DBC_EQ.html +1 -1
  57. package/docs/modules/DBC_GREATER.html +1 -1
  58. package/docs/modules/DBC_INSTANCE.html +1 -1
  59. package/docs/modules/DBC_JSON.OP.html +1 -1
  60. package/docs/modules/DBC_JSON.Parse.html +1 -1
  61. package/docs/modules/DBC_OR.html +1 -1
  62. package/docs/modules/DBC_REGEX.html +1 -1
  63. package/docs/modules/DBC_TYPE.html +1 -1
  64. package/docs/modules/Demo.html +1 -1
  65. package/jest.config.js +29 -18
  66. package/package.json +6 -2
  67. package/src/DBC/AE.ts +14 -9
  68. package/src/DBC/COMPARISON/GREATER.ts +2 -2
  69. package/src/DBC/COMPARISON.ts +159 -136
  70. package/src/DBC/DEFINED.ts +10 -10
  71. package/src/DBC/EQ/DIFFERENT.ts +3 -3
  72. package/src/DBC/EQ.ts +25 -9
  73. package/src/DBC/HasAttribute.ts +17 -3
  74. package/src/DBC/IF.ts +63 -19
  75. package/src/DBC/INSTANCE.ts +29 -14
  76. package/src/DBC/JSON.OP.ts +18 -3
  77. package/src/DBC/JSON.Parse.ts +21 -4
  78. package/src/DBC/OR.ts +12 -7
  79. package/src/DBC/REGEX.ts +30 -21
  80. package/src/DBC/TYPE.ts +15 -11
  81. package/src/DBC/UNDEFINED.ts +7 -10
  82. package/src/DBC/ZOD.ts +14 -9
  83. package/src/DBC.ts +165 -69
  84. package/src/Demo.ts +21 -18
  85. package/test.drawio +0 -0
  86. package/tsconfig.json +3 -5
  87. package/tsconfig.test.json +6 -11
  88. package/webpack.config.js +1 -1
package/dist/DBC.d.ts ADDED
@@ -0,0 +1,244 @@
1
+ /**
2
+ * Provides a **D**esign **B**y **C**ontract Framework using decorators.
3
+ *
4
+ * @remarks
5
+ * Maintainer: Callari, Salvatore (XDBC@WaXCode.net) */
6
+ export declare class DBC {
7
+ private static readonly MAX_CACHE_SIZE;
8
+ private static dbcCache;
9
+ private static pathTokenCache;
10
+ /** Evicts the oldest entry if the cache exceeds the maximum size. */
11
+ private static evictIfNeeded;
12
+ private static getHost;
13
+ private static getDBC;
14
+ /** Stores all request for parameter values registered by {@link decPrecondition }. */
15
+ static paramValueRequests: Map<string, Map<number, Array<(value: any) => undefined>>>;
16
+ /**
17
+ * Generate a unique key for storing parameter value requests.
18
+ * Format: "ClassName:methodName"
19
+ */
20
+ private static getRequestKey;
21
+ /**
22
+ * Make a request to get the value of a certain parameter of specific method in a specific {@link object }.
23
+ * That request gets enlisted in {@link paramValueRequests } which is used by {@link ParamvalueProvider} to invoke the
24
+ * given "receptor" with the parameter value stored in there. Thus a parameter decorator using this method will
25
+ * not receive any value of the top method is not tagged with {@link ParamvalueProvider}.
26
+ *
27
+ * @param target The {@link object } containing the method with the parameter which's value is requested.
28
+ * @param methodName The name of the method with the parameter which's value is requested.
29
+ * @param index The index of the parameter which's value is requested.
30
+ * @param receptor The method the requested parameter-value shall be passed to when it becomes available. */
31
+ protected static requestParamValue(target: object, methodName: string | symbol, index: number, receptor: (value: any) => undefined): undefined;
32
+ /**
33
+ * A decorator usable on both **methods** (including setters) and **classes**.
34
+ *
35
+ * - **On a method**: wraps the method so that any {@link DBC } preconditions registered on its parameters
36
+ * via XDBC parameter-decorator factories are dispatched with the actual argument values on each call.
37
+ * - **On a class**: wraps the constructor so that preconditions registered on constructor parameters are
38
+ * dispatched **before** the original constructor body runs, upholding true precondition semantics.
39
+ *
40
+ * In both cases the "receptor" callbacks enlisted in {@link paramValueRequests } by {@link requestParamValue }
41
+ * are invoked with the live argument values.
42
+ *
43
+ * @param targetOrConstructor When used as a **method** decorator: the {@link object } hosting the tagged method.
44
+ * When used as a **class** decorator: the class constructor.
45
+ * @param propertyKey *(method decorator only)* The tagged method's name as provided by the runtime.
46
+ * @param descriptor *(method decorator only)* The {@link PropertyDescriptor } as provided by the runtime.
47
+ *
48
+ * @returns When used as a **method** decorator: the (modified) {@link PropertyDescriptor }.
49
+ * When used as a **class** decorator: a replacement constructor that performs precondition checks. */
50
+ static ParamvalueProvider(target: object, propertyKey: string, descriptor: PropertyDescriptor): PropertyDescriptor;
51
+ static ParamvalueProvider<T extends new (...args: any[]) => any>(constructor: T): T;
52
+ /**
53
+ * A property-decorator factory serving as a **D**esign **B**y **C**ontract Invariant.
54
+ * This invariant aims to check the instance of the class not the value to be get or set.
55
+ *
56
+ * @param contracts The {@link DBC }-Contracts the value shall uphold.
57
+ *
58
+ * @throws A {@link DBC.Infringement } whenever the property is tried to be get or set without the instance of it's class
59
+ * fulfilling the specified **contracts**. */
60
+ static decClassInvariant(contracts: Array<{
61
+ check: (toCheck: unknown | null | undefined) => boolean | string;
62
+ }>, path?: string | undefined, dbc?: string): (target: unknown, propertyKey: string | symbol, descriptor: PropertyDescriptor) => void;
63
+ /**
64
+ * A property-decorator factory serving as a **D**esign **B**y **C**ontract Invariant.
65
+ * Since the value must be initialized or set according to the specified **contracts** the value will only be checked
66
+ * when assigning it.
67
+ *
68
+ * @param contracts The {@link DBC }-Contracts the value shall uphold.
69
+ *
70
+ * @throws A {@link DBC.Infringement } whenever the property is tried to be set to a value that does not comply to the
71
+ * specified **contracts**, by the returned method.*/
72
+ static decInvariant(contracts: Array<{
73
+ check: (toCheck: unknown | null | undefined) => boolean | string;
74
+ }>, path?: string | undefined, dbc?: string | undefined, hint?: string | undefined): (target: unknown, propertyKey: string | symbol) => void;
75
+ /**
76
+ * A method decorator factory checking the result of a method whenever it is invoked thus also usable on getters.
77
+ *
78
+ * @param check The **(toCheck: any, object, string) => boolean | string** to use for checking.
79
+ * @param dbc See {@link DBC.resolveDBCPath }.
80
+ * @param path The dotted path referring to the actual value to check, starting form the specified one.
81
+ *
82
+ * @returns The **( target : object, propertyKey : string, descriptor : PropertyDescriptor ) : PropertyDescriptor**
83
+ * invoked by Typescript.
84
+ */
85
+ static decPostcondition(check: (toCheck: any, target: object, propertyKey: string) => boolean | string, dbc?: string | undefined, path?: string | undefined, hint?: string | undefined): (target: object, propertyKey: string, descriptor: PropertyDescriptor) => PropertyDescriptor;
86
+ /**
87
+ * A parameter-decorator factory that requests the tagged parameter's value passing it to the provided
88
+ * "check"-method when the value becomes available.
89
+ *
90
+ * @param check The "( unknown ) => void" to be invoked along with the tagged parameter's value as soon
91
+ * as it becomes available.
92
+ * @param dbc See {@link DBC.resolveDBCPath }.
93
+ * @param path The dotted path referring to the actual value to check, starting form the specified one.
94
+ * May contain :: to separate multiple paths.
95
+ *
96
+ * @returns The **(target: object, methodName: string | symbol, parameterIndex: number ) => void** invoked by Typescript- */
97
+ protected static decPrecondition(check: (value: unknown, target: object, methodName: string | symbol, parameterIndex: number) => boolean | string, dbc?: string | undefined, path?: string | undefined, hint?: string | undefined): (target: object, methodName: string | symbol, parameterIndex: number) => void;
98
+ /**
99
+ * Creates a PRE decorator from a checkAlgorithm function and its bound arguments.
100
+ * Reduces boilerplate across contract classes.
101
+ *
102
+ * @param checkFn A function that takes (value, ...boundArgs) and returns true or an error string.
103
+ * @param boundArgs The arguments to bind to the check function after the value.
104
+ * @param dbc See {@link DBC.decPrecondition}.
105
+ * @param path See {@link DBC.decPrecondition}.
106
+ * @param hint See {@link DBC.decPrecondition}.
107
+ */
108
+ static createPRE(checkFn: (...args: any[]) => boolean | string, boundArgs: any[], dbc?: string, path?: string, hint?: string): (target: object, methodName: string | symbol, parameterIndex: number) => void;
109
+ /**
110
+ * Creates a POST decorator from a checkAlgorithm function and its bound arguments.
111
+ *
112
+ * @param checkFn A function that takes (value, ...boundArgs) and returns true or an error string.
113
+ * @param boundArgs The arguments to bind to the check function after the value.
114
+ * @param dbc See {@link DBC.decPostcondition}.
115
+ * @param path See {@link DBC.decPostcondition}.
116
+ * @param hint See {@link DBC.decPostcondition}.
117
+ */
118
+ static createPOST(checkFn: (...args: any[]) => boolean | string, boundArgs: any[], dbc?: string, path?: string, hint?: string): (target: object, propertyKey: string, descriptor: PropertyDescriptor) => PropertyDescriptor;
119
+ /**
120
+ * Creates an INVARIANT decorator from a contract constructor and its bound arguments.
121
+ *
122
+ * @param ContractClass A class with a constructor that produces an object with a `check` method.
123
+ * @param ctorArgs The arguments to pass to the contract constructor.
124
+ * @param dbc See {@link DBC.decInvariant}.
125
+ * @param path See {@link DBC.decInvariant}.
126
+ * @param hint See {@link DBC.decInvariant}.
127
+ */
128
+ static createINVARIANT(ContractClass: new (...args: any[]) => {
129
+ check: (toCheck: unknown | null | undefined) => boolean | string;
130
+ }, ctorArgs: any[], dbc?: string, path?: string, hint?: string): (target: unknown, propertyKey: string | symbol) => void;
131
+ /** Stores settings concerning the execution of checks. */
132
+ executionSettings: {
133
+ checkPreconditions: boolean;
134
+ checkPostconditions: boolean;
135
+ checkInvariants: boolean;
136
+ };
137
+ /** Stores settings concerning warnings. */
138
+ warningSettings: {
139
+ logToConsole: boolean;
140
+ };
141
+ /**
142
+ * Reports a warning.
143
+ *
144
+ * @param message The message containing the warning. */
145
+ protected reportWarning(message: string): undefined;
146
+ /** Stores the settings concerning infringements */
147
+ infringementSettings: {
148
+ throwException: boolean;
149
+ logToConsole: boolean;
150
+ };
151
+ /** Sanitizes a value for safe inclusion in error messages. */
152
+ private static sanitize;
153
+ /**
154
+ * Reports an infringement according to the {@link infringementSettings } also generating a proper {@link string }-wrapper
155
+ * for the given "message" & violator.
156
+ *
157
+ * @param message The {@link string } describing the infringement and it's provenience.
158
+ * @param violator The {@link string } describing or naming the violator. */
159
+ protected reportInfringement(message: string, violator: string, target: object, value: unknown, path: string | undefined, hint?: string | undefined): undefined;
160
+ /**
161
+ * Reports a parameter-infringement via {@link reportInfringement } also generating a proper {@link string }-wrapper
162
+ * for the given "message","method", parameter-"index" & value.
163
+ *
164
+ * @param message The {@link string } describing the infringement and it's provenience.
165
+ * @param method The {@link string } describing or naming the violator.
166
+ * @param index The index of the parameter within the argument listing.
167
+ * @param value The parameter's value. */
168
+ reportParameterInfringement(message: string, target: object, path: string | undefined, method: string, index: number, value: unknown, hint?: string | undefined): undefined;
169
+ /**
170
+ * Reports a field-infringement via {@link reportInfringement } also generating a proper {@link string }-wrapper
171
+ * for the given **message** & **name**.
172
+ *
173
+ * @param message A {@link string } describing the infringement and it's provenience.
174
+ * @param key The property key.
175
+ * @param path The dotted-path {@link string } that leads to the value not fulfilling the contract starting from
176
+ * the tagged one.
177
+ * @param value The value not fulfilling a contract. */
178
+ reportFieldInfringement(message: string, target: object, path: string | undefined, key: string, value: unknown, hint?: string | undefined): undefined;
179
+ /**
180
+ * Reports a returnvalue-infringement according via {@link reportInfringement } also generating a proper {@link string }-wrapper
181
+ * for the given "message","method" & value.
182
+ *
183
+ * @param message The {@link string } describing the infringement and it's provenience.
184
+ * @param method The {@link string } describing or naming the violator.
185
+ * @param value The parameter's value. */
186
+ reportReturnvalueInfringement(message: string, target: object, path: string | undefined, method: string, value: any, hint?: string | undefined): void;
187
+ /** An {@link Error } to be thrown whenever an infringement is detected. */
188
+ static Infringement: {
189
+ new (message: string): {
190
+ name: string;
191
+ message: string;
192
+ stack?: string;
193
+ };
194
+ };
195
+ /**
196
+ * Resolves the specified dotted {@link string }-path to a {@link DBC }.
197
+ *
198
+ * @param obj The {@link object } to start resolving from.
199
+ * @param path The dotted {@link string }-path leading to the {@link DBC }.
200
+ *
201
+ * @returns The requested {@link DBC }.
202
+ */
203
+ static resolveDBCPath: (obj: any, path: string) => DBC;
204
+ /**
205
+ * Constructs this {@link DBC } without mounting it on the global namespace.
206
+ * Use {@link DBC.register } to make the instance available at a specific path on globalThis.
207
+ *
208
+ * @param infringementSettings See {@link DBC.infringementSettings }.
209
+ * @param executionSettings See {@link DBC.executionSettings }. */
210
+ constructor(infringementSettings?: {
211
+ throwException: boolean;
212
+ logToConsole: boolean;
213
+ }, executionSettings?: {
214
+ checkPreconditions: boolean;
215
+ checkPostconditions: boolean;
216
+ checkInvariants: boolean;
217
+ });
218
+ /**
219
+ * Registers a {@link DBC } instance at the specified dotted path on globalThis (or window),
220
+ * making it available for decorator resolution via string paths.
221
+ *
222
+ * @param instance The {@link DBC } instance to register.
223
+ * @param path The dotted path to register at (default: `"WaXCode.DBC"`). */
224
+ static register(instance: DBC, path?: string): void;
225
+ /**
226
+ * Executes a callback with an isolated {@link DBC } instance temporarily registered at the default path.
227
+ * The previous instance (if any) is restored after the callback completes — even if it throws.
228
+ * Useful for test isolation.
229
+ *
230
+ * @param fn The callback receiving the isolated {@link DBC } instance. */
231
+ static isolated(fn: (dbc: DBC) => void): void;
232
+ /**
233
+ * Resolves the desired {@link object } out a given one **toResolveFrom** using the specified **path**.
234
+ *
235
+ * @param toResolveFrom The {@link object } starting to resolve from.
236
+ * @param path The dotted path-{@link string }.
237
+ * This string uses ., [...], and () to represent accessing nested properties,
238
+ * array elements/object keys, and calling methods, respectively, mimicking JavaScript syntax to navigate
239
+ * an object's structure. Code, e.g. something like a.b( 1 as number ).c, will not be executed and
240
+ * thus make the retrieval fail.
241
+ *
242
+ * @returns The requested {@link object }, NULL or UNDEFINED. */
243
+ static resolve(toResolveFrom: unknown, path: string): any;
244
+ }
package/dist/Demo.d.ts ADDED
@@ -0,0 +1,20 @@
1
+ /** Demonstrative use of **D**esign **B**y **C**ontract Decorators */
2
+ export declare class Demo {
3
+ testProperty: string;
4
+ testParamvalueAndReturnvalue(a: string): string;
5
+ testReturnvalue(a: string): string;
6
+ testEQAndPath(o: HTMLElement): void;
7
+ testEQAndPathWithInversion(o: HTMLElement): void;
8
+ testTYPE(o: unknown): void;
9
+ testAE(x: Array<unknown>): void;
10
+ testREGEXWithAE(x: Array<string>): void;
11
+ testINSTANCE(candidate: any): undefined;
12
+ testAERange(x: Array<unknown>): void;
13
+ testAEIndex(x: Array<unknown>): void;
14
+ testGREATER(input: number): void;
15
+ testGREATER_OR_EQUAL(input: number): void;
16
+ testLESS(input: number): void;
17
+ testLESS_OR_EQUAL(input: number): void;
18
+ testDIFFERENT(input: number): void;
19
+ static testStaticMethod(message: string, count: number): string;
20
+ }