@ayapapa-npm/contracts-js 0.2.1 → 0.2.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/dist/index.cjs CHANGED
@@ -36,145 +36,417 @@ module.exports = __toCommonJS(index_exports);
36
36
  var _Contracts_static, check_fn, checkDebug_fn;
37
37
  var _Contracts = class _Contracts {
38
38
  /**
39
- * Configures the contract checking behavior.
40
- * @param {{debug?: boolean}} config Configuration object
39
+ * Configures contract checking behavior.
40
+ *
41
+ * @param {{debug?: boolean}} config
42
+ * Configuration options.
43
+ *
44
+ * The `debug` property enables or disables
45
+ * debug-only contract checks.
46
+ *
47
+ * When `debug` is `true`,
48
+ * methods ending with `_DEBUG` perform validation.
49
+ *
50
+ * When `debug` is `false` or omitted,
51
+ * methods ending with `_DEBUG` skip validation.
52
+ *
53
+ * @example
54
+ * Contracts.setConfig({ debug: true });
41
55
  */
42
56
  static setConfig(config) {
43
- if (config) {
44
- _Contracts.DEBUG_MODE = config.debug || false;
45
- }
57
+ _Contracts.DEBUG_MODE = Boolean(config.debug);
46
58
  }
47
59
  /**
48
- * Checks a general condition.
49
- * If `isOk` is false and an `error` class is provided, throws an exception with `ngMsg`.
50
- * Otherwise, logs the error message to `console.error`.
51
- * @param {boolean} isOk The result of the condition check.
52
- * @param {string} [ngMsg] The error message.
53
- * @param {new (...args: any[]) => Error} [error=Error] The error class to throw (Error or its subclass).
54
- * @param {Object<string, *>} [eProps={}] Properties to assign to the error instance.
55
- * @return {boolean} The evaluation result (returns `isOk` as is).
60
+ * Verifies an intermediate condition during execution.
61
+ *
62
+ * VERIFY is used to validate intermediate results
63
+ * and internal assumptions during execution.
64
+ *
65
+ * Unlike REQUIRE and ENSURE, VERIFY does not represent
66
+ * a condition at the function boundary.
67
+ *
68
+ * Unlike INVARIANT, VERIFY does not represent a condition
69
+ * that must always remain true.
70
+ *
71
+ * Typical usage:
72
+ * - Validate intermediate calculation results.
73
+ * - Confirm internal processing states.
74
+ * - Check temporary assumptions during execution.
75
+ *
76
+ * @param {boolean} isOk
77
+ * Condition result to verify.
78
+ *
79
+ * @param {string|null} [ngMsg]
80
+ * Failure message.
81
+ *
82
+ * @param {(new (...args:any[])=>Error)|null} [ErrorClass=Error]
83
+ * Error constructor used when the check fails.
84
+ *
85
+ * Supported values:
86
+ * - `Error` (default)
87
+ * - `TypeError`
88
+ * - `RangeError`
89
+ * - Custom Error subclasses
90
+ * - `null` to skip throwing and log the failure.
91
+ *
92
+ * @param {Object<string, *>} [eProps]
93
+ * Additional properties assigned to the error object.
94
+ *
95
+ * @returns {boolean}
96
+ * Returns the original condition value.
97
+ *
98
+ * @example
99
+ * const result = calculate();
100
+ *
101
+ * Contracts.VERIFY(
102
+ * result >= 0,
103
+ * 'Calculation result must not be negative'
104
+ * );
56
105
  */
57
- static VERIFY(isOk, ngMsg, error = Error, eProps = {}) {
106
+ static VERIFY(isOk, ngMsg, ErrorClass = Error, eProps = {}) {
58
107
  var _a;
59
- return __privateMethod(_a = _Contracts, _Contracts_static, check_fn).call(_a, isOk, "VERIFY", ngMsg, error, eProps);
108
+ return __privateMethod(_a = _Contracts, _Contracts_static, check_fn).call(_a, isOk, "VERIFY", ngMsg, ErrorClass, eProps);
60
109
  }
61
110
  /**
62
- * Checks a general condition (for debugging: outputs execution message but throws no exception in debug mode).
63
- * If the result is false and an `error` class is provided, throws an exception.
64
- * Otherwise, logs the message to `console.error`.
65
- * @param {boolean} isOk The result of the condition check.
66
- * @param {string} [ngMsg] The error message.
67
- * @param {new (...args: any[]) => Error} [error] The error class to throw (Error or its subclass).
68
- * @param {Object<string, *>} [eProps={}] Properties to assign to the error instance.
69
- * @return {boolean} The evaluation result (returns `isOk` as is).
111
+ * Verifies an intermediate condition in debug mode only.
112
+ *
113
+ * Performs the same validation as VERIFY only when
114
+ * DEBUG_MODE is enabled.
115
+ *
116
+ * When DEBUG_MODE is disabled,
117
+ * no validation is performed.
118
+ *
119
+ * Typical usage:
120
+ * - Validate intermediate results during development.
121
+ * - Check internal assumptions while debugging.
122
+ *
123
+ * @param {boolean} isOk
124
+ * Condition result to verify.
125
+ *
126
+ * @param {string|null} [ngMsg]
127
+ * Failure message.
128
+ *
129
+ * @param {(new (...args:any[])=>Error)|null} [ErrorClass=Error]
130
+ * Error constructor used when the check fails.
131
+ *
132
+ * @param {Object<string, *>} [eProps]
133
+ * Additional properties assigned to the error object.
134
+ *
135
+ * @returns {boolean}
136
+ * Returns the original condition value.
137
+ *
138
+ * @example
139
+ * Contracts.VERIFY_DEBUG(
140
+ * intermediate !== null,
141
+ * 'Intermediate value must not be null'
142
+ * );
70
143
  */
71
- static VERIFY_DEBUG(isOk, ngMsg, error = Error, eProps = {}) {
144
+ static VERIFY_DEBUG(isOk, ngMsg, ErrorClass = Error, eProps = {}) {
72
145
  var _a;
73
- return __privateMethod(_a = _Contracts, _Contracts_static, checkDebug_fn).call(_a, isOk, "VERIFY_DEBUG", ngMsg, error, eProps);
146
+ return __privateMethod(_a = _Contracts, _Contracts_static, checkDebug_fn).call(_a, isOk, "VERIFY_DEBUG", ngMsg, ErrorClass, eProps);
74
147
  }
75
148
  /**
76
- * Checks a precondition.
77
- * Use this to verify conditions that must be true before a function executes (e.g., argument validation).
78
- * If `isOk` is false and an `error` class is provided, throws an exception with `ngMsg`.
79
- * Otherwise, logs the error message to `console.error`.
80
- * @param {boolean} isOk The result of the condition check.
81
- * @param {string} [ngMsg] The error message.
82
- * @param {new (...args: any[]) => Error} [error=Error] The error class to throw (Error or its subclass).
83
- * @param {Object<string, *>} [eProps={}] Properties to assign to the error instance.
84
- * @return {boolean} The evaluation result (returns `isOk` as is).
149
+ * Checks a precondition before execution.
150
+ *
151
+ * A precondition defines conditions that must be satisfied
152
+ * before a function or operation starts.
153
+ *
154
+ * The caller is responsible for satisfying preconditions.
155
+ *
156
+ * Typical usage:
157
+ * - Validate function arguments.
158
+ * - Validate required object state.
159
+ * - Check required external conditions.
160
+ *
161
+ * @param {boolean} isOk
162
+ * Condition result to verify.
163
+ *
164
+ * @param {string|null} [ngMsg]
165
+ * Failure message.
166
+ *
167
+ * @param {(new (...args:any[])=>Error)|null} [ErrorClass=Error]
168
+ * Error constructor used when the check fails.
169
+ *
170
+ * Supported values:
171
+ * - `Error` (default)
172
+ * - `TypeError`
173
+ * - `RangeError`
174
+ * - Custom Error subclasses
175
+ * - `null` to skip throwing and log the failure.
176
+ *
177
+ * @param {Object<string, *>} [eProps]
178
+ * Additional properties assigned to the error object.
179
+ *
180
+ * @returns {boolean}
181
+ * Returns the original condition value.
182
+ *
183
+ * @example
184
+ * function divide(a, b) {
185
+ * Contracts.REQUIRE(
186
+ * typeof a === 'number',
187
+ * 'a must be a number'
188
+ * );
189
+ *
190
+ * Contracts.REQUIRE(
191
+ * b !== 0,
192
+ * 'Divisor cannot be zero'
193
+ * );
194
+ *
195
+ * return a / b;
196
+ * }
85
197
  */
86
- static REQUIRE(isOk, ngMsg, error = Error, eProps = {}) {
198
+ static REQUIRE(isOk, ngMsg, ErrorClass = Error, eProps = {}) {
87
199
  var _a;
88
- return __privateMethod(_a = _Contracts, _Contracts_static, check_fn).call(_a, isOk, "REQUIRE", ngMsg, error, eProps);
200
+ return __privateMethod(_a = _Contracts, _Contracts_static, check_fn).call(_a, isOk, "REQUIRE", ngMsg, ErrorClass, eProps);
89
201
  }
90
202
  /**
91
- * Checks a precondition (for debugging: outputs execution message but throws no exception in debug mode).
92
- * If the result is false and an `error` class is provided, throws an exception.
93
- * Otherwise, logs the message to `console.error`.
94
- * @param {boolean} isOk The result of the condition check.
95
- * @param {string} [ngMsg] The error message.
96
- * @param {new (...args: any[]) => Error} [error] The error class to throw (Error or its subclass).
97
- * @param {Object<string, *>} [eProps={}] Properties to assign to the error instance.
98
- * @return {boolean} The evaluation result (returns `isOk` as is).
203
+ * Checks a precondition in debug mode only.
204
+ *
205
+ * Performs the same validation as REQUIRE only when
206
+ * DEBUG_MODE is enabled.
207
+ *
208
+ * When DEBUG_MODE is disabled,
209
+ * no validation is performed.
210
+ *
211
+ * Typical usage:
212
+ * - Validate assumptions during development.
213
+ * - Perform additional argument checks while debugging.
214
+ *
215
+ * @param {boolean} isOk
216
+ * Condition result to verify.
217
+ *
218
+ * @param {string|null} [ngMsg]
219
+ * Failure message.
220
+ *
221
+ * @param {(new (...args:any[])=>Error)|null} [ErrorClass=Error]
222
+ * Error constructor used when the check fails.
223
+ *
224
+ * @param {Object<string, *>} [eProps]
225
+ * Additional properties assigned to the error object.
226
+ *
227
+ * @returns {boolean}
228
+ * Returns the original condition value.
229
+ *
230
+ * @example
231
+ * Contracts.REQUIRE_DEBUG(
232
+ * user !== null,
233
+ * 'User must exist during debugging'
234
+ * );
99
235
  */
100
- static REQUIRE_DEBUG(isOk, ngMsg, error = Error, eProps = {}) {
236
+ static REQUIRE_DEBUG(isOk, ngMsg, ErrorClass = Error, eProps = {}) {
101
237
  var _a;
102
- return __privateMethod(_a = _Contracts, _Contracts_static, checkDebug_fn).call(_a, isOk, "REQUIRE_DEBUG", ngMsg, error, eProps);
238
+ return __privateMethod(_a = _Contracts, _Contracts_static, checkDebug_fn).call(_a, isOk, "REQUIRE_DEBUG", ngMsg, ErrorClass, eProps);
103
239
  }
104
240
  /**
105
- * Checks a postcondition.
106
- * Use this to verify conditions that must be true after a function executes (e.g., return value or object state).
107
- * If `isOk` is false and an `error` class is provided, throws an exception with `ngMsg`.
108
- * Otherwise, logs the error message to `console.error`.
109
- * @param {boolean} isOk The result of the condition check.
110
- * @param {string} [ngMsg] The error message.
111
- * @param {new (...args: any[]) => Error} [error=Error] The error class to throw (Error or its subclass).
112
- * @param {Object<string, *>} [eProps={}] Properties to assign to the error instance.
113
- * @return {boolean} The evaluation result (returns `isOk` as is).
241
+ * Checks a postcondition after execution.
242
+ *
243
+ * A postcondition defines conditions that must be satisfied
244
+ * after a function or operation completes.
245
+ *
246
+ * ENSURE represents guarantees provided by the function
247
+ * to its caller.
248
+ *
249
+ * Typical usage:
250
+ * - Validate return values.
251
+ * - Confirm state changes.
252
+ * - Verify that processing completed correctly.
253
+ *
254
+ * @param {boolean} isOk
255
+ * Condition result to verify.
256
+ *
257
+ * @param {string|null} [ngMsg]
258
+ * Failure message.
259
+ *
260
+ * @param {(new (...args:any[])=>Error)|null} [ErrorClass=Error]
261
+ * Error constructor used when the check fails.
262
+ *
263
+ * Supported values:
264
+ * - `Error` (default)
265
+ * - `TypeError`
266
+ * - `RangeError`
267
+ * - Custom Error subclasses
268
+ * - `null` to skip throwing and log the failure.
269
+ *
270
+ * @param {Object<string, *>} [eProps]
271
+ * Additional properties assigned to the error object.
272
+ *
273
+ * @returns {boolean}
274
+ * Returns the original condition value.
275
+ *
276
+ * @example
277
+ * function double(value) {
278
+ * Contracts.REQUIRE(
279
+ * value >= 0,
280
+ * 'Value must not be negative'
281
+ * );
282
+ *
283
+ * const result = value * 2;
284
+ *
285
+ * Contracts.ENSURE(
286
+ * result >= 0,
287
+ * 'Result must not be negative'
288
+ * );
289
+ *
290
+ * return result;
291
+ * }
114
292
  */
115
- static ENSURE(isOk, ngMsg, error = Error, eProps = {}) {
293
+ static ENSURE(isOk, ngMsg, ErrorClass = Error, eProps = {}) {
116
294
  var _a;
117
- return __privateMethod(_a = _Contracts, _Contracts_static, check_fn).call(_a, isOk, "ENSURE", ngMsg, error, eProps);
295
+ return __privateMethod(_a = _Contracts, _Contracts_static, check_fn).call(_a, isOk, "ENSURE", ngMsg, ErrorClass, eProps);
118
296
  }
119
297
  /**
120
- * Checks a postcondition (for debugging: outputs execution message but throws no exception in debug mode).
121
- * If the result is false and an `error` class is provided, throws an exception.
122
- * Otherwise, logs the message to `console.error`.
123
- * @param {boolean} isOk The result of the condition check.
124
- * @param {string} [ngMsg] The error message.
125
- * @param {new (...args: any[]) => Error} [error] The error class to throw (Error or its subclass).
126
- * @param {Object<string, *>} [eProps={}] Properties to assign to the error instance.
127
- * @return {boolean} The evaluation result (returns `isOk` as is).
298
+ * Checks a postcondition in debug mode only.
299
+ *
300
+ * Performs the same validation as ENSURE only when
301
+ * DEBUG_MODE is enabled.
302
+ *
303
+ * When DEBUG_MODE is disabled,
304
+ * no validation is performed.
305
+ *
306
+ * Typical usage:
307
+ * - Validate detailed results during development.
308
+ * - Confirm internal behavior while debugging.
309
+ *
310
+ * @param {boolean} isOk
311
+ * Condition result to verify.
312
+ *
313
+ * @param {string|null} [ngMsg]
314
+ * Failure message.
315
+ *
316
+ * @param {(new (...args:any[])=>Error)|null} [ErrorClass=Error]
317
+ * Error constructor used when the check fails.
318
+ *
319
+ * @param {Object<string, *>} [eProps]
320
+ * Additional properties assigned to the error object.
321
+ *
322
+ * @returns {boolean}
323
+ * Returns the original condition value.
324
+ *
325
+ * @example
326
+ * Contracts.ENSURE_DEBUG(
327
+ * result !== undefined,
328
+ * 'Result should exist during debugging'
329
+ * );
128
330
  */
129
- static ENSURE_DEBUG(isOk, ngMsg, error = Error, eProps = {}) {
331
+ static ENSURE_DEBUG(isOk, ngMsg, ErrorClass = Error, eProps = {}) {
130
332
  var _a;
131
- return __privateMethod(_a = _Contracts, _Contracts_static, checkDebug_fn).call(_a, isOk, "ENSURE_DEBUG", ngMsg, error, eProps);
333
+ return __privateMethod(_a = _Contracts, _Contracts_static, checkDebug_fn).call(_a, isOk, "ENSURE_DEBUG", ngMsg, ErrorClass, eProps);
132
334
  }
133
335
  /**
134
- * Checks an invariant.
135
- * Use this to verify conditions that must remain true before and after a function call (e.g., object integrity).
136
- * If `isOk` is false and an `error` class is provided, throws an exception with `ngMsg`.
137
- * Otherwise, logs the error message to `console.error`.
138
- * @param {boolean} isOk The result of the condition check.
139
- * @param {string} [ngMsg] The error message.
140
- * @param {new (...args: any[]) => Error} [error=Error] The error class to throw (Error or its subclass).
141
- * @param {Object<string, *>} [eProps={}] Properties to assign to the error instance.
142
- * @return {boolean} The evaluation result (returns `isOk` as is).
336
+ * Checks an invariant condition.
337
+ *
338
+ * An invariant represents a condition that must remain valid
339
+ * throughout the lifetime of an object or component.
340
+ *
341
+ * Typical usage:
342
+ * - Validate internal object consistency.
343
+ * - Protect class state integrity.
344
+ * - Confirm assumptions that must always hold.
345
+ *
346
+ * In Design by Contract terminology,
347
+ * INVARIANT represents conditions that must always remain true.
348
+ *
349
+ * @param {boolean} isOk
350
+ * Condition result to verify.
351
+ *
352
+ * @param {string|null} [ngMsg]
353
+ * Failure message.
354
+ *
355
+ * @param {(new (...args:any[])=>Error)|null} [ErrorClass=Error]
356
+ * Error constructor used when the check fails.
357
+ *
358
+ * Supported values:
359
+ * - `Error` (default)
360
+ * - `TypeError`
361
+ * - `RangeError`
362
+ * - Custom Error subclasses
363
+ * - `null` to skip throwing and log the failure.
364
+ *
365
+ * @param {Object<string, *>} [eProps]
366
+ * Additional properties assigned to the error object.
367
+ *
368
+ * @returns {boolean}
369
+ * Returns the original condition value.
370
+ *
371
+ * @example
372
+ * class BankAccount {
373
+ *
374
+ * withdraw(amount) {
375
+ * this.balance -= amount;
376
+ *
377
+ * Contracts.INVARIANT(
378
+ * this.balance >= 0,
379
+ * 'Balance cannot be negative'
380
+ * );
381
+ * }
382
+ *
383
+ * }
143
384
  */
144
- static INVARIANT(isOk, ngMsg, error = Error, eProps = {}) {
385
+ static INVARIANT(isOk, ngMsg, ErrorClass = Error, eProps = {}) {
145
386
  var _a;
146
- return __privateMethod(_a = _Contracts, _Contracts_static, check_fn).call(_a, isOk, "INVARIANT", ngMsg, error, eProps);
387
+ return __privateMethod(_a = _Contracts, _Contracts_static, check_fn).call(_a, isOk, "INVARIANT", ngMsg, ErrorClass, eProps);
147
388
  }
148
389
  /**
149
- * Checks an invariant (for debugging: outputs execution message but throws no exception in debug mode).
150
- * If the result is false and an `error` class is provided, throws an exception.
151
- * Otherwise, logs the message to `console.error`.
152
- * @param {boolean} isOk The result of the condition check.
153
- * @param {string} [ngMsg] The error message.
154
- * @param {new (...args: any[]) => Error} [error] The error class to throw (Error or its subclass).
155
- * @param {Object<string, *>} [eProps={}] Properties to assign to the error instance.
156
- * @return {boolean} The evaluation result (returns `isOk` as is).
390
+ * Checks an invariant condition in debug mode only.
391
+ *
392
+ * Performs the same validation as INVARIANT only when
393
+ * DEBUG_MODE is enabled.
394
+ *
395
+ * When DEBUG_MODE is disabled,
396
+ * no validation is performed.
397
+ *
398
+ * Typical usage:
399
+ * - Validate object consistency during development.
400
+ * - Detect unexpected state changes while debugging.
401
+ *
402
+ * @param {boolean} isOk
403
+ * Condition result to verify.
404
+ *
405
+ * @param {string|null} [ngMsg]
406
+ * Failure message.
407
+ *
408
+ * @param {(new (...args:any[])=>Error)|null} [ErrorClass=Error]
409
+ * Error constructor used when the check fails.
410
+ *
411
+ * @param {Object<string, *>} [eProps]
412
+ * Additional properties assigned to the error object.
413
+ *
414
+ * @returns {boolean}
415
+ * Returns the original condition value.
416
+ *
417
+ * @example
418
+ * Contracts.INVARIANT_DEBUG(
419
+ * cache.size < 1000,
420
+ * 'Cache size exceeded expected limit'
421
+ * );
157
422
  */
158
- static INVARIANT_DEBUG(isOk, ngMsg, error = Error, eProps = {}) {
423
+ static INVARIANT_DEBUG(isOk, ngMsg, ErrorClass = Error, eProps = {}) {
159
424
  var _a;
160
- return __privateMethod(_a = _Contracts, _Contracts_static, checkDebug_fn).call(_a, isOk, "INVARIANT_DEBUG", ngMsg, error, eProps);
425
+ return __privateMethod(_a = _Contracts, _Contracts_static, checkDebug_fn).call(_a, isOk, "INVARIANT_DEBUG", ngMsg, ErrorClass, eProps);
161
426
  }
162
427
  };
163
428
  _Contracts_static = new WeakSet();
164
- check_fn = function(isOk, prefix = "CHECK", ngMsg = "", error = Error, eProps = {}) {
429
+ check_fn = function(isOk, prefix = "CHECK", ngMsg = "", ErrorClass = Error, eProps = {}) {
165
430
  if (!isOk) {
166
- const msg = `[${prefix}]${ngMsg ?? ""}`;
167
- if (error) throw Object.assign(new error(msg), eProps);
168
- if (ngMsg) console.error(msg, eProps);
431
+ const msg = `[${prefix}] ${ngMsg ?? ""}`;
432
+ if (ErrorClass) {
433
+ throw Object.assign(
434
+ new ErrorClass(msg),
435
+ eProps
436
+ );
437
+ }
438
+ if (ngMsg) {
439
+ console.error(msg, eProps);
440
+ }
169
441
  }
170
442
  return isOk;
171
443
  };
172
- checkDebug_fn = function(isOk, prefix = "CHECK_DEBUG", ngMsg = "", error = Error, eProps = {}) {
444
+ checkDebug_fn = function(isOk, prefix = "CHECK_DEBUG", ngMsg = "", ErrorClass = Error, eProps = {}) {
173
445
  var _a;
174
- return _Contracts.DEBUG_MODE ? __privateMethod(_a = _Contracts, _Contracts_static, check_fn).call(_a, isOk, prefix, ngMsg, error, eProps) : isOk;
446
+ return _Contracts.DEBUG_MODE ? __privateMethod(_a = _Contracts, _Contracts_static, check_fn).call(_a, isOk, prefix, ngMsg, ErrorClass, eProps) : isOk;
175
447
  };
176
448
  __privateAdd(_Contracts, _Contracts_static);
177
- // @type {boolean}
449
+ /** @type {boolean} Debug mode state */
178
450
  __publicField(_Contracts, "DEBUG_MODE", false);
179
451
  var Contracts = _Contracts;
180
452