@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 +9 -0
- package/dist/index.cjs +170 -65
- package/dist/index.d.cts +163 -57
- package/dist/index.d.ts +163 -57
- package/dist/index.js +169 -65
- package/package.json +43 -38
package/dist/index.js
CHANGED
|
@@ -6,30 +6,39 @@ var Contracts = class _Contracts {
|
|
|
6
6
|
/** Debug mode state */
|
|
7
7
|
static DEBUG_MODE = false;
|
|
8
8
|
/** default configuration */
|
|
9
|
-
static defaultConf = {
|
|
9
|
+
static #defaultConf = {
|
|
10
10
|
debug: false,
|
|
11
11
|
logger: console
|
|
12
12
|
};
|
|
13
|
+
/** Current config. */
|
|
14
|
+
static #config = { ..._Contracts.#defaultConf };
|
|
13
15
|
/** logger */
|
|
14
|
-
static logger = console;
|
|
16
|
+
//private static logger: LogProvider = console;
|
|
15
17
|
/**
|
|
16
18
|
* Configures contract checking behavior.
|
|
17
19
|
*
|
|
18
20
|
* @param config
|
|
19
|
-
* Configuration options.
|
|
20
|
-
*
|
|
21
|
-
* The `debug` property
|
|
22
|
-
*
|
|
23
|
-
*
|
|
21
|
+
* Configuration options. <br>
|
|
22
|
+
* <br>
|
|
23
|
+
* The `debug` property toggles the behavior—specifically,
|
|
24
|
+
* throwing an exception or outputting to the console when the condition is
|
|
25
|
+
* false—for the validation of contracts intended for use during debugging (methods ending in `_DEBUG`). <br>
|
|
26
|
+
* <br>
|
|
24
27
|
* When `debug` is `true`,
|
|
25
|
-
* methods ending with `_DEBUG` perform validation.
|
|
26
|
-
*
|
|
27
|
-
* When `debug` is `false
|
|
28
|
-
* methods ending with `_DEBUG` skip validation.
|
|
29
|
-
*
|
|
28
|
+
* methods ending with `_DEBUG` perform validation. <br>
|
|
29
|
+
* <br>
|
|
30
|
+
* When `debug` is `false`,
|
|
31
|
+
* methods ending with `_DEBUG` skip validation. <br>
|
|
32
|
+
* <br>
|
|
30
33
|
* Is the `logger` property is specified,
|
|
31
34
|
* it is used instead of the standard logger, `console`.
|
|
32
|
-
* This module uses only the `error` method of the `logger`.
|
|
35
|
+
* This module uses only the `error` method of the `logger`. <br>
|
|
36
|
+
* <br>
|
|
37
|
+
* Note: If the value of a property is `undefined`, it is treated as unspecified.
|
|
38
|
+
*
|
|
39
|
+
* @param reset
|
|
40
|
+
* If `true`, unspecified values are saved to the settings as default values. <br>
|
|
41
|
+
* If `false`, unspecified values remain at their current settings.
|
|
33
42
|
*
|
|
34
43
|
* @example
|
|
35
44
|
* // Use a logger that is slightly more advanced than the standard logger—namely, `console`.
|
|
@@ -39,9 +48,36 @@ var Contracts = class _Contracts {
|
|
|
39
48
|
* Contracts.setConfig({ debug: true, logger: prettyConsole });
|
|
40
49
|
* // Node: ` The `logger` property is optional.
|
|
41
50
|
*/
|
|
42
|
-
static setConfig(config) {
|
|
43
|
-
|
|
44
|
-
|
|
51
|
+
static setConfig(config, reset = true) {
|
|
52
|
+
const rConf = { ...config };
|
|
53
|
+
for (let key of Object.keys(rConf)) {
|
|
54
|
+
if (rConf[key] == null) delete rConf[key];
|
|
55
|
+
}
|
|
56
|
+
if (reset) _Contracts.#config = { ..._Contracts.getDefaultConfig() };
|
|
57
|
+
Object.assign(_Contracts.#config, rConf);
|
|
58
|
+
_Contracts.DEBUG_MODE = _Contracts.#config.debug;
|
|
59
|
+
}
|
|
60
|
+
/**
|
|
61
|
+
* Get the default configurations.
|
|
62
|
+
* @returns Default configurations.
|
|
63
|
+
*/
|
|
64
|
+
static getDefaultConfig() {
|
|
65
|
+
return { ..._Contracts.#defaultConf };
|
|
66
|
+
}
|
|
67
|
+
/**
|
|
68
|
+
* Get the current configurations.
|
|
69
|
+
* @returns Default configurations.
|
|
70
|
+
*/
|
|
71
|
+
static getConfig() {
|
|
72
|
+
_Contracts.#config.debug = _Contracts.DEBUG_MODE;
|
|
73
|
+
return { ..._Contracts.#config };
|
|
74
|
+
}
|
|
75
|
+
/**
|
|
76
|
+
* Reset the current configurations to the default configurations.
|
|
77
|
+
* @returns Default configurations.
|
|
78
|
+
*/
|
|
79
|
+
static resetConfig() {
|
|
80
|
+
_Contracts.setConfig({}, true);
|
|
45
81
|
}
|
|
46
82
|
/**
|
|
47
83
|
* Verifies an intermediate condition during execution.
|
|
@@ -61,13 +97,14 @@ var Contracts = class _Contracts {
|
|
|
61
97
|
* - Check temporary assumptions during execution.
|
|
62
98
|
*
|
|
63
99
|
* @param isOk
|
|
64
|
-
* Condition result
|
|
100
|
+
* Condition result(`boolean`) to be verified.
|
|
65
101
|
*
|
|
66
|
-
* @param
|
|
102
|
+
* @param ngMsg
|
|
67
103
|
* Failure message.
|
|
68
104
|
*
|
|
69
|
-
* @param
|
|
105
|
+
* @param ErrorClass
|
|
70
106
|
* Error constructor used when the check fails.
|
|
107
|
+
* This is used as follows: throw Object.assign(new ErrorClass(msg, eParams), eProps);
|
|
71
108
|
*
|
|
72
109
|
* Supported values:
|
|
73
110
|
* - `Error` (default)
|
|
@@ -76,7 +113,10 @@ var Contracts = class _Contracts {
|
|
|
76
113
|
* - Custom Error subclasses
|
|
77
114
|
* - `null` to skip throwing and log the failure.
|
|
78
115
|
*
|
|
79
|
-
* @param
|
|
116
|
+
* @param eParams
|
|
117
|
+
* Parameter options following the message passed to the Error constructor.
|
|
118
|
+
*
|
|
119
|
+
* @param eProps
|
|
80
120
|
* Additional properties assigned to the error object.
|
|
81
121
|
*
|
|
82
122
|
* @returns
|
|
@@ -90,12 +130,13 @@ var Contracts = class _Contracts {
|
|
|
90
130
|
* 'Calculation result must not be negative'
|
|
91
131
|
* );
|
|
92
132
|
*/
|
|
93
|
-
static VERIFY(isOk, ngMsg, ErrorClass = Error, eProps
|
|
133
|
+
static VERIFY(isOk, ngMsg, ErrorClass = Error, eParams, eProps) {
|
|
94
134
|
return _Contracts.check(
|
|
95
135
|
isOk,
|
|
96
136
|
"VERIFY",
|
|
97
137
|
ngMsg,
|
|
98
138
|
ErrorClass,
|
|
139
|
+
eParams,
|
|
99
140
|
eProps
|
|
100
141
|
);
|
|
101
142
|
}
|
|
@@ -113,13 +154,14 @@ var Contracts = class _Contracts {
|
|
|
113
154
|
* - Check internal assumptions while debugging.
|
|
114
155
|
*
|
|
115
156
|
* @param isOk
|
|
116
|
-
* Condition result
|
|
157
|
+
* Condition result(`boolean`) to be verified.
|
|
117
158
|
*
|
|
118
|
-
* @param
|
|
159
|
+
* @param ngMsg
|
|
119
160
|
* Failure message.
|
|
120
161
|
*
|
|
121
|
-
* @param
|
|
162
|
+
* @param ErrorClass
|
|
122
163
|
* Error constructor used when the check fails.
|
|
164
|
+
* This is used as follows: throw Object.assign(new ErrorClass(msg, eParams), eProps);
|
|
123
165
|
*
|
|
124
166
|
* Supported values:
|
|
125
167
|
* - `Error` (default)
|
|
@@ -128,7 +170,10 @@ var Contracts = class _Contracts {
|
|
|
128
170
|
* - Custom Error subclasses
|
|
129
171
|
* - `null` to skip throwing and log the failure.
|
|
130
172
|
*
|
|
131
|
-
* @param
|
|
173
|
+
* @param eParams
|
|
174
|
+
* Parameter options following the message passed to the Error constructor.
|
|
175
|
+
*
|
|
176
|
+
* @param eProps
|
|
132
177
|
* Additional properties assigned to the error object.
|
|
133
178
|
*
|
|
134
179
|
* @returns
|
|
@@ -140,12 +185,13 @@ var Contracts = class _Contracts {
|
|
|
140
185
|
* 'Intermediate value must not be null'
|
|
141
186
|
* );
|
|
142
187
|
*/
|
|
143
|
-
static VERIFY_DEBUG(isOk, ngMsg, ErrorClass = Error, eProps
|
|
188
|
+
static VERIFY_DEBUG(isOk, ngMsg, ErrorClass = Error, eParams, eProps) {
|
|
144
189
|
return _Contracts.checkDebug(
|
|
145
190
|
isOk,
|
|
146
191
|
"VERIFY_DEBUG",
|
|
147
192
|
ngMsg,
|
|
148
193
|
ErrorClass,
|
|
194
|
+
eParams,
|
|
149
195
|
eProps
|
|
150
196
|
);
|
|
151
197
|
}
|
|
@@ -163,13 +209,14 @@ var Contracts = class _Contracts {
|
|
|
163
209
|
* - Check required external conditions.
|
|
164
210
|
*
|
|
165
211
|
* @param isOk
|
|
166
|
-
* Condition result
|
|
212
|
+
* Condition result(`boolean`) to be verified.
|
|
167
213
|
*
|
|
168
|
-
* @param
|
|
214
|
+
* @param ngMsg
|
|
169
215
|
* Failure message.
|
|
170
216
|
*
|
|
171
|
-
* @param
|
|
217
|
+
* @param ErrorClass
|
|
172
218
|
* Error constructor used when the check fails.
|
|
219
|
+
* This is used as follows: throw Object.assign(new ErrorClass(msg, eParams), eProps);
|
|
173
220
|
*
|
|
174
221
|
* Supported values:
|
|
175
222
|
* - `Error` (default)
|
|
@@ -178,7 +225,10 @@ var Contracts = class _Contracts {
|
|
|
178
225
|
* - Custom Error subclasses
|
|
179
226
|
* - `null` to skip throwing and log the failure.
|
|
180
227
|
*
|
|
181
|
-
* @param
|
|
228
|
+
* @param eParams
|
|
229
|
+
* Parameter options following the message passed to the Error constructor.
|
|
230
|
+
*
|
|
231
|
+
* @param eProps
|
|
182
232
|
* Additional properties assigned to the error object.
|
|
183
233
|
*
|
|
184
234
|
* @returns
|
|
@@ -199,12 +249,13 @@ var Contracts = class _Contracts {
|
|
|
199
249
|
* return a / b;
|
|
200
250
|
* }
|
|
201
251
|
*/
|
|
202
|
-
static REQUIRE(isOk, ngMsg, ErrorClass = Error, eProps
|
|
252
|
+
static REQUIRE(isOk, ngMsg, ErrorClass = Error, eParams, eProps) {
|
|
203
253
|
return _Contracts.check(
|
|
204
254
|
isOk,
|
|
205
255
|
"REQUIRE",
|
|
206
256
|
ngMsg,
|
|
207
257
|
ErrorClass,
|
|
258
|
+
eParams,
|
|
208
259
|
eProps
|
|
209
260
|
);
|
|
210
261
|
}
|
|
@@ -222,13 +273,14 @@ var Contracts = class _Contracts {
|
|
|
222
273
|
* - Perform additional argument checks while debugging.
|
|
223
274
|
*
|
|
224
275
|
* @param isOk
|
|
225
|
-
* Condition result
|
|
276
|
+
* Condition result(`boolean`) to be verified.
|
|
226
277
|
*
|
|
227
|
-
* @param
|
|
278
|
+
* @param ngMsg
|
|
228
279
|
* Failure message.
|
|
229
280
|
*
|
|
230
|
-
* @param
|
|
281
|
+
* @param ErrorClass
|
|
231
282
|
* Error constructor used when the check fails.
|
|
283
|
+
* This is used as follows: throw Object.assign(new ErrorClass(msg, eParams), eProps);
|
|
232
284
|
*
|
|
233
285
|
* Supported values:
|
|
234
286
|
* - `Error` (default)
|
|
@@ -237,7 +289,10 @@ var Contracts = class _Contracts {
|
|
|
237
289
|
* - Custom Error subclasses
|
|
238
290
|
* - `null` to skip throwing and log the failure.
|
|
239
291
|
*
|
|
240
|
-
* @param
|
|
292
|
+
* @param eParams
|
|
293
|
+
* Parameter options following the message passed to the Error constructor.
|
|
294
|
+
*
|
|
295
|
+
* @param eProps
|
|
241
296
|
* Additional properties assigned to the error object.
|
|
242
297
|
*
|
|
243
298
|
* @returns
|
|
@@ -249,12 +304,13 @@ var Contracts = class _Contracts {
|
|
|
249
304
|
* 'User must exist during debugging'
|
|
250
305
|
* );
|
|
251
306
|
*/
|
|
252
|
-
static REQUIRE_DEBUG(isOk, ngMsg, ErrorClass = Error, eProps
|
|
307
|
+
static REQUIRE_DEBUG(isOk, ngMsg, ErrorClass = Error, eParams, eProps) {
|
|
253
308
|
return _Contracts.checkDebug(
|
|
254
309
|
isOk,
|
|
255
310
|
"REQUIRE_DEBUG",
|
|
256
311
|
ngMsg,
|
|
257
312
|
ErrorClass,
|
|
313
|
+
eParams,
|
|
258
314
|
eProps
|
|
259
315
|
);
|
|
260
316
|
}
|
|
@@ -273,13 +329,14 @@ var Contracts = class _Contracts {
|
|
|
273
329
|
* - Verify that processing completed correctly.
|
|
274
330
|
*
|
|
275
331
|
* @param isOk
|
|
276
|
-
* Condition result
|
|
332
|
+
* Condition result(`boolean`) to be verified.
|
|
277
333
|
*
|
|
278
|
-
* @param
|
|
334
|
+
* @param ngMsg
|
|
279
335
|
* Failure message.
|
|
280
336
|
*
|
|
281
|
-
* @param
|
|
337
|
+
* @param ErrorClass
|
|
282
338
|
* Error constructor used when the check fails.
|
|
339
|
+
* This is used as follows: throw Object.assign(new ErrorClass(msg, eParams), eProps);
|
|
283
340
|
*
|
|
284
341
|
* Supported values:
|
|
285
342
|
* - `Error` (default)
|
|
@@ -288,7 +345,10 @@ var Contracts = class _Contracts {
|
|
|
288
345
|
* - Custom Error subclasses
|
|
289
346
|
* - `null` to skip throwing and log the failure.
|
|
290
347
|
*
|
|
291
|
-
* @param
|
|
348
|
+
* @param eParams
|
|
349
|
+
* Parameter options following the message passed to the Error constructor.
|
|
350
|
+
*
|
|
351
|
+
* @param eProps
|
|
292
352
|
* Additional properties assigned to the error object.
|
|
293
353
|
*
|
|
294
354
|
* @returns
|
|
@@ -311,12 +371,13 @@ var Contracts = class _Contracts {
|
|
|
311
371
|
* return result;
|
|
312
372
|
* }
|
|
313
373
|
*/
|
|
314
|
-
static ENSURE(isOk, ngMsg, ErrorClass = Error, eProps
|
|
374
|
+
static ENSURE(isOk, ngMsg, ErrorClass = Error, eParams, eProps) {
|
|
315
375
|
return _Contracts.check(
|
|
316
376
|
isOk,
|
|
317
377
|
"ENSURE",
|
|
318
378
|
ngMsg,
|
|
319
379
|
ErrorClass,
|
|
380
|
+
eParams,
|
|
320
381
|
eProps
|
|
321
382
|
);
|
|
322
383
|
}
|
|
@@ -334,13 +395,14 @@ var Contracts = class _Contracts {
|
|
|
334
395
|
* - Confirm internal behavior while debugging.
|
|
335
396
|
*
|
|
336
397
|
* @param isOk
|
|
337
|
-
* Condition result
|
|
398
|
+
* Condition result(`boolean`) to be verified.
|
|
338
399
|
*
|
|
339
|
-
* @param
|
|
400
|
+
* @param ngMsg
|
|
340
401
|
* Failure message.
|
|
341
402
|
*
|
|
342
|
-
* @param
|
|
403
|
+
* @param ErrorClass
|
|
343
404
|
* Error constructor used when the check fails.
|
|
405
|
+
* This is used as follows: throw Object.assign(new ErrorClass(msg, eParams), eProps);
|
|
344
406
|
*
|
|
345
407
|
* Supported values:
|
|
346
408
|
* - `Error` (default)
|
|
@@ -349,7 +411,10 @@ var Contracts = class _Contracts {
|
|
|
349
411
|
* - Custom Error subclasses
|
|
350
412
|
* - `null` to skip throwing and log the failure.
|
|
351
413
|
*
|
|
352
|
-
* @param
|
|
414
|
+
* @param eParams
|
|
415
|
+
* Parameter options following the message passed to the Error constructor.
|
|
416
|
+
*
|
|
417
|
+
* @param eProps
|
|
353
418
|
* Additional properties assigned to the error object.
|
|
354
419
|
*
|
|
355
420
|
* @returns
|
|
@@ -361,12 +426,13 @@ var Contracts = class _Contracts {
|
|
|
361
426
|
* 'Result should exist during debugging'
|
|
362
427
|
* );
|
|
363
428
|
*/
|
|
364
|
-
static ENSURE_DEBUG(isOk, ngMsg, ErrorClass = Error, eProps
|
|
429
|
+
static ENSURE_DEBUG(isOk, ngMsg, ErrorClass = Error, eParams, eProps) {
|
|
365
430
|
return _Contracts.checkDebug(
|
|
366
431
|
isOk,
|
|
367
432
|
"ENSURE_DEBUG",
|
|
368
433
|
ngMsg,
|
|
369
434
|
ErrorClass,
|
|
435
|
+
eParams,
|
|
370
436
|
eProps
|
|
371
437
|
);
|
|
372
438
|
}
|
|
@@ -385,13 +451,14 @@ var Contracts = class _Contracts {
|
|
|
385
451
|
* INVARIANT represents conditions that must always remain true.
|
|
386
452
|
*
|
|
387
453
|
* @param isOk
|
|
388
|
-
* Condition result
|
|
454
|
+
* Condition result(`boolean`) to be verified.
|
|
389
455
|
*
|
|
390
|
-
* @param
|
|
456
|
+
* @param ngMsg
|
|
391
457
|
* Failure message.
|
|
392
458
|
*
|
|
393
|
-
* @param
|
|
459
|
+
* @param ErrorClass
|
|
394
460
|
* Error constructor used when the check fails.
|
|
461
|
+
* This is used as follows: throw Object.assign(new ErrorClass(msg, eParams), eProps);
|
|
395
462
|
*
|
|
396
463
|
* Supported values:
|
|
397
464
|
* - `Error` (default)
|
|
@@ -400,7 +467,10 @@ var Contracts = class _Contracts {
|
|
|
400
467
|
* - Custom Error subclasses
|
|
401
468
|
* - `null` to skip throwing and log the failure.
|
|
402
469
|
*
|
|
403
|
-
* @param
|
|
470
|
+
* @param eParams
|
|
471
|
+
* Parameter options following the message passed to the Error constructor.
|
|
472
|
+
*
|
|
473
|
+
* @param eProps
|
|
404
474
|
* Additional properties assigned to the error object.
|
|
405
475
|
*
|
|
406
476
|
* @returns
|
|
@@ -420,12 +490,13 @@ var Contracts = class _Contracts {
|
|
|
420
490
|
*
|
|
421
491
|
* }
|
|
422
492
|
*/
|
|
423
|
-
static INVARIANT(isOk, ngMsg, ErrorClass = Error, eProps
|
|
493
|
+
static INVARIANT(isOk, ngMsg, ErrorClass = Error, eParams, eProps) {
|
|
424
494
|
return _Contracts.check(
|
|
425
495
|
isOk,
|
|
426
496
|
"INVARIANT",
|
|
427
497
|
ngMsg,
|
|
428
498
|
ErrorClass,
|
|
499
|
+
eParams,
|
|
429
500
|
eProps
|
|
430
501
|
);
|
|
431
502
|
}
|
|
@@ -443,13 +514,14 @@ var Contracts = class _Contracts {
|
|
|
443
514
|
* - Detect unexpected state changes while debugging.
|
|
444
515
|
*
|
|
445
516
|
* @param isOk
|
|
446
|
-
* Condition result
|
|
517
|
+
* Condition result(`boolean`) to be verified.
|
|
447
518
|
*
|
|
448
519
|
* @param [ngMsg]
|
|
449
520
|
* Failure message.
|
|
450
521
|
*
|
|
451
522
|
* @param [ErrorClass=Error]
|
|
452
523
|
* Error constructor used when the check fails.
|
|
524
|
+
* This is used as follows: throw Object.assign(new ErrorClass(msg, eParams), eProps);
|
|
453
525
|
*
|
|
454
526
|
* Supported values:
|
|
455
527
|
* - `Error` (default)
|
|
@@ -458,6 +530,9 @@ var Contracts = class _Contracts {
|
|
|
458
530
|
* - Custom Error subclasses
|
|
459
531
|
* - `null` to skip throwing and log the failure.
|
|
460
532
|
*
|
|
533
|
+
* @param eParams
|
|
534
|
+
* Parameter options following the message passed to the Error constructor.
|
|
535
|
+
*
|
|
461
536
|
* @param [eProps = {}]
|
|
462
537
|
* Additional properties assigned to the error object.
|
|
463
538
|
*
|
|
@@ -470,12 +545,13 @@ var Contracts = class _Contracts {
|
|
|
470
545
|
* 'Cache size exceeded expected limit'
|
|
471
546
|
* );
|
|
472
547
|
*/
|
|
473
|
-
static INVARIANT_DEBUG(isOk, ngMsg, ErrorClass = Error, eProps
|
|
548
|
+
static INVARIANT_DEBUG(isOk, ngMsg, ErrorClass = Error, eParams, eProps) {
|
|
474
549
|
return _Contracts.checkDebug(
|
|
475
550
|
isOk,
|
|
476
551
|
"INVARIANT_DEBUG",
|
|
477
552
|
ngMsg,
|
|
478
553
|
ErrorClass,
|
|
554
|
+
eParams,
|
|
479
555
|
eProps
|
|
480
556
|
);
|
|
481
557
|
}
|
|
@@ -496,7 +572,7 @@ var Contracts = class _Contracts {
|
|
|
496
572
|
* @internal
|
|
497
573
|
*
|
|
498
574
|
* @param isOk
|
|
499
|
-
* Condition result.
|
|
575
|
+
* Condition result(`boolean`) to be verified.
|
|
500
576
|
*
|
|
501
577
|
* @param prefix
|
|
502
578
|
* Contract type prefix used in the error message.
|
|
@@ -505,8 +581,19 @@ var Contracts = class _Contracts {
|
|
|
505
581
|
* Failure message.
|
|
506
582
|
*
|
|
507
583
|
* @param ErrorClass
|
|
508
|
-
* Error constructor.
|
|
584
|
+
* Error constructor used when the check fails.
|
|
585
|
+
* This is used as follows: throw Object.assign(new ErrorClass(msg, eParams), eProps);
|
|
509
586
|
*
|
|
587
|
+
* Supported values:
|
|
588
|
+
* - `Error` (default)
|
|
589
|
+
* - `TypeError`
|
|
590
|
+
* - `RangeError`
|
|
591
|
+
* - Custom Error subclasses
|
|
592
|
+
* - `null` to skip throwing and log the failure.
|
|
593
|
+
*
|
|
594
|
+
* @param eParams
|
|
595
|
+
* Parameter options following the message passed to the Error constructor.
|
|
596
|
+
*
|
|
510
597
|
* @param eProps
|
|
511
598
|
* Additional properties assigned to the error object.
|
|
512
599
|
*
|
|
@@ -514,14 +601,19 @@ var Contracts = class _Contracts {
|
|
|
514
601
|
* Returns the original condition value.
|
|
515
602
|
*
|
|
516
603
|
*/
|
|
517
|
-
static check(isOk, prefix = "CHECK", ngMsg = "", ErrorClass = Error, eProps
|
|
604
|
+
static check(isOk, prefix = "CHECK", ngMsg = "", ErrorClass = Error, eParams, eProps) {
|
|
605
|
+
if (eProps === void 0) {
|
|
606
|
+
eProps = eParams ?? {};
|
|
607
|
+
eParams = null;
|
|
608
|
+
} else {
|
|
609
|
+
eProps ??= {};
|
|
610
|
+
}
|
|
518
611
|
if (!isOk) {
|
|
519
612
|
const msg = `[${prefix}] ${ngMsg ?? ""}`;
|
|
520
613
|
if (ErrorClass) {
|
|
521
|
-
|
|
522
|
-
|
|
523
|
-
|
|
524
|
-
);
|
|
614
|
+
const err = eParams ? new ErrorClass(msg, eParams) : new ErrorClass(msg);
|
|
615
|
+
eProps && Object.assign(err, eProps);
|
|
616
|
+
throw err;
|
|
525
617
|
}
|
|
526
618
|
if (ngMsg) {
|
|
527
619
|
_Contracts.getLogger().error(msg, eProps);
|
|
@@ -542,7 +634,7 @@ var Contracts = class _Contracts {
|
|
|
542
634
|
* @internal
|
|
543
635
|
*
|
|
544
636
|
* @param isOk
|
|
545
|
-
* Condition result.
|
|
637
|
+
* Condition result(`boolean`) to be verified.
|
|
546
638
|
*
|
|
547
639
|
* @param prefix
|
|
548
640
|
* Contract type prefix used in the error message.
|
|
@@ -550,8 +642,19 @@ var Contracts = class _Contracts {
|
|
|
550
642
|
* @param ngMsg
|
|
551
643
|
* Failure message.
|
|
552
644
|
*
|
|
553
|
-
* @param ErrorClass
|
|
554
|
-
* Error constructor.
|
|
645
|
+
* @param ErrorClass=Error
|
|
646
|
+
* Error constructor used when the check fails.
|
|
647
|
+
* This is used as follows: throw Object.assign(new ErrorClass(msg, eParams), eProps);
|
|
648
|
+
*
|
|
649
|
+
* Supported values:
|
|
650
|
+
* - `Error` (default)
|
|
651
|
+
* - `TypeError`
|
|
652
|
+
* - `RangeError`
|
|
653
|
+
* - Custom Error subclasses
|
|
654
|
+
* - `null` to skip throwing and log the failure.
|
|
655
|
+
*
|
|
656
|
+
* @param eParams
|
|
657
|
+
* Parameter options following the message passed to the Error constructor.
|
|
555
658
|
*
|
|
556
659
|
* @param eProps
|
|
557
660
|
* Additional properties assigned to the error object.
|
|
@@ -560,12 +663,13 @@ var Contracts = class _Contracts {
|
|
|
560
663
|
* Returns the original condition value.
|
|
561
664
|
*
|
|
562
665
|
*/
|
|
563
|
-
static checkDebug(isOk, prefix = "CHECK", ngMsg = "", ErrorClass = Error, eProps
|
|
666
|
+
static checkDebug(isOk, prefix = "CHECK", ngMsg = "", ErrorClass = Error, eParams, eProps) {
|
|
564
667
|
return _Contracts.DEBUG_MODE ? _Contracts.check(
|
|
565
668
|
isOk,
|
|
566
669
|
prefix,
|
|
567
670
|
ngMsg,
|
|
568
671
|
ErrorClass,
|
|
672
|
+
eParams,
|
|
569
673
|
eProps
|
|
570
674
|
) : isOk;
|
|
571
675
|
}
|
|
@@ -575,7 +679,7 @@ var Contracts = class _Contracts {
|
|
|
575
679
|
* @internal
|
|
576
680
|
*/
|
|
577
681
|
static getLogger() {
|
|
578
|
-
return _Contracts.logger
|
|
682
|
+
return _Contracts.#config.logger;
|
|
579
683
|
}
|
|
580
684
|
};
|
|
581
685
|
|
package/package.json
CHANGED
|
@@ -1,38 +1,43 @@
|
|
|
1
|
-
{
|
|
2
|
-
"name": "@ayapapa-npm/contracts-js",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"description": "A lightweight Design by Contract library for JavaScript.",
|
|
5
|
-
"license": "MIT",
|
|
6
|
-
"author": "ayapapa",
|
|
7
|
-
"type": "module",
|
|
8
|
-
"main": "./dist/index.cjs",
|
|
9
|
-
"module": "./dist/index.js",
|
|
10
|
-
"exports": {
|
|
11
|
-
".": {
|
|
12
|
-
"import": "./dist/index.js",
|
|
13
|
-
"require": "./dist/index.cjs"
|
|
14
|
-
}
|
|
15
|
-
},
|
|
16
|
-
"files": [
|
|
17
|
-
"dist"
|
|
18
|
-
],
|
|
19
|
-
"scripts": {
|
|
20
|
-
"build": "tsup src/index.ts --format esm,cjs --out-dir dist --clean --dts",
|
|
21
|
-
"typecheck": "tsc --noEmit",
|
|
22
|
-
"test": "vitest run",
|
|
23
|
-
"test:debug": "vitest --inspect-brk --run",
|
|
24
|
-
"test:watch": "vitest",
|
|
25
|
-
"
|
|
26
|
-
"
|
|
27
|
-
"
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
"
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
"
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
"
|
|
37
|
-
|
|
38
|
-
|
|
1
|
+
{
|
|
2
|
+
"name": "@ayapapa-npm/contracts-js",
|
|
3
|
+
"version": "0.4.0",
|
|
4
|
+
"description": "A lightweight Design by Contract library for JavaScript.",
|
|
5
|
+
"license": "MIT",
|
|
6
|
+
"author": "ayapapa",
|
|
7
|
+
"type": "module",
|
|
8
|
+
"main": "./dist/index.cjs",
|
|
9
|
+
"module": "./dist/index.js",
|
|
10
|
+
"exports": {
|
|
11
|
+
".": {
|
|
12
|
+
"import": "./dist/index.js",
|
|
13
|
+
"require": "./dist/index.cjs"
|
|
14
|
+
}
|
|
15
|
+
},
|
|
16
|
+
"files": [
|
|
17
|
+
"dist"
|
|
18
|
+
],
|
|
19
|
+
"scripts": {
|
|
20
|
+
"build": "tsup src/index.ts --format esm,cjs --out-dir dist --clean --dts",
|
|
21
|
+
"typecheck": "tsc --noEmit",
|
|
22
|
+
"test": "vitest run",
|
|
23
|
+
"test:debug": "vitest --inspect-brk --run",
|
|
24
|
+
"test:watch": "vitest",
|
|
25
|
+
"test:ui": "vitest --ui",
|
|
26
|
+
"coverage": "vitest run --coverage",
|
|
27
|
+
"coverage:ui": "vitest run --coverage && start coverage/index.html",
|
|
28
|
+
"check": "npm run typecheck && npm run build && npm run test",
|
|
29
|
+
"release:check": "npm run check",
|
|
30
|
+
"prepublishOnly": "npm run release:check"
|
|
31
|
+
},
|
|
32
|
+
"devDependencies": {
|
|
33
|
+
"@types/node": "^26.2.0",
|
|
34
|
+
"@vitest/coverage-v8": "^4.1.11",
|
|
35
|
+
"@vitest/ui": "^4.1.11",
|
|
36
|
+
"tsup": "^8.5.1",
|
|
37
|
+
"typedoc": "^0.28.20",
|
|
38
|
+
"typedoc-plugin-markdown": "^4.13.0",
|
|
39
|
+
"typescript": "^5.9.3",
|
|
40
|
+
"vite": "^8.2.2",
|
|
41
|
+
"vitest": "^4.1.11"
|
|
42
|
+
}
|
|
43
|
+
}
|