@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.js CHANGED
@@ -1,20 +1,21 @@
1
- var __defProp = Object.defineProperty;
2
- var __typeError = (msg) => {
3
- throw TypeError(msg);
4
- };
5
- var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
6
- var __publicField = (obj, key, value) => __defNormalProp(obj, typeof key !== "symbol" ? key + "" : key, value);
7
- var __accessCheck = (obj, member, msg) => member.has(obj) || __typeError("Cannot " + msg);
8
- var __privateAdd = (obj, member, value) => member.has(obj) ? __typeError("Cannot add the same private member more than once") : member instanceof WeakSet ? member.add(obj) : member.set(obj, value);
9
- var __privateMethod = (obj, member, method) => (__accessCheck(obj, member, "access private method"), method);
10
-
11
- // src/lib/Contracts.js
12
- var _Contracts_static, check_fn, checkDebug_fn;
13
- var _Contracts = class _Contracts {
1
+ // src/lib/Contracts.ts
2
+ var Contracts = class _Contracts {
3
+ /**
4
+ * Static fields
5
+ */
6
+ /** Debug mode state */
7
+ static DEBUG_MODE = false;
8
+ /** default configuration */
9
+ static defaultConf = {
10
+ debug: false,
11
+ logger: console
12
+ };
13
+ /** logger */
14
+ static logger = console;
14
15
  /**
15
16
  * Configures contract checking behavior.
16
17
  *
17
- * @param {{debug?: boolean}} config
18
+ * @param config
18
19
  * Configuration options.
19
20
  *
20
21
  * The `debug` property enables or disables
@@ -25,12 +26,22 @@ var _Contracts = class _Contracts {
25
26
  *
26
27
  * When `debug` is `false` or omitted,
27
28
  * methods ending with `_DEBUG` skip validation.
29
+ *
30
+ * Is the `logger` property is specified,
31
+ * it is used instead of the standard logger, `console`.
32
+ * This module uses only the `error` method.
28
33
  *
29
34
  * @example
30
- * Contracts.setConfig({ debug: true });
35
+ * // Use a logger that is slightly more advanced than the standard logger—namely, `console`.
36
+ * import { PrettyConsole } from '@ayapapa-npm/pretty-console-js';
37
+ *
38
+ * const prettyConsole = new PrettyConsole();
39
+ * Contracts.setConfig({ debug: true, logger: prettyConsole });
40
+ * // Node: ` The `logger` property is optional.
31
41
  */
32
42
  static setConfig(config) {
33
- _Contracts.DEBUG_MODE = Boolean(config.debug);
43
+ _Contracts.DEBUG_MODE = Boolean(config?.debug);
44
+ _Contracts.logger = config?.logger ?? console;
34
45
  }
35
46
  /**
36
47
  * Verifies an intermediate condition during execution.
@@ -49,13 +60,13 @@ var _Contracts = class _Contracts {
49
60
  * - Confirm internal processing states.
50
61
  * - Check temporary assumptions during execution.
51
62
  *
52
- * @param {boolean} isOk
63
+ * @param isOk
53
64
  * Condition result to verify.
54
65
  *
55
- * @param {string|null} [ngMsg]
66
+ * @param [ngMsg]
56
67
  * Failure message.
57
68
  *
58
- * @param {(new (...args:any[])=>Error)|null} [ErrorClass=Error]
69
+ * @param [ErrorClass=Error]
59
70
  * Error constructor used when the check fails.
60
71
  *
61
72
  * Supported values:
@@ -65,10 +76,10 @@ var _Contracts = class _Contracts {
65
76
  * - Custom Error subclasses
66
77
  * - `null` to skip throwing and log the failure.
67
78
  *
68
- * @param {Object<string, *>} [eProps]
79
+ * @param [eProps = {}]
69
80
  * Additional properties assigned to the error object.
70
81
  *
71
- * @returns {boolean}
82
+ * @returns
72
83
  * Returns the original condition value.
73
84
  *
74
85
  * @example
@@ -80,8 +91,13 @@ var _Contracts = class _Contracts {
80
91
  * );
81
92
  */
82
93
  static VERIFY(isOk, ngMsg, ErrorClass = Error, eProps = {}) {
83
- var _a;
84
- return __privateMethod(_a = _Contracts, _Contracts_static, check_fn).call(_a, isOk, "VERIFY", ngMsg, ErrorClass, eProps);
94
+ return _Contracts.check(
95
+ isOk,
96
+ "VERIFY",
97
+ ngMsg,
98
+ ErrorClass,
99
+ eProps
100
+ );
85
101
  }
86
102
  /**
87
103
  * Verifies an intermediate condition in debug mode only.
@@ -96,19 +112,26 @@ var _Contracts = class _Contracts {
96
112
  * - Validate intermediate results during development.
97
113
  * - Check internal assumptions while debugging.
98
114
  *
99
- * @param {boolean} isOk
115
+ * @param isOk
100
116
  * Condition result to verify.
101
117
  *
102
- * @param {string|null} [ngMsg]
118
+ * @param [ngMsg]
103
119
  * Failure message.
104
120
  *
105
- * @param {(new (...args:any[])=>Error)|null} [ErrorClass=Error]
121
+ * @param [ErrorClass=Error]
106
122
  * Error constructor used when the check fails.
107
123
  *
108
- * @param {Object<string, *>} [eProps]
124
+ * Supported values:
125
+ * - `Error` (default)
126
+ * - `TypeError`
127
+ * - `RangeError`
128
+ * - Custom Error subclasses
129
+ * - `null` to skip throwing and log the failure.
130
+ *
131
+ * @param [eProps = {}]
109
132
  * Additional properties assigned to the error object.
110
133
  *
111
- * @returns {boolean}
134
+ * @returns
112
135
  * Returns the original condition value.
113
136
  *
114
137
  * @example
@@ -118,8 +141,13 @@ var _Contracts = class _Contracts {
118
141
  * );
119
142
  */
120
143
  static VERIFY_DEBUG(isOk, ngMsg, ErrorClass = Error, eProps = {}) {
121
- var _a;
122
- return __privateMethod(_a = _Contracts, _Contracts_static, checkDebug_fn).call(_a, isOk, "VERIFY_DEBUG", ngMsg, ErrorClass, eProps);
144
+ return _Contracts.checkDebug(
145
+ isOk,
146
+ "VERIFY_DEBUG",
147
+ ngMsg,
148
+ ErrorClass,
149
+ eProps
150
+ );
123
151
  }
124
152
  /**
125
153
  * Checks a precondition before execution.
@@ -134,13 +162,13 @@ var _Contracts = class _Contracts {
134
162
  * - Validate required object state.
135
163
  * - Check required external conditions.
136
164
  *
137
- * @param {boolean} isOk
165
+ * @param isOk
138
166
  * Condition result to verify.
139
167
  *
140
- * @param {string|null} [ngMsg]
168
+ * @param [ngMsg]
141
169
  * Failure message.
142
170
  *
143
- * @param {(new (...args:any[])=>Error)|null} [ErrorClass=Error]
171
+ * @param [ErrorClass=Error]
144
172
  * Error constructor used when the check fails.
145
173
  *
146
174
  * Supported values:
@@ -150,10 +178,10 @@ var _Contracts = class _Contracts {
150
178
  * - Custom Error subclasses
151
179
  * - `null` to skip throwing and log the failure.
152
180
  *
153
- * @param {Object<string, *>} [eProps]
181
+ * @param [eProps = {}]
154
182
  * Additional properties assigned to the error object.
155
183
  *
156
- * @returns {boolean}
184
+ * @returns
157
185
  * Returns the original condition value.
158
186
  *
159
187
  * @example
@@ -172,8 +200,13 @@ var _Contracts = class _Contracts {
172
200
  * }
173
201
  */
174
202
  static REQUIRE(isOk, ngMsg, ErrorClass = Error, eProps = {}) {
175
- var _a;
176
- return __privateMethod(_a = _Contracts, _Contracts_static, check_fn).call(_a, isOk, "REQUIRE", ngMsg, ErrorClass, eProps);
203
+ return _Contracts.check(
204
+ isOk,
205
+ "REQUIRE",
206
+ ngMsg,
207
+ ErrorClass,
208
+ eProps
209
+ );
177
210
  }
178
211
  /**
179
212
  * Checks a precondition in debug mode only.
@@ -188,19 +221,26 @@ var _Contracts = class _Contracts {
188
221
  * - Validate assumptions during development.
189
222
  * - Perform additional argument checks while debugging.
190
223
  *
191
- * @param {boolean} isOk
224
+ * @param isOk
192
225
  * Condition result to verify.
193
226
  *
194
- * @param {string|null} [ngMsg]
227
+ * @param [ngMsg]
195
228
  * Failure message.
196
229
  *
197
- * @param {(new (...args:any[])=>Error)|null} [ErrorClass=Error]
230
+ * @param [ErrorClass=Error]
198
231
  * Error constructor used when the check fails.
199
232
  *
200
- * @param {Object<string, *>} [eProps]
233
+ * Supported values:
234
+ * - `Error` (default)
235
+ * - `TypeError`
236
+ * - `RangeError`
237
+ * - Custom Error subclasses
238
+ * - `null` to skip throwing and log the failure.
239
+ *
240
+ * @param [eProps = {}]
201
241
  * Additional properties assigned to the error object.
202
242
  *
203
- * @returns {boolean}
243
+ * @returns
204
244
  * Returns the original condition value.
205
245
  *
206
246
  * @example
@@ -210,8 +250,13 @@ var _Contracts = class _Contracts {
210
250
  * );
211
251
  */
212
252
  static REQUIRE_DEBUG(isOk, ngMsg, ErrorClass = Error, eProps = {}) {
213
- var _a;
214
- return __privateMethod(_a = _Contracts, _Contracts_static, checkDebug_fn).call(_a, isOk, "REQUIRE_DEBUG", ngMsg, ErrorClass, eProps);
253
+ return _Contracts.checkDebug(
254
+ isOk,
255
+ "REQUIRE_DEBUG",
256
+ ngMsg,
257
+ ErrorClass,
258
+ eProps
259
+ );
215
260
  }
216
261
  /**
217
262
  * Checks a postcondition after execution.
@@ -227,13 +272,13 @@ var _Contracts = class _Contracts {
227
272
  * - Confirm state changes.
228
273
  * - Verify that processing completed correctly.
229
274
  *
230
- * @param {boolean} isOk
275
+ * @param isOk
231
276
  * Condition result to verify.
232
277
  *
233
- * @param {string|null} [ngMsg]
278
+ * @param [ngMsg]
234
279
  * Failure message.
235
280
  *
236
- * @param {(new (...args:any[])=>Error)|null} [ErrorClass=Error]
281
+ * @param [ErrorClass=Error]
237
282
  * Error constructor used when the check fails.
238
283
  *
239
284
  * Supported values:
@@ -243,10 +288,10 @@ var _Contracts = class _Contracts {
243
288
  * - Custom Error subclasses
244
289
  * - `null` to skip throwing and log the failure.
245
290
  *
246
- * @param {Object<string, *>} [eProps]
291
+ * @param [eProps = {}]
247
292
  * Additional properties assigned to the error object.
248
293
  *
249
- * @returns {boolean}
294
+ * @returns
250
295
  * Returns the original condition value.
251
296
  *
252
297
  * @example
@@ -267,8 +312,13 @@ var _Contracts = class _Contracts {
267
312
  * }
268
313
  */
269
314
  static ENSURE(isOk, ngMsg, ErrorClass = Error, eProps = {}) {
270
- var _a;
271
- return __privateMethod(_a = _Contracts, _Contracts_static, check_fn).call(_a, isOk, "ENSURE", ngMsg, ErrorClass, eProps);
315
+ return _Contracts.check(
316
+ isOk,
317
+ "ENSURE",
318
+ ngMsg,
319
+ ErrorClass,
320
+ eProps
321
+ );
272
322
  }
273
323
  /**
274
324
  * Checks a postcondition in debug mode only.
@@ -283,19 +333,26 @@ var _Contracts = class _Contracts {
283
333
  * - Validate detailed results during development.
284
334
  * - Confirm internal behavior while debugging.
285
335
  *
286
- * @param {boolean} isOk
336
+ * @param isOk
287
337
  * Condition result to verify.
288
338
  *
289
- * @param {string|null} [ngMsg]
339
+ * @param [ngMsg]
290
340
  * Failure message.
291
341
  *
292
- * @param {(new (...args:any[])=>Error)|null} [ErrorClass=Error]
342
+ * @param [ErrorClass=Error]
293
343
  * Error constructor used when the check fails.
294
344
  *
295
- * @param {Object<string, *>} [eProps]
345
+ * Supported values:
346
+ * - `Error` (default)
347
+ * - `TypeError`
348
+ * - `RangeError`
349
+ * - Custom Error subclasses
350
+ * - `null` to skip throwing and log the failure.
351
+ *
352
+ * @param [eProps = {}]
296
353
  * Additional properties assigned to the error object.
297
354
  *
298
- * @returns {boolean}
355
+ * @returns
299
356
  * Returns the original condition value.
300
357
  *
301
358
  * @example
@@ -305,8 +362,13 @@ var _Contracts = class _Contracts {
305
362
  * );
306
363
  */
307
364
  static ENSURE_DEBUG(isOk, ngMsg, ErrorClass = Error, eProps = {}) {
308
- var _a;
309
- return __privateMethod(_a = _Contracts, _Contracts_static, checkDebug_fn).call(_a, isOk, "ENSURE_DEBUG", ngMsg, ErrorClass, eProps);
365
+ return _Contracts.checkDebug(
366
+ isOk,
367
+ "ENSURE_DEBUG",
368
+ ngMsg,
369
+ ErrorClass,
370
+ eProps
371
+ );
310
372
  }
311
373
  /**
312
374
  * Checks an invariant condition.
@@ -322,13 +384,13 @@ var _Contracts = class _Contracts {
322
384
  * In Design by Contract terminology,
323
385
  * INVARIANT represents conditions that must always remain true.
324
386
  *
325
- * @param {boolean} isOk
387
+ * @param isOk
326
388
  * Condition result to verify.
327
389
  *
328
- * @param {string|null} [ngMsg]
390
+ * @param [ngMsg]
329
391
  * Failure message.
330
392
  *
331
- * @param {(new (...args:any[])=>Error)|null} [ErrorClass=Error]
393
+ * @param [ErrorClass=Error]
332
394
  * Error constructor used when the check fails.
333
395
  *
334
396
  * Supported values:
@@ -338,10 +400,10 @@ var _Contracts = class _Contracts {
338
400
  * - Custom Error subclasses
339
401
  * - `null` to skip throwing and log the failure.
340
402
  *
341
- * @param {Object<string, *>} [eProps]
403
+ * @param [eProps = {}]
342
404
  * Additional properties assigned to the error object.
343
405
  *
344
- * @returns {boolean}
406
+ * @returns
345
407
  * Returns the original condition value.
346
408
  *
347
409
  * @example
@@ -359,8 +421,13 @@ var _Contracts = class _Contracts {
359
421
  * }
360
422
  */
361
423
  static INVARIANT(isOk, ngMsg, ErrorClass = Error, eProps = {}) {
362
- var _a;
363
- return __privateMethod(_a = _Contracts, _Contracts_static, check_fn).call(_a, isOk, "INVARIANT", ngMsg, ErrorClass, eProps);
424
+ return _Contracts.check(
425
+ isOk,
426
+ "INVARIANT",
427
+ ngMsg,
428
+ ErrorClass,
429
+ eProps
430
+ );
364
431
  }
365
432
  /**
366
433
  * Checks an invariant condition in debug mode only.
@@ -375,19 +442,26 @@ var _Contracts = class _Contracts {
375
442
  * - Validate object consistency during development.
376
443
  * - Detect unexpected state changes while debugging.
377
444
  *
378
- * @param {boolean} isOk
445
+ * @param isOk
379
446
  * Condition result to verify.
380
447
  *
381
- * @param {string|null} [ngMsg]
448
+ * @param [ngMsg]
382
449
  * Failure message.
383
450
  *
384
- * @param {(new (...args:any[])=>Error)|null} [ErrorClass=Error]
451
+ * @param [ErrorClass=Error]
385
452
  * Error constructor used when the check fails.
386
453
  *
387
- * @param {Object<string, *>} [eProps]
454
+ * Supported values:
455
+ * - `Error` (default)
456
+ * - `TypeError`
457
+ * - `RangeError`
458
+ * - Custom Error subclasses
459
+ * - `null` to skip throwing and log the failure.
460
+ *
461
+ * @param [eProps = {}]
388
462
  * Additional properties assigned to the error object.
389
463
  *
390
- * @returns {boolean}
464
+ * @returns
391
465
  * Returns the original condition value.
392
466
  *
393
467
  * @example
@@ -397,36 +471,106 @@ var _Contracts = class _Contracts {
397
471
  * );
398
472
  */
399
473
  static INVARIANT_DEBUG(isOk, ngMsg, ErrorClass = Error, eProps = {}) {
400
- var _a;
401
- return __privateMethod(_a = _Contracts, _Contracts_static, checkDebug_fn).call(_a, isOk, "INVARIANT_DEBUG", ngMsg, ErrorClass, eProps);
474
+ return _Contracts.checkDebug(
475
+ isOk,
476
+ "INVARIANT_DEBUG",
477
+ ngMsg,
478
+ ErrorClass,
479
+ eProps
480
+ );
402
481
  }
403
- };
404
- _Contracts_static = new WeakSet();
405
- check_fn = function(isOk, prefix = "CHECK", ngMsg = "", ErrorClass = Error, eProps = {}) {
406
- if (!isOk) {
407
- const msg = `[${prefix}] ${ngMsg ?? ""}`;
408
- if (ErrorClass) {
409
- throw Object.assign(
410
- new ErrorClass(msg),
411
- eProps
412
- );
413
- }
414
- if (ngMsg) {
415
- console.error(msg, eProps);
482
+ /**
483
+ * Core contract evaluation logic.
484
+ *
485
+ * Evaluates a condition and handles failures.
486
+ * All public contract methods delegate to this method.
487
+ *
488
+ * Behavior:
489
+ * - When the condition is true, returns the original value.
490
+ * - When the condition is false and ErrorClass is specified,
491
+ * throws an instance of the specified error class.
492
+ * - When ErrorClass is null,
493
+ * logs the failure message instead of throwing.
494
+ *
495
+ * @param isOk
496
+ * Condition result.
497
+ *
498
+ * @param prefix
499
+ * Contract type prefix used in the error message.
500
+ *
501
+ * @param ngMsg
502
+ * Failure message.
503
+ *
504
+ * @param ErrorClass
505
+ * Error constructor.
506
+ *
507
+ * @param eProps
508
+ * Additional properties assigned to the error object.
509
+ *
510
+ * @returns
511
+ * Returns the original condition value.
512
+ *
513
+ */
514
+ static check(isOk, prefix = "CHECK", ngMsg = "", ErrorClass = Error, eProps = {}) {
515
+ if (!isOk) {
516
+ const msg = `[${prefix}] ${ngMsg ?? ""}`;
517
+ if (ErrorClass) {
518
+ throw Object.assign(
519
+ new ErrorClass(msg),
520
+ eProps
521
+ );
522
+ }
523
+ if (ngMsg) {
524
+ _Contracts.getLogger().error(msg, eProps);
525
+ }
416
526
  }
527
+ return isOk;
528
+ }
529
+ /**
530
+ * Debug-only contract evaluation logic.
531
+ *
532
+ * Executes contract validation only when DEBUG_MODE
533
+ * is enabled.
534
+ *
535
+ * When DEBUG_MODE is disabled,
536
+ * this method returns the original condition value
537
+ * without performing any validation.
538
+ *
539
+ * @param isOk
540
+ * Condition result.
541
+ *
542
+ * @param prefix
543
+ * Contract type prefix used in the error message.
544
+ *
545
+ * @param ngMsg
546
+ * Failure message.
547
+ *
548
+ * @param ErrorClass
549
+ * Error constructor.
550
+ *
551
+ * @param eProps
552
+ * Additional properties assigned to the error object.
553
+ *
554
+ * @returns
555
+ * Returns the original condition value.
556
+ *
557
+ */
558
+ static checkDebug(isOk, prefix = "CHECK", ngMsg = "", ErrorClass = Error, eProps = {}) {
559
+ return _Contracts.DEBUG_MODE ? _Contracts.check(
560
+ isOk,
561
+ prefix,
562
+ ngMsg,
563
+ ErrorClass,
564
+ eProps
565
+ ) : isOk;
566
+ }
567
+ /** Get logger */
568
+ static getLogger() {
569
+ return _Contracts.logger ?? console;
417
570
  }
418
- return isOk;
419
- };
420
- checkDebug_fn = function(isOk, prefix = "CHECK_DEBUG", ngMsg = "", ErrorClass = Error, eProps = {}) {
421
- var _a;
422
- return _Contracts.DEBUG_MODE ? __privateMethod(_a = _Contracts, _Contracts_static, check_fn).call(_a, isOk, prefix, ngMsg, ErrorClass, eProps) : isOk;
423
571
  };
424
- __privateAdd(_Contracts, _Contracts_static);
425
- /** @type {boolean} Debug mode state */
426
- __publicField(_Contracts, "DEBUG_MODE", false);
427
- var Contracts = _Contracts;
428
572
 
429
- // src/index.js
573
+ // src/index.ts
430
574
  var index_default = Contracts;
431
575
  export {
432
576
  Contracts,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@ayapapa-npm/contracts-js",
3
- "version": "0.2.2",
3
+ "version": "0.2.4",
4
4
  "description": "A lightweight Design by Contract library for JavaScript.",
5
5
  "license": "MIT",
6
6
  "author": "ayapapa",
@@ -17,15 +17,19 @@
17
17
  "dist"
18
18
  ],
19
19
  "scripts": {
20
- "build": "tsup src/index.js --format esm,cjs --out-dir dist --clean --dts",
20
+ "build": "tsup src/index.ts --format esm,cjs --out-dir dist --clean --dts",
21
+ "typecheck": "tsc --noEmit",
21
22
  "test": "vitest run",
22
23
  "test:debug": "vitest --inspect-brk --run",
23
24
  "test:watch": "vitest",
24
- "prepublishOnly": "npm run build && npm test"
25
+ "check": "npm run typecheck && npm run build && npm run test",
26
+ "release:check": "npm run typecheck && npm run build && npm run test",
27
+ "prepublishOnly": "npm run release:check"
25
28
  },
26
29
  "devDependencies": {
30
+ "@types/node": "^26.2.0",
27
31
  "tsup": "^8.5.1",
28
- "typescript": "^5.7.3",
32
+ "typescript": "^5.9.3",
29
33
  "vitest": "^4.1.10"
30
34
  },
31
35
  "allowScripts": {