@ayapapa-npm/contracts-js 0.2.5 → 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.cjs CHANGED
@@ -89,11 +89,12 @@ var Contracts = class _Contracts {
89
89
  * @param isOk
90
90
  * Condition result to verify.
91
91
  *
92
- * @param [ngMsg]
92
+ * @param ngMsg
93
93
  * Failure message.
94
94
  *
95
- * @param [ErrorClass=Error]
95
+ * @param ErrorClass
96
96
  * Error constructor used when the check fails.
97
+ * This is used as follows: throw Object.assign(new ErrorClass(msg, eParams), eProps);
97
98
  *
98
99
  * Supported values:
99
100
  * - `Error` (default)
@@ -102,7 +103,10 @@ var Contracts = class _Contracts {
102
103
  * - Custom Error subclasses
103
104
  * - `null` to skip throwing and log the failure.
104
105
  *
105
- * @param [eProps = {}]
106
+ * @param eParams
107
+ * Parameter options following the message passed to the Error constructor.
108
+ *
109
+ * @param eProps
106
110
  * Additional properties assigned to the error object.
107
111
  *
108
112
  * @returns
@@ -116,12 +120,13 @@ var Contracts = class _Contracts {
116
120
  * 'Calculation result must not be negative'
117
121
  * );
118
122
  */
119
- static VERIFY(isOk, ngMsg, ErrorClass = Error, eProps = {}) {
123
+ static VERIFY(isOk, ngMsg, ErrorClass = Error, eParams, eProps) {
120
124
  return _Contracts.check(
121
125
  isOk,
122
126
  "VERIFY",
123
127
  ngMsg,
124
128
  ErrorClass,
129
+ eParams,
125
130
  eProps
126
131
  );
127
132
  }
@@ -141,11 +146,12 @@ var Contracts = class _Contracts {
141
146
  * @param isOk
142
147
  * Condition result to verify.
143
148
  *
144
- * @param [ngMsg]
149
+ * @param ngMsg
145
150
  * Failure message.
146
151
  *
147
- * @param [ErrorClass=Error]
152
+ * @param ErrorClass
148
153
  * Error constructor used when the check fails.
154
+ * This is used as follows: throw Object.assign(new ErrorClass(msg, eParams), eProps);
149
155
  *
150
156
  * Supported values:
151
157
  * - `Error` (default)
@@ -154,7 +160,10 @@ var Contracts = class _Contracts {
154
160
  * - Custom Error subclasses
155
161
  * - `null` to skip throwing and log the failure.
156
162
  *
157
- * @param [eProps = {}]
163
+ * @param eParams
164
+ * Parameter options following the message passed to the Error constructor.
165
+ *
166
+ * @param eProps
158
167
  * Additional properties assigned to the error object.
159
168
  *
160
169
  * @returns
@@ -166,12 +175,13 @@ var Contracts = class _Contracts {
166
175
  * 'Intermediate value must not be null'
167
176
  * );
168
177
  */
169
- static VERIFY_DEBUG(isOk, ngMsg, ErrorClass = Error, eProps = {}) {
178
+ static VERIFY_DEBUG(isOk, ngMsg, ErrorClass = Error, eParams, eProps) {
170
179
  return _Contracts.checkDebug(
171
180
  isOk,
172
181
  "VERIFY_DEBUG",
173
182
  ngMsg,
174
183
  ErrorClass,
184
+ eParams,
175
185
  eProps
176
186
  );
177
187
  }
@@ -191,11 +201,12 @@ var Contracts = class _Contracts {
191
201
  * @param isOk
192
202
  * Condition result to verify.
193
203
  *
194
- * @param [ngMsg]
204
+ * @param ngMsg
195
205
  * Failure message.
196
206
  *
197
- * @param [ErrorClass=Error]
207
+ * @param ErrorClass
198
208
  * Error constructor used when the check fails.
209
+ * This is used as follows: throw Object.assign(new ErrorClass(msg, eParams), eProps);
199
210
  *
200
211
  * Supported values:
201
212
  * - `Error` (default)
@@ -204,7 +215,10 @@ var Contracts = class _Contracts {
204
215
  * - Custom Error subclasses
205
216
  * - `null` to skip throwing and log the failure.
206
217
  *
207
- * @param [eProps = {}]
218
+ * @param eParams
219
+ * Parameter options following the message passed to the Error constructor.
220
+ *
221
+ * @param eProps
208
222
  * Additional properties assigned to the error object.
209
223
  *
210
224
  * @returns
@@ -225,12 +239,13 @@ var Contracts = class _Contracts {
225
239
  * return a / b;
226
240
  * }
227
241
  */
228
- static REQUIRE(isOk, ngMsg, ErrorClass = Error, eProps = {}) {
242
+ static REQUIRE(isOk, ngMsg, ErrorClass = Error, eParams, eProps) {
229
243
  return _Contracts.check(
230
244
  isOk,
231
245
  "REQUIRE",
232
246
  ngMsg,
233
247
  ErrorClass,
248
+ eParams,
234
249
  eProps
235
250
  );
236
251
  }
@@ -250,11 +265,12 @@ var Contracts = class _Contracts {
250
265
  * @param isOk
251
266
  * Condition result to verify.
252
267
  *
253
- * @param [ngMsg]
268
+ * @param ngMsg
254
269
  * Failure message.
255
270
  *
256
- * @param [ErrorClass=Error]
271
+ * @param ErrorClass
257
272
  * Error constructor used when the check fails.
273
+ * This is used as follows: throw Object.assign(new ErrorClass(msg, eParams), eProps);
258
274
  *
259
275
  * Supported values:
260
276
  * - `Error` (default)
@@ -263,7 +279,10 @@ var Contracts = class _Contracts {
263
279
  * - Custom Error subclasses
264
280
  * - `null` to skip throwing and log the failure.
265
281
  *
266
- * @param [eProps = {}]
282
+ * @param eParams
283
+ * Parameter options following the message passed to the Error constructor.
284
+ *
285
+ * @param eProps
267
286
  * Additional properties assigned to the error object.
268
287
  *
269
288
  * @returns
@@ -275,12 +294,13 @@ var Contracts = class _Contracts {
275
294
  * 'User must exist during debugging'
276
295
  * );
277
296
  */
278
- static REQUIRE_DEBUG(isOk, ngMsg, ErrorClass = Error, eProps = {}) {
297
+ static REQUIRE_DEBUG(isOk, ngMsg, ErrorClass = Error, eParams, eProps) {
279
298
  return _Contracts.checkDebug(
280
299
  isOk,
281
300
  "REQUIRE_DEBUG",
282
301
  ngMsg,
283
302
  ErrorClass,
303
+ eParams,
284
304
  eProps
285
305
  );
286
306
  }
@@ -301,11 +321,12 @@ var Contracts = class _Contracts {
301
321
  * @param isOk
302
322
  * Condition result to verify.
303
323
  *
304
- * @param [ngMsg]
324
+ * @param ngMsg
305
325
  * Failure message.
306
326
  *
307
- * @param [ErrorClass=Error]
327
+ * @param ErrorClass
308
328
  * Error constructor used when the check fails.
329
+ * This is used as follows: throw Object.assign(new ErrorClass(msg, eParams), eProps);
309
330
  *
310
331
  * Supported values:
311
332
  * - `Error` (default)
@@ -314,7 +335,10 @@ var Contracts = class _Contracts {
314
335
  * - Custom Error subclasses
315
336
  * - `null` to skip throwing and log the failure.
316
337
  *
317
- * @param [eProps = {}]
338
+ * @param eParams
339
+ * Parameter options following the message passed to the Error constructor.
340
+ *
341
+ * @param eProps
318
342
  * Additional properties assigned to the error object.
319
343
  *
320
344
  * @returns
@@ -337,12 +361,13 @@ var Contracts = class _Contracts {
337
361
  * return result;
338
362
  * }
339
363
  */
340
- static ENSURE(isOk, ngMsg, ErrorClass = Error, eProps = {}) {
364
+ static ENSURE(isOk, ngMsg, ErrorClass = Error, eParams, eProps) {
341
365
  return _Contracts.check(
342
366
  isOk,
343
367
  "ENSURE",
344
368
  ngMsg,
345
369
  ErrorClass,
370
+ eParams,
346
371
  eProps
347
372
  );
348
373
  }
@@ -362,11 +387,12 @@ var Contracts = class _Contracts {
362
387
  * @param isOk
363
388
  * Condition result to verify.
364
389
  *
365
- * @param [ngMsg]
390
+ * @param ngMsg
366
391
  * Failure message.
367
392
  *
368
- * @param [ErrorClass=Error]
393
+ * @param ErrorClass
369
394
  * Error constructor used when the check fails.
395
+ * This is used as follows: throw Object.assign(new ErrorClass(msg, eParams), eProps);
370
396
  *
371
397
  * Supported values:
372
398
  * - `Error` (default)
@@ -375,7 +401,10 @@ var Contracts = class _Contracts {
375
401
  * - Custom Error subclasses
376
402
  * - `null` to skip throwing and log the failure.
377
403
  *
378
- * @param [eProps = {}]
404
+ * @param eParams
405
+ * Parameter options following the message passed to the Error constructor.
406
+ *
407
+ * @param eProps
379
408
  * Additional properties assigned to the error object.
380
409
  *
381
410
  * @returns
@@ -387,12 +416,13 @@ var Contracts = class _Contracts {
387
416
  * 'Result should exist during debugging'
388
417
  * );
389
418
  */
390
- static ENSURE_DEBUG(isOk, ngMsg, ErrorClass = Error, eProps = {}) {
419
+ static ENSURE_DEBUG(isOk, ngMsg, ErrorClass = Error, eParams, eProps) {
391
420
  return _Contracts.checkDebug(
392
421
  isOk,
393
422
  "ENSURE_DEBUG",
394
423
  ngMsg,
395
424
  ErrorClass,
425
+ eParams,
396
426
  eProps
397
427
  );
398
428
  }
@@ -413,11 +443,12 @@ var Contracts = class _Contracts {
413
443
  * @param isOk
414
444
  * Condition result to verify.
415
445
  *
416
- * @param [ngMsg]
446
+ * @param ngMsg
417
447
  * Failure message.
418
448
  *
419
- * @param [ErrorClass=Error]
449
+ * @param ErrorClass
420
450
  * Error constructor used when the check fails.
451
+ * This is used as follows: throw Object.assign(new ErrorClass(msg, eParams), eProps);
421
452
  *
422
453
  * Supported values:
423
454
  * - `Error` (default)
@@ -426,7 +457,10 @@ var Contracts = class _Contracts {
426
457
  * - Custom Error subclasses
427
458
  * - `null` to skip throwing and log the failure.
428
459
  *
429
- * @param [eProps = {}]
460
+ * @param eParams
461
+ * Parameter options following the message passed to the Error constructor.
462
+ *
463
+ * @param eProps
430
464
  * Additional properties assigned to the error object.
431
465
  *
432
466
  * @returns
@@ -446,12 +480,13 @@ var Contracts = class _Contracts {
446
480
  *
447
481
  * }
448
482
  */
449
- static INVARIANT(isOk, ngMsg, ErrorClass = Error, eProps = {}) {
483
+ static INVARIANT(isOk, ngMsg, ErrorClass = Error, eParams, eProps) {
450
484
  return _Contracts.check(
451
485
  isOk,
452
486
  "INVARIANT",
453
487
  ngMsg,
454
488
  ErrorClass,
489
+ eParams,
455
490
  eProps
456
491
  );
457
492
  }
@@ -476,6 +511,7 @@ var Contracts = class _Contracts {
476
511
  *
477
512
  * @param [ErrorClass=Error]
478
513
  * Error constructor used when the check fails.
514
+ * This is used as follows: throw Object.assign(new ErrorClass(msg, eParams), eProps);
479
515
  *
480
516
  * Supported values:
481
517
  * - `Error` (default)
@@ -484,6 +520,9 @@ var Contracts = class _Contracts {
484
520
  * - Custom Error subclasses
485
521
  * - `null` to skip throwing and log the failure.
486
522
  *
523
+ * @param eParams
524
+ * Parameter options following the message passed to the Error constructor.
525
+ *
487
526
  * @param [eProps = {}]
488
527
  * Additional properties assigned to the error object.
489
528
  *
@@ -496,12 +535,13 @@ var Contracts = class _Contracts {
496
535
  * 'Cache size exceeded expected limit'
497
536
  * );
498
537
  */
499
- static INVARIANT_DEBUG(isOk, ngMsg, ErrorClass = Error, eProps = {}) {
538
+ static INVARIANT_DEBUG(isOk, ngMsg, ErrorClass = Error, eParams, eProps) {
500
539
  return _Contracts.checkDebug(
501
540
  isOk,
502
541
  "INVARIANT_DEBUG",
503
542
  ngMsg,
504
543
  ErrorClass,
544
+ eParams,
505
545
  eProps
506
546
  );
507
547
  }
@@ -531,8 +571,19 @@ var Contracts = class _Contracts {
531
571
  * Failure message.
532
572
  *
533
573
  * @param ErrorClass
534
- * Error constructor.
574
+ * Error constructor used when the check fails.
575
+ * This is used as follows: throw Object.assign(new ErrorClass(msg, eParams), eProps);
535
576
  *
577
+ * Supported values:
578
+ * - `Error` (default)
579
+ * - `TypeError`
580
+ * - `RangeError`
581
+ * - Custom Error subclasses
582
+ * - `null` to skip throwing and log the failure.
583
+ *
584
+ * @param eParams
585
+ * Parameter options following the message passed to the Error constructor.
586
+ *
536
587
  * @param eProps
537
588
  * Additional properties assigned to the error object.
538
589
  *
@@ -540,14 +591,21 @@ var Contracts = class _Contracts {
540
591
  * Returns the original condition value.
541
592
  *
542
593
  */
543
- static check(isOk, prefix = "CHECK", ngMsg = "", ErrorClass = Error, eProps = {}) {
594
+ static check(isOk, prefix = "CHECK", ngMsg = "", ErrorClass = Error, eParams, eProps) {
595
+ if (eProps === void 0) {
596
+ eProps = eParams ?? {};
597
+ eParams = null;
598
+ } else {
599
+ eProps ??= {};
600
+ }
544
601
  if (!isOk) {
545
602
  const msg = `[${prefix}] ${ngMsg ?? ""}`;
546
603
  if (ErrorClass) {
547
- throw Object.assign(
548
- new ErrorClass(msg),
549
- eProps
550
- );
604
+ const err = eParams ? new ErrorClass(msg, eParams) : new ErrorClass(msg);
605
+ if (eProps) {
606
+ Object.assign(err, eProps);
607
+ }
608
+ throw err;
551
609
  }
552
610
  if (ngMsg) {
553
611
  _Contracts.getLogger().error(msg, eProps);
@@ -576,8 +634,19 @@ var Contracts = class _Contracts {
576
634
  * @param ngMsg
577
635
  * Failure message.
578
636
  *
579
- * @param ErrorClass
580
- * Error constructor.
637
+ * @param ErrorClass=Error
638
+ * Error constructor used when the check fails.
639
+ * This is used as follows: throw Object.assign(new ErrorClass(msg, eParams), eProps);
640
+ *
641
+ * Supported values:
642
+ * - `Error` (default)
643
+ * - `TypeError`
644
+ * - `RangeError`
645
+ * - Custom Error subclasses
646
+ * - `null` to skip throwing and log the failure.
647
+ *
648
+ * @param eParams
649
+ * Parameter options following the message passed to the Error constructor.
581
650
  *
582
651
  * @param eProps
583
652
  * Additional properties assigned to the error object.
@@ -586,12 +655,13 @@ var Contracts = class _Contracts {
586
655
  * Returns the original condition value.
587
656
  *
588
657
  */
589
- static checkDebug(isOk, prefix = "CHECK", ngMsg = "", ErrorClass = Error, eProps = {}) {
658
+ static checkDebug(isOk, prefix = "CHECK", ngMsg = "", ErrorClass = Error, eParams, eProps) {
590
659
  return _Contracts.DEBUG_MODE ? _Contracts.check(
591
660
  isOk,
592
661
  prefix,
593
662
  ngMsg,
594
663
  ErrorClass,
664
+ eParams,
595
665
  eProps
596
666
  ) : isOk;
597
667
  }