@ayapapa-npm/contracts-js 0.2.5 → 0.4.0

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,4 +1,10 @@
1
1
  [![CI](https://github.com/ayapapa/contracts-js/actions/workflows/ci.yml/badge.svg)](https://github.com/ayapapa/contracts-js/actions/workflows/ci.yml)
2
+ ![Coverage](https://raw.githubusercontent.com/ayapapa/pretty-console-js/main/badges/coverage-total.svg)
3
+ ![Branches](https://raw.githubusercontent.com/ayapapa/pretty-console-js/main/badges/coverage-branches.svg)
4
+ ![Functions](https://raw.githubusercontent.com/ayapapa/pretty-console-js/main/badges/coverage-functions.svg)
5
+ ![Lines](https://raw.githubusercontent.com/ayapapa/pretty-console-js/main/badges/coverage-lines.svg)
6
+ ![Statements](https://raw.githubusercontent.com/ayapapa/pretty-console-js/main/badges/coverage-statements.svg)
7
+
2
8
  # contracts-js
3
9
  A lightweight Design by Contract library for JavaScript.</br>
4
10
  Provides runtime contract checks based on Design by Contract principles.</br>
@@ -126,6 +132,9 @@ Contracts.INVARIANT(
126
132
  );
127
133
  ```
128
134
 
135
+ ## API Reference
136
+ [API document](docs/api.md)
137
+
129
138
  ## Usage
130
139
 
131
140
  ```javascript
package/dist/index.cjs CHANGED
@@ -1,3 +1,4 @@
1
+ "use strict";
1
2
  var __defProp = Object.defineProperty;
2
3
  var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
3
4
  var __getOwnPropNames = Object.getOwnPropertyNames;
@@ -32,30 +33,39 @@ var Contracts = class _Contracts {
32
33
  /** Debug mode state */
33
34
  static DEBUG_MODE = false;
34
35
  /** default configuration */
35
- static defaultConf = {
36
+ static #defaultConf = {
36
37
  debug: false,
37
38
  logger: console
38
39
  };
40
+ /** Current config. */
41
+ static #config = { ..._Contracts.#defaultConf };
39
42
  /** logger */
40
- static logger = console;
43
+ //private static logger: LogProvider = console;
41
44
  /**
42
45
  * Configures contract checking behavior.
43
46
  *
44
47
  * @param config
45
- * Configuration options.
46
- *
47
- * The `debug` property enables or disables
48
- * debug-only contract checks.
49
- *
48
+ * Configuration options. <br>
49
+ * <br>
50
+ * The `debug` property toggles the behavior—specifically,
51
+ * throwing an exception or outputting to the console when the condition is
52
+ * false—for the validation of contracts intended for use during debugging (methods ending in `_DEBUG`). <br>
53
+ * <br>
50
54
  * When `debug` is `true`,
51
- * methods ending with `_DEBUG` perform validation.
52
- *
53
- * When `debug` is `false` or omitted,
54
- * methods ending with `_DEBUG` skip validation.
55
- *
55
+ * methods ending with `_DEBUG` perform validation. <br>
56
+ * <br>
57
+ * When `debug` is `false`,
58
+ * methods ending with `_DEBUG` skip validation. <br>
59
+ * <br>
56
60
  * Is the `logger` property is specified,
57
61
  * it is used instead of the standard logger, `console`.
58
- * This module uses only the `error` method of the `logger`.
62
+ * This module uses only the `error` method of the `logger`. <br>
63
+ * <br>
64
+ * Note: If the value of a property is `undefined`, it is treated as unspecified.
65
+ *
66
+ * @param reset
67
+ * If `true`, unspecified values ​​are saved to the settings as default values. <br>
68
+ * If `false`, unspecified values ​​remain at their current settings.
59
69
  *
60
70
  * @example
61
71
  * // Use a logger that is slightly more advanced than the standard logger—namely, `console`.
@@ -65,9 +75,36 @@ var Contracts = class _Contracts {
65
75
  * Contracts.setConfig({ debug: true, logger: prettyConsole });
66
76
  * // Node: ` The `logger` property is optional.
67
77
  */
68
- static setConfig(config) {
69
- _Contracts.DEBUG_MODE = Boolean(config?.debug);
70
- _Contracts.logger = config?.logger ?? console;
78
+ static setConfig(config, reset = true) {
79
+ const rConf = { ...config };
80
+ for (let key of Object.keys(rConf)) {
81
+ if (rConf[key] == null) delete rConf[key];
82
+ }
83
+ if (reset) _Contracts.#config = { ..._Contracts.getDefaultConfig() };
84
+ Object.assign(_Contracts.#config, rConf);
85
+ _Contracts.DEBUG_MODE = _Contracts.#config.debug;
86
+ }
87
+ /**
88
+ * Get the default configurations.
89
+ * @returns Default configurations.
90
+ */
91
+ static getDefaultConfig() {
92
+ return { ..._Contracts.#defaultConf };
93
+ }
94
+ /**
95
+ * Get the current configurations.
96
+ * @returns Default configurations.
97
+ */
98
+ static getConfig() {
99
+ _Contracts.#config.debug = _Contracts.DEBUG_MODE;
100
+ return { ..._Contracts.#config };
101
+ }
102
+ /**
103
+ * Reset the current configurations to the default configurations.
104
+ * @returns Default configurations.
105
+ */
106
+ static resetConfig() {
107
+ _Contracts.setConfig({}, true);
71
108
  }
72
109
  /**
73
110
  * Verifies an intermediate condition during execution.
@@ -87,13 +124,14 @@ var Contracts = class _Contracts {
87
124
  * - Check temporary assumptions during execution.
88
125
  *
89
126
  * @param isOk
90
- * Condition result to verify.
127
+ * Condition result(`boolean`) to be verified.
91
128
  *
92
- * @param [ngMsg]
129
+ * @param ngMsg
93
130
  * Failure message.
94
131
  *
95
- * @param [ErrorClass=Error]
132
+ * @param ErrorClass
96
133
  * Error constructor used when the check fails.
134
+ * This is used as follows: throw Object.assign(new ErrorClass(msg, eParams), eProps);
97
135
  *
98
136
  * Supported values:
99
137
  * - `Error` (default)
@@ -102,7 +140,10 @@ var Contracts = class _Contracts {
102
140
  * - Custom Error subclasses
103
141
  * - `null` to skip throwing and log the failure.
104
142
  *
105
- * @param [eProps = {}]
143
+ * @param eParams
144
+ * Parameter options following the message passed to the Error constructor.
145
+ *
146
+ * @param eProps
106
147
  * Additional properties assigned to the error object.
107
148
  *
108
149
  * @returns
@@ -116,12 +157,13 @@ var Contracts = class _Contracts {
116
157
  * 'Calculation result must not be negative'
117
158
  * );
118
159
  */
119
- static VERIFY(isOk, ngMsg, ErrorClass = Error, eProps = {}) {
160
+ static VERIFY(isOk, ngMsg, ErrorClass = Error, eParams, eProps) {
120
161
  return _Contracts.check(
121
162
  isOk,
122
163
  "VERIFY",
123
164
  ngMsg,
124
165
  ErrorClass,
166
+ eParams,
125
167
  eProps
126
168
  );
127
169
  }
@@ -139,13 +181,14 @@ var Contracts = class _Contracts {
139
181
  * - Check internal assumptions while debugging.
140
182
  *
141
183
  * @param isOk
142
- * Condition result to verify.
184
+ * Condition result(`boolean`) to be verified.
143
185
  *
144
- * @param [ngMsg]
186
+ * @param ngMsg
145
187
  * Failure message.
146
188
  *
147
- * @param [ErrorClass=Error]
189
+ * @param ErrorClass
148
190
  * Error constructor used when the check fails.
191
+ * This is used as follows: throw Object.assign(new ErrorClass(msg, eParams), eProps);
149
192
  *
150
193
  * Supported values:
151
194
  * - `Error` (default)
@@ -154,7 +197,10 @@ var Contracts = class _Contracts {
154
197
  * - Custom Error subclasses
155
198
  * - `null` to skip throwing and log the failure.
156
199
  *
157
- * @param [eProps = {}]
200
+ * @param eParams
201
+ * Parameter options following the message passed to the Error constructor.
202
+ *
203
+ * @param eProps
158
204
  * Additional properties assigned to the error object.
159
205
  *
160
206
  * @returns
@@ -166,12 +212,13 @@ var Contracts = class _Contracts {
166
212
  * 'Intermediate value must not be null'
167
213
  * );
168
214
  */
169
- static VERIFY_DEBUG(isOk, ngMsg, ErrorClass = Error, eProps = {}) {
215
+ static VERIFY_DEBUG(isOk, ngMsg, ErrorClass = Error, eParams, eProps) {
170
216
  return _Contracts.checkDebug(
171
217
  isOk,
172
218
  "VERIFY_DEBUG",
173
219
  ngMsg,
174
220
  ErrorClass,
221
+ eParams,
175
222
  eProps
176
223
  );
177
224
  }
@@ -189,13 +236,14 @@ var Contracts = class _Contracts {
189
236
  * - Check required external conditions.
190
237
  *
191
238
  * @param isOk
192
- * Condition result to verify.
239
+ * Condition result(`boolean`) to be verified.
193
240
  *
194
- * @param [ngMsg]
241
+ * @param ngMsg
195
242
  * Failure message.
196
243
  *
197
- * @param [ErrorClass=Error]
244
+ * @param ErrorClass
198
245
  * Error constructor used when the check fails.
246
+ * This is used as follows: throw Object.assign(new ErrorClass(msg, eParams), eProps);
199
247
  *
200
248
  * Supported values:
201
249
  * - `Error` (default)
@@ -204,7 +252,10 @@ var Contracts = class _Contracts {
204
252
  * - Custom Error subclasses
205
253
  * - `null` to skip throwing and log the failure.
206
254
  *
207
- * @param [eProps = {}]
255
+ * @param eParams
256
+ * Parameter options following the message passed to the Error constructor.
257
+ *
258
+ * @param eProps
208
259
  * Additional properties assigned to the error object.
209
260
  *
210
261
  * @returns
@@ -225,12 +276,13 @@ var Contracts = class _Contracts {
225
276
  * return a / b;
226
277
  * }
227
278
  */
228
- static REQUIRE(isOk, ngMsg, ErrorClass = Error, eProps = {}) {
279
+ static REQUIRE(isOk, ngMsg, ErrorClass = Error, eParams, eProps) {
229
280
  return _Contracts.check(
230
281
  isOk,
231
282
  "REQUIRE",
232
283
  ngMsg,
233
284
  ErrorClass,
285
+ eParams,
234
286
  eProps
235
287
  );
236
288
  }
@@ -248,13 +300,14 @@ var Contracts = class _Contracts {
248
300
  * - Perform additional argument checks while debugging.
249
301
  *
250
302
  * @param isOk
251
- * Condition result to verify.
303
+ * Condition result(`boolean`) to be verified.
252
304
  *
253
- * @param [ngMsg]
305
+ * @param ngMsg
254
306
  * Failure message.
255
307
  *
256
- * @param [ErrorClass=Error]
308
+ * @param ErrorClass
257
309
  * Error constructor used when the check fails.
310
+ * This is used as follows: throw Object.assign(new ErrorClass(msg, eParams), eProps);
258
311
  *
259
312
  * Supported values:
260
313
  * - `Error` (default)
@@ -263,7 +316,10 @@ var Contracts = class _Contracts {
263
316
  * - Custom Error subclasses
264
317
  * - `null` to skip throwing and log the failure.
265
318
  *
266
- * @param [eProps = {}]
319
+ * @param eParams
320
+ * Parameter options following the message passed to the Error constructor.
321
+ *
322
+ * @param eProps
267
323
  * Additional properties assigned to the error object.
268
324
  *
269
325
  * @returns
@@ -275,12 +331,13 @@ var Contracts = class _Contracts {
275
331
  * 'User must exist during debugging'
276
332
  * );
277
333
  */
278
- static REQUIRE_DEBUG(isOk, ngMsg, ErrorClass = Error, eProps = {}) {
334
+ static REQUIRE_DEBUG(isOk, ngMsg, ErrorClass = Error, eParams, eProps) {
279
335
  return _Contracts.checkDebug(
280
336
  isOk,
281
337
  "REQUIRE_DEBUG",
282
338
  ngMsg,
283
339
  ErrorClass,
340
+ eParams,
284
341
  eProps
285
342
  );
286
343
  }
@@ -299,13 +356,14 @@ var Contracts = class _Contracts {
299
356
  * - Verify that processing completed correctly.
300
357
  *
301
358
  * @param isOk
302
- * Condition result to verify.
359
+ * Condition result(`boolean`) to be verified.
303
360
  *
304
- * @param [ngMsg]
361
+ * @param ngMsg
305
362
  * Failure message.
306
363
  *
307
- * @param [ErrorClass=Error]
364
+ * @param ErrorClass
308
365
  * Error constructor used when the check fails.
366
+ * This is used as follows: throw Object.assign(new ErrorClass(msg, eParams), eProps);
309
367
  *
310
368
  * Supported values:
311
369
  * - `Error` (default)
@@ -314,7 +372,10 @@ var Contracts = class _Contracts {
314
372
  * - Custom Error subclasses
315
373
  * - `null` to skip throwing and log the failure.
316
374
  *
317
- * @param [eProps = {}]
375
+ * @param eParams
376
+ * Parameter options following the message passed to the Error constructor.
377
+ *
378
+ * @param eProps
318
379
  * Additional properties assigned to the error object.
319
380
  *
320
381
  * @returns
@@ -337,12 +398,13 @@ var Contracts = class _Contracts {
337
398
  * return result;
338
399
  * }
339
400
  */
340
- static ENSURE(isOk, ngMsg, ErrorClass = Error, eProps = {}) {
401
+ static ENSURE(isOk, ngMsg, ErrorClass = Error, eParams, eProps) {
341
402
  return _Contracts.check(
342
403
  isOk,
343
404
  "ENSURE",
344
405
  ngMsg,
345
406
  ErrorClass,
407
+ eParams,
346
408
  eProps
347
409
  );
348
410
  }
@@ -360,13 +422,14 @@ var Contracts = class _Contracts {
360
422
  * - Confirm internal behavior while debugging.
361
423
  *
362
424
  * @param isOk
363
- * Condition result to verify.
425
+ * Condition result(`boolean`) to be verified.
364
426
  *
365
- * @param [ngMsg]
427
+ * @param ngMsg
366
428
  * Failure message.
367
429
  *
368
- * @param [ErrorClass=Error]
430
+ * @param ErrorClass
369
431
  * Error constructor used when the check fails.
432
+ * This is used as follows: throw Object.assign(new ErrorClass(msg, eParams), eProps);
370
433
  *
371
434
  * Supported values:
372
435
  * - `Error` (default)
@@ -375,7 +438,10 @@ var Contracts = class _Contracts {
375
438
  * - Custom Error subclasses
376
439
  * - `null` to skip throwing and log the failure.
377
440
  *
378
- * @param [eProps = {}]
441
+ * @param eParams
442
+ * Parameter options following the message passed to the Error constructor.
443
+ *
444
+ * @param eProps
379
445
  * Additional properties assigned to the error object.
380
446
  *
381
447
  * @returns
@@ -387,12 +453,13 @@ var Contracts = class _Contracts {
387
453
  * 'Result should exist during debugging'
388
454
  * );
389
455
  */
390
- static ENSURE_DEBUG(isOk, ngMsg, ErrorClass = Error, eProps = {}) {
456
+ static ENSURE_DEBUG(isOk, ngMsg, ErrorClass = Error, eParams, eProps) {
391
457
  return _Contracts.checkDebug(
392
458
  isOk,
393
459
  "ENSURE_DEBUG",
394
460
  ngMsg,
395
461
  ErrorClass,
462
+ eParams,
396
463
  eProps
397
464
  );
398
465
  }
@@ -411,13 +478,14 @@ var Contracts = class _Contracts {
411
478
  * INVARIANT represents conditions that must always remain true.
412
479
  *
413
480
  * @param isOk
414
- * Condition result to verify.
481
+ * Condition result(`boolean`) to be verified.
415
482
  *
416
- * @param [ngMsg]
483
+ * @param ngMsg
417
484
  * Failure message.
418
485
  *
419
- * @param [ErrorClass=Error]
486
+ * @param ErrorClass
420
487
  * Error constructor used when the check fails.
488
+ * This is used as follows: throw Object.assign(new ErrorClass(msg, eParams), eProps);
421
489
  *
422
490
  * Supported values:
423
491
  * - `Error` (default)
@@ -426,7 +494,10 @@ var Contracts = class _Contracts {
426
494
  * - Custom Error subclasses
427
495
  * - `null` to skip throwing and log the failure.
428
496
  *
429
- * @param [eProps = {}]
497
+ * @param eParams
498
+ * Parameter options following the message passed to the Error constructor.
499
+ *
500
+ * @param eProps
430
501
  * Additional properties assigned to the error object.
431
502
  *
432
503
  * @returns
@@ -446,12 +517,13 @@ var Contracts = class _Contracts {
446
517
  *
447
518
  * }
448
519
  */
449
- static INVARIANT(isOk, ngMsg, ErrorClass = Error, eProps = {}) {
520
+ static INVARIANT(isOk, ngMsg, ErrorClass = Error, eParams, eProps) {
450
521
  return _Contracts.check(
451
522
  isOk,
452
523
  "INVARIANT",
453
524
  ngMsg,
454
525
  ErrorClass,
526
+ eParams,
455
527
  eProps
456
528
  );
457
529
  }
@@ -469,13 +541,14 @@ var Contracts = class _Contracts {
469
541
  * - Detect unexpected state changes while debugging.
470
542
  *
471
543
  * @param isOk
472
- * Condition result to verify.
544
+ * Condition result(`boolean`) to be verified.
473
545
  *
474
546
  * @param [ngMsg]
475
547
  * Failure message.
476
548
  *
477
549
  * @param [ErrorClass=Error]
478
550
  * Error constructor used when the check fails.
551
+ * This is used as follows: throw Object.assign(new ErrorClass(msg, eParams), eProps);
479
552
  *
480
553
  * Supported values:
481
554
  * - `Error` (default)
@@ -484,6 +557,9 @@ var Contracts = class _Contracts {
484
557
  * - Custom Error subclasses
485
558
  * - `null` to skip throwing and log the failure.
486
559
  *
560
+ * @param eParams
561
+ * Parameter options following the message passed to the Error constructor.
562
+ *
487
563
  * @param [eProps = {}]
488
564
  * Additional properties assigned to the error object.
489
565
  *
@@ -496,12 +572,13 @@ var Contracts = class _Contracts {
496
572
  * 'Cache size exceeded expected limit'
497
573
  * );
498
574
  */
499
- static INVARIANT_DEBUG(isOk, ngMsg, ErrorClass = Error, eProps = {}) {
575
+ static INVARIANT_DEBUG(isOk, ngMsg, ErrorClass = Error, eParams, eProps) {
500
576
  return _Contracts.checkDebug(
501
577
  isOk,
502
578
  "INVARIANT_DEBUG",
503
579
  ngMsg,
504
580
  ErrorClass,
581
+ eParams,
505
582
  eProps
506
583
  );
507
584
  }
@@ -522,7 +599,7 @@ var Contracts = class _Contracts {
522
599
  * @internal
523
600
  *
524
601
  * @param isOk
525
- * Condition result.
602
+ * Condition result(`boolean`) to be verified.
526
603
  *
527
604
  * @param prefix
528
605
  * Contract type prefix used in the error message.
@@ -531,8 +608,19 @@ var Contracts = class _Contracts {
531
608
  * Failure message.
532
609
  *
533
610
  * @param ErrorClass
534
- * Error constructor.
611
+ * Error constructor used when the check fails.
612
+ * This is used as follows: throw Object.assign(new ErrorClass(msg, eParams), eProps);
535
613
  *
614
+ * Supported values:
615
+ * - `Error` (default)
616
+ * - `TypeError`
617
+ * - `RangeError`
618
+ * - Custom Error subclasses
619
+ * - `null` to skip throwing and log the failure.
620
+ *
621
+ * @param eParams
622
+ * Parameter options following the message passed to the Error constructor.
623
+ *
536
624
  * @param eProps
537
625
  * Additional properties assigned to the error object.
538
626
  *
@@ -540,14 +628,19 @@ var Contracts = class _Contracts {
540
628
  * Returns the original condition value.
541
629
  *
542
630
  */
543
- static check(isOk, prefix = "CHECK", ngMsg = "", ErrorClass = Error, eProps = {}) {
631
+ static check(isOk, prefix = "CHECK", ngMsg = "", ErrorClass = Error, eParams, eProps) {
632
+ if (eProps === void 0) {
633
+ eProps = eParams ?? {};
634
+ eParams = null;
635
+ } else {
636
+ eProps ??= {};
637
+ }
544
638
  if (!isOk) {
545
639
  const msg = `[${prefix}] ${ngMsg ?? ""}`;
546
640
  if (ErrorClass) {
547
- throw Object.assign(
548
- new ErrorClass(msg),
549
- eProps
550
- );
641
+ const err = eParams ? new ErrorClass(msg, eParams) : new ErrorClass(msg);
642
+ eProps && Object.assign(err, eProps);
643
+ throw err;
551
644
  }
552
645
  if (ngMsg) {
553
646
  _Contracts.getLogger().error(msg, eProps);
@@ -568,7 +661,7 @@ var Contracts = class _Contracts {
568
661
  * @internal
569
662
  *
570
663
  * @param isOk
571
- * Condition result.
664
+ * Condition result(`boolean`) to be verified.
572
665
  *
573
666
  * @param prefix
574
667
  * Contract type prefix used in the error message.
@@ -576,8 +669,19 @@ var Contracts = class _Contracts {
576
669
  * @param ngMsg
577
670
  * Failure message.
578
671
  *
579
- * @param ErrorClass
580
- * Error constructor.
672
+ * @param ErrorClass=Error
673
+ * Error constructor used when the check fails.
674
+ * This is used as follows: throw Object.assign(new ErrorClass(msg, eParams), eProps);
675
+ *
676
+ * Supported values:
677
+ * - `Error` (default)
678
+ * - `TypeError`
679
+ * - `RangeError`
680
+ * - Custom Error subclasses
681
+ * - `null` to skip throwing and log the failure.
682
+ *
683
+ * @param eParams
684
+ * Parameter options following the message passed to the Error constructor.
581
685
  *
582
686
  * @param eProps
583
687
  * Additional properties assigned to the error object.
@@ -586,12 +690,13 @@ var Contracts = class _Contracts {
586
690
  * Returns the original condition value.
587
691
  *
588
692
  */
589
- static checkDebug(isOk, prefix = "CHECK", ngMsg = "", ErrorClass = Error, eProps = {}) {
693
+ static checkDebug(isOk, prefix = "CHECK", ngMsg = "", ErrorClass = Error, eParams, eProps) {
590
694
  return _Contracts.DEBUG_MODE ? _Contracts.check(
591
695
  isOk,
592
696
  prefix,
593
697
  ngMsg,
594
698
  ErrorClass,
699
+ eParams,
595
700
  eProps
596
701
  ) : isOk;
597
702
  }
@@ -601,7 +706,7 @@ var Contracts = class _Contracts {
601
706
  * @internal
602
707
  */
603
708
  static getLogger() {
604
- return _Contracts.logger ?? console;
709
+ return _Contracts.#config.logger;
605
710
  }
606
711
  };
607
712