@ayapapa-npm/contracts-js 0.2.4 → 0.3.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/dist/index.d.cts CHANGED
@@ -11,14 +11,16 @@ interface Config {
11
11
  * If `true`, `DEBUG_MODE` is enabled; otherwise, it is disabled.
12
12
  * The default is `false`.
13
13
  */
14
- debug: boolean;
14
+ debug?: boolean;
15
15
  /**
16
16
  * External logger.
17
17
  * If specified, it is used instead of the standard logger, `console`.
18
18
  * This module uses only the `error` method.
19
19
  */
20
- logger: LogProvider;
20
+ logger?: LogProvider;
21
21
  }
22
+ /** Type of `Config`'s key. */
23
+ type ConfigKey = keyof Config;
22
24
  /**
23
25
  * A lightweight Design by Contract library for JavaScript.
24
26
  *
@@ -82,7 +84,7 @@ declare class Contracts {
82
84
  *
83
85
  * Is the `logger` property is specified,
84
86
  * it is used instead of the standard logger, `console`.
85
- * This module uses only the `error` method.
87
+ * This module uses only the `error` method of the `logger`.
86
88
  *
87
89
  * @example
88
90
  * // Use a logger that is slightly more advanced than the standard logger—namely, `console`.
@@ -113,11 +115,12 @@ declare class Contracts {
113
115
  * @param isOk
114
116
  * Condition result to verify.
115
117
  *
116
- * @param [ngMsg]
118
+ * @param ngMsg
117
119
  * Failure message.
118
120
  *
119
- * @param [ErrorClass=Error]
121
+ * @param ErrorClass
120
122
  * Error constructor used when the check fails.
123
+ * This is used as follows: throw Object.assign(new ErrorClass(msg, eParams), eProps);
121
124
  *
122
125
  * Supported values:
123
126
  * - `Error` (default)
@@ -126,7 +129,10 @@ declare class Contracts {
126
129
  * - Custom Error subclasses
127
130
  * - `null` to skip throwing and log the failure.
128
131
  *
129
- * @param [eProps = {}]
132
+ * @param eParams
133
+ * Parameter options following the message passed to the Error constructor.
134
+ *
135
+ * @param eProps
130
136
  * Additional properties assigned to the error object.
131
137
  *
132
138
  * @returns
@@ -140,7 +146,11 @@ declare class Contracts {
140
146
  * 'Calculation result must not be negative'
141
147
  * );
142
148
  */
143
- static VERIFY(isOk: boolean, ngMsg: string | null, ErrorClass?: (new (...args: any[]) => Error) | null, eProps?: {}): boolean;
149
+ static VERIFY(isOk: boolean, ngMsg: string | null, ErrorClass?: (new (...args: any[]) => Error) | null, eParams?: {
150
+ [key: string]: any;
151
+ } | null, eProps?: {
152
+ [key: string]: any;
153
+ } | null): boolean;
144
154
  /**
145
155
  * Verifies an intermediate condition in debug mode only.
146
156
  *
@@ -157,11 +167,12 @@ declare class Contracts {
157
167
  * @param isOk
158
168
  * Condition result to verify.
159
169
  *
160
- * @param [ngMsg]
170
+ * @param ngMsg
161
171
  * Failure message.
162
172
  *
163
- * @param [ErrorClass=Error]
173
+ * @param ErrorClass
164
174
  * Error constructor used when the check fails.
175
+ * This is used as follows: throw Object.assign(new ErrorClass(msg, eParams), eProps);
165
176
  *
166
177
  * Supported values:
167
178
  * - `Error` (default)
@@ -170,7 +181,10 @@ declare class Contracts {
170
181
  * - Custom Error subclasses
171
182
  * - `null` to skip throwing and log the failure.
172
183
  *
173
- * @param [eProps = {}]
184
+ * @param eParams
185
+ * Parameter options following the message passed to the Error constructor.
186
+ *
187
+ * @param eProps
174
188
  * Additional properties assigned to the error object.
175
189
  *
176
190
  * @returns
@@ -182,7 +196,11 @@ declare class Contracts {
182
196
  * 'Intermediate value must not be null'
183
197
  * );
184
198
  */
185
- static VERIFY_DEBUG(isOk: boolean, ngMsg: string | null, ErrorClass?: (new (...args: any[]) => Error) | null, eProps?: {}): boolean;
199
+ static VERIFY_DEBUG(isOk: boolean, ngMsg: string | null, ErrorClass?: (new (...args: any[]) => Error) | null, eParams?: {
200
+ [key: string]: any;
201
+ } | null, eProps?: {
202
+ [key: string]: any;
203
+ } | null): boolean;
186
204
  /**
187
205
  * Checks a precondition before execution.
188
206
  *
@@ -199,11 +217,12 @@ declare class Contracts {
199
217
  * @param isOk
200
218
  * Condition result to verify.
201
219
  *
202
- * @param [ngMsg]
220
+ * @param ngMsg
203
221
  * Failure message.
204
222
  *
205
- * @param [ErrorClass=Error]
223
+ * @param ErrorClass
206
224
  * Error constructor used when the check fails.
225
+ * This is used as follows: throw Object.assign(new ErrorClass(msg, eParams), eProps);
207
226
  *
208
227
  * Supported values:
209
228
  * - `Error` (default)
@@ -212,7 +231,10 @@ declare class Contracts {
212
231
  * - Custom Error subclasses
213
232
  * - `null` to skip throwing and log the failure.
214
233
  *
215
- * @param [eProps = {}]
234
+ * @param eParams
235
+ * Parameter options following the message passed to the Error constructor.
236
+ *
237
+ * @param eProps
216
238
  * Additional properties assigned to the error object.
217
239
  *
218
240
  * @returns
@@ -233,7 +255,11 @@ declare class Contracts {
233
255
  * return a / b;
234
256
  * }
235
257
  */
236
- static REQUIRE(isOk: boolean, ngMsg: string | null, ErrorClass?: (new (...args: any[]) => Error) | null, eProps?: {}): boolean;
258
+ static REQUIRE(isOk: boolean, ngMsg: string | null, ErrorClass?: (new (...args: any[]) => Error) | null, eParams?: {
259
+ [key: string]: any;
260
+ } | null, eProps?: {
261
+ [key: string]: any;
262
+ } | null): boolean;
237
263
  /**
238
264
  * Checks a precondition in debug mode only.
239
265
  *
@@ -250,11 +276,12 @@ declare class Contracts {
250
276
  * @param isOk
251
277
  * Condition result to verify.
252
278
  *
253
- * @param [ngMsg]
279
+ * @param ngMsg
254
280
  * Failure message.
255
281
  *
256
- * @param [ErrorClass=Error]
282
+ * @param ErrorClass
257
283
  * Error constructor used when the check fails.
284
+ * This is used as follows: throw Object.assign(new ErrorClass(msg, eParams), eProps);
258
285
  *
259
286
  * Supported values:
260
287
  * - `Error` (default)
@@ -263,7 +290,10 @@ declare class Contracts {
263
290
  * - Custom Error subclasses
264
291
  * - `null` to skip throwing and log the failure.
265
292
  *
266
- * @param [eProps = {}]
293
+ * @param eParams
294
+ * Parameter options following the message passed to the Error constructor.
295
+ *
296
+ * @param eProps
267
297
  * Additional properties assigned to the error object.
268
298
  *
269
299
  * @returns
@@ -275,7 +305,11 @@ declare class Contracts {
275
305
  * 'User must exist during debugging'
276
306
  * );
277
307
  */
278
- static REQUIRE_DEBUG(isOk: boolean, ngMsg: string | null, ErrorClass?: (new (...args: any[]) => Error) | null, eProps?: {}): boolean;
308
+ static REQUIRE_DEBUG(isOk: boolean, ngMsg: string | null, ErrorClass?: (new (...args: any[]) => Error) | null, eParams?: {
309
+ [key: string]: any;
310
+ } | null, eProps?: {
311
+ [key: string]: any;
312
+ } | null): boolean;
279
313
  /**
280
314
  * Checks a postcondition after execution.
281
315
  *
@@ -293,11 +327,12 @@ declare class Contracts {
293
327
  * @param isOk
294
328
  * Condition result to verify.
295
329
  *
296
- * @param [ngMsg]
330
+ * @param ngMsg
297
331
  * Failure message.
298
332
  *
299
- * @param [ErrorClass=Error]
333
+ * @param ErrorClass
300
334
  * Error constructor used when the check fails.
335
+ * This is used as follows: throw Object.assign(new ErrorClass(msg, eParams), eProps);
301
336
  *
302
337
  * Supported values:
303
338
  * - `Error` (default)
@@ -306,7 +341,10 @@ declare class Contracts {
306
341
  * - Custom Error subclasses
307
342
  * - `null` to skip throwing and log the failure.
308
343
  *
309
- * @param [eProps = {}]
344
+ * @param eParams
345
+ * Parameter options following the message passed to the Error constructor.
346
+ *
347
+ * @param eProps
310
348
  * Additional properties assigned to the error object.
311
349
  *
312
350
  * @returns
@@ -329,7 +367,11 @@ declare class Contracts {
329
367
  * return result;
330
368
  * }
331
369
  */
332
- static ENSURE(isOk: boolean, ngMsg: string | null, ErrorClass?: (new (...args: any[]) => Error) | null, eProps?: {}): boolean;
370
+ static ENSURE(isOk: boolean, ngMsg: string | null, ErrorClass?: (new (...args: any[]) => Error) | null, eParams?: {
371
+ [key: string]: any;
372
+ } | null, eProps?: {
373
+ [key: string]: any;
374
+ } | null): boolean;
333
375
  /**
334
376
  * Checks a postcondition in debug mode only.
335
377
  *
@@ -346,11 +388,12 @@ declare class Contracts {
346
388
  * @param isOk
347
389
  * Condition result to verify.
348
390
  *
349
- * @param [ngMsg]
391
+ * @param ngMsg
350
392
  * Failure message.
351
393
  *
352
- * @param [ErrorClass=Error]
394
+ * @param ErrorClass
353
395
  * Error constructor used when the check fails.
396
+ * This is used as follows: throw Object.assign(new ErrorClass(msg, eParams), eProps);
354
397
  *
355
398
  * Supported values:
356
399
  * - `Error` (default)
@@ -359,7 +402,10 @@ declare class Contracts {
359
402
  * - Custom Error subclasses
360
403
  * - `null` to skip throwing and log the failure.
361
404
  *
362
- * @param [eProps = {}]
405
+ * @param eParams
406
+ * Parameter options following the message passed to the Error constructor.
407
+ *
408
+ * @param eProps
363
409
  * Additional properties assigned to the error object.
364
410
  *
365
411
  * @returns
@@ -371,7 +417,11 @@ declare class Contracts {
371
417
  * 'Result should exist during debugging'
372
418
  * );
373
419
  */
374
- static ENSURE_DEBUG(isOk: boolean, ngMsg: string | null, ErrorClass?: (new (...args: any[]) => Error) | null, eProps?: {}): boolean;
420
+ static ENSURE_DEBUG(isOk: boolean, ngMsg: string | null, ErrorClass?: (new (...args: any[]) => Error) | null, eParams?: {
421
+ [key: string]: any;
422
+ } | null, eProps?: {
423
+ [key: string]: any;
424
+ } | null): boolean;
375
425
  /**
376
426
  * Checks an invariant condition.
377
427
  *
@@ -389,11 +439,12 @@ declare class Contracts {
389
439
  * @param isOk
390
440
  * Condition result to verify.
391
441
  *
392
- * @param [ngMsg]
442
+ * @param ngMsg
393
443
  * Failure message.
394
444
  *
395
- * @param [ErrorClass=Error]
445
+ * @param ErrorClass
396
446
  * Error constructor used when the check fails.
447
+ * This is used as follows: throw Object.assign(new ErrorClass(msg, eParams), eProps);
397
448
  *
398
449
  * Supported values:
399
450
  * - `Error` (default)
@@ -402,7 +453,10 @@ declare class Contracts {
402
453
  * - Custom Error subclasses
403
454
  * - `null` to skip throwing and log the failure.
404
455
  *
405
- * @param [eProps = {}]
456
+ * @param eParams
457
+ * Parameter options following the message passed to the Error constructor.
458
+ *
459
+ * @param eProps
406
460
  * Additional properties assigned to the error object.
407
461
  *
408
462
  * @returns
@@ -422,7 +476,11 @@ declare class Contracts {
422
476
  *
423
477
  * }
424
478
  */
425
- static INVARIANT(isOk: boolean, ngMsg: string | null, ErrorClass?: (new (...args: any[]) => Error) | null, eProps?: {}): boolean;
479
+ static INVARIANT(isOk: boolean, ngMsg: string | null, ErrorClass?: (new (...args: any[]) => Error) | null, eParams?: {
480
+ [key: string]: any;
481
+ } | null, eProps?: {
482
+ [key: string]: any;
483
+ } | null): boolean;
426
484
  /**
427
485
  * Checks an invariant condition in debug mode only.
428
486
  *
@@ -444,6 +502,7 @@ declare class Contracts {
444
502
  *
445
503
  * @param [ErrorClass=Error]
446
504
  * Error constructor used when the check fails.
505
+ * This is used as follows: throw Object.assign(new ErrorClass(msg, eParams), eProps);
447
506
  *
448
507
  * Supported values:
449
508
  * - `Error` (default)
@@ -452,6 +511,9 @@ declare class Contracts {
452
511
  * - Custom Error subclasses
453
512
  * - `null` to skip throwing and log the failure.
454
513
  *
514
+ * @param eParams
515
+ * Parameter options following the message passed to the Error constructor.
516
+ *
455
517
  * @param [eProps = {}]
456
518
  * Additional properties assigned to the error object.
457
519
  *
@@ -464,7 +526,11 @@ declare class Contracts {
464
526
  * 'Cache size exceeded expected limit'
465
527
  * );
466
528
  */
467
- static INVARIANT_DEBUG(isOk: boolean, ngMsg: string | null, ErrorClass?: (new (...args: any[]) => Error) | null, eProps?: {}): boolean;
529
+ static INVARIANT_DEBUG(isOk: boolean, ngMsg: string | null, ErrorClass?: (new (...args: any[]) => Error) | null, eParams?: {
530
+ [key: string]: any;
531
+ } | null, eProps?: {
532
+ [key: string]: any;
533
+ } | null): boolean;
468
534
  /**
469
535
  * Core contract evaluation logic.
470
536
  *
@@ -478,6 +544,9 @@ declare class Contracts {
478
544
  * - When ErrorClass is null,
479
545
  * logs the failure message instead of throwing.
480
546
  *
547
+ *
548
+ * @internal
549
+ *
481
550
  * @param isOk
482
551
  * Condition result.
483
552
  *
@@ -488,7 +557,18 @@ declare class Contracts {
488
557
  * Failure message.
489
558
  *
490
559
  * @param ErrorClass
491
- * Error constructor.
560
+ * Error constructor used when the check fails.
561
+ * This is used as follows: throw Object.assign(new ErrorClass(msg, eParams), eProps);
562
+ *
563
+ * Supported values:
564
+ * - `Error` (default)
565
+ * - `TypeError`
566
+ * - `RangeError`
567
+ * - Custom Error subclasses
568
+ * - `null` to skip throwing and log the failure.
569
+ *
570
+ * @param eParams
571
+ * Parameter options following the message passed to the Error constructor.
492
572
  *
493
573
  * @param eProps
494
574
  * Additional properties assigned to the error object.
@@ -508,6 +588,8 @@ declare class Contracts {
508
588
  * this method returns the original condition value
509
589
  * without performing any validation.
510
590
  *
591
+ * @internal
592
+ *
511
593
  * @param isOk
512
594
  * Condition result.
513
595
  *
@@ -517,8 +599,19 @@ declare class Contracts {
517
599
  * @param ngMsg
518
600
  * Failure message.
519
601
  *
520
- * @param ErrorClass
521
- * Error constructor.
602
+ * @param ErrorClass=Error
603
+ * Error constructor used when the check fails.
604
+ * This is used as follows: throw Object.assign(new ErrorClass(msg, eParams), eProps);
605
+ *
606
+ * Supported values:
607
+ * - `Error` (default)
608
+ * - `TypeError`
609
+ * - `RangeError`
610
+ * - Custom Error subclasses
611
+ * - `null` to skip throwing and log the failure.
612
+ *
613
+ * @param eParams
614
+ * Parameter options following the message passed to the Error constructor.
522
615
  *
523
616
  * @param eProps
524
617
  * Additional properties assigned to the error object.
@@ -527,9 +620,13 @@ declare class Contracts {
527
620
  * Returns the original condition value.
528
621
  *
529
622
  */
530
- static checkDebug(isOk: boolean, prefix?: string, ngMsg?: string | null, ErrorClass?: (new (...args: any[]) => Error) | null, eProps?: {}): boolean;
531
- /** Get logger */
623
+ private static checkDebug;
624
+ /**
625
+ * Get logger
626
+ *
627
+ * @internal
628
+ */
532
629
  private static getLogger;
533
630
  }
534
631
 
535
- export { type Config, Contracts, type LogProvider, Contracts as default };
632
+ export { type Config, type ConfigKey, Contracts, type LogProvider, Contracts as default };