@ayapapa-npm/contracts-js 0.2.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/LICENSE +21 -0
- package/README.md +56 -0
- package/dist/index.cjs +186 -0
- package/dist/index.d.cts +154 -0
- package/dist/index.d.ts +154 -0
- package/dist/index.js +162 -0
- package/package.json +34 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 ayapapa
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
# contracts-js
|
|
2
|
+
A lightweight Design by Contract library for JavaScript.
|
|
3
|
+
|
|
4
|
+
## Installation
|
|
5
|
+
|
|
6
|
+
### Install directly from GitHub Release:
|
|
7
|
+
Available now.
|
|
8
|
+
```bash
|
|
9
|
+
npm install https://github.com/ayapapa/contracts-js/releases/download/0.2.0/contracts-js-0.2.0.tgz
|
|
10
|
+
```
|
|
11
|
+
If you see the error:
|
|
12
|
+
```
|
|
13
|
+
npm error code EALLOWREMOTE
|
|
14
|
+
npm error Fetching packages of type "remote" have been disabled
|
|
15
|
+
```
|
|
16
|
+
allow remote packages:
|
|
17
|
+
```bash
|
|
18
|
+
npm config set allow-remote all
|
|
19
|
+
```
|
|
20
|
+
Then run the install command again:
|
|
21
|
+
```bash
|
|
22
|
+
npm install https://github.com/ayapapa/contracts-js/releases/download/0.2.0/contracts-js-0.2.0.tgz
|
|
23
|
+
```
|
|
24
|
+
|
|
25
|
+
### Install from npm registry
|
|
26
|
+
Coming soon: installable with `npm install contracts-js`.
|
|
27
|
+
|
|
28
|
+
## Usage
|
|
29
|
+
|
|
30
|
+
```javascript
|
|
31
|
+
import { Contracts } from 'contracts-js';
|
|
32
|
+
// or,
|
|
33
|
+
// import Contracts from 'contracts-jss';
|
|
34
|
+
|
|
35
|
+
// In the case of CommonJS,
|
|
36
|
+
// const { Contracts } = require('contracts-js');
|
|
37
|
+
// or,
|
|
38
|
+
// const Contracts = require('contracts-js').Contracts;
|
|
39
|
+
|
|
40
|
+
// Enable debug mode
|
|
41
|
+
Contracts.setConfig({ debug: true });
|
|
42
|
+
|
|
43
|
+
// Check a precondition (e.g., argument validation)
|
|
44
|
+
function divide(a, b) {
|
|
45
|
+
Contracts.REQUIRE(b !== 0, 'Divisor cannot be zero');
|
|
46
|
+
return a / b;
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
// Check a postcondition (e.g., return value validation)
|
|
50
|
+
function getPositiveNumber(x) {
|
|
51
|
+
Contracts.REQUIRE(x > 0, 'Input must be positive');
|
|
52
|
+
const result = x * 2;
|
|
53
|
+
Contracts ENSURE(result > 0, 'Result must be positive');
|
|
54
|
+
return result;
|
|
55
|
+
}
|
|
56
|
+
```
|
package/dist/index.cjs
ADDED
|
@@ -0,0 +1,186 @@
|
|
|
1
|
+
var __defProp = Object.defineProperty;
|
|
2
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
3
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
4
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
5
|
+
var __typeError = (msg) => {
|
|
6
|
+
throw TypeError(msg);
|
|
7
|
+
};
|
|
8
|
+
var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
|
|
9
|
+
var __export = (target, all) => {
|
|
10
|
+
for (var name in all)
|
|
11
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
12
|
+
};
|
|
13
|
+
var __copyProps = (to, from, except, desc) => {
|
|
14
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
15
|
+
for (let key of __getOwnPropNames(from))
|
|
16
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
17
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
18
|
+
}
|
|
19
|
+
return to;
|
|
20
|
+
};
|
|
21
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
22
|
+
var __publicField = (obj, key, value) => __defNormalProp(obj, typeof key !== "symbol" ? key + "" : key, value);
|
|
23
|
+
var __accessCheck = (obj, member, msg) => member.has(obj) || __typeError("Cannot " + msg);
|
|
24
|
+
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);
|
|
25
|
+
var __privateMethod = (obj, member, method) => (__accessCheck(obj, member, "access private method"), method);
|
|
26
|
+
|
|
27
|
+
// src/index.js
|
|
28
|
+
var index_exports = {};
|
|
29
|
+
__export(index_exports, {
|
|
30
|
+
Contracts: () => Contracts,
|
|
31
|
+
default: () => index_default
|
|
32
|
+
});
|
|
33
|
+
module.exports = __toCommonJS(index_exports);
|
|
34
|
+
|
|
35
|
+
// src/lib/Contracts.js
|
|
36
|
+
var _Contracts_static, check_fn, checkDebug_fn;
|
|
37
|
+
var _Contracts = class _Contracts {
|
|
38
|
+
/**
|
|
39
|
+
* Configures the contract checking behavior.
|
|
40
|
+
* @param {{debug?: boolean}} config Configuration object
|
|
41
|
+
*/
|
|
42
|
+
static setConfig(config) {
|
|
43
|
+
if (config) {
|
|
44
|
+
_Contracts.DEBUG_MODE = config.debug || false;
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
/**
|
|
48
|
+
* Checks a general condition.
|
|
49
|
+
* If `isOk` is false and an `error` class is provided, throws an exception with `ngMsg`.
|
|
50
|
+
* Otherwise, logs the error message to `console.error`.
|
|
51
|
+
* @param {boolean} isOk The result of the condition check.
|
|
52
|
+
* @param {string} [ngMsg] The error message.
|
|
53
|
+
* @param {new (...args: any[]) => Error} [error=Error] The error class to throw (Error or its subclass).
|
|
54
|
+
* @param {Object<string, *>} [eProps={}] Properties to assign to the error instance.
|
|
55
|
+
* @return {boolean} The evaluation result (returns `isOk` as is).
|
|
56
|
+
*/
|
|
57
|
+
static VERIFY(isOk, ngMsg, error = Error, eProps = {}) {
|
|
58
|
+
var _a;
|
|
59
|
+
return __privateMethod(_a = _Contracts, _Contracts_static, check_fn).call(_a, isOk, "VERIFY", ngMsg, error, eProps);
|
|
60
|
+
}
|
|
61
|
+
/**
|
|
62
|
+
* Checks a general condition (for debugging: outputs execution message but throws no exception in debug mode).
|
|
63
|
+
* If the result is false and an `error` class is provided, throws an exception.
|
|
64
|
+
* Otherwise, logs the message to `console.error`.
|
|
65
|
+
* @param {boolean} isOk The result of the condition check.
|
|
66
|
+
* @param {string} [ngMsg] The error message.
|
|
67
|
+
* @param {new (...args: any[]) => Error} [error] The error class to throw (Error or its subclass).
|
|
68
|
+
* @param {Object<string, *>} [eProps={}] Properties to assign to the error instance.
|
|
69
|
+
* @return {boolean} The evaluation result (returns `isOk` as is).
|
|
70
|
+
*/
|
|
71
|
+
static VERIFY_DEBUG(isOk, ngMsg, error = Error, eProps = {}) {
|
|
72
|
+
var _a;
|
|
73
|
+
return __privateMethod(_a = _Contracts, _Contracts_static, checkDebug_fn).call(_a, isOk, "VERIFY_DEBUG", ngMsg, error, eProps);
|
|
74
|
+
}
|
|
75
|
+
/**
|
|
76
|
+
* Checks a precondition.
|
|
77
|
+
* Use this to verify conditions that must be true before a function executes (e.g., argument validation).
|
|
78
|
+
* If `isOk` is false and an `error` class is provided, throws an exception with `ngMsg`.
|
|
79
|
+
* Otherwise, logs the error message to `console.error`.
|
|
80
|
+
* @param {boolean} isOk The result of the condition check.
|
|
81
|
+
* @param {string} [ngMsg] The error message.
|
|
82
|
+
* @param {new (...args: any[]) => Error} [error=Error] The error class to throw (Error or its subclass).
|
|
83
|
+
* @param {Object<string, *>} [eProps={}] Properties to assign to the error instance.
|
|
84
|
+
* @return {boolean} The evaluation result (returns `isOk` as is).
|
|
85
|
+
*/
|
|
86
|
+
static REQUIRE(isOk, ngMsg, error = Error, eProps = {}) {
|
|
87
|
+
var _a;
|
|
88
|
+
return __privateMethod(_a = _Contracts, _Contracts_static, check_fn).call(_a, isOk, "REQUIRE", ngMsg, error, eProps);
|
|
89
|
+
}
|
|
90
|
+
/**
|
|
91
|
+
* Checks a precondition (for debugging: outputs execution message but throws no exception in debug mode).
|
|
92
|
+
* If the result is false and an `error` class is provided, throws an exception.
|
|
93
|
+
* Otherwise, logs the message to `console.error`.
|
|
94
|
+
* @param {boolean} isOk The result of the condition check.
|
|
95
|
+
* @param {string} [ngMsg] The error message.
|
|
96
|
+
* @param {new (...args: any[]) => Error} [error] The error class to throw (Error or its subclass).
|
|
97
|
+
* @param {Object<string, *>} [eProps={}] Properties to assign to the error instance.
|
|
98
|
+
* @return {boolean} The evaluation result (returns `isOk` as is).
|
|
99
|
+
*/
|
|
100
|
+
static REQUIRE_DEBUG(isOk, ngMsg, error = Error, eProps = {}) {
|
|
101
|
+
var _a;
|
|
102
|
+
return __privateMethod(_a = _Contracts, _Contracts_static, checkDebug_fn).call(_a, isOk, "REQUIRE_DEBUG", ngMsg, error, eProps);
|
|
103
|
+
}
|
|
104
|
+
/**
|
|
105
|
+
* Checks a postcondition.
|
|
106
|
+
* Use this to verify conditions that must be true after a function executes (e.g., return value or object state).
|
|
107
|
+
* If `isOk` is false and an `error` class is provided, throws an exception with `ngMsg`.
|
|
108
|
+
* Otherwise, logs the error message to `console.error`.
|
|
109
|
+
* @param {boolean} isOk The result of the condition check.
|
|
110
|
+
* @param {string} [ngMsg] The error message.
|
|
111
|
+
* @param {new (...args: any[]) => Error} [error=Error] The error class to throw (Error or its subclass).
|
|
112
|
+
* @param {Object<string, *>} [eProps={}] Properties to assign to the error instance.
|
|
113
|
+
* @return {boolean} The evaluation result (returns `isOk` as is).
|
|
114
|
+
*/
|
|
115
|
+
static ENSURE(isOk, ngMsg, error = Error, eProps = {}) {
|
|
116
|
+
var _a;
|
|
117
|
+
return __privateMethod(_a = _Contracts, _Contracts_static, check_fn).call(_a, isOk, "ENSURE", ngMsg, error, eProps);
|
|
118
|
+
}
|
|
119
|
+
/**
|
|
120
|
+
* Checks a postcondition (for debugging: outputs execution message but throws no exception in debug mode).
|
|
121
|
+
* If the result is false and an `error` class is provided, throws an exception.
|
|
122
|
+
* Otherwise, logs the message to `console.error`.
|
|
123
|
+
* @param {boolean} isOk The result of the condition check.
|
|
124
|
+
* @param {string} [ngMsg] The error message.
|
|
125
|
+
* @param {new (...args: any[]) => Error} [error] The error class to throw (Error or its subclass).
|
|
126
|
+
* @param {Object<string, *>} [eProps={}] Properties to assign to the error instance.
|
|
127
|
+
* @return {boolean} The evaluation result (returns `isOk` as is).
|
|
128
|
+
*/
|
|
129
|
+
static ENSURE_DEBUG(isOk, ngMsg, error = Error, eProps = {}) {
|
|
130
|
+
var _a;
|
|
131
|
+
return __privateMethod(_a = _Contracts, _Contracts_static, checkDebug_fn).call(_a, isOk, "ENSURE_DEBUG", ngMsg, error, eProps);
|
|
132
|
+
}
|
|
133
|
+
/**
|
|
134
|
+
* Checks an invariant.
|
|
135
|
+
* Use this to verify conditions that must remain true before and after a function call (e.g., object integrity).
|
|
136
|
+
* If `isOk` is false and an `error` class is provided, throws an exception with `ngMsg`.
|
|
137
|
+
* Otherwise, logs the error message to `console.error`.
|
|
138
|
+
* @param {boolean} isOk The result of the condition check.
|
|
139
|
+
* @param {string} [ngMsg] The error message.
|
|
140
|
+
* @param {new (...args: any[]) => Error} [error=Error] The error class to throw (Error or its subclass).
|
|
141
|
+
* @param {Object<string, *>} [eProps={}] Properties to assign to the error instance.
|
|
142
|
+
* @return {boolean} The evaluation result (returns `isOk` as is).
|
|
143
|
+
*/
|
|
144
|
+
static INVARIANT(isOk, ngMsg, error = Error, eProps = {}) {
|
|
145
|
+
var _a;
|
|
146
|
+
return __privateMethod(_a = _Contracts, _Contracts_static, check_fn).call(_a, isOk, "INVARIANT", ngMsg, error, eProps);
|
|
147
|
+
}
|
|
148
|
+
/**
|
|
149
|
+
* Checks an invariant (for debugging: outputs execution message but throws no exception in debug mode).
|
|
150
|
+
* If the result is false and an `error` class is provided, throws an exception.
|
|
151
|
+
* Otherwise, logs the message to `console.error`.
|
|
152
|
+
* @param {boolean} isOk The result of the condition check.
|
|
153
|
+
* @param {string} [ngMsg] The error message.
|
|
154
|
+
* @param {new (...args: any[]) => Error} [error] The error class to throw (Error or its subclass).
|
|
155
|
+
* @param {Object<string, *>} [eProps={}] Properties to assign to the error instance.
|
|
156
|
+
* @return {boolean} The evaluation result (returns `isOk` as is).
|
|
157
|
+
*/
|
|
158
|
+
static INVARIANT_DEBUG(isOk, ngMsg, error = Error, eProps = {}) {
|
|
159
|
+
var _a;
|
|
160
|
+
return __privateMethod(_a = _Contracts, _Contracts_static, checkDebug_fn).call(_a, isOk, "INVARIANT_DEBUG", ngMsg, error, eProps);
|
|
161
|
+
}
|
|
162
|
+
};
|
|
163
|
+
_Contracts_static = new WeakSet();
|
|
164
|
+
check_fn = function(isOk, prefix = "CHECK", ngMsg = "", error = Error, eProps = {}) {
|
|
165
|
+
if (!isOk) {
|
|
166
|
+
const msg = `[${prefix}]${ngMsg ?? ""}`;
|
|
167
|
+
if (error) throw Object.assign(new error(msg), eProps);
|
|
168
|
+
if (ngMsg) console.error(msg, eProps);
|
|
169
|
+
}
|
|
170
|
+
return isOk;
|
|
171
|
+
};
|
|
172
|
+
checkDebug_fn = function(isOk, prefix = "CHECK_DEBUG", ngMsg = "", error = Error, eProps = {}) {
|
|
173
|
+
var _a;
|
|
174
|
+
return _Contracts.DEBUG_MODE ? __privateMethod(_a = _Contracts, _Contracts_static, check_fn).call(_a, isOk, prefix, ngMsg, error, eProps) : isOk;
|
|
175
|
+
};
|
|
176
|
+
__privateAdd(_Contracts, _Contracts_static);
|
|
177
|
+
// @type {boolean}
|
|
178
|
+
__publicField(_Contracts, "DEBUG_MODE", false);
|
|
179
|
+
var Contracts = _Contracts;
|
|
180
|
+
|
|
181
|
+
// src/index.js
|
|
182
|
+
var index_default = Contracts;
|
|
183
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
184
|
+
0 && (module.exports = {
|
|
185
|
+
Contracts
|
|
186
|
+
});
|
package/dist/index.d.cts
ADDED
|
@@ -0,0 +1,154 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Contract checking utilities.
|
|
3
|
+
* Provides methods to verify preconditions, postconditions, invariants, and general conditions.
|
|
4
|
+
*/
|
|
5
|
+
declare class Contracts {
|
|
6
|
+
static DEBUG_MODE: boolean;
|
|
7
|
+
/**
|
|
8
|
+
* Configures the contract checking behavior.
|
|
9
|
+
* @param {{debug?: boolean}} config Configuration object
|
|
10
|
+
*/
|
|
11
|
+
static setConfig(config: {
|
|
12
|
+
debug?: boolean;
|
|
13
|
+
}): void;
|
|
14
|
+
/**
|
|
15
|
+
* Checks a general condition.
|
|
16
|
+
* If `isOk` is false and an `error` class is provided, throws an exception with `ngMsg`.
|
|
17
|
+
* Otherwise, logs the error message to `console.error`.
|
|
18
|
+
* @param {boolean} isOk The result of the condition check.
|
|
19
|
+
* @param {string} [ngMsg] The error message.
|
|
20
|
+
* @param {new (...args: any[]) => Error} [error=Error] The error class to throw (Error or its subclass).
|
|
21
|
+
* @param {Object<string, *>} [eProps={}] Properties to assign to the error instance.
|
|
22
|
+
* @return {boolean} The evaluation result (returns `isOk` as is).
|
|
23
|
+
*/
|
|
24
|
+
static VERIFY(isOk: boolean, ngMsg?: string, error?: new (...args: any[]) => Error, eProps?: {
|
|
25
|
+
[x: string]: any;
|
|
26
|
+
}): boolean;
|
|
27
|
+
/**
|
|
28
|
+
* Checks a general condition (for debugging: outputs execution message but throws no exception in debug mode).
|
|
29
|
+
* If the result is false and an `error` class is provided, throws an exception.
|
|
30
|
+
* Otherwise, logs the message to `console.error`.
|
|
31
|
+
* @param {boolean} isOk The result of the condition check.
|
|
32
|
+
* @param {string} [ngMsg] The error message.
|
|
33
|
+
* @param {new (...args: any[]) => Error} [error] The error class to throw (Error or its subclass).
|
|
34
|
+
* @param {Object<string, *>} [eProps={}] Properties to assign to the error instance.
|
|
35
|
+
* @return {boolean} The evaluation result (returns `isOk` as is).
|
|
36
|
+
*/
|
|
37
|
+
static VERIFY_DEBUG(isOk: boolean, ngMsg?: string, error?: new (...args: any[]) => Error, eProps?: {
|
|
38
|
+
[x: string]: any;
|
|
39
|
+
}): boolean;
|
|
40
|
+
/**
|
|
41
|
+
* Checks a precondition.
|
|
42
|
+
* Use this to verify conditions that must be true before a function executes (e.g., argument validation).
|
|
43
|
+
* If `isOk` is false and an `error` class is provided, throws an exception with `ngMsg`.
|
|
44
|
+
* Otherwise, logs the error message to `console.error`.
|
|
45
|
+
* @param {boolean} isOk The result of the condition check.
|
|
46
|
+
* @param {string} [ngMsg] The error message.
|
|
47
|
+
* @param {new (...args: any[]) => Error} [error=Error] The error class to throw (Error or its subclass).
|
|
48
|
+
* @param {Object<string, *>} [eProps={}] Properties to assign to the error instance.
|
|
49
|
+
* @return {boolean} The evaluation result (returns `isOk` as is).
|
|
50
|
+
*/
|
|
51
|
+
static REQUIRE(isOk: boolean, ngMsg?: string, error?: new (...args: any[]) => Error, eProps?: {
|
|
52
|
+
[x: string]: any;
|
|
53
|
+
}): boolean;
|
|
54
|
+
/**
|
|
55
|
+
* Checks a precondition (for debugging: outputs execution message but throws no exception in debug mode).
|
|
56
|
+
* If the result is false and an `error` class is provided, throws an exception.
|
|
57
|
+
* Otherwise, logs the message to `console.error`.
|
|
58
|
+
* @param {boolean} isOk The result of the condition check.
|
|
59
|
+
* @param {string} [ngMsg] The error message.
|
|
60
|
+
* @param {new (...args: any[]) => Error} [error] The error class to throw (Error or its subclass).
|
|
61
|
+
* @param {Object<string, *>} [eProps={}] Properties to assign to the error instance.
|
|
62
|
+
* @return {boolean} The evaluation result (returns `isOk` as is).
|
|
63
|
+
*/
|
|
64
|
+
static REQUIRE_DEBUG(isOk: boolean, ngMsg?: string, error?: new (...args: any[]) => Error, eProps?: {
|
|
65
|
+
[x: string]: any;
|
|
66
|
+
}): boolean;
|
|
67
|
+
/**
|
|
68
|
+
* Checks a postcondition.
|
|
69
|
+
* Use this to verify conditions that must be true after a function executes (e.g., return value or object state).
|
|
70
|
+
* If `isOk` is false and an `error` class is provided, throws an exception with `ngMsg`.
|
|
71
|
+
* Otherwise, logs the error message to `console.error`.
|
|
72
|
+
* @param {boolean} isOk The result of the condition check.
|
|
73
|
+
* @param {string} [ngMsg] The error message.
|
|
74
|
+
* @param {new (...args: any[]) => Error} [error=Error] The error class to throw (Error or its subclass).
|
|
75
|
+
* @param {Object<string, *>} [eProps={}] Properties to assign to the error instance.
|
|
76
|
+
* @return {boolean} The evaluation result (returns `isOk` as is).
|
|
77
|
+
*/
|
|
78
|
+
static ENSURE(isOk: boolean, ngMsg?: string, error?: new (...args: any[]) => Error, eProps?: {
|
|
79
|
+
[x: string]: any;
|
|
80
|
+
}): boolean;
|
|
81
|
+
/**
|
|
82
|
+
* Checks a postcondition (for debugging: outputs execution message but throws no exception in debug mode).
|
|
83
|
+
* If the result is false and an `error` class is provided, throws an exception.
|
|
84
|
+
* Otherwise, logs the message to `console.error`.
|
|
85
|
+
* @param {boolean} isOk The result of the condition check.
|
|
86
|
+
* @param {string} [ngMsg] The error message.
|
|
87
|
+
* @param {new (...args: any[]) => Error} [error] The error class to throw (Error or its subclass).
|
|
88
|
+
* @param {Object<string, *>} [eProps={}] Properties to assign to the error instance.
|
|
89
|
+
* @return {boolean} The evaluation result (returns `isOk` as is).
|
|
90
|
+
*/
|
|
91
|
+
static ENSURE_DEBUG(isOk: boolean, ngMsg?: string, error?: new (...args: any[]) => Error, eProps?: {
|
|
92
|
+
[x: string]: any;
|
|
93
|
+
}): boolean;
|
|
94
|
+
/**
|
|
95
|
+
* Checks an invariant.
|
|
96
|
+
* Use this to verify conditions that must remain true before and after a function call (e.g., object integrity).
|
|
97
|
+
* If `isOk` is false and an `error` class is provided, throws an exception with `ngMsg`.
|
|
98
|
+
* Otherwise, logs the error message to `console.error`.
|
|
99
|
+
* @param {boolean} isOk The result of the condition check.
|
|
100
|
+
* @param {string} [ngMsg] The error message.
|
|
101
|
+
* @param {new (...args: any[]) => Error} [error=Error] The error class to throw (Error or its subclass).
|
|
102
|
+
* @param {Object<string, *>} [eProps={}] Properties to assign to the error instance.
|
|
103
|
+
* @return {boolean} The evaluation result (returns `isOk` as is).
|
|
104
|
+
*/
|
|
105
|
+
static INVARIANT(isOk: boolean, ngMsg?: string, error?: new (...args: any[]) => Error, eProps?: {
|
|
106
|
+
[x: string]: any;
|
|
107
|
+
}): boolean;
|
|
108
|
+
/**
|
|
109
|
+
* Checks an invariant (for debugging: outputs execution message but throws no exception in debug mode).
|
|
110
|
+
* If the result is false and an `error` class is provided, throws an exception.
|
|
111
|
+
* Otherwise, logs the message to `console.error`.
|
|
112
|
+
* @param {boolean} isOk The result of the condition check.
|
|
113
|
+
* @param {string} [ngMsg] The error message.
|
|
114
|
+
* @param {new (...args: any[]) => Error} [error] The error class to throw (Error or its subclass).
|
|
115
|
+
* @param {Object<string, *>} [eProps={}] Properties to assign to the error instance.
|
|
116
|
+
* @return {boolean} The evaluation result (returns `isOk` as is).
|
|
117
|
+
*/
|
|
118
|
+
static INVARIANT_DEBUG(isOk: boolean, ngMsg?: string, error?: new (...args: any[]) => Error, eProps?: {
|
|
119
|
+
[x: string]: any;
|
|
120
|
+
}): boolean;
|
|
121
|
+
/**
|
|
122
|
+
* Private methods
|
|
123
|
+
*/
|
|
124
|
+
/**
|
|
125
|
+
* Core evaluation logic.
|
|
126
|
+
* If the result is false and an `error` class is provided, throws an exception.
|
|
127
|
+
* Otherwise, logs the message to `console.error`.
|
|
128
|
+
* @param {boolean} isOk The result of the condition check.
|
|
129
|
+
* @param {string} [prefix] Prefix for the error message.
|
|
130
|
+
* @param {string} [ngMsg] The error message.
|
|
131
|
+
* @param {new (...args: any[]) => Error} [error] The error class to throw (Error or its subclass).
|
|
132
|
+
* @param {Object<string, *>} [eProps={}] Properties to assign to the error instance.
|
|
133
|
+
* @return {boolean} The evaluation result (returns `isOk` as is).
|
|
134
|
+
*/
|
|
135
|
+
static "__#1@#check"(isOk: boolean, prefix?: string, ngMsg?: string, error?: new (...args: any[]) => Error, eProps?: {
|
|
136
|
+
[x: string]: any;
|
|
137
|
+
}): boolean;
|
|
138
|
+
/**
|
|
139
|
+
* Debug evaluation logic (no message output or exception thrown unless in debug mode).
|
|
140
|
+
* If the result is false and an `error` class is provided, throws an exception.
|
|
141
|
+
* Otherwise, logs the message to `console.error`.
|
|
142
|
+
* @param {boolean} isOk The result of the condition check.
|
|
143
|
+
* @param {string} [prefix] Prefix for the error message.
|
|
144
|
+
* @param {string} [ngMsg] The error message.
|
|
145
|
+
* @param {new (...args: any[]) => Error} [error] The error class to throw (Error or its subclass).
|
|
146
|
+
* @param {Object<string, *>} [eProps={}] Properties to assign to the error instance.
|
|
147
|
+
* @return {boolean} The evaluation result (returns `isOk` as is).
|
|
148
|
+
*/
|
|
149
|
+
static "__#1@#checkDebug"(isOk: boolean, prefix?: string, ngMsg?: string, error?: new (...args: any[]) => Error, eProps?: {
|
|
150
|
+
[x: string]: any;
|
|
151
|
+
}): boolean;
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
export { Contracts, Contracts as default };
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,154 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Contract checking utilities.
|
|
3
|
+
* Provides methods to verify preconditions, postconditions, invariants, and general conditions.
|
|
4
|
+
*/
|
|
5
|
+
declare class Contracts {
|
|
6
|
+
static DEBUG_MODE: boolean;
|
|
7
|
+
/**
|
|
8
|
+
* Configures the contract checking behavior.
|
|
9
|
+
* @param {{debug?: boolean}} config Configuration object
|
|
10
|
+
*/
|
|
11
|
+
static setConfig(config: {
|
|
12
|
+
debug?: boolean;
|
|
13
|
+
}): void;
|
|
14
|
+
/**
|
|
15
|
+
* Checks a general condition.
|
|
16
|
+
* If `isOk` is false and an `error` class is provided, throws an exception with `ngMsg`.
|
|
17
|
+
* Otherwise, logs the error message to `console.error`.
|
|
18
|
+
* @param {boolean} isOk The result of the condition check.
|
|
19
|
+
* @param {string} [ngMsg] The error message.
|
|
20
|
+
* @param {new (...args: any[]) => Error} [error=Error] The error class to throw (Error or its subclass).
|
|
21
|
+
* @param {Object<string, *>} [eProps={}] Properties to assign to the error instance.
|
|
22
|
+
* @return {boolean} The evaluation result (returns `isOk` as is).
|
|
23
|
+
*/
|
|
24
|
+
static VERIFY(isOk: boolean, ngMsg?: string, error?: new (...args: any[]) => Error, eProps?: {
|
|
25
|
+
[x: string]: any;
|
|
26
|
+
}): boolean;
|
|
27
|
+
/**
|
|
28
|
+
* Checks a general condition (for debugging: outputs execution message but throws no exception in debug mode).
|
|
29
|
+
* If the result is false and an `error` class is provided, throws an exception.
|
|
30
|
+
* Otherwise, logs the message to `console.error`.
|
|
31
|
+
* @param {boolean} isOk The result of the condition check.
|
|
32
|
+
* @param {string} [ngMsg] The error message.
|
|
33
|
+
* @param {new (...args: any[]) => Error} [error] The error class to throw (Error or its subclass).
|
|
34
|
+
* @param {Object<string, *>} [eProps={}] Properties to assign to the error instance.
|
|
35
|
+
* @return {boolean} The evaluation result (returns `isOk` as is).
|
|
36
|
+
*/
|
|
37
|
+
static VERIFY_DEBUG(isOk: boolean, ngMsg?: string, error?: new (...args: any[]) => Error, eProps?: {
|
|
38
|
+
[x: string]: any;
|
|
39
|
+
}): boolean;
|
|
40
|
+
/**
|
|
41
|
+
* Checks a precondition.
|
|
42
|
+
* Use this to verify conditions that must be true before a function executes (e.g., argument validation).
|
|
43
|
+
* If `isOk` is false and an `error` class is provided, throws an exception with `ngMsg`.
|
|
44
|
+
* Otherwise, logs the error message to `console.error`.
|
|
45
|
+
* @param {boolean} isOk The result of the condition check.
|
|
46
|
+
* @param {string} [ngMsg] The error message.
|
|
47
|
+
* @param {new (...args: any[]) => Error} [error=Error] The error class to throw (Error or its subclass).
|
|
48
|
+
* @param {Object<string, *>} [eProps={}] Properties to assign to the error instance.
|
|
49
|
+
* @return {boolean} The evaluation result (returns `isOk` as is).
|
|
50
|
+
*/
|
|
51
|
+
static REQUIRE(isOk: boolean, ngMsg?: string, error?: new (...args: any[]) => Error, eProps?: {
|
|
52
|
+
[x: string]: any;
|
|
53
|
+
}): boolean;
|
|
54
|
+
/**
|
|
55
|
+
* Checks a precondition (for debugging: outputs execution message but throws no exception in debug mode).
|
|
56
|
+
* If the result is false and an `error` class is provided, throws an exception.
|
|
57
|
+
* Otherwise, logs the message to `console.error`.
|
|
58
|
+
* @param {boolean} isOk The result of the condition check.
|
|
59
|
+
* @param {string} [ngMsg] The error message.
|
|
60
|
+
* @param {new (...args: any[]) => Error} [error] The error class to throw (Error or its subclass).
|
|
61
|
+
* @param {Object<string, *>} [eProps={}] Properties to assign to the error instance.
|
|
62
|
+
* @return {boolean} The evaluation result (returns `isOk` as is).
|
|
63
|
+
*/
|
|
64
|
+
static REQUIRE_DEBUG(isOk: boolean, ngMsg?: string, error?: new (...args: any[]) => Error, eProps?: {
|
|
65
|
+
[x: string]: any;
|
|
66
|
+
}): boolean;
|
|
67
|
+
/**
|
|
68
|
+
* Checks a postcondition.
|
|
69
|
+
* Use this to verify conditions that must be true after a function executes (e.g., return value or object state).
|
|
70
|
+
* If `isOk` is false and an `error` class is provided, throws an exception with `ngMsg`.
|
|
71
|
+
* Otherwise, logs the error message to `console.error`.
|
|
72
|
+
* @param {boolean} isOk The result of the condition check.
|
|
73
|
+
* @param {string} [ngMsg] The error message.
|
|
74
|
+
* @param {new (...args: any[]) => Error} [error=Error] The error class to throw (Error or its subclass).
|
|
75
|
+
* @param {Object<string, *>} [eProps={}] Properties to assign to the error instance.
|
|
76
|
+
* @return {boolean} The evaluation result (returns `isOk` as is).
|
|
77
|
+
*/
|
|
78
|
+
static ENSURE(isOk: boolean, ngMsg?: string, error?: new (...args: any[]) => Error, eProps?: {
|
|
79
|
+
[x: string]: any;
|
|
80
|
+
}): boolean;
|
|
81
|
+
/**
|
|
82
|
+
* Checks a postcondition (for debugging: outputs execution message but throws no exception in debug mode).
|
|
83
|
+
* If the result is false and an `error` class is provided, throws an exception.
|
|
84
|
+
* Otherwise, logs the message to `console.error`.
|
|
85
|
+
* @param {boolean} isOk The result of the condition check.
|
|
86
|
+
* @param {string} [ngMsg] The error message.
|
|
87
|
+
* @param {new (...args: any[]) => Error} [error] The error class to throw (Error or its subclass).
|
|
88
|
+
* @param {Object<string, *>} [eProps={}] Properties to assign to the error instance.
|
|
89
|
+
* @return {boolean} The evaluation result (returns `isOk` as is).
|
|
90
|
+
*/
|
|
91
|
+
static ENSURE_DEBUG(isOk: boolean, ngMsg?: string, error?: new (...args: any[]) => Error, eProps?: {
|
|
92
|
+
[x: string]: any;
|
|
93
|
+
}): boolean;
|
|
94
|
+
/**
|
|
95
|
+
* Checks an invariant.
|
|
96
|
+
* Use this to verify conditions that must remain true before and after a function call (e.g., object integrity).
|
|
97
|
+
* If `isOk` is false and an `error` class is provided, throws an exception with `ngMsg`.
|
|
98
|
+
* Otherwise, logs the error message to `console.error`.
|
|
99
|
+
* @param {boolean} isOk The result of the condition check.
|
|
100
|
+
* @param {string} [ngMsg] The error message.
|
|
101
|
+
* @param {new (...args: any[]) => Error} [error=Error] The error class to throw (Error or its subclass).
|
|
102
|
+
* @param {Object<string, *>} [eProps={}] Properties to assign to the error instance.
|
|
103
|
+
* @return {boolean} The evaluation result (returns `isOk` as is).
|
|
104
|
+
*/
|
|
105
|
+
static INVARIANT(isOk: boolean, ngMsg?: string, error?: new (...args: any[]) => Error, eProps?: {
|
|
106
|
+
[x: string]: any;
|
|
107
|
+
}): boolean;
|
|
108
|
+
/**
|
|
109
|
+
* Checks an invariant (for debugging: outputs execution message but throws no exception in debug mode).
|
|
110
|
+
* If the result is false and an `error` class is provided, throws an exception.
|
|
111
|
+
* Otherwise, logs the message to `console.error`.
|
|
112
|
+
* @param {boolean} isOk The result of the condition check.
|
|
113
|
+
* @param {string} [ngMsg] The error message.
|
|
114
|
+
* @param {new (...args: any[]) => Error} [error] The error class to throw (Error or its subclass).
|
|
115
|
+
* @param {Object<string, *>} [eProps={}] Properties to assign to the error instance.
|
|
116
|
+
* @return {boolean} The evaluation result (returns `isOk` as is).
|
|
117
|
+
*/
|
|
118
|
+
static INVARIANT_DEBUG(isOk: boolean, ngMsg?: string, error?: new (...args: any[]) => Error, eProps?: {
|
|
119
|
+
[x: string]: any;
|
|
120
|
+
}): boolean;
|
|
121
|
+
/**
|
|
122
|
+
* Private methods
|
|
123
|
+
*/
|
|
124
|
+
/**
|
|
125
|
+
* Core evaluation logic.
|
|
126
|
+
* If the result is false and an `error` class is provided, throws an exception.
|
|
127
|
+
* Otherwise, logs the message to `console.error`.
|
|
128
|
+
* @param {boolean} isOk The result of the condition check.
|
|
129
|
+
* @param {string} [prefix] Prefix for the error message.
|
|
130
|
+
* @param {string} [ngMsg] The error message.
|
|
131
|
+
* @param {new (...args: any[]) => Error} [error] The error class to throw (Error or its subclass).
|
|
132
|
+
* @param {Object<string, *>} [eProps={}] Properties to assign to the error instance.
|
|
133
|
+
* @return {boolean} The evaluation result (returns `isOk` as is).
|
|
134
|
+
*/
|
|
135
|
+
static "__#1@#check"(isOk: boolean, prefix?: string, ngMsg?: string, error?: new (...args: any[]) => Error, eProps?: {
|
|
136
|
+
[x: string]: any;
|
|
137
|
+
}): boolean;
|
|
138
|
+
/**
|
|
139
|
+
* Debug evaluation logic (no message output or exception thrown unless in debug mode).
|
|
140
|
+
* If the result is false and an `error` class is provided, throws an exception.
|
|
141
|
+
* Otherwise, logs the message to `console.error`.
|
|
142
|
+
* @param {boolean} isOk The result of the condition check.
|
|
143
|
+
* @param {string} [prefix] Prefix for the error message.
|
|
144
|
+
* @param {string} [ngMsg] The error message.
|
|
145
|
+
* @param {new (...args: any[]) => Error} [error] The error class to throw (Error or its subclass).
|
|
146
|
+
* @param {Object<string, *>} [eProps={}] Properties to assign to the error instance.
|
|
147
|
+
* @return {boolean} The evaluation result (returns `isOk` as is).
|
|
148
|
+
*/
|
|
149
|
+
static "__#1@#checkDebug"(isOk: boolean, prefix?: string, ngMsg?: string, error?: new (...args: any[]) => Error, eProps?: {
|
|
150
|
+
[x: string]: any;
|
|
151
|
+
}): boolean;
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
export { Contracts, Contracts as default };
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,162 @@
|
|
|
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 {
|
|
14
|
+
/**
|
|
15
|
+
* Configures the contract checking behavior.
|
|
16
|
+
* @param {{debug?: boolean}} config Configuration object
|
|
17
|
+
*/
|
|
18
|
+
static setConfig(config) {
|
|
19
|
+
if (config) {
|
|
20
|
+
_Contracts.DEBUG_MODE = config.debug || false;
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
/**
|
|
24
|
+
* Checks a general condition.
|
|
25
|
+
* If `isOk` is false and an `error` class is provided, throws an exception with `ngMsg`.
|
|
26
|
+
* Otherwise, logs the error message to `console.error`.
|
|
27
|
+
* @param {boolean} isOk The result of the condition check.
|
|
28
|
+
* @param {string} [ngMsg] The error message.
|
|
29
|
+
* @param {new (...args: any[]) => Error} [error=Error] The error class to throw (Error or its subclass).
|
|
30
|
+
* @param {Object<string, *>} [eProps={}] Properties to assign to the error instance.
|
|
31
|
+
* @return {boolean} The evaluation result (returns `isOk` as is).
|
|
32
|
+
*/
|
|
33
|
+
static VERIFY(isOk, ngMsg, error = Error, eProps = {}) {
|
|
34
|
+
var _a;
|
|
35
|
+
return __privateMethod(_a = _Contracts, _Contracts_static, check_fn).call(_a, isOk, "VERIFY", ngMsg, error, eProps);
|
|
36
|
+
}
|
|
37
|
+
/**
|
|
38
|
+
* Checks a general condition (for debugging: outputs execution message but throws no exception in debug mode).
|
|
39
|
+
* If the result is false and an `error` class is provided, throws an exception.
|
|
40
|
+
* Otherwise, logs the message to `console.error`.
|
|
41
|
+
* @param {boolean} isOk The result of the condition check.
|
|
42
|
+
* @param {string} [ngMsg] The error message.
|
|
43
|
+
* @param {new (...args: any[]) => Error} [error] The error class to throw (Error or its subclass).
|
|
44
|
+
* @param {Object<string, *>} [eProps={}] Properties to assign to the error instance.
|
|
45
|
+
* @return {boolean} The evaluation result (returns `isOk` as is).
|
|
46
|
+
*/
|
|
47
|
+
static VERIFY_DEBUG(isOk, ngMsg, error = Error, eProps = {}) {
|
|
48
|
+
var _a;
|
|
49
|
+
return __privateMethod(_a = _Contracts, _Contracts_static, checkDebug_fn).call(_a, isOk, "VERIFY_DEBUG", ngMsg, error, eProps);
|
|
50
|
+
}
|
|
51
|
+
/**
|
|
52
|
+
* Checks a precondition.
|
|
53
|
+
* Use this to verify conditions that must be true before a function executes (e.g., argument validation).
|
|
54
|
+
* If `isOk` is false and an `error` class is provided, throws an exception with `ngMsg`.
|
|
55
|
+
* Otherwise, logs the error message to `console.error`.
|
|
56
|
+
* @param {boolean} isOk The result of the condition check.
|
|
57
|
+
* @param {string} [ngMsg] The error message.
|
|
58
|
+
* @param {new (...args: any[]) => Error} [error=Error] The error class to throw (Error or its subclass).
|
|
59
|
+
* @param {Object<string, *>} [eProps={}] Properties to assign to the error instance.
|
|
60
|
+
* @return {boolean} The evaluation result (returns `isOk` as is).
|
|
61
|
+
*/
|
|
62
|
+
static REQUIRE(isOk, ngMsg, error = Error, eProps = {}) {
|
|
63
|
+
var _a;
|
|
64
|
+
return __privateMethod(_a = _Contracts, _Contracts_static, check_fn).call(_a, isOk, "REQUIRE", ngMsg, error, eProps);
|
|
65
|
+
}
|
|
66
|
+
/**
|
|
67
|
+
* Checks a precondition (for debugging: outputs execution message but throws no exception in debug mode).
|
|
68
|
+
* If the result is false and an `error` class is provided, throws an exception.
|
|
69
|
+
* Otherwise, logs the message to `console.error`.
|
|
70
|
+
* @param {boolean} isOk The result of the condition check.
|
|
71
|
+
* @param {string} [ngMsg] The error message.
|
|
72
|
+
* @param {new (...args: any[]) => Error} [error] The error class to throw (Error or its subclass).
|
|
73
|
+
* @param {Object<string, *>} [eProps={}] Properties to assign to the error instance.
|
|
74
|
+
* @return {boolean} The evaluation result (returns `isOk` as is).
|
|
75
|
+
*/
|
|
76
|
+
static REQUIRE_DEBUG(isOk, ngMsg, error = Error, eProps = {}) {
|
|
77
|
+
var _a;
|
|
78
|
+
return __privateMethod(_a = _Contracts, _Contracts_static, checkDebug_fn).call(_a, isOk, "REQUIRE_DEBUG", ngMsg, error, eProps);
|
|
79
|
+
}
|
|
80
|
+
/**
|
|
81
|
+
* Checks a postcondition.
|
|
82
|
+
* Use this to verify conditions that must be true after a function executes (e.g., return value or object state).
|
|
83
|
+
* If `isOk` is false and an `error` class is provided, throws an exception with `ngMsg`.
|
|
84
|
+
* Otherwise, logs the error message to `console.error`.
|
|
85
|
+
* @param {boolean} isOk The result of the condition check.
|
|
86
|
+
* @param {string} [ngMsg] The error message.
|
|
87
|
+
* @param {new (...args: any[]) => Error} [error=Error] The error class to throw (Error or its subclass).
|
|
88
|
+
* @param {Object<string, *>} [eProps={}] Properties to assign to the error instance.
|
|
89
|
+
* @return {boolean} The evaluation result (returns `isOk` as is).
|
|
90
|
+
*/
|
|
91
|
+
static ENSURE(isOk, ngMsg, error = Error, eProps = {}) {
|
|
92
|
+
var _a;
|
|
93
|
+
return __privateMethod(_a = _Contracts, _Contracts_static, check_fn).call(_a, isOk, "ENSURE", ngMsg, error, eProps);
|
|
94
|
+
}
|
|
95
|
+
/**
|
|
96
|
+
* Checks a postcondition (for debugging: outputs execution message but throws no exception in debug mode).
|
|
97
|
+
* If the result is false and an `error` class is provided, throws an exception.
|
|
98
|
+
* Otherwise, logs the message to `console.error`.
|
|
99
|
+
* @param {boolean} isOk The result of the condition check.
|
|
100
|
+
* @param {string} [ngMsg] The error message.
|
|
101
|
+
* @param {new (...args: any[]) => Error} [error] The error class to throw (Error or its subclass).
|
|
102
|
+
* @param {Object<string, *>} [eProps={}] Properties to assign to the error instance.
|
|
103
|
+
* @return {boolean} The evaluation result (returns `isOk` as is).
|
|
104
|
+
*/
|
|
105
|
+
static ENSURE_DEBUG(isOk, ngMsg, error = Error, eProps = {}) {
|
|
106
|
+
var _a;
|
|
107
|
+
return __privateMethod(_a = _Contracts, _Contracts_static, checkDebug_fn).call(_a, isOk, "ENSURE_DEBUG", ngMsg, error, eProps);
|
|
108
|
+
}
|
|
109
|
+
/**
|
|
110
|
+
* Checks an invariant.
|
|
111
|
+
* Use this to verify conditions that must remain true before and after a function call (e.g., object integrity).
|
|
112
|
+
* If `isOk` is false and an `error` class is provided, throws an exception with `ngMsg`.
|
|
113
|
+
* Otherwise, logs the error message to `console.error`.
|
|
114
|
+
* @param {boolean} isOk The result of the condition check.
|
|
115
|
+
* @param {string} [ngMsg] The error message.
|
|
116
|
+
* @param {new (...args: any[]) => Error} [error=Error] The error class to throw (Error or its subclass).
|
|
117
|
+
* @param {Object<string, *>} [eProps={}] Properties to assign to the error instance.
|
|
118
|
+
* @return {boolean} The evaluation result (returns `isOk` as is).
|
|
119
|
+
*/
|
|
120
|
+
static INVARIANT(isOk, ngMsg, error = Error, eProps = {}) {
|
|
121
|
+
var _a;
|
|
122
|
+
return __privateMethod(_a = _Contracts, _Contracts_static, check_fn).call(_a, isOk, "INVARIANT", ngMsg, error, eProps);
|
|
123
|
+
}
|
|
124
|
+
/**
|
|
125
|
+
* Checks an invariant (for debugging: outputs execution message but throws no exception in debug mode).
|
|
126
|
+
* If the result is false and an `error` class is provided, throws an exception.
|
|
127
|
+
* Otherwise, logs the message to `console.error`.
|
|
128
|
+
* @param {boolean} isOk The result of the condition check.
|
|
129
|
+
* @param {string} [ngMsg] The error message.
|
|
130
|
+
* @param {new (...args: any[]) => Error} [error] The error class to throw (Error or its subclass).
|
|
131
|
+
* @param {Object<string, *>} [eProps={}] Properties to assign to the error instance.
|
|
132
|
+
* @return {boolean} The evaluation result (returns `isOk` as is).
|
|
133
|
+
*/
|
|
134
|
+
static INVARIANT_DEBUG(isOk, ngMsg, error = Error, eProps = {}) {
|
|
135
|
+
var _a;
|
|
136
|
+
return __privateMethod(_a = _Contracts, _Contracts_static, checkDebug_fn).call(_a, isOk, "INVARIANT_DEBUG", ngMsg, error, eProps);
|
|
137
|
+
}
|
|
138
|
+
};
|
|
139
|
+
_Contracts_static = new WeakSet();
|
|
140
|
+
check_fn = function(isOk, prefix = "CHECK", ngMsg = "", error = Error, eProps = {}) {
|
|
141
|
+
if (!isOk) {
|
|
142
|
+
const msg = `[${prefix}]${ngMsg ?? ""}`;
|
|
143
|
+
if (error) throw Object.assign(new error(msg), eProps);
|
|
144
|
+
if (ngMsg) console.error(msg, eProps);
|
|
145
|
+
}
|
|
146
|
+
return isOk;
|
|
147
|
+
};
|
|
148
|
+
checkDebug_fn = function(isOk, prefix = "CHECK_DEBUG", ngMsg = "", error = Error, eProps = {}) {
|
|
149
|
+
var _a;
|
|
150
|
+
return _Contracts.DEBUG_MODE ? __privateMethod(_a = _Contracts, _Contracts_static, check_fn).call(_a, isOk, prefix, ngMsg, error, eProps) : isOk;
|
|
151
|
+
};
|
|
152
|
+
__privateAdd(_Contracts, _Contracts_static);
|
|
153
|
+
// @type {boolean}
|
|
154
|
+
__publicField(_Contracts, "DEBUG_MODE", false);
|
|
155
|
+
var Contracts = _Contracts;
|
|
156
|
+
|
|
157
|
+
// src/index.js
|
|
158
|
+
var index_default = Contracts;
|
|
159
|
+
export {
|
|
160
|
+
Contracts,
|
|
161
|
+
index_default as default
|
|
162
|
+
};
|
package/package.json
ADDED
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@ayapapa-npm/contracts-js",
|
|
3
|
+
"version": "0.2.0",
|
|
4
|
+
"description": "Design by Contract utilities for JavaScript: preconditions, postconditions, and invariants.",
|
|
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.js --format esm,cjs --out-dir dist --clean --dts",
|
|
21
|
+
"test": "vitest run",
|
|
22
|
+
"test:debug": "vitest --inspect-brk --run",
|
|
23
|
+
"test:watch": "vitest",
|
|
24
|
+
"prepublishOnly": "npm run build && npm test"
|
|
25
|
+
},
|
|
26
|
+
"devDependencies": {
|
|
27
|
+
"tsup": "^8.5.1",
|
|
28
|
+
"typescript": "^5.7.3",
|
|
29
|
+
"vitest": "^4.1.10"
|
|
30
|
+
},
|
|
31
|
+
"allowScripts": {
|
|
32
|
+
"esbuild@0.28.1": true
|
|
33
|
+
}
|
|
34
|
+
}
|