@ayapapa-npm/contracts-js 0.2.1 → 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,8 +1,9 @@
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.
3
4
 
4
- ## Installation
5
5
 
6
+ ## Installation
6
7
 
7
8
  ### Install from npm registry
8
9
  Available now.
@@ -31,33 +32,175 @@ Then run the install command again:
31
32
  npm install https://github.com/ayapapa/contracts-js/releases/download/X.Y.Z/ayapapa-npm-contracts-js-X.Y.Z.tgz
32
33
  ```
33
34
 
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
+ ```
34
126
 
35
127
  ## Usage
36
128
 
37
129
  ```javascript
38
- import { Contracts } from 'contracts-js';
39
- // or,
40
- // import Contracts from 'contracts-jss';
130
+ import { Contracts } from '@ayapapa-npm/contracts-js';
131
+ // or:
132
+ // import Contracts from '@ayapapa-npm/contracts-js';
41
133
 
42
- // In the case of CommonJS,
43
- // const { Contracts } = require('contracts-js');
44
- // or,
45
- // const Contracts = require('contracts-js').Contracts;
134
+ // CommonJS:
135
+ // const { Contracts } = require('@ayapapa-npm/contracts-js');
46
136
 
47
137
  // Enable debug mode
48
138
  Contracts.setConfig({ debug: true });
49
139
 
50
- // Check a precondition (e.g., argument validation)
140
+
141
+ // REQUIRE: Check a precondition
51
142
  function divide(a, b) {
52
- Contracts.REQUIRE(b !== 0, 'Divisor cannot be zero');
143
+ Contracts.REQUIRE(
144
+ b !== 0,
145
+ 'Divisor cannot be zero'
146
+ );
147
+
53
148
  return a / b;
54
149
  }
55
150
 
56
- // Check a postcondition (e.g., return value validation)
151
+
152
+ // ENSURE: Check a postcondition
57
153
  function getPositiveNumber(x) {
58
- Contracts.REQUIRE(x > 0, 'Input must be positive');
154
+ Contracts.REQUIRE(
155
+ x > 0,
156
+ 'Input must be positive'
157
+ );
158
+
59
159
  const result = x * 2;
60
- Contracts ENSURE(result > 0, 'Result must be positive');
160
+
161
+ Contracts.ENSURE(
162
+ result > 0,
163
+ 'Result must be positive'
164
+ );
165
+
61
166
  return result;
62
167
  }
63
- ```
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
+