xdbc 1.0.97 → 1.0.98

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 CHANGED
@@ -1,5 +1,5 @@
1
1
  {
2
- "version": "1.0.97",
2
+ "version": "1.0.98",
3
3
  "name": "xdbc",
4
4
  "scripts": {
5
5
  "docs": "typedoc",
@@ -109,6 +109,23 @@ export class HasAttribute extends DBC {
109
109
  ) {
110
110
  return DBC.decInvariant([new HasAttribute(toCheckFor, invert)], path, dbc);
111
111
  }
112
+ /**
113
+ * A field-decorator factory using the {@link hasAttribute.checkAlgorithm } to determine whether this {@link DBC } is
114
+ * fulfilled by the tagged field's class instance.
115
+ *
116
+ * @param toCheckFor See {@link hasAttribute.checkAlgorithm }.
117
+ * @param path See {@link DBC.decInvariant }.
118
+ * @param dbc See {@link DBC.decInvariant }.
119
+ *
120
+ * @returns See {@link DBC.decInvariant }. */
121
+ public static cINVARIANT(
122
+ toCheckFor: any,
123
+ invert = false,
124
+ path: string | undefined = undefined,
125
+ dbc = "WaXCode.DBC",
126
+ ) {
127
+ return DBC.decClassInvariant([new HasAttribute(toCheckFor, invert)], path, dbc);
128
+ }
112
129
  // #endregion Condition checking.
113
130
  // #region Referenced Condition checking.
114
131
  //
package/src/DBC.ts CHANGED
@@ -123,6 +123,89 @@ export class DBC {
123
123
  return descriptor;
124
124
  }
125
125
  // #endregion Parameter-value requests.
126
+ // #region Class
127
+ /**
128
+ * A property-decorator factory serving as a **D**esign **B**y **C**ontract Invariant.
129
+ * This invariant aims to check the instance of the class not the value to be get or set.
130
+ *
131
+ * @param contracts The {@link DBC }-Contracts the value shall uphold.
132
+ *
133
+ * @throws A {@link DBC.Infringement } whenever the property is tried to be get or set without the instance of it's class
134
+ * fulfilling the specified **contracts**. */
135
+ public static decClassInvariant(
136
+ contracts: Array<{
137
+ check: (toCheck: unknown | null | undefined) => boolean | string;
138
+ }>,
139
+ path: string | undefined = undefined,
140
+ dbc = "WaXCode.DBC",
141
+ ) {
142
+ return (target: unknown, propertyKey: string | symbol, descriptor: PropertyDescriptor) => {
143
+ if (!DBC.resolveDBCPath(window, dbc).executionSettings.checkInvariants) {
144
+ return;
145
+ }
146
+ const originalSetter = descriptor.set;
147
+ const originalGetter = descriptor.get;
148
+ // biome-ignore lint/suspicious/noExplicitAny: Necessary to intercept UNDEFINED and NULL.
149
+ let value: any;
150
+ // #region Replace original property.
151
+ Object.defineProperty(target, propertyKey, {
152
+ get() {
153
+ if (
154
+ !DBC.resolveDBCPath(window, dbc).executionSettings.checkInvariants
155
+ ) {
156
+ return;
157
+ }
158
+
159
+ const realValue = path ? DBC.resolve(this, path) : this;
160
+ // #region Check if all "contracts" are fulfilled.
161
+ for (const contract of contracts) {
162
+ const result = contract.check(realValue);
163
+
164
+ if (typeof result === "string") {
165
+ DBC.resolveDBCPath(window, dbc).reportFieldInfringement(
166
+ result,
167
+ target as object,
168
+ path,
169
+ propertyKey as string,
170
+ realValue,
171
+ );
172
+ }
173
+ }
174
+ // #endregion Check if all "contracts" are fulfilled.
175
+ return originalGetter[ propertyKey ];
176
+ },
177
+ set(newValue) {
178
+ if (
179
+ !DBC.resolveDBCPath(window, dbc).executionSettings.checkInvariants
180
+ ) {
181
+ return;
182
+ }
183
+
184
+ const realValue = path ? DBC.resolve(this, path) : this;
185
+ // #region Check if all "contracts" are fulfilled.
186
+ for (const contract of contracts) {
187
+ const result = contract.check(realValue);
188
+
189
+ if (typeof result === "string") {
190
+ DBC.resolveDBCPath(window, dbc).reportFieldInfringement(
191
+ result,
192
+ target as object,
193
+ path,
194
+ propertyKey as string,
195
+ realValue,
196
+ );
197
+ }
198
+ }
199
+ // #endregion Check if all "contracts" are fulfilled.
200
+ value = newValue;
201
+ },
202
+ enumerable: true,
203
+ configurable: true,
204
+ });
205
+ // #endregion Replace original property.
206
+ };
207
+ }
208
+ // #endregion Class
126
209
  // #region Invariant
127
210
  /**
128
211
  * A property-decorator factory serving as a **D**esign **B**y **C**ontract Invariant.