@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/dist/index.d.ts CHANGED
@@ -1,8 +1,30 @@
1
1
  /**
2
- * Design by Contract utilities.
2
+ * Type of the console replacement object
3
+ */
4
+ type LogProvider = Pick<Console, 'error'>;
5
+ /**
6
+ * Configuration
7
+ */
8
+ interface Config {
9
+ /**
10
+ * Debug mode state.
11
+ * If `true`, `DEBUG_MODE` is enabled; otherwise, it is disabled.
12
+ * The default is `false`.
13
+ */
14
+ debug: boolean;
15
+ /**
16
+ * External logger.
17
+ * If specified, it is used instead of the standard logger, `console`.
18
+ * This module uses only the `error` method.
19
+ */
20
+ logger: LogProvider;
21
+ }
22
+ /**
23
+ * A lightweight Design by Contract library for JavaScript.
3
24
  *
4
- * Provides runtime contract checks based on
5
- * Design by Contract principles.
25
+ * Provides runtime contract checks based on Design by Contract principles.
26
+ * All check functions return the evaluated condition itself,
27
+ * so they can be used directly in control flow when exception throwing is suppressed.
6
28
  *
7
29
  * ## Contract Types
8
30
  *
@@ -34,12 +56,19 @@
34
56
  * @module Contracts
35
57
  */
36
58
  declare class Contracts {
37
- /** @type {boolean} Debug mode state */
59
+ /**
60
+ * Static fields
61
+ */
62
+ /** Debug mode state */
38
63
  static DEBUG_MODE: boolean;
64
+ /** default configuration */
65
+ private static readonly defaultConf;
66
+ /** logger */
67
+ private static logger;
39
68
  /**
40
69
  * Configures contract checking behavior.
41
70
  *
42
- * @param {{debug?: boolean}} config
71
+ * @param config
43
72
  * Configuration options.
44
73
  *
45
74
  * The `debug` property enables or disables
@@ -51,12 +80,19 @@ declare class Contracts {
51
80
  * When `debug` is `false` or omitted,
52
81
  * methods ending with `_DEBUG` skip validation.
53
82
  *
83
+ * Is the `logger` property is specified,
84
+ * it is used instead of the standard logger, `console`.
85
+ * This module uses only the `error` method.
86
+ *
54
87
  * @example
55
- * Contracts.setConfig({ debug: true });
88
+ * // Use a logger that is slightly more advanced than the standard logger—namely, `console`.
89
+ * import { PrettyConsole } from '@ayapapa-npm/pretty-console-js';
90
+ *
91
+ * const prettyConsole = new PrettyConsole();
92
+ * Contracts.setConfig({ debug: true, logger: prettyConsole });
93
+ * // Node: ` The `logger` property is optional.
56
94
  */
57
- static setConfig(config: {
58
- debug?: boolean;
59
- }): void;
95
+ static setConfig(config: Config): void;
60
96
  /**
61
97
  * Verifies an intermediate condition during execution.
62
98
  *
@@ -74,13 +110,13 @@ declare class Contracts {
74
110
  * - Confirm internal processing states.
75
111
  * - Check temporary assumptions during execution.
76
112
  *
77
- * @param {boolean} isOk
113
+ * @param isOk
78
114
  * Condition result to verify.
79
115
  *
80
- * @param {string|null} [ngMsg]
116
+ * @param [ngMsg]
81
117
  * Failure message.
82
118
  *
83
- * @param {(new (...args:any[])=>Error)|null} [ErrorClass=Error]
119
+ * @param [ErrorClass=Error]
84
120
  * Error constructor used when the check fails.
85
121
  *
86
122
  * Supported values:
@@ -90,10 +126,10 @@ declare class Contracts {
90
126
  * - Custom Error subclasses
91
127
  * - `null` to skip throwing and log the failure.
92
128
  *
93
- * @param {Object<string, *>} [eProps]
129
+ * @param [eProps = {}]
94
130
  * Additional properties assigned to the error object.
95
131
  *
96
- * @returns {boolean}
132
+ * @returns
97
133
  * Returns the original condition value.
98
134
  *
99
135
  * @example
@@ -104,9 +140,7 @@ declare class Contracts {
104
140
  * 'Calculation result must not be negative'
105
141
  * );
106
142
  */
107
- static VERIFY(isOk: boolean, ngMsg?: string | null, ErrorClass?: (new (...args: any[]) => Error) | null, eProps?: {
108
- [x: string]: any;
109
- }): boolean;
143
+ static VERIFY(isOk: boolean, ngMsg: string | null, ErrorClass?: (new (...args: any[]) => Error) | null, eProps?: {}): boolean;
110
144
  /**
111
145
  * Verifies an intermediate condition in debug mode only.
112
146
  *
@@ -120,19 +154,26 @@ declare class Contracts {
120
154
  * - Validate intermediate results during development.
121
155
  * - Check internal assumptions while debugging.
122
156
  *
123
- * @param {boolean} isOk
157
+ * @param isOk
124
158
  * Condition result to verify.
125
159
  *
126
- * @param {string|null} [ngMsg]
160
+ * @param [ngMsg]
127
161
  * Failure message.
128
162
  *
129
- * @param {(new (...args:any[])=>Error)|null} [ErrorClass=Error]
163
+ * @param [ErrorClass=Error]
130
164
  * Error constructor used when the check fails.
131
165
  *
132
- * @param {Object<string, *>} [eProps]
166
+ * Supported values:
167
+ * - `Error` (default)
168
+ * - `TypeError`
169
+ * - `RangeError`
170
+ * - Custom Error subclasses
171
+ * - `null` to skip throwing and log the failure.
172
+ *
173
+ * @param [eProps = {}]
133
174
  * Additional properties assigned to the error object.
134
175
  *
135
- * @returns {boolean}
176
+ * @returns
136
177
  * Returns the original condition value.
137
178
  *
138
179
  * @example
@@ -141,9 +182,7 @@ declare class Contracts {
141
182
  * 'Intermediate value must not be null'
142
183
  * );
143
184
  */
144
- static VERIFY_DEBUG(isOk: boolean, ngMsg?: string | null, ErrorClass?: (new (...args: any[]) => Error) | null, eProps?: {
145
- [x: string]: any;
146
- }): boolean;
185
+ static VERIFY_DEBUG(isOk: boolean, ngMsg: string | null, ErrorClass?: (new (...args: any[]) => Error) | null, eProps?: {}): boolean;
147
186
  /**
148
187
  * Checks a precondition before execution.
149
188
  *
@@ -157,13 +196,13 @@ declare class Contracts {
157
196
  * - Validate required object state.
158
197
  * - Check required external conditions.
159
198
  *
160
- * @param {boolean} isOk
199
+ * @param isOk
161
200
  * Condition result to verify.
162
201
  *
163
- * @param {string|null} [ngMsg]
202
+ * @param [ngMsg]
164
203
  * Failure message.
165
204
  *
166
- * @param {(new (...args:any[])=>Error)|null} [ErrorClass=Error]
205
+ * @param [ErrorClass=Error]
167
206
  * Error constructor used when the check fails.
168
207
  *
169
208
  * Supported values:
@@ -173,10 +212,10 @@ declare class Contracts {
173
212
  * - Custom Error subclasses
174
213
  * - `null` to skip throwing and log the failure.
175
214
  *
176
- * @param {Object<string, *>} [eProps]
215
+ * @param [eProps = {}]
177
216
  * Additional properties assigned to the error object.
178
217
  *
179
- * @returns {boolean}
218
+ * @returns
180
219
  * Returns the original condition value.
181
220
  *
182
221
  * @example
@@ -194,9 +233,7 @@ declare class Contracts {
194
233
  * return a / b;
195
234
  * }
196
235
  */
197
- static REQUIRE(isOk: boolean, ngMsg?: string | null, ErrorClass?: (new (...args: any[]) => Error) | null, eProps?: {
198
- [x: string]: any;
199
- }): boolean;
236
+ static REQUIRE(isOk: boolean, ngMsg: string | null, ErrorClass?: (new (...args: any[]) => Error) | null, eProps?: {}): boolean;
200
237
  /**
201
238
  * Checks a precondition in debug mode only.
202
239
  *
@@ -210,19 +247,26 @@ declare class Contracts {
210
247
  * - Validate assumptions during development.
211
248
  * - Perform additional argument checks while debugging.
212
249
  *
213
- * @param {boolean} isOk
250
+ * @param isOk
214
251
  * Condition result to verify.
215
252
  *
216
- * @param {string|null} [ngMsg]
253
+ * @param [ngMsg]
217
254
  * Failure message.
218
255
  *
219
- * @param {(new (...args:any[])=>Error)|null} [ErrorClass=Error]
256
+ * @param [ErrorClass=Error]
220
257
  * Error constructor used when the check fails.
221
258
  *
222
- * @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 = {}]
223
267
  * Additional properties assigned to the error object.
224
268
  *
225
- * @returns {boolean}
269
+ * @returns
226
270
  * Returns the original condition value.
227
271
  *
228
272
  * @example
@@ -231,9 +275,7 @@ declare class Contracts {
231
275
  * 'User must exist during debugging'
232
276
  * );
233
277
  */
234
- static REQUIRE_DEBUG(isOk: boolean, ngMsg?: string | null, ErrorClass?: (new (...args: any[]) => Error) | null, eProps?: {
235
- [x: string]: any;
236
- }): boolean;
278
+ static REQUIRE_DEBUG(isOk: boolean, ngMsg: string | null, ErrorClass?: (new (...args: any[]) => Error) | null, eProps?: {}): boolean;
237
279
  /**
238
280
  * Checks a postcondition after execution.
239
281
  *
@@ -248,13 +290,13 @@ declare class Contracts {
248
290
  * - Confirm state changes.
249
291
  * - Verify that processing completed correctly.
250
292
  *
251
- * @param {boolean} isOk
293
+ * @param isOk
252
294
  * Condition result to verify.
253
295
  *
254
- * @param {string|null} [ngMsg]
296
+ * @param [ngMsg]
255
297
  * Failure message.
256
298
  *
257
- * @param {(new (...args:any[])=>Error)|null} [ErrorClass=Error]
299
+ * @param [ErrorClass=Error]
258
300
  * Error constructor used when the check fails.
259
301
  *
260
302
  * Supported values:
@@ -264,10 +306,10 @@ declare class Contracts {
264
306
  * - Custom Error subclasses
265
307
  * - `null` to skip throwing and log the failure.
266
308
  *
267
- * @param {Object<string, *>} [eProps]
309
+ * @param [eProps = {}]
268
310
  * Additional properties assigned to the error object.
269
311
  *
270
- * @returns {boolean}
312
+ * @returns
271
313
  * Returns the original condition value.
272
314
  *
273
315
  * @example
@@ -287,9 +329,7 @@ declare class Contracts {
287
329
  * return result;
288
330
  * }
289
331
  */
290
- static ENSURE(isOk: boolean, ngMsg?: string | null, ErrorClass?: (new (...args: any[]) => Error) | null, eProps?: {
291
- [x: string]: any;
292
- }): boolean;
332
+ static ENSURE(isOk: boolean, ngMsg: string | null, ErrorClass?: (new (...args: any[]) => Error) | null, eProps?: {}): boolean;
293
333
  /**
294
334
  * Checks a postcondition in debug mode only.
295
335
  *
@@ -303,19 +343,26 @@ declare class Contracts {
303
343
  * - Validate detailed results during development.
304
344
  * - Confirm internal behavior while debugging.
305
345
  *
306
- * @param {boolean} isOk
346
+ * @param isOk
307
347
  * Condition result to verify.
308
348
  *
309
- * @param {string|null} [ngMsg]
349
+ * @param [ngMsg]
310
350
  * Failure message.
311
351
  *
312
- * @param {(new (...args:any[])=>Error)|null} [ErrorClass=Error]
352
+ * @param [ErrorClass=Error]
313
353
  * Error constructor used when the check fails.
314
354
  *
315
- * @param {Object<string, *>} [eProps]
355
+ * Supported values:
356
+ * - `Error` (default)
357
+ * - `TypeError`
358
+ * - `RangeError`
359
+ * - Custom Error subclasses
360
+ * - `null` to skip throwing and log the failure.
361
+ *
362
+ * @param [eProps = {}]
316
363
  * Additional properties assigned to the error object.
317
364
  *
318
- * @returns {boolean}
365
+ * @returns
319
366
  * Returns the original condition value.
320
367
  *
321
368
  * @example
@@ -324,9 +371,7 @@ declare class Contracts {
324
371
  * 'Result should exist during debugging'
325
372
  * );
326
373
  */
327
- static ENSURE_DEBUG(isOk: boolean, ngMsg?: string | null, ErrorClass?: (new (...args: any[]) => Error) | null, eProps?: {
328
- [x: string]: any;
329
- }): boolean;
374
+ static ENSURE_DEBUG(isOk: boolean, ngMsg: string | null, ErrorClass?: (new (...args: any[]) => Error) | null, eProps?: {}): boolean;
330
375
  /**
331
376
  * Checks an invariant condition.
332
377
  *
@@ -341,13 +386,13 @@ declare class Contracts {
341
386
  * In Design by Contract terminology,
342
387
  * INVARIANT represents conditions that must always remain true.
343
388
  *
344
- * @param {boolean} isOk
389
+ * @param isOk
345
390
  * Condition result to verify.
346
391
  *
347
- * @param {string|null} [ngMsg]
392
+ * @param [ngMsg]
348
393
  * Failure message.
349
394
  *
350
- * @param {(new (...args:any[])=>Error)|null} [ErrorClass=Error]
395
+ * @param [ErrorClass=Error]
351
396
  * Error constructor used when the check fails.
352
397
  *
353
398
  * Supported values:
@@ -357,10 +402,10 @@ declare class Contracts {
357
402
  * - Custom Error subclasses
358
403
  * - `null` to skip throwing and log the failure.
359
404
  *
360
- * @param {Object<string, *>} [eProps]
405
+ * @param [eProps = {}]
361
406
  * Additional properties assigned to the error object.
362
407
  *
363
- * @returns {boolean}
408
+ * @returns
364
409
  * Returns the original condition value.
365
410
  *
366
411
  * @example
@@ -377,9 +422,7 @@ declare class Contracts {
377
422
  *
378
423
  * }
379
424
  */
380
- static INVARIANT(isOk: boolean, ngMsg?: string | null, ErrorClass?: (new (...args: any[]) => Error) | null, eProps?: {
381
- [x: string]: any;
382
- }): boolean;
425
+ static INVARIANT(isOk: boolean, ngMsg: string | null, ErrorClass?: (new (...args: any[]) => Error) | null, eProps?: {}): boolean;
383
426
  /**
384
427
  * Checks an invariant condition in debug mode only.
385
428
  *
@@ -393,19 +436,26 @@ declare class Contracts {
393
436
  * - Validate object consistency during development.
394
437
  * - Detect unexpected state changes while debugging.
395
438
  *
396
- * @param {boolean} isOk
439
+ * @param isOk
397
440
  * Condition result to verify.
398
441
  *
399
- * @param {string|null} [ngMsg]
442
+ * @param [ngMsg]
400
443
  * Failure message.
401
444
  *
402
- * @param {(new (...args:any[])=>Error)|null} [ErrorClass=Error]
445
+ * @param [ErrorClass=Error]
403
446
  * Error constructor used when the check fails.
404
447
  *
405
- * @param {Object<string, *>} [eProps]
448
+ * Supported values:
449
+ * - `Error` (default)
450
+ * - `TypeError`
451
+ * - `RangeError`
452
+ * - Custom Error subclasses
453
+ * - `null` to skip throwing and log the failure.
454
+ *
455
+ * @param [eProps = {}]
406
456
  * Additional properties assigned to the error object.
407
457
  *
408
- * @returns {boolean}
458
+ * @returns
409
459
  * Returns the original condition value.
410
460
  *
411
461
  * @example
@@ -414,9 +464,7 @@ declare class Contracts {
414
464
  * 'Cache size exceeded expected limit'
415
465
  * );
416
466
  */
417
- static INVARIANT_DEBUG(isOk: boolean, ngMsg?: string | null, ErrorClass?: (new (...args: any[]) => Error) | null, eProps?: {
418
- [x: string]: any;
419
- }): boolean;
467
+ static INVARIANT_DEBUG(isOk: boolean, ngMsg: string | null, ErrorClass?: (new (...args: any[]) => Error) | null, eProps?: {}): boolean;
420
468
  /**
421
469
  * Core contract evaluation logic.
422
470
  *
@@ -430,28 +478,26 @@ declare class Contracts {
430
478
  * - When ErrorClass is null,
431
479
  * logs the failure message instead of throwing.
432
480
  *
433
- * @param {boolean} isOk
481
+ * @param isOk
434
482
  * Condition result.
435
483
  *
436
- * @param {string} prefix
484
+ * @param prefix
437
485
  * Contract type prefix used in the error message.
438
486
  *
439
- * @param {string|null} ngMsg
487
+ * @param ngMsg
440
488
  * Failure message.
441
489
  *
442
- * @param {(new (...args:any[])=>Error)|null} ErrorClass
490
+ * @param ErrorClass
443
491
  * Error constructor.
444
492
  *
445
- * @param {Object<string, *>} eProps
493
+ * @param eProps
446
494
  * Additional properties assigned to the error object.
447
495
  *
448
- * @returns {boolean}
496
+ * @returns
449
497
  * Returns the original condition value.
450
498
  *
451
499
  */
452
- static "__#1@#check"(isOk: boolean, prefix?: string, ngMsg?: string | null, ErrorClass?: (new (...args: any[]) => Error) | null, eProps?: {
453
- [x: string]: any;
454
- }): boolean;
500
+ private static check;
455
501
  /**
456
502
  * Debug-only contract evaluation logic.
457
503
  *
@@ -462,28 +508,28 @@ declare class Contracts {
462
508
  * this method returns the original condition value
463
509
  * without performing any validation.
464
510
  *
465
- * @param {boolean} isOk
511
+ * @param isOk
466
512
  * Condition result.
467
513
  *
468
- * @param {string} prefix
514
+ * @param prefix
469
515
  * Contract type prefix used in the error message.
470
516
  *
471
- * @param {string|null} ngMsg
517
+ * @param ngMsg
472
518
  * Failure message.
473
519
  *
474
- * @param {(new (...args:any[])=>Error)|null} ErrorClass
520
+ * @param ErrorClass
475
521
  * Error constructor.
476
522
  *
477
- * @param {Object<string, *>} eProps
523
+ * @param eProps
478
524
  * Additional properties assigned to the error object.
479
525
  *
480
- * @returns {boolean}
526
+ * @returns
481
527
  * Returns the original condition value.
482
528
  *
483
529
  */
484
- static "__#1@#checkDebug"(isOk: boolean, prefix?: string, ngMsg?: string | null, ErrorClass?: (new (...args: any[]) => Error) | null, eProps?: {
485
- [x: string]: any;
486
- }): boolean;
530
+ static checkDebug(isOk: boolean, prefix?: string, ngMsg?: string | null, ErrorClass?: (new (...args: any[]) => Error) | null, eProps?: {}): boolean;
531
+ /** Get logger */
532
+ private static getLogger;
487
533
  }
488
534
 
489
- export { Contracts, Contracts as default };
535
+ export { type Config, Contracts, type LogProvider, Contracts as default };