@ayapapa-npm/contracts-js 0.2.0 → 0.2.2

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 CHANGED
@@ -1,12 +1,21 @@
1
1
  # contracts-js
2
- A lightweight Design by Contract library for JavaScript.
2
+ A lightweight Design by Contract library for JavaScript.</br>
3
+ All check functions return the evaluated condition itself, so they can be used directly in control flow when exception throwing is suppressed.
4
+
3
5
 
4
6
  ## Installation
5
7
 
8
+ ### Install from npm registry
9
+ Available now.
10
+ ```bash
11
+ npm install @ayapapa-npm/contracts-js
12
+ ```
13
+
6
14
  ### Install directly from GitHub Release:
7
15
  Available now.
16
+ Check for the latest version before installation: https://github.com/ayapapa/contracts-js/releases
8
17
  ```bash
9
- npm install https://github.com/ayapapa/contracts-js/releases/download/0.2.0/contracts-js-0.2.0.tgz
18
+ npm install https://github.com/ayapapa/contracts-js/releases/download/X.Y.Z/ayapapa-npm-contracts-js-X.Y.Z.tgz
10
19
  ```
11
20
  If you see the error:
12
21
  ```
@@ -14,43 +23,184 @@ npm error code EALLOWREMOTE
14
23
  npm error Fetching packages of type "remote" have been disabled
15
24
  ```
16
25
  allow remote packages:
26
+
17
27
  ```bash
18
28
  npm config set allow-remote all
19
29
  ```
20
30
  Then run the install command again:
21
31
  ```bash
22
- npm install https://github.com/ayapapa/contracts-js/releases/download/0.2.0/contracts-js-0.2.0.tgz
32
+ npm install https://github.com/ayapapa/contracts-js/releases/download/X.Y.Z/ayapapa-npm-contracts-js-X.Y.Z.tgz
23
33
  ```
24
34
 
25
- ### Install from npm registry
26
- Coming soon: installable with `npm install contracts-js`.
35
+ ## Contract Types
36
+
37
+ `contracts-js` provides runtime checks based on **Design by Contract** principles.
38
+
39
+ The library provides four types of contracts:
40
+
41
+ | Contract | Purpose | When to use | Responsibility |
42
+ | ----------- | ---------------------- | ---------------------------------------------------------- | ----------------------- |
43
+ | `REQUIRE` | Precondition | Check conditions before execution starts | Caller |
44
+ | `VERIFY` | Intermediate condition | Check assumptions or intermediate results during execution | Internal process |
45
+ | `ENSURE` | Postcondition | Check conditions after execution completes | Function |
46
+ | `INVARIANT` | State consistency | Check conditions that must remain valid over time | Object / Data structure |
47
+
48
+ ### Quick Guide
49
+
50
+ #### REQUIRE - "Can this operation start?"
51
+
52
+ Use `REQUIRE` to validate conditions that must be satisfied before calling a function.
53
+
54
+ Examples:
55
+
56
+ * Function arguments are valid.
57
+ * Required objects exist.
58
+ * Required external conditions are available.
59
+
60
+ ```javascript
61
+ Contracts.REQUIRE(
62
+ user !== null,
63
+ 'User is required'
64
+ );
65
+ ```
66
+
67
+ ---
68
+
69
+ #### VERIFY - "Is the current processing state valid?"
70
+
71
+ Use `VERIFY` to check intermediate assumptions or temporary states during execution.
72
+
73
+ Examples:
74
+
75
+ * Intermediate calculation results.
76
+ * Internal processing states.
77
+ * Temporary assumptions.
78
+
79
+ Do not use `VERIFY` for input validation.
80
+ Use `REQUIRE` for conditions required before execution.
81
+
82
+ ```javascript
83
+ Contracts.VERIFY(
84
+ result >= 0,
85
+ 'Intermediate result must not be negative'
86
+ );
87
+ ```
88
+
89
+ ---
90
+
91
+ #### ENSURE - "Did the operation complete correctly?"
92
+
93
+ Use `ENSURE` to verify guarantees provided by a function after execution.
94
+
95
+ Examples:
96
+
97
+ * Return values are valid.
98
+ * State changes completed correctly.
99
+ * Processing results satisfy expected conditions.
100
+
101
+ ```javascript
102
+ Contracts.ENSURE(
103
+ result !== null,
104
+ 'Result must be available'
105
+ );
106
+ ```
107
+
108
+ ---
109
+
110
+ #### INVARIANT - "Is the object still valid?"
111
+
112
+ Use `INVARIANT` to verify conditions that represent the internal consistency of an object or data structure.
113
+
114
+ Examples:
115
+
116
+ * Internal values remain consistent.
117
+ * Object state rules are maintained.
118
+ * Data structure integrity is protected.
119
+
120
+ ```javascript
121
+ Contracts.INVARIANT(
122
+ balance >= 0,
123
+ 'Balance cannot be negative'
124
+ );
125
+ ```
27
126
 
28
127
  ## Usage
29
128
 
30
129
  ```javascript
31
- import { Contracts } from 'contracts-js';
32
- // or,
33
- // import Contracts from 'contracts-jss';
130
+ import { Contracts } from '@ayapapa-npm/contracts-js';
131
+ // or:
132
+ // import Contracts from '@ayapapa-npm/contracts-js';
34
133
 
35
- // In the case of CommonJS,
36
- // const { Contracts } = require('contracts-js');
37
- // or,
38
- // const Contracts = require('contracts-js').Contracts;
134
+ // CommonJS:
135
+ // const { Contracts } = require('@ayapapa-npm/contracts-js');
39
136
 
40
137
  // Enable debug mode
41
138
  Contracts.setConfig({ debug: true });
42
139
 
43
- // Check a precondition (e.g., argument validation)
140
+
141
+ // REQUIRE: Check a precondition
44
142
  function divide(a, b) {
45
- Contracts.REQUIRE(b !== 0, 'Divisor cannot be zero');
143
+ Contracts.REQUIRE(
144
+ b !== 0,
145
+ 'Divisor cannot be zero'
146
+ );
147
+
46
148
  return a / b;
47
149
  }
48
150
 
49
- // Check a postcondition (e.g., return value validation)
151
+
152
+ // ENSURE: Check a postcondition
50
153
  function getPositiveNumber(x) {
51
- Contracts.REQUIRE(x > 0, 'Input must be positive');
154
+ Contracts.REQUIRE(
155
+ x > 0,
156
+ 'Input must be positive'
157
+ );
158
+
52
159
  const result = x * 2;
53
- Contracts ENSURE(result > 0, 'Result must be positive');
160
+
161
+ Contracts.ENSURE(
162
+ result > 0,
163
+ 'Result must be positive'
164
+ );
165
+
54
166
  return result;
55
167
  }
56
- ```
168
+
169
+
170
+ // INVARIANT: Check object state consistency
171
+ class BankAccount {
172
+ constructor(balance) {
173
+ this.balance = balance;
174
+ }
175
+
176
+ withdraw(amount) {
177
+ Contracts.REQUIRE(
178
+ amount >= 0,
179
+ 'Amount must not be negative'
180
+ );
181
+
182
+ this.balance -= amount;
183
+
184
+ Contracts.INVARIANT(
185
+ this.balance >= 0,
186
+ 'Balance cannot be negative'
187
+ );
188
+ }
189
+ }
190
+
191
+
192
+ // VERIFY: Check an intermediate condition
193
+ function processAccount(account) {
194
+ const calculatedBalance = calculateBalance(account);
195
+
196
+ Contracts.VERIFY(
197
+ calculatedBalance >= 0,
198
+ 'Intermediate balance is invalid'
199
+ );
200
+
201
+ return calculatedBalance;
202
+ }
203
+ ```
204
+
205
+ **XXX_DEBUG() performs the same action as XXX() in debug mode; otherwise, no action.**
206
+