promise-logic 1.3.1 β 2.3.3
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 +211 -259
- package/dist/factory.cjs.js +15 -1
- package/dist/factory.cjs.js.map +1 -1
- package/dist/factory.esm.js +15 -1
- package/dist/factory.esm.js.map +1 -1
- package/dist/index.cjs.js +15 -1
- package/dist/index.cjs.js.map +1 -1
- package/dist/index.esm.js +15 -1
- package/dist/index.esm.js.map +1 -1
- package/dist/v2/factory.cjs.js +287 -0
- package/dist/v2/factory.cjs.js.map +1 -0
- package/dist/v2/factory.esm.js +285 -0
- package/dist/v2/factory.esm.js.map +1 -0
- package/dist/v2/gates/BaseGate.d.ts +6 -0
- package/dist/v2/gates/BaseGate.d.ts.map +1 -0
- package/dist/v2/gates/and.d.ts +5 -0
- package/dist/v2/gates/and.d.ts.map +1 -0
- package/dist/v2/gates/majority.d.ts +5 -0
- package/dist/v2/gates/majority.d.ts.map +1 -0
- package/dist/v2/gates/nand.d.ts +5 -0
- package/dist/v2/gates/nand.d.ts.map +1 -0
- package/dist/v2/gates/nor.d.ts +5 -0
- package/dist/v2/gates/nor.d.ts.map +1 -0
- package/dist/v2/gates/or.d.ts +5 -0
- package/dist/v2/gates/or.d.ts.map +1 -0
- package/dist/v2/gates/xnor.d.ts +5 -0
- package/dist/v2/gates/xnor.d.ts.map +1 -0
- package/dist/v2/gates/xor.d.ts +5 -0
- package/dist/v2/gates/xor.d.ts.map +1 -0
- package/dist/v2/index.cjs.js +289 -0
- package/dist/v2/index.cjs.js.map +1 -0
- package/dist/v2/index.esm.js +285 -0
- package/dist/v2/index.esm.js.map +1 -0
- package/dist/v2/ts/gates/BaseGate.d.ts +6 -0
- package/dist/v2/ts/gates/BaseGate.d.ts.map +1 -0
- package/dist/v2/ts/gates/and.d.ts +5 -0
- package/dist/v2/ts/gates/and.d.ts.map +1 -0
- package/dist/v2/ts/gates/majority.d.ts +5 -0
- package/dist/v2/ts/gates/majority.d.ts.map +1 -0
- package/dist/v2/ts/gates/nand.d.ts +5 -0
- package/dist/v2/ts/gates/nand.d.ts.map +1 -0
- package/dist/v2/ts/gates/nor.d.ts +5 -0
- package/dist/v2/ts/gates/nor.d.ts.map +1 -0
- package/dist/v2/ts/gates/or.d.ts +5 -0
- package/dist/v2/ts/gates/or.d.ts.map +1 -0
- package/dist/v2/ts/gates/xnor.d.ts +5 -0
- package/dist/v2/ts/gates/xnor.d.ts.map +1 -0
- package/dist/v2/ts/gates/xor.d.ts +5 -0
- package/dist/v2/ts/gates/xor.d.ts.map +1 -0
- package/dist/v2/types/factory.d.ts +10 -0
- package/dist/v2/types/index.d.ts +85 -0
- package/dist/v2/utils/v2/errors.d.ts +7 -0
- package/dist/v2/utils/v2/errors.d.ts.map +1 -0
- package/dist/v2/v2/PromiseLogic.d.ts +29 -0
- package/dist/v2/v2/PromiseLogic.d.ts.map +1 -0
- package/dist/v2/v2/factory.d.ts +7 -0
- package/dist/v2/v2/factory.d.ts.map +1 -0
- package/dist/v2/v2/index.d.ts +6 -0
- package/dist/v2/v2/index.d.ts.map +1 -0
- package/package.json +14 -10
package/README.md
CHANGED
|
@@ -1,21 +1,48 @@
|
|
|
1
|
-
# PromiseLogic
|
|
2
1
|
|
|
3
|
-
|
|
2
|
+
# Promise Logic - Advanced Promise Logic Gates for Async Programming
|
|
4
3
|
|
|
5
|
-
|
|
4
|
+
Compose promises with logic gate semantics (AND, OR, NOT, XOR, NAND, NOR, XNOR, Majority). Forget APIs, remember logic.
|
|
6
5
|
|
|
7
|
-
##
|
|
6
|
+
## Features
|
|
8
7
|
|
|
9
|
-
**
|
|
8
|
+
- **Logic Gate Semantics**: Extend Promise API with AND, OR, NOT, XOR, NAND, NOR, XNOR, Majority operations
|
|
9
|
+
- **Dual Entry Points**: Choose between JavaScript or enhanced TypeScript experience
|
|
10
|
+
- **Type Safety**: Complete TypeScript definitions with strict type checking
|
|
11
|
+
- **Promise Utilities**: Additional utilities like Flip-Flop state management
|
|
12
|
+
- **Zero Dependencies**: Pure JavaScript/TypeScript implementation
|
|
13
|
+
- **Tree Shakeable**: Optimized for modern bundlers
|
|
10
14
|
|
|
11
|
-
##
|
|
15
|
+
## Recent Updates
|
|
16
|
+
|
|
17
|
+
### Version 2.3.2 Highlights
|
|
18
|
+
|
|
19
|
+
**π NOT Gate Implementation**
|
|
20
|
+
Introduced the NOT logic gate for promise inversion, enabling flexible negation patterns in asynchronous workflows:
|
|
21
|
+
|
|
22
|
+
```javascript
|
|
23
|
+
// Success -> Failure transformation
|
|
24
|
+
await PromiseLogic.not(Promise.resolve('success')); // Rejects with 'success'
|
|
12
25
|
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
26
|
+
// Failure -> Success transformation
|
|
27
|
+
const result = await PromiseLogic.not(Promise.reject('error')); // Resolves with 'error'
|
|
28
|
+
```
|
|
29
|
+
|
|
30
|
+
**π¦ Enhanced TypeScript System**
|
|
31
|
+
Completely revamped TypeScript architecture with:
|
|
32
|
+
- Full generic type propagation
|
|
33
|
+
- Strict type checking with zero `any` types
|
|
34
|
+
- Advanced type inference for all operations
|
|
35
|
+
- Seamless IDE integration with IntelliSense
|
|
36
|
+
|
|
37
|
+
**β‘ Performance Optimizations**
|
|
38
|
+
- Optimized internal logic for better efficiency
|
|
39
|
+
- Reduced memory overhead in gate operations
|
|
40
|
+
- Improved error handling and edge case management
|
|
41
|
+
|
|
42
|
+
**π Documentation Enhancements**
|
|
43
|
+
- Comprehensive API reference with TypeScript examples
|
|
44
|
+
- Better usage guidelines and best practices
|
|
45
|
+
- Clear entry point documentation for different environments
|
|
19
46
|
|
|
20
47
|
## Installation
|
|
21
48
|
|
|
@@ -25,324 +52,249 @@ npm install promise-logic
|
|
|
25
52
|
|
|
26
53
|
## Quick Start
|
|
27
54
|
|
|
28
|
-
###
|
|
55
|
+
### JavaScript (Default)
|
|
29
56
|
|
|
30
57
|
```javascript
|
|
58
|
+
// ES Modules
|
|
31
59
|
import { PromiseLogic } from 'promise-logic';
|
|
32
60
|
|
|
33
|
-
//
|
|
61
|
+
// CommonJS
|
|
62
|
+
const { PromiseLogic } = require('promise-logic');
|
|
63
|
+
|
|
64
|
+
// Use logic gates
|
|
34
65
|
const results = await PromiseLogic.and([
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
fetch('/api/comments')
|
|
66
|
+
Promise.resolve('data1'),
|
|
67
|
+
Promise.resolve('data2')
|
|
38
68
|
]);
|
|
69
|
+
// results = ['data1', 'data2']
|
|
70
|
+
```
|
|
39
71
|
|
|
40
|
-
|
|
41
|
-
const data = await PromiseLogic.or([
|
|
42
|
-
fetchPrimaryService(),
|
|
43
|
-
fetchBackupService()
|
|
44
|
-
]);
|
|
72
|
+
### TypeScript (Enhanced)
|
|
45
73
|
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
74
|
+
```typescript
|
|
75
|
+
// TypeScript version with full type inference
|
|
76
|
+
import { PromiseLogic } from 'promise-logic/typescript';
|
|
77
|
+
|
|
78
|
+
// Type-safe operations with automatic inference
|
|
79
|
+
const numbers = await PromiseLogic.and<number>([
|
|
80
|
+
Promise.resolve(1),
|
|
81
|
+
Promise.resolve(2)
|
|
82
|
+
]);
|
|
83
|
+
|
|
84
|
+
const strings = await PromiseLogic.or<string>([
|
|
85
|
+
Promise.resolve('hello'),
|
|
86
|
+
Promise.resolve('world')
|
|
87
|
+
]);
|
|
51
88
|
```
|
|
52
89
|
|
|
53
|
-
|
|
90
|
+
## Core Logic Gates
|
|
91
|
+
|
|
92
|
+
### `and(promises)`
|
|
93
|
+
Resolves with all values when all promises fulfill. Equivalent to `Promise.all()`.
|
|
54
94
|
|
|
55
95
|
```javascript
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
verifyResources()
|
|
96
|
+
const results = await PromiseLogic.and([
|
|
97
|
+
Promise.resolve(1),
|
|
98
|
+
Promise.resolve(2),
|
|
99
|
+
Promise.resolve(3)
|
|
61
100
|
]);
|
|
101
|
+
// results = [1, 2, 3]
|
|
102
|
+
```
|
|
62
103
|
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
experimentalFeature(),
|
|
66
|
-
deprecatedAPI()
|
|
67
|
-
]);
|
|
104
|
+
### `or(promises)`
|
|
105
|
+
Resolves with the first fulfilled promise. Equivalent to `Promise.any()`.
|
|
68
106
|
|
|
69
|
-
|
|
70
|
-
const
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
server3.query()
|
|
107
|
+
```javascript
|
|
108
|
+
const result = await PromiseLogic.or([
|
|
109
|
+
Promise.reject('error'),
|
|
110
|
+
Promise.resolve('success')
|
|
74
111
|
]);
|
|
112
|
+
// result = 'success'
|
|
75
113
|
```
|
|
76
114
|
|
|
77
|
-
###
|
|
115
|
+
### `xor(promises)`
|
|
116
|
+
Exclusive OR - resolves only when exactly one promise fulfills.
|
|
78
117
|
|
|
79
118
|
```javascript
|
|
80
|
-
|
|
81
|
-
const
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
]);
|
|
119
|
+
try {
|
|
120
|
+
const result = await PromiseLogic.xor([
|
|
121
|
+
Promise.reject('error1'),
|
|
122
|
+
Promise.resolve('success'),
|
|
123
|
+
Promise.reject('error2')
|
|
124
|
+
]);
|
|
125
|
+
// result = 'success'
|
|
126
|
+
} catch (error) {
|
|
127
|
+
// Throws if zero or multiple promises fulfill
|
|
128
|
+
}
|
|
129
|
+
```
|
|
86
130
|
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
riskyOperation1(),
|
|
90
|
-
riskyOperation2()
|
|
91
|
-
]);
|
|
131
|
+
### `nand(promises)`
|
|
132
|
+
Not AND - resolves when not all promises fulfill.
|
|
92
133
|
|
|
93
|
-
|
|
94
|
-
const
|
|
95
|
-
|
|
96
|
-
|
|
134
|
+
```javascript
|
|
135
|
+
const results = await PromiseLogic.nand([
|
|
136
|
+
Promise.resolve('success'),
|
|
137
|
+
Promise.reject('error')
|
|
97
138
|
]);
|
|
139
|
+
// results = ['success']
|
|
140
|
+
```
|
|
98
141
|
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
validation1(),
|
|
102
|
-
validation2(),
|
|
103
|
-
validation3()
|
|
104
|
-
]);
|
|
142
|
+
### `nor(promises)`
|
|
143
|
+
Not OR - resolves only when all promises reject.
|
|
105
144
|
|
|
106
|
-
|
|
107
|
-
const
|
|
108
|
-
|
|
109
|
-
|
|
145
|
+
```javascript
|
|
146
|
+
const results = await PromiseLogic.nor([
|
|
147
|
+
Promise.reject('error1'),
|
|
148
|
+
Promise.reject('error2')
|
|
110
149
|
]);
|
|
150
|
+
// results = []
|
|
111
151
|
```
|
|
112
152
|
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
### Core Logic Gates
|
|
116
|
-
|
|
117
|
-
#### `PromiseLogic.and(iterable)`
|
|
118
|
-
Equivalent to `Promise.all`. Resolves when all promises fulfill, rejects if any promise rejects.
|
|
119
|
-
|
|
120
|
-
#### `PromiseLogic.or(iterable)`
|
|
121
|
-
Resolves with the first successful promise. Rejects only if all promises reject.
|
|
122
|
-
|
|
123
|
-
#### `PromiseLogic.xor(iterable)`
|
|
124
|
-
Resolves when exactly one promise fulfills. Rejects if zero or multiple promises fulfill.
|
|
125
|
-
|
|
126
|
-
#### `PromiseLogic.nand(iterable)`
|
|
127
|
-
Resolves when not all promises fulfill (at least one rejection). Rejects if all promises fulfill.
|
|
128
|
-
|
|
129
|
-
#### `PromiseLogic.nor(iterable)`
|
|
130
|
-
Resolves when all promises reject. Rejects if any promise fulfills.
|
|
131
|
-
|
|
132
|
-
#### `PromiseLogic.xnor(iterable)`
|
|
133
|
-
Resolves when all promises fulfill or all reject. Rejects in mixed scenarios.
|
|
134
|
-
|
|
135
|
-
#### `PromiseLogic.majority(iterable)`
|
|
136
|
-
Resolves when more than half of promises fulfill. Rejects otherwise.
|
|
137
|
-
|
|
138
|
-
### Extended Operations
|
|
139
|
-
|
|
140
|
-
#### `PromiseLogic.allFulfilled(iterable)`
|
|
141
|
-
Always resolves with an array of fulfilled values (empty if none).
|
|
142
|
-
|
|
143
|
-
#### `PromiseLogic.allRejected(iterable)`
|
|
144
|
-
Always resolves with an array of rejection reasons (empty if none).
|
|
153
|
+
### `xnor(promises)`
|
|
154
|
+
Exclusive NOR - resolves when all promises have the same outcome.
|
|
145
155
|
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
Resolves with all rejection reasons only if every promise rejects. Rejects otherwise.
|
|
154
|
-
|
|
155
|
-
### Utility Methods
|
|
156
|
-
|
|
157
|
-
#### `PromiseLogic.race(iterable)`
|
|
158
|
-
Equivalent to `Promise.race`. Resolves or rejects with the first settled promise.
|
|
159
|
-
|
|
160
|
-
#### `PromiseLogic.allSettled(iterable)`
|
|
161
|
-
Equivalent to `Promise.allSettled`. Always resolves with settlement results.
|
|
162
|
-
|
|
163
|
-
#### `PromiseLogic.createFlipFlop(initialState?)`
|
|
164
|
-
Creates a stateful flip-flop utility for managing boolean state transitions.
|
|
165
|
-
|
|
166
|
-
## Factory Function
|
|
156
|
+
```javascript
|
|
157
|
+
const results = await PromiseLogic.xnor([
|
|
158
|
+
Promise.resolve('a'),
|
|
159
|
+
Promise.resolve('b')
|
|
160
|
+
]);
|
|
161
|
+
// results = ['a', 'b']
|
|
162
|
+
```
|
|
167
163
|
|
|
168
|
-
|
|
164
|
+
### `majority(promises)`
|
|
165
|
+
Resolves when majority (>50%) of promises fulfill.
|
|
169
166
|
|
|
170
167
|
```javascript
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
// With prefix
|
|
178
|
-
const asyncLogic = createPromiseLogic({ prefix: 'async' });
|
|
179
|
-
await asyncLogic.asyncand([promise1, promise2]);
|
|
180
|
-
|
|
181
|
-
// With suffix
|
|
182
|
-
const logicUtils = createPromiseLogic({ suffix: 'Logic' });
|
|
183
|
-
await logicUtils.andLogic([promise1, promise2]);
|
|
184
|
-
|
|
185
|
-
// With custom renaming
|
|
186
|
-
const customLogic = createPromiseLogic({
|
|
187
|
-
rename: {
|
|
188
|
-
and: 'conjunction',
|
|
189
|
-
or: 'disjunction',
|
|
190
|
-
xor: 'exclusiveOr'
|
|
191
|
-
}
|
|
192
|
-
});
|
|
193
|
-
await customLogic.conjunction([promise1, promise2]);
|
|
194
|
-
|
|
195
|
-
// Combined transformations
|
|
196
|
-
const advancedLogic = createPromiseLogic({
|
|
197
|
-
prefix: 'async',
|
|
198
|
-
suffix: 'Logic',
|
|
199
|
-
rename: { and: 'conjunction' }
|
|
200
|
-
});
|
|
201
|
-
await advancedLogic.asyncconjunctionLogic([promise1, promise2]);
|
|
168
|
+
const results = await PromiseLogic.majority([
|
|
169
|
+
Promise.resolve('a'),
|
|
170
|
+
Promise.resolve('b'),
|
|
171
|
+
Promise.reject('error')
|
|
172
|
+
]);
|
|
173
|
+
// results = ['a', 'b']
|
|
202
174
|
```
|
|
203
175
|
|
|
204
|
-
##
|
|
176
|
+
## NOT Gate - New in v2.3.2
|
|
205
177
|
|
|
206
|
-
###
|
|
178
|
+
### `not(promise)`
|
|
179
|
+
Inverts promise resolution - successful promises become rejections, and vice versa.
|
|
207
180
|
|
|
208
181
|
```javascript
|
|
209
|
-
//
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
// Multi-step validation
|
|
217
|
-
const isValid = await PromiseLogic.and([
|
|
218
|
-
validateEmail(email),
|
|
219
|
-
validatePassword(password),
|
|
220
|
-
checkRateLimit(ipAddress)
|
|
221
|
-
]);
|
|
182
|
+
// Success -> Failure
|
|
183
|
+
try {
|
|
184
|
+
await PromiseLogic.not(Promise.resolve('success'));
|
|
185
|
+
} catch (error) {
|
|
186
|
+
console.log(error); // 'success'
|
|
187
|
+
}
|
|
222
188
|
|
|
223
|
-
//
|
|
224
|
-
const
|
|
225
|
-
|
|
226
|
-
configServer2.getConfig(),
|
|
227
|
-
configServer3.getConfig()
|
|
228
|
-
]);
|
|
189
|
+
// Failure -> Success
|
|
190
|
+
const result = await PromiseLogic.not(Promise.reject('error'));
|
|
191
|
+
console.log(result); // 'error'
|
|
229
192
|
```
|
|
230
193
|
|
|
231
|
-
|
|
194
|
+
**Use Cases:**
|
|
195
|
+
- Transform error handling patterns
|
|
196
|
+
- Create conditional promise flows
|
|
197
|
+
- Implement retry logic with inverted conditions
|
|
198
|
+
- Build fallback mechanisms
|
|
232
199
|
|
|
233
|
-
|
|
234
|
-
// Graceful degradation
|
|
235
|
-
const [successfulResults, failedOperations] = await Promise.all([
|
|
236
|
-
PromiseLogic.allFulfilled(operations),
|
|
237
|
-
PromiseLogic.allRejected(operations)
|
|
238
|
-
]);
|
|
200
|
+
## Advanced Utilities
|
|
239
201
|
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
const successful = results.filter(r => r.status === 'fulfilled');
|
|
243
|
-
const failed = results.filter(r => r.status === 'rejected');
|
|
202
|
+
### `race(promises)`
|
|
203
|
+
Equivalent to `Promise.race()`.
|
|
244
204
|
|
|
245
|
-
|
|
246
|
-
|
|
205
|
+
### `allSettled(promises)`
|
|
206
|
+
Equivalent to `Promise.allSettled()`.
|
|
247
207
|
|
|
248
|
-
###
|
|
208
|
+
### `allFulfilled(promises)`
|
|
209
|
+
Resolves with all fulfilled values, ignoring rejections.
|
|
249
210
|
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
const toggle = PromiseLogic.createFlipFlop(false);
|
|
211
|
+
### `allRejected(promises)`
|
|
212
|
+
Resolves with all rejection reasons, ignoring fulfillments.
|
|
253
213
|
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
console.log(toggle.getState()); // true
|
|
214
|
+
### `createFlipFlop(initialState?)`
|
|
215
|
+
Creates a stateful flip-flop for managing boolean state across async operations.
|
|
257
216
|
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
```
|
|
217
|
+
```javascript
|
|
218
|
+
const flipFlop = PromiseLogic.createFlipFlop(false);
|
|
261
219
|
|
|
262
|
-
|
|
220
|
+
// Get current state
|
|
221
|
+
console.log(flipFlop.getState()); // false
|
|
263
222
|
|
|
264
|
-
|
|
223
|
+
// Toggle state
|
|
224
|
+
await flipFlop.toggle();
|
|
225
|
+
console.log(flipFlop.getState()); // true
|
|
265
226
|
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
}
|
|
273
|
-
console.log('Failed promises:', error.results);
|
|
274
|
-
}
|
|
227
|
+
// Wait for specific state
|
|
228
|
+
await flipFlop.waitFor(true); // Resolves immediately if already true
|
|
229
|
+
|
|
230
|
+
// Async state change
|
|
231
|
+
setTimeout(() => flipFlop.set(false), 100);
|
|
232
|
+
await flipFlop.waitForChange(); // Waits for state change
|
|
275
233
|
```
|
|
276
234
|
|
|
277
|
-
## TypeScript Support
|
|
235
|
+
## TypeScript Support - Enhanced in v2.3.2
|
|
236
|
+
|
|
237
|
+
The TypeScript version (`promise-logic/typescript`) provides:
|
|
278
238
|
|
|
279
|
-
Full
|
|
239
|
+
- **Full Type Inference**: Automatic type deduction for all operations
|
|
240
|
+
- **Strict Type Checking**: Zero `any` types, complete type safety
|
|
241
|
+
- **IDE Support**: Enhanced IntelliSense and code completion
|
|
242
|
+
- **Generic Type Propagation**: Proper handling of complex generic scenarios
|
|
280
243
|
|
|
281
244
|
```typescript
|
|
282
|
-
import { PromiseLogic } from 'promise-logic';
|
|
245
|
+
import { PromiseLogic } from 'promise-logic/typescript';
|
|
283
246
|
|
|
284
|
-
//
|
|
285
|
-
const
|
|
286
|
-
Promise.resolve(1),
|
|
287
|
-
Promise.resolve(2)
|
|
288
|
-
Promise.resolve(3)
|
|
247
|
+
// TypeScript infers everything
|
|
248
|
+
const result = await PromiseLogic.and([
|
|
249
|
+
Promise.resolve({ id: 1, name: 'Alice' }),
|
|
250
|
+
Promise.resolve({ id: 2, name: 'Bob' })
|
|
289
251
|
]);
|
|
252
|
+
// result type: Array<{ id: number, name: string }>
|
|
290
253
|
|
|
291
|
-
//
|
|
292
|
-
|
|
293
|
-
await PromiseLogic.
|
|
294
|
-
} catch (error: PromiseLogicError) {
|
|
295
|
-
// error has full type information
|
|
254
|
+
// Complex generic types work seamlessly
|
|
255
|
+
async function processPromises<T>(promises: Promise<T>[]): Promise<T[]> {
|
|
256
|
+
return await PromiseLogic.and(promises);
|
|
296
257
|
}
|
|
297
258
|
```
|
|
298
259
|
|
|
299
|
-
##
|
|
260
|
+
## Entry Points
|
|
300
261
|
|
|
301
|
-
|
|
262
|
+
| Import Path | Purpose | Recommended For |
|
|
263
|
+
|------------|---------|----------------|
|
|
264
|
+
| `promise-logic` | Default JavaScript version | General use, mixed codebases |
|
|
265
|
+
| `promise-logic/typescript` | Enhanced TypeScript version | TypeScript projects, strict type safety |
|
|
302
266
|
|
|
303
|
-
|
|
304
|
-
// Create complex logic flows
|
|
305
|
-
async function advancedOperation(promises) {
|
|
306
|
-
const [successes, failures] = await Promise.all([
|
|
307
|
-
PromiseLogic.allFulfilled(promises),
|
|
308
|
-
PromiseLogic.allRejected(promises)
|
|
309
|
-
]);
|
|
267
|
+
## Migration Note
|
|
310
268
|
|
|
311
|
-
|
|
312
|
-
return { data: successes, warnings: failures };
|
|
313
|
-
} else {
|
|
314
|
-
throw new Error('Operation mostly failed');
|
|
315
|
-
}
|
|
316
|
-
}
|
|
317
|
-
```
|
|
269
|
+
If you're upgrading from earlier versions, note that the TypeScript system has been completely redesigned for better type safety and developer experience. All existing APIs remain compatible.
|
|
318
270
|
|
|
319
|
-
|
|
271
|
+
## Development
|
|
320
272
|
|
|
321
|
-
```
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
const batchResults = await PromiseLogic.allFulfilled(
|
|
325
|
-
batches.map(batch => PromiseLogic.and(batch))
|
|
326
|
-
);
|
|
327
|
-
|
|
328
|
-
// Flatten results
|
|
329
|
-
const allResults = batchResults.flat();
|
|
330
|
-
```
|
|
273
|
+
```bash
|
|
274
|
+
# Install dependencies
|
|
275
|
+
npm install
|
|
331
276
|
|
|
332
|
-
|
|
277
|
+
# Build the project
|
|
278
|
+
npm run build
|
|
333
279
|
|
|
334
|
-
|
|
280
|
+
# Run tests
|
|
281
|
+
npm test
|
|
335
282
|
|
|
336
|
-
|
|
283
|
+
# Run tests in watch mode
|
|
284
|
+
npm run test:watch
|
|
337
285
|
|
|
338
|
-
|
|
286
|
+
# Check code coverage
|
|
287
|
+
npm run test:coverage
|
|
288
|
+
```
|
|
339
289
|
|
|
340
|
-
##
|
|
290
|
+
## Contributing
|
|
341
291
|
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
|
|
292
|
+
1. Fork the repository
|
|
293
|
+
2. Create a feature branch
|
|
294
|
+
3. Make your changes
|
|
295
|
+
4. Add tests for new functionality
|
|
296
|
+
5. Submit a pull request
|
|
345
297
|
|
|
346
|
-
|
|
298
|
+
## License
|
|
347
299
|
|
|
348
|
-
|
|
300
|
+
MIT Β© 2026
|
package/dist/factory.cjs.js
CHANGED
|
@@ -14,6 +14,7 @@ function createLogicError(type, fulfilledCount, total, results) {
|
|
|
14
14
|
const messages = {
|
|
15
15
|
XOR_ERROR: `XOR condition failed: expected exactly 1 promise to fulfill, but ${fulfilledCount} fulfilled.`,
|
|
16
16
|
NAND_ERROR: `NAND condition failed: all ${total} promises fulfilled (expected at least one rejection).`,
|
|
17
|
+
NOT_ERROR: `NOT condition failed: promise resolved (expected rejection).`,
|
|
17
18
|
NOR_ERROR: `NOR condition failed: ${fulfilledCount} promises fulfilled (expected all rejected).`,
|
|
18
19
|
MAJORITY_ERROR: `Majority condition failed: ${fulfilledCount}/${total} fulfilled (need majority).`,
|
|
19
20
|
ALL_SUCCESSFUL_ERROR: `All successful condition failed: ${fulfilledCount}/${total} promises fulfilled (expected all to succeed).`,
|
|
@@ -32,6 +33,15 @@ class PromiseLogic {
|
|
|
32
33
|
return Promise.any(iterable);
|
|
33
34
|
}
|
|
34
35
|
|
|
36
|
+
|
|
37
|
+
static not(promise) {
|
|
38
|
+
return Promise.resolve(promise).then(
|
|
39
|
+
(value) => Promise.reject(value),
|
|
40
|
+
(reason) => Promise.resolve(reason)
|
|
41
|
+
)
|
|
42
|
+
.catch((error) => Promise.resolve(error));
|
|
43
|
+
}
|
|
44
|
+
|
|
35
45
|
static race(iterable) {
|
|
36
46
|
return Promise.race(iterable);
|
|
37
47
|
}
|
|
@@ -121,6 +131,7 @@ class PromiseLogic {
|
|
|
121
131
|
});
|
|
122
132
|
}
|
|
123
133
|
|
|
134
|
+
|
|
124
135
|
// θΏεζζζεηPromiseη»ζ
|
|
125
136
|
static allFulfilled(iterable) {
|
|
126
137
|
return Promise.allSettled(iterable).then((results) => {
|
|
@@ -203,10 +214,13 @@ function createPromiseLogic(options = {}) {
|
|
|
203
214
|
race: PromiseLogic.race,
|
|
204
215
|
allSettled: PromiseLogic.allSettled,
|
|
205
216
|
xor: PromiseLogic.xor,
|
|
217
|
+
not: PromiseLogic.not,
|
|
206
218
|
nand: PromiseLogic.nand,
|
|
207
219
|
nor: PromiseLogic.nor,
|
|
208
220
|
xnor: PromiseLogic.xnor,
|
|
209
|
-
majority: PromiseLogic.majority
|
|
221
|
+
majority: PromiseLogic.majority,
|
|
222
|
+
allFulfilled: PromiseLogic.allFulfilled,
|
|
223
|
+
allRejected: PromiseLogic.allRejected
|
|
210
224
|
};
|
|
211
225
|
|
|
212
226
|
// εΊη¨ε½ε转ζ’
|