promise-logic 1.3.2 → 2.4.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/dist/index.esm.js CHANGED
@@ -12,6 +12,7 @@ function createLogicError(type, fulfilledCount, total, results) {
12
12
  const messages = {
13
13
  XOR_ERROR: `XOR condition failed: expected exactly 1 promise to fulfill, but ${fulfilledCount} fulfilled.`,
14
14
  NAND_ERROR: `NAND condition failed: all ${total} promises fulfilled (expected at least one rejection).`,
15
+ NOT_ERROR: `NOT condition failed: promise resolved (expected rejection).`,
15
16
  NOR_ERROR: `NOR condition failed: ${fulfilledCount} promises fulfilled (expected all rejected).`,
16
17
  MAJORITY_ERROR: `Majority condition failed: ${fulfilledCount}/${total} fulfilled (need majority).`,
17
18
  ALL_SUCCESSFUL_ERROR: `All successful condition failed: ${fulfilledCount}/${total} promises fulfilled (expected all to succeed).`,
@@ -30,6 +31,15 @@ class PromiseLogic {
30
31
  return Promise.any(iterable);
31
32
  }
32
33
 
34
+
35
+ static not(promise) {
36
+ return Promise.resolve(promise).then(
37
+ (value) => Promise.reject(value),
38
+ (reason) => Promise.resolve(reason)
39
+ )
40
+ .catch((error) => Promise.resolve(error));
41
+ }
42
+
33
43
  static race(iterable) {
34
44
  return Promise.race(iterable);
35
45
  }
@@ -119,6 +129,7 @@ class PromiseLogic {
119
129
  });
120
130
  }
121
131
 
132
+
122
133
  // 返回所有成功的Promise结果
123
134
  static allFulfilled(iterable) {
124
135
  return Promise.allSettled(iterable).then((results) => {
@@ -201,10 +212,13 @@ function createPromiseLogic(options = {}) {
201
212
  race: PromiseLogic.race,
202
213
  allSettled: PromiseLogic.allSettled,
203
214
  xor: PromiseLogic.xor,
215
+ not: PromiseLogic.not,
204
216
  nand: PromiseLogic.nand,
205
217
  nor: PromiseLogic.nor,
206
218
  xnor: PromiseLogic.xnor,
207
- majority: PromiseLogic.majority
219
+ majority: PromiseLogic.majority,
220
+ allFulfilled: PromiseLogic.allFulfilled,
221
+ allRejected: PromiseLogic.allRejected
208
222
  };
209
223
 
210
224
  // 应用命名转换
@@ -220,4 +234,3 @@ function createPromiseLogic(options = {}) {
220
234
  }
221
235
 
222
236
  export { PromiseLogic, PromiseLogicError, createLogicError, createPromiseLogic };
223
- //# sourceMappingURL=index.esm.js.map
@@ -0,0 +1,286 @@
1
+ 'use strict';
2
+
3
+ class BaseGate {
4
+ filterResults(results, status) {
5
+ if (status === 'fulfilled') {
6
+ return results
7
+ .filter((result) => result.status === 'fulfilled')
8
+ .map(result => result.value);
9
+ }
10
+ else {
11
+ return results
12
+ .filter((result) => result.status === 'rejected')
13
+ .map(result => result.reason);
14
+ }
15
+ }
16
+ countFulfilled(results) {
17
+ return results.filter(result => result.status === 'fulfilled').length;
18
+ }
19
+ }
20
+
21
+ class OrGate extends BaseGate {
22
+ async execute(iterable) {
23
+ return Promise.any(iterable);
24
+ }
25
+ }
26
+
27
+ class AndGate extends BaseGate {
28
+ async execute(iterable) {
29
+ try {
30
+ return Promise.all(iterable);
31
+ }
32
+ catch (error) {
33
+ throw new PromiseLogicError('AND_ERROR', 'AND gate failed', [error]);
34
+ }
35
+ }
36
+ }
37
+
38
+ let PromiseLogicError$1 = class PromiseLogicError extends Error {
39
+ constructor(type, message, results) {
40
+ super(message);
41
+ this.type = type;
42
+ this.results = results;
43
+ this.name = 'PromiseLogicError';
44
+ }
45
+ };
46
+ // Error factory function
47
+ function createLogicError(type, fulfilledCount, total, results) {
48
+ const messages = {
49
+ XOR_ERROR: `XOR condition failed: expected exactly 1 promise to fulfill, but ${fulfilledCount} fulfilled.`,
50
+ NAND_ERROR: `NAND condition failed: all ${total} promises fulfilled (expected at least one rejection).`,
51
+ NOR_ERROR: `NOR condition failed: ${fulfilledCount} promises fulfilled (expected all rejected).`,
52
+ XNOR_ERROR: `XNOR condition failed: ${fulfilledCount}/${total} promises fulfilled (expected all or none).`,
53
+ MAJORITY_ERROR: `Majority condition failed: ${fulfilledCount}/${total} fulfilled (need majority).`,
54
+ ALL_SUCCESSFUL_ERROR: `All successful condition failed: ${fulfilledCount}/${total} promises fulfilled (expected all to succeed).`,
55
+ ALL_FAILED_ERROR: `All failed condition failed: ${total - fulfilledCount}/${total} promises rejected (expected all to fail).`
56
+ };
57
+ return new PromiseLogicError$1(type, messages[type] || 'Logic condition failed', results);
58
+ }
59
+
60
+ class MajorityGate extends BaseGate {
61
+ async execute(iterable) {
62
+ const results = await Promise.allSettled(iterable);
63
+ const fulfilled = this.filterResults(results, 'fulfilled');
64
+ const fulfilledCount = fulfilled.length;
65
+ const total = results.length;
66
+ // Majority logic: success count > failure count
67
+ if (fulfilledCount > total - fulfilledCount) {
68
+ return fulfilled;
69
+ }
70
+ else {
71
+ throw createLogicError('MAJORITY_ERROR', fulfilledCount, total, results);
72
+ }
73
+ }
74
+ }
75
+
76
+ class NandGate extends BaseGate {
77
+ async execute(iterable) {
78
+ const results = await Promise.allSettled(iterable);
79
+ const fulfilled = this.filterResults(results, 'fulfilled');
80
+ const fulfilledCount = fulfilled.length;
81
+ const total = results.length;
82
+ if (fulfilledCount === total) {
83
+ // 全部成功,抛出NAND_ERROR
84
+ throw createLogicError('NAND_ERROR', fulfilledCount, total, results);
85
+ }
86
+ else {
87
+ // 不是所有都成功,返回成功的值数组
88
+ return fulfilled;
89
+ }
90
+ }
91
+ }
92
+
93
+ class NorGate extends BaseGate {
94
+ async execute(iterable) {
95
+ const results = await Promise.allSettled(iterable);
96
+ const fulfilled = this.filterResults(results, 'fulfilled');
97
+ const fulfilledCount = fulfilled.length;
98
+ const total = results.length;
99
+ if (fulfilledCount === 0) {
100
+ // 全部失败,返回空数组表示成功
101
+ return [];
102
+ }
103
+ else {
104
+ // 任意成功,抛出NOR_ERROR
105
+ throw createLogicError('NOR_ERROR', fulfilledCount, total, results);
106
+ }
107
+ }
108
+ }
109
+
110
+ class XnorGate extends BaseGate {
111
+ async execute(iterable) {
112
+ const results = await Promise.allSettled(iterable);
113
+ const fulfilled = this.filterResults(results, 'fulfilled');
114
+ const fulfilledCount = fulfilled.length;
115
+ const total = results.length;
116
+ if (fulfilledCount === 0 || fulfilledCount === total) {
117
+ // 全部成功或全部失败,返回成功的值数组
118
+ return fulfilled;
119
+ }
120
+ else {
121
+ // 部分成功部分失败,抛出XNOR_ERROR
122
+ throw createLogicError('XNOR_ERROR', fulfilledCount, total, results);
123
+ }
124
+ }
125
+ }
126
+
127
+ class XorGate extends BaseGate {
128
+ async execute(iterable) {
129
+ const results = await Promise.allSettled(iterable);
130
+ const fulfilled = results.filter(result => result.status === 'fulfilled');
131
+ const fulfilledCount = fulfilled.length;
132
+ const total = results.length;
133
+ if (fulfilledCount === 1) {
134
+ return fulfilled[0].value;
135
+ }
136
+ else {
137
+ throw createLogicError('XOR_ERROR', fulfilledCount, total, results);
138
+ }
139
+ }
140
+ }
141
+
142
+ class PromiseLogicError extends Error {
143
+ constructor(type, message, results) {
144
+ super(message);
145
+ this.type = type;
146
+ this.results = results;
147
+ this.name = 'PromiseLogicError';
148
+ }
149
+ }
150
+ class PromiseLogic {
151
+ static get gates() {
152
+ return {
153
+ and: new AndGate(),
154
+ or: new OrGate(),
155
+ xor: new XorGate(),
156
+ nand: new NandGate(),
157
+ nor: new NorGate(),
158
+ xnor: new XnorGate(),
159
+ majority: new MajorityGate()
160
+ };
161
+ }
162
+ // Core Logic Gates
163
+ static and(iterable) {
164
+ return this.gates.and.execute(iterable);
165
+ }
166
+ static or(iterable) {
167
+ return this.gates.or.execute(iterable);
168
+ }
169
+ static xor(iterable) {
170
+ return this.gates.xor.execute(iterable);
171
+ }
172
+ static nand(iterable) {
173
+ return this.gates.nand.execute(iterable);
174
+ }
175
+ static nor(iterable) {
176
+ return this.gates.nor.execute(iterable);
177
+ }
178
+ static xnor(iterable) {
179
+ return this.gates.xnor.execute(iterable);
180
+ }
181
+ static majority(iterable) {
182
+ return this.gates.majority.execute(iterable);
183
+ }
184
+ // Extended Operations
185
+ static allFulfilled(iterable) {
186
+ return Promise.allSettled(iterable).then((results) => {
187
+ const fulfilled = results.filter(result => result.status === 'fulfilled');
188
+ return fulfilled.map(result => result.value);
189
+ });
190
+ }
191
+ static allRejected(iterable) {
192
+ return Promise.allSettled(iterable).then((results) => {
193
+ return results
194
+ .filter((result) => result.status === 'rejected')
195
+ .map(result => result.reason);
196
+ });
197
+ }
198
+ // NOT logic - Inverts promise resolution
199
+ static not(promise) {
200
+ return Promise.resolve(promise).then((value) => Promise.reject(value), (reason) => Promise.resolve(reason));
201
+ }
202
+ // Utility Methods
203
+ static race(iterable) {
204
+ return Promise.race(iterable);
205
+ }
206
+ static allSettled(iterable) {
207
+ return Promise.allSettled(iterable);
208
+ }
209
+ static createFlipFlop(initialState = false) {
210
+ let state = initialState;
211
+ let resolveCurrent = null;
212
+ let currentPromise = Promise.resolve(state);
213
+ const waitForChange = () => {
214
+ if (!resolveCurrent) {
215
+ currentPromise = new Promise((resolve) => {
216
+ resolveCurrent = resolve;
217
+ });
218
+ }
219
+ return currentPromise;
220
+ };
221
+ return {
222
+ getState() {
223
+ return state;
224
+ },
225
+ async set(newState) {
226
+ state = newState;
227
+ if (resolveCurrent) {
228
+ resolveCurrent(state);
229
+ resolveCurrent = null;
230
+ }
231
+ currentPromise = Promise.resolve(state);
232
+ return state;
233
+ },
234
+ async toggle() {
235
+ return this.set(!state);
236
+ },
237
+ waitForChange,
238
+ waitFor(targetState) {
239
+ if (state === targetState) {
240
+ return Promise.resolve(state);
241
+ }
242
+ return new Promise((resolve) => {
243
+ const checkState = () => {
244
+ if (state === targetState) {
245
+ resolve(state);
246
+ }
247
+ else {
248
+ waitForChange().then(checkState);
249
+ }
250
+ };
251
+ checkState();
252
+ });
253
+ }
254
+ };
255
+ }
256
+ }
257
+
258
+ function createPromiseLogic(options = {}) {
259
+ const { prefix = '', suffix = '', rename = {} } = options;
260
+ // 基础方法映射
261
+ const methods = {
262
+ and: PromiseLogic.and,
263
+ or: PromiseLogic.or,
264
+ not: PromiseLogic.not,
265
+ race: PromiseLogic.race,
266
+ allSettled: PromiseLogic.allSettled,
267
+ xor: PromiseLogic.xor,
268
+ nand: PromiseLogic.nand,
269
+ nor: PromiseLogic.nor,
270
+ xnor: PromiseLogic.xnor,
271
+ majority: PromiseLogic.majority,
272
+ allFulfilled: PromiseLogic.allFulfilled,
273
+ allRejected: PromiseLogic.allRejected
274
+ };
275
+ // 应用命名转换
276
+ const result = {};
277
+ Object.entries(methods).forEach(([key, fn]) => {
278
+ // 优先使用rename映射,然后应用prefix和suffix
279
+ const baseName = rename[key] || key;
280
+ const finalName = `${prefix}${baseName}${suffix}`;
281
+ result[finalName] = fn;
282
+ });
283
+ return result;
284
+ }
285
+
286
+ exports.createPromiseLogic = createPromiseLogic;
@@ -0,0 +1,284 @@
1
+ class BaseGate {
2
+ filterResults(results, status) {
3
+ if (status === 'fulfilled') {
4
+ return results
5
+ .filter((result) => result.status === 'fulfilled')
6
+ .map(result => result.value);
7
+ }
8
+ else {
9
+ return results
10
+ .filter((result) => result.status === 'rejected')
11
+ .map(result => result.reason);
12
+ }
13
+ }
14
+ countFulfilled(results) {
15
+ return results.filter(result => result.status === 'fulfilled').length;
16
+ }
17
+ }
18
+
19
+ class OrGate extends BaseGate {
20
+ async execute(iterable) {
21
+ return Promise.any(iterable);
22
+ }
23
+ }
24
+
25
+ class AndGate extends BaseGate {
26
+ async execute(iterable) {
27
+ try {
28
+ return Promise.all(iterable);
29
+ }
30
+ catch (error) {
31
+ throw new PromiseLogicError('AND_ERROR', 'AND gate failed', [error]);
32
+ }
33
+ }
34
+ }
35
+
36
+ let PromiseLogicError$1 = class PromiseLogicError extends Error {
37
+ constructor(type, message, results) {
38
+ super(message);
39
+ this.type = type;
40
+ this.results = results;
41
+ this.name = 'PromiseLogicError';
42
+ }
43
+ };
44
+ // Error factory function
45
+ function createLogicError(type, fulfilledCount, total, results) {
46
+ const messages = {
47
+ XOR_ERROR: `XOR condition failed: expected exactly 1 promise to fulfill, but ${fulfilledCount} fulfilled.`,
48
+ NAND_ERROR: `NAND condition failed: all ${total} promises fulfilled (expected at least one rejection).`,
49
+ NOR_ERROR: `NOR condition failed: ${fulfilledCount} promises fulfilled (expected all rejected).`,
50
+ XNOR_ERROR: `XNOR condition failed: ${fulfilledCount}/${total} promises fulfilled (expected all or none).`,
51
+ MAJORITY_ERROR: `Majority condition failed: ${fulfilledCount}/${total} fulfilled (need majority).`,
52
+ ALL_SUCCESSFUL_ERROR: `All successful condition failed: ${fulfilledCount}/${total} promises fulfilled (expected all to succeed).`,
53
+ ALL_FAILED_ERROR: `All failed condition failed: ${total - fulfilledCount}/${total} promises rejected (expected all to fail).`
54
+ };
55
+ return new PromiseLogicError$1(type, messages[type] || 'Logic condition failed', results);
56
+ }
57
+
58
+ class MajorityGate extends BaseGate {
59
+ async execute(iterable) {
60
+ const results = await Promise.allSettled(iterable);
61
+ const fulfilled = this.filterResults(results, 'fulfilled');
62
+ const fulfilledCount = fulfilled.length;
63
+ const total = results.length;
64
+ // Majority logic: success count > failure count
65
+ if (fulfilledCount > total - fulfilledCount) {
66
+ return fulfilled;
67
+ }
68
+ else {
69
+ throw createLogicError('MAJORITY_ERROR', fulfilledCount, total, results);
70
+ }
71
+ }
72
+ }
73
+
74
+ class NandGate extends BaseGate {
75
+ async execute(iterable) {
76
+ const results = await Promise.allSettled(iterable);
77
+ const fulfilled = this.filterResults(results, 'fulfilled');
78
+ const fulfilledCount = fulfilled.length;
79
+ const total = results.length;
80
+ if (fulfilledCount === total) {
81
+ // 全部成功,抛出NAND_ERROR
82
+ throw createLogicError('NAND_ERROR', fulfilledCount, total, results);
83
+ }
84
+ else {
85
+ // 不是所有都成功,返回成功的值数组
86
+ return fulfilled;
87
+ }
88
+ }
89
+ }
90
+
91
+ class NorGate extends BaseGate {
92
+ async execute(iterable) {
93
+ const results = await Promise.allSettled(iterable);
94
+ const fulfilled = this.filterResults(results, 'fulfilled');
95
+ const fulfilledCount = fulfilled.length;
96
+ const total = results.length;
97
+ if (fulfilledCount === 0) {
98
+ // 全部失败,返回空数组表示成功
99
+ return [];
100
+ }
101
+ else {
102
+ // 任意成功,抛出NOR_ERROR
103
+ throw createLogicError('NOR_ERROR', fulfilledCount, total, results);
104
+ }
105
+ }
106
+ }
107
+
108
+ class XnorGate extends BaseGate {
109
+ async execute(iterable) {
110
+ const results = await Promise.allSettled(iterable);
111
+ const fulfilled = this.filterResults(results, 'fulfilled');
112
+ const fulfilledCount = fulfilled.length;
113
+ const total = results.length;
114
+ if (fulfilledCount === 0 || fulfilledCount === total) {
115
+ // 全部成功或全部失败,返回成功的值数组
116
+ return fulfilled;
117
+ }
118
+ else {
119
+ // 部分成功部分失败,抛出XNOR_ERROR
120
+ throw createLogicError('XNOR_ERROR', fulfilledCount, total, results);
121
+ }
122
+ }
123
+ }
124
+
125
+ class XorGate extends BaseGate {
126
+ async execute(iterable) {
127
+ const results = await Promise.allSettled(iterable);
128
+ const fulfilled = results.filter(result => result.status === 'fulfilled');
129
+ const fulfilledCount = fulfilled.length;
130
+ const total = results.length;
131
+ if (fulfilledCount === 1) {
132
+ return fulfilled[0].value;
133
+ }
134
+ else {
135
+ throw createLogicError('XOR_ERROR', fulfilledCount, total, results);
136
+ }
137
+ }
138
+ }
139
+
140
+ class PromiseLogicError extends Error {
141
+ constructor(type, message, results) {
142
+ super(message);
143
+ this.type = type;
144
+ this.results = results;
145
+ this.name = 'PromiseLogicError';
146
+ }
147
+ }
148
+ class PromiseLogic {
149
+ static get gates() {
150
+ return {
151
+ and: new AndGate(),
152
+ or: new OrGate(),
153
+ xor: new XorGate(),
154
+ nand: new NandGate(),
155
+ nor: new NorGate(),
156
+ xnor: new XnorGate(),
157
+ majority: new MajorityGate()
158
+ };
159
+ }
160
+ // Core Logic Gates
161
+ static and(iterable) {
162
+ return this.gates.and.execute(iterable);
163
+ }
164
+ static or(iterable) {
165
+ return this.gates.or.execute(iterable);
166
+ }
167
+ static xor(iterable) {
168
+ return this.gates.xor.execute(iterable);
169
+ }
170
+ static nand(iterable) {
171
+ return this.gates.nand.execute(iterable);
172
+ }
173
+ static nor(iterable) {
174
+ return this.gates.nor.execute(iterable);
175
+ }
176
+ static xnor(iterable) {
177
+ return this.gates.xnor.execute(iterable);
178
+ }
179
+ static majority(iterable) {
180
+ return this.gates.majority.execute(iterable);
181
+ }
182
+ // Extended Operations
183
+ static allFulfilled(iterable) {
184
+ return Promise.allSettled(iterable).then((results) => {
185
+ const fulfilled = results.filter(result => result.status === 'fulfilled');
186
+ return fulfilled.map(result => result.value);
187
+ });
188
+ }
189
+ static allRejected(iterable) {
190
+ return Promise.allSettled(iterable).then((results) => {
191
+ return results
192
+ .filter((result) => result.status === 'rejected')
193
+ .map(result => result.reason);
194
+ });
195
+ }
196
+ // NOT logic - Inverts promise resolution
197
+ static not(promise) {
198
+ return Promise.resolve(promise).then((value) => Promise.reject(value), (reason) => Promise.resolve(reason));
199
+ }
200
+ // Utility Methods
201
+ static race(iterable) {
202
+ return Promise.race(iterable);
203
+ }
204
+ static allSettled(iterable) {
205
+ return Promise.allSettled(iterable);
206
+ }
207
+ static createFlipFlop(initialState = false) {
208
+ let state = initialState;
209
+ let resolveCurrent = null;
210
+ let currentPromise = Promise.resolve(state);
211
+ const waitForChange = () => {
212
+ if (!resolveCurrent) {
213
+ currentPromise = new Promise((resolve) => {
214
+ resolveCurrent = resolve;
215
+ });
216
+ }
217
+ return currentPromise;
218
+ };
219
+ return {
220
+ getState() {
221
+ return state;
222
+ },
223
+ async set(newState) {
224
+ state = newState;
225
+ if (resolveCurrent) {
226
+ resolveCurrent(state);
227
+ resolveCurrent = null;
228
+ }
229
+ currentPromise = Promise.resolve(state);
230
+ return state;
231
+ },
232
+ async toggle() {
233
+ return this.set(!state);
234
+ },
235
+ waitForChange,
236
+ waitFor(targetState) {
237
+ if (state === targetState) {
238
+ return Promise.resolve(state);
239
+ }
240
+ return new Promise((resolve) => {
241
+ const checkState = () => {
242
+ if (state === targetState) {
243
+ resolve(state);
244
+ }
245
+ else {
246
+ waitForChange().then(checkState);
247
+ }
248
+ };
249
+ checkState();
250
+ });
251
+ }
252
+ };
253
+ }
254
+ }
255
+
256
+ function createPromiseLogic(options = {}) {
257
+ const { prefix = '', suffix = '', rename = {} } = options;
258
+ // 基础方法映射
259
+ const methods = {
260
+ and: PromiseLogic.and,
261
+ or: PromiseLogic.or,
262
+ not: PromiseLogic.not,
263
+ race: PromiseLogic.race,
264
+ allSettled: PromiseLogic.allSettled,
265
+ xor: PromiseLogic.xor,
266
+ nand: PromiseLogic.nand,
267
+ nor: PromiseLogic.nor,
268
+ xnor: PromiseLogic.xnor,
269
+ majority: PromiseLogic.majority,
270
+ allFulfilled: PromiseLogic.allFulfilled,
271
+ allRejected: PromiseLogic.allRejected
272
+ };
273
+ // 应用命名转换
274
+ const result = {};
275
+ Object.entries(methods).forEach(([key, fn]) => {
276
+ // 优先使用rename映射,然后应用prefix和suffix
277
+ const baseName = rename[key] || key;
278
+ const finalName = `${prefix}${baseName}${suffix}`;
279
+ result[finalName] = fn;
280
+ });
281
+ return result;
282
+ }
283
+
284
+ export { createPromiseLogic };