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