@ayapapa-npm/contracts-js 0.2.3 → 0.2.5

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