@ayapapa-npm/contracts-js 0.2.2 → 0.2.4

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/README.md CHANGED
@@ -1,5 +1,7 @@
1
+ [![CI](https://github.com/ayapapa/contracts-js/actions/workflows/ci.yml/badge.svg)](https://github.com/ayapapa/contracts-js/actions/workflows/ci.yml)
1
2
  # contracts-js
2
3
  A lightweight Design by Contract library for JavaScript.</br>
4
+ Provides runtime contract checks based on Design by Contract principles.</br>
3
5
  All check functions return the evaluated condition itself, so they can be used directly in control flow when exception throwing is suppressed.
4
6
 
5
7
 
package/dist/index.cjs CHANGED
@@ -2,10 +2,6 @@ var __defProp = Object.defineProperty;
2
2
  var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
3
3
  var __getOwnPropNames = Object.getOwnPropertyNames;
4
4
  var __hasOwnProp = Object.prototype.hasOwnProperty;
5
- var __typeError = (msg) => {
6
- throw TypeError(msg);
7
- };
8
- var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
9
5
  var __export = (target, all) => {
10
6
  for (var name in all)
11
7
  __defProp(target, name, { get: all[name], enumerable: true });
@@ -19,12 +15,8 @@ var __copyProps = (to, from, except, desc) => {
19
15
  return to;
20
16
  };
21
17
  var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
22
- var __publicField = (obj, key, value) => __defNormalProp(obj, typeof key !== "symbol" ? key + "" : key, value);
23
- var __accessCheck = (obj, member, msg) => member.has(obj) || __typeError("Cannot " + msg);
24
- var __privateAdd = (obj, member, value) => member.has(obj) ? __typeError("Cannot add the same private member more than once") : member instanceof WeakSet ? member.add(obj) : member.set(obj, value);
25
- var __privateMethod = (obj, member, method) => (__accessCheck(obj, member, "access private method"), method);
26
18
 
27
- // src/index.js
19
+ // src/index.ts
28
20
  var index_exports = {};
29
21
  __export(index_exports, {
30
22
  Contracts: () => Contracts,
@@ -32,13 +24,24 @@ __export(index_exports, {
32
24
  });
33
25
  module.exports = __toCommonJS(index_exports);
34
26
 
35
- // src/lib/Contracts.js
36
- var _Contracts_static, check_fn, checkDebug_fn;
37
- var _Contracts = class _Contracts {
27
+ // src/lib/Contracts.ts
28
+ var Contracts = class _Contracts {
29
+ /**
30
+ * Static fields
31
+ */
32
+ /** Debug mode state */
33
+ static DEBUG_MODE = false;
34
+ /** default configuration */
35
+ static defaultConf = {
36
+ debug: false,
37
+ logger: console
38
+ };
39
+ /** logger */
40
+ static logger = console;
38
41
  /**
39
42
  * Configures contract checking behavior.
40
43
  *
41
- * @param {{debug?: boolean}} config
44
+ * @param config
42
45
  * Configuration options.
43
46
  *
44
47
  * The `debug` property enables or disables
@@ -49,12 +52,22 @@ var _Contracts = class _Contracts {
49
52
  *
50
53
  * When `debug` is `false` or omitted,
51
54
  * methods ending with `_DEBUG` skip validation.
55
+ *
56
+ * Is the `logger` property is specified,
57
+ * it is used instead of the standard logger, `console`.
58
+ * This module uses only the `error` method.
52
59
  *
53
60
  * @example
54
- * Contracts.setConfig({ debug: true });
61
+ * // Use a logger that is slightly more advanced than the standard logger—namely, `console`.
62
+ * import { PrettyConsole } from '@ayapapa-npm/pretty-console-js';
63
+ *
64
+ * const prettyConsole = new PrettyConsole();
65
+ * Contracts.setConfig({ debug: true, logger: prettyConsole });
66
+ * // Node: ` The `logger` property is optional.
55
67
  */
56
68
  static setConfig(config) {
57
- _Contracts.DEBUG_MODE = Boolean(config.debug);
69
+ _Contracts.DEBUG_MODE = Boolean(config?.debug);
70
+ _Contracts.logger = config?.logger ?? console;
58
71
  }
59
72
  /**
60
73
  * Verifies an intermediate condition during execution.
@@ -73,13 +86,13 @@ var _Contracts = class _Contracts {
73
86
  * - Confirm internal processing states.
74
87
  * - Check temporary assumptions during execution.
75
88
  *
76
- * @param {boolean} isOk
89
+ * @param isOk
77
90
  * Condition result to verify.
78
91
  *
79
- * @param {string|null} [ngMsg]
92
+ * @param [ngMsg]
80
93
  * Failure message.
81
94
  *
82
- * @param {(new (...args:any[])=>Error)|null} [ErrorClass=Error]
95
+ * @param [ErrorClass=Error]
83
96
  * Error constructor used when the check fails.
84
97
  *
85
98
  * Supported values:
@@ -89,10 +102,10 @@ var _Contracts = class _Contracts {
89
102
  * - Custom Error subclasses
90
103
  * - `null` to skip throwing and log the failure.
91
104
  *
92
- * @param {Object<string, *>} [eProps]
105
+ * @param [eProps = {}]
93
106
  * Additional properties assigned to the error object.
94
107
  *
95
- * @returns {boolean}
108
+ * @returns
96
109
  * Returns the original condition value.
97
110
  *
98
111
  * @example
@@ -104,8 +117,13 @@ var _Contracts = class _Contracts {
104
117
  * );
105
118
  */
106
119
  static VERIFY(isOk, ngMsg, ErrorClass = Error, eProps = {}) {
107
- var _a;
108
- return __privateMethod(_a = _Contracts, _Contracts_static, check_fn).call(_a, isOk, "VERIFY", ngMsg, ErrorClass, eProps);
120
+ return _Contracts.check(
121
+ isOk,
122
+ "VERIFY",
123
+ ngMsg,
124
+ ErrorClass,
125
+ eProps
126
+ );
109
127
  }
110
128
  /**
111
129
  * Verifies an intermediate condition in debug mode only.
@@ -120,19 +138,26 @@ var _Contracts = class _Contracts {
120
138
  * - Validate intermediate results during development.
121
139
  * - Check internal assumptions while debugging.
122
140
  *
123
- * @param {boolean} isOk
141
+ * @param isOk
124
142
  * Condition result to verify.
125
143
  *
126
- * @param {string|null} [ngMsg]
144
+ * @param [ngMsg]
127
145
  * Failure message.
128
146
  *
129
- * @param {(new (...args:any[])=>Error)|null} [ErrorClass=Error]
147
+ * @param [ErrorClass=Error]
130
148
  * Error constructor used when the check fails.
131
149
  *
132
- * @param {Object<string, *>} [eProps]
150
+ * Supported values:
151
+ * - `Error` (default)
152
+ * - `TypeError`
153
+ * - `RangeError`
154
+ * - Custom Error subclasses
155
+ * - `null` to skip throwing and log the failure.
156
+ *
157
+ * @param [eProps = {}]
133
158
  * Additional properties assigned to the error object.
134
159
  *
135
- * @returns {boolean}
160
+ * @returns
136
161
  * Returns the original condition value.
137
162
  *
138
163
  * @example
@@ -142,8 +167,13 @@ var _Contracts = class _Contracts {
142
167
  * );
143
168
  */
144
169
  static VERIFY_DEBUG(isOk, ngMsg, ErrorClass = Error, eProps = {}) {
145
- var _a;
146
- return __privateMethod(_a = _Contracts, _Contracts_static, checkDebug_fn).call(_a, isOk, "VERIFY_DEBUG", ngMsg, ErrorClass, eProps);
170
+ return _Contracts.checkDebug(
171
+ isOk,
172
+ "VERIFY_DEBUG",
173
+ ngMsg,
174
+ ErrorClass,
175
+ eProps
176
+ );
147
177
  }
148
178
  /**
149
179
  * Checks a precondition before execution.
@@ -158,13 +188,13 @@ var _Contracts = class _Contracts {
158
188
  * - Validate required object state.
159
189
  * - Check required external conditions.
160
190
  *
161
- * @param {boolean} isOk
191
+ * @param isOk
162
192
  * Condition result to verify.
163
193
  *
164
- * @param {string|null} [ngMsg]
194
+ * @param [ngMsg]
165
195
  * Failure message.
166
196
  *
167
- * @param {(new (...args:any[])=>Error)|null} [ErrorClass=Error]
197
+ * @param [ErrorClass=Error]
168
198
  * Error constructor used when the check fails.
169
199
  *
170
200
  * Supported values:
@@ -174,10 +204,10 @@ var _Contracts = class _Contracts {
174
204
  * - Custom Error subclasses
175
205
  * - `null` to skip throwing and log the failure.
176
206
  *
177
- * @param {Object<string, *>} [eProps]
207
+ * @param [eProps = {}]
178
208
  * Additional properties assigned to the error object.
179
209
  *
180
- * @returns {boolean}
210
+ * @returns
181
211
  * Returns the original condition value.
182
212
  *
183
213
  * @example
@@ -196,8 +226,13 @@ var _Contracts = class _Contracts {
196
226
  * }
197
227
  */
198
228
  static REQUIRE(isOk, ngMsg, ErrorClass = Error, eProps = {}) {
199
- var _a;
200
- return __privateMethod(_a = _Contracts, _Contracts_static, check_fn).call(_a, isOk, "REQUIRE", ngMsg, ErrorClass, eProps);
229
+ return _Contracts.check(
230
+ isOk,
231
+ "REQUIRE",
232
+ ngMsg,
233
+ ErrorClass,
234
+ eProps
235
+ );
201
236
  }
202
237
  /**
203
238
  * Checks a precondition in debug mode only.
@@ -212,19 +247,26 @@ var _Contracts = class _Contracts {
212
247
  * - Validate assumptions during development.
213
248
  * - Perform additional argument checks while debugging.
214
249
  *
215
- * @param {boolean} isOk
250
+ * @param isOk
216
251
  * Condition result to verify.
217
252
  *
218
- * @param {string|null} [ngMsg]
253
+ * @param [ngMsg]
219
254
  * Failure message.
220
255
  *
221
- * @param {(new (...args:any[])=>Error)|null} [ErrorClass=Error]
256
+ * @param [ErrorClass=Error]
222
257
  * Error constructor used when the check fails.
223
258
  *
224
- * @param {Object<string, *>} [eProps]
259
+ * Supported values:
260
+ * - `Error` (default)
261
+ * - `TypeError`
262
+ * - `RangeError`
263
+ * - Custom Error subclasses
264
+ * - `null` to skip throwing and log the failure.
265
+ *
266
+ * @param [eProps = {}]
225
267
  * Additional properties assigned to the error object.
226
268
  *
227
- * @returns {boolean}
269
+ * @returns
228
270
  * Returns the original condition value.
229
271
  *
230
272
  * @example
@@ -234,8 +276,13 @@ var _Contracts = class _Contracts {
234
276
  * );
235
277
  */
236
278
  static REQUIRE_DEBUG(isOk, ngMsg, ErrorClass = Error, eProps = {}) {
237
- var _a;
238
- return __privateMethod(_a = _Contracts, _Contracts_static, checkDebug_fn).call(_a, isOk, "REQUIRE_DEBUG", ngMsg, ErrorClass, eProps);
279
+ return _Contracts.checkDebug(
280
+ isOk,
281
+ "REQUIRE_DEBUG",
282
+ ngMsg,
283
+ ErrorClass,
284
+ eProps
285
+ );
239
286
  }
240
287
  /**
241
288
  * Checks a postcondition after execution.
@@ -251,13 +298,13 @@ var _Contracts = class _Contracts {
251
298
  * - Confirm state changes.
252
299
  * - Verify that processing completed correctly.
253
300
  *
254
- * @param {boolean} isOk
301
+ * @param isOk
255
302
  * Condition result to verify.
256
303
  *
257
- * @param {string|null} [ngMsg]
304
+ * @param [ngMsg]
258
305
  * Failure message.
259
306
  *
260
- * @param {(new (...args:any[])=>Error)|null} [ErrorClass=Error]
307
+ * @param [ErrorClass=Error]
261
308
  * Error constructor used when the check fails.
262
309
  *
263
310
  * Supported values:
@@ -267,10 +314,10 @@ var _Contracts = class _Contracts {
267
314
  * - Custom Error subclasses
268
315
  * - `null` to skip throwing and log the failure.
269
316
  *
270
- * @param {Object<string, *>} [eProps]
317
+ * @param [eProps = {}]
271
318
  * Additional properties assigned to the error object.
272
319
  *
273
- * @returns {boolean}
320
+ * @returns
274
321
  * Returns the original condition value.
275
322
  *
276
323
  * @example
@@ -291,8 +338,13 @@ var _Contracts = class _Contracts {
291
338
  * }
292
339
  */
293
340
  static ENSURE(isOk, ngMsg, ErrorClass = Error, eProps = {}) {
294
- var _a;
295
- return __privateMethod(_a = _Contracts, _Contracts_static, check_fn).call(_a, isOk, "ENSURE", ngMsg, ErrorClass, eProps);
341
+ return _Contracts.check(
342
+ isOk,
343
+ "ENSURE",
344
+ ngMsg,
345
+ ErrorClass,
346
+ eProps
347
+ );
296
348
  }
297
349
  /**
298
350
  * Checks a postcondition in debug mode only.
@@ -307,19 +359,26 @@ var _Contracts = class _Contracts {
307
359
  * - Validate detailed results during development.
308
360
  * - Confirm internal behavior while debugging.
309
361
  *
310
- * @param {boolean} isOk
362
+ * @param isOk
311
363
  * Condition result to verify.
312
364
  *
313
- * @param {string|null} [ngMsg]
365
+ * @param [ngMsg]
314
366
  * Failure message.
315
367
  *
316
- * @param {(new (...args:any[])=>Error)|null} [ErrorClass=Error]
368
+ * @param [ErrorClass=Error]
317
369
  * Error constructor used when the check fails.
318
370
  *
319
- * @param {Object<string, *>} [eProps]
371
+ * Supported values:
372
+ * - `Error` (default)
373
+ * - `TypeError`
374
+ * - `RangeError`
375
+ * - Custom Error subclasses
376
+ * - `null` to skip throwing and log the failure.
377
+ *
378
+ * @param [eProps = {}]
320
379
  * Additional properties assigned to the error object.
321
380
  *
322
- * @returns {boolean}
381
+ * @returns
323
382
  * Returns the original condition value.
324
383
  *
325
384
  * @example
@@ -329,8 +388,13 @@ var _Contracts = class _Contracts {
329
388
  * );
330
389
  */
331
390
  static ENSURE_DEBUG(isOk, ngMsg, ErrorClass = Error, eProps = {}) {
332
- var _a;
333
- return __privateMethod(_a = _Contracts, _Contracts_static, checkDebug_fn).call(_a, isOk, "ENSURE_DEBUG", ngMsg, ErrorClass, eProps);
391
+ return _Contracts.checkDebug(
392
+ isOk,
393
+ "ENSURE_DEBUG",
394
+ ngMsg,
395
+ ErrorClass,
396
+ eProps
397
+ );
334
398
  }
335
399
  /**
336
400
  * Checks an invariant condition.
@@ -346,13 +410,13 @@ var _Contracts = class _Contracts {
346
410
  * In Design by Contract terminology,
347
411
  * INVARIANT represents conditions that must always remain true.
348
412
  *
349
- * @param {boolean} isOk
413
+ * @param isOk
350
414
  * Condition result to verify.
351
415
  *
352
- * @param {string|null} [ngMsg]
416
+ * @param [ngMsg]
353
417
  * Failure message.
354
418
  *
355
- * @param {(new (...args:any[])=>Error)|null} [ErrorClass=Error]
419
+ * @param [ErrorClass=Error]
356
420
  * Error constructor used when the check fails.
357
421
  *
358
422
  * Supported values:
@@ -362,10 +426,10 @@ var _Contracts = class _Contracts {
362
426
  * - Custom Error subclasses
363
427
  * - `null` to skip throwing and log the failure.
364
428
  *
365
- * @param {Object<string, *>} [eProps]
429
+ * @param [eProps = {}]
366
430
  * Additional properties assigned to the error object.
367
431
  *
368
- * @returns {boolean}
432
+ * @returns
369
433
  * Returns the original condition value.
370
434
  *
371
435
  * @example
@@ -383,8 +447,13 @@ var _Contracts = class _Contracts {
383
447
  * }
384
448
  */
385
449
  static INVARIANT(isOk, ngMsg, ErrorClass = Error, eProps = {}) {
386
- var _a;
387
- return __privateMethod(_a = _Contracts, _Contracts_static, check_fn).call(_a, isOk, "INVARIANT", ngMsg, ErrorClass, eProps);
450
+ return _Contracts.check(
451
+ isOk,
452
+ "INVARIANT",
453
+ ngMsg,
454
+ ErrorClass,
455
+ eProps
456
+ );
388
457
  }
389
458
  /**
390
459
  * Checks an invariant condition in debug mode only.
@@ -399,19 +468,26 @@ var _Contracts = class _Contracts {
399
468
  * - Validate object consistency during development.
400
469
  * - Detect unexpected state changes while debugging.
401
470
  *
402
- * @param {boolean} isOk
471
+ * @param isOk
403
472
  * Condition result to verify.
404
473
  *
405
- * @param {string|null} [ngMsg]
474
+ * @param [ngMsg]
406
475
  * Failure message.
407
476
  *
408
- * @param {(new (...args:any[])=>Error)|null} [ErrorClass=Error]
477
+ * @param [ErrorClass=Error]
409
478
  * Error constructor used when the check fails.
410
479
  *
411
- * @param {Object<string, *>} [eProps]
480
+ * Supported values:
481
+ * - `Error` (default)
482
+ * - `TypeError`
483
+ * - `RangeError`
484
+ * - Custom Error subclasses
485
+ * - `null` to skip throwing and log the failure.
486
+ *
487
+ * @param [eProps = {}]
412
488
  * Additional properties assigned to the error object.
413
489
  *
414
- * @returns {boolean}
490
+ * @returns
415
491
  * Returns the original condition value.
416
492
  *
417
493
  * @example
@@ -421,36 +497,106 @@ var _Contracts = class _Contracts {
421
497
  * );
422
498
  */
423
499
  static INVARIANT_DEBUG(isOk, ngMsg, ErrorClass = Error, eProps = {}) {
424
- var _a;
425
- return __privateMethod(_a = _Contracts, _Contracts_static, checkDebug_fn).call(_a, isOk, "INVARIANT_DEBUG", ngMsg, ErrorClass, eProps);
500
+ return _Contracts.checkDebug(
501
+ isOk,
502
+ "INVARIANT_DEBUG",
503
+ ngMsg,
504
+ ErrorClass,
505
+ eProps
506
+ );
426
507
  }
427
- };
428
- _Contracts_static = new WeakSet();
429
- check_fn = function(isOk, prefix = "CHECK", ngMsg = "", ErrorClass = Error, eProps = {}) {
430
- if (!isOk) {
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);
508
+ /**
509
+ * Core contract evaluation logic.
510
+ *
511
+ * Evaluates a condition and handles failures.
512
+ * All public contract methods delegate to this method.
513
+ *
514
+ * Behavior:
515
+ * - When the condition is true, returns the original value.
516
+ * - When the condition is false and ErrorClass is specified,
517
+ * throws an instance of the specified error class.
518
+ * - When ErrorClass is null,
519
+ * logs the failure message instead of throwing.
520
+ *
521
+ * @param isOk
522
+ * Condition result.
523
+ *
524
+ * @param prefix
525
+ * Contract type prefix used in the error message.
526
+ *
527
+ * @param ngMsg
528
+ * Failure message.
529
+ *
530
+ * @param ErrorClass
531
+ * Error constructor.
532
+ *
533
+ * @param eProps
534
+ * Additional properties assigned to the error object.
535
+ *
536
+ * @returns
537
+ * Returns the original condition value.
538
+ *
539
+ */
540
+ static check(isOk, prefix = "CHECK", ngMsg = "", ErrorClass = Error, eProps = {}) {
541
+ if (!isOk) {
542
+ const msg = `[${prefix}] ${ngMsg ?? ""}`;
543
+ if (ErrorClass) {
544
+ throw Object.assign(
545
+ new ErrorClass(msg),
546
+ eProps
547
+ );
548
+ }
549
+ if (ngMsg) {
550
+ _Contracts.getLogger().error(msg, eProps);
551
+ }
440
552
  }
553
+ return isOk;
554
+ }
555
+ /**
556
+ * Debug-only contract evaluation logic.
557
+ *
558
+ * Executes contract validation only when DEBUG_MODE
559
+ * is enabled.
560
+ *
561
+ * When DEBUG_MODE is disabled,
562
+ * this method returns the original condition value
563
+ * without performing any validation.
564
+ *
565
+ * @param isOk
566
+ * Condition result.
567
+ *
568
+ * @param prefix
569
+ * Contract type prefix used in the error message.
570
+ *
571
+ * @param ngMsg
572
+ * Failure message.
573
+ *
574
+ * @param ErrorClass
575
+ * Error constructor.
576
+ *
577
+ * @param eProps
578
+ * Additional properties assigned to the error object.
579
+ *
580
+ * @returns
581
+ * Returns the original condition value.
582
+ *
583
+ */
584
+ static checkDebug(isOk, prefix = "CHECK", ngMsg = "", ErrorClass = Error, eProps = {}) {
585
+ return _Contracts.DEBUG_MODE ? _Contracts.check(
586
+ isOk,
587
+ prefix,
588
+ ngMsg,
589
+ ErrorClass,
590
+ eProps
591
+ ) : isOk;
592
+ }
593
+ /** Get logger */
594
+ static getLogger() {
595
+ return _Contracts.logger ?? console;
441
596
  }
442
- return isOk;
443
- };
444
- checkDebug_fn = function(isOk, prefix = "CHECK_DEBUG", ngMsg = "", ErrorClass = Error, eProps = {}) {
445
- var _a;
446
- return _Contracts.DEBUG_MODE ? __privateMethod(_a = _Contracts, _Contracts_static, check_fn).call(_a, isOk, prefix, ngMsg, ErrorClass, eProps) : isOk;
447
597
  };
448
- __privateAdd(_Contracts, _Contracts_static);
449
- /** @type {boolean} Debug mode state */
450
- __publicField(_Contracts, "DEBUG_MODE", false);
451
- var Contracts = _Contracts;
452
598
 
453
- // src/index.js
599
+ // src/index.ts
454
600
  var index_default = Contracts;
455
601
  // Annotate the CommonJS export names for ESM import in node:
456
602
  0 && (module.exports = {