promise-logic 2.5.7 → 2.6.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
@@ -31,6 +31,14 @@ Traditional Promise combinations (such as `Promise.all`, `Promise.race`) have na
31
31
  - `PromiseLogicError` unified error type
32
32
  - `error.type` distinguishes specific logical errors (e.g., `'XOR_ERROR'`)
33
33
 
34
+ 5. **Timeout Control**
35
+ - `maxTimer`: Adds timeout functionality to any Promise operation
36
+
37
+ 6. **Extended Operations**
38
+ - `allFulfilled`: Returns all successful results
39
+ - `allRejected`: Returns all failed results
40
+ - `allSettled`: Returns all results (both successful and failed)
41
+
34
42
  ---
35
43
 
36
44
  ### **3. Installation**
@@ -103,18 +111,93 @@ PromiseLogic.majority<Response>(services)
103
111
  });
104
112
  ```
105
113
 
114
+ #### Example: Timeout Control
115
+ ```javascript
116
+ import { PromiseLogic } from 'promise-logic';
117
+
118
+ // Execute operation with timeout
119
+ PromiseLogic.and([
120
+ Promise.resolve(1),
121
+ new Promise(resolve => setTimeout(resolve, 1000)), // 1 second operation
122
+ Promise.resolve(3)
123
+ ])
124
+ .maxTimer(2000) // 2 second timeout
125
+ .then(result => {
126
+ console.log('Operation completed within timeout:', result);
127
+ })
128
+ .catch(error => {
129
+ console.error('Operation timed out or failed:', error.message);
130
+ });
131
+ ```
132
+
133
+ #### Example: Extended Operations
134
+ ```javascript
135
+ import { PromiseLogic } from 'promise-logic';
136
+
137
+ const operations = [
138
+ Promise.resolve('success1'),
139
+ Promise.reject('error1'),
140
+ Promise.resolve('success2'),
141
+ Promise.reject('error2')
142
+ ];
143
+
144
+ // Get all successful results
145
+ PromiseLogic.allFulfilled(operations)
146
+ .then(results => {
147
+ console.log('Successful results:', results); // ['success1', 'success2']
148
+ });
149
+
150
+ // Get all failed results
151
+ PromiseLogic.allRejected(operations)
152
+ .then(errors => {
153
+ console.log('Failed results:', errors); // ['error1', 'error2']
154
+ });
155
+
156
+ // Get all results (both success and failure)
157
+ PromiseLogic.allSettled(operations)
158
+ .then(results => {
159
+ console.log('All results:', results);
160
+ });
161
+ ```
162
+
163
+ #### Example: Custom majority threshold
164
+ ```javascript
165
+ import { PromiseLogic } from 'promise-logic';
166
+
167
+ const services = [
168
+ Promise.resolve('service1'),
169
+ Promise.resolve('service2'),
170
+ Promise.reject('service3'),
171
+ Promise.reject('service4')
172
+ ];
173
+
174
+ // Default threshold (0.5): requires at least 3 successes
175
+ // Custom threshold (0.4): requires at least 2 successes
176
+ PromiseLogic.majority(services, { max: 0.4 })
177
+ .then(results => {
178
+ console.log('Custom threshold met, successful results:', results); // ['service1', 'service2']
179
+ })
180
+ .catch(error => {
181
+ console.error('Custom threshold not met:', error);
182
+ });
183
+ ```
184
+
106
185
  ---
107
186
 
108
187
  ### **5. API Reference**
109
188
 
110
- | API | Description |
111
- | :--------- | :-------------------------------------------------------------------------- |
112
- | `and` | All Promises succeed, returns result array; any failure causes overall failure. |
113
- | `or` | At least one Promise succeeds, returns first success result; all failures cause overall failure. |
114
- | `xor` | **Exactly one Promise succeeds**, returns that result; otherwise throws `XOR_ERROR`. |
115
- | `nand` | All Promises fail, returns error array; any success causes overall failure. |
116
- | `not` | Inverts the result of a single Promise |
117
- | `majority` | More than half of Promises succeed, returns success result array; otherwise overall failure. |
189
+ | API | Description |
190
+ | :------------ | :-------------------------------------------------------------------------- |
191
+ | `and` | All Promises succeed, returns result array; any failure causes overall failure. |
192
+ | `or` | At least one Promise succeeds, returns first success result; all failures cause overall failure. |
193
+ | `xor` | **Exactly one Promise succeeds**, returns that result; otherwise throws `XOR_ERROR`. |
194
+ | `nand` | All Promises fail, returns error array; any success causes overall failure. |
195
+ | `not` | Inverts the result of a single Promise |
196
+ | `majority` | More than half of Promises succeed, returns success result array; otherwise overall failure. Accepts `options` parameter, where `max` property can customize threshold (default: 0.5). |
197
+ | `allFulfilled` | Returns all successful results as an array, ignoring failures. |
198
+ | `allRejected` | Returns all failed results as an array, ignoring successes. |
199
+ | `allSettled` | Returns all results (both successful and failed) as an array. |
200
+ | `maxTimer` | Adds timeout functionality to any Promise operation (unit: milliseconds). |
118
201
 
119
202
  ---
120
203
 
@@ -1 +1 @@
1
- "use strict";class e extends Error{constructor(e,t,l){super(t),this.name="PromiseLogicError",this.type=e,this.results=l}}function t(t,l,r,i){return new e(t,{XOR_ERROR:`XOR condition failed: expected exactly 1 promise to fulfill, but ${l} fulfilled.`,NAND_ERROR:`NAND condition failed: all ${r} promises fulfilled (expected at least one rejection).`,NOT_ERROR:"NOT condition failed: promise resolved (expected rejection).",NOR_ERROR:`NOR condition failed: ${l} promises fulfilled (expected all rejected).`,MAJORITY_ERROR:`Majority condition failed: ${l}/${r} fulfilled (need majority).`,ALL_SUCCESSFUL_ERROR:`All successful condition failed: ${l}/${r} promises fulfilled (expected all to succeed).`,ALL_FAILED_ERROR:`All failed condition failed: ${r-l}/${r} promises rejected (expected all to fail).`}[t]||"Logic condition failed",i)}class l{static and(e){return Promise.all(e)}static or(e){return Promise.any(e)}static not(e){return Promise.resolve(e).then(e=>Promise.reject(e),e=>Promise.resolve(e)).catch(e=>Promise.resolve(e))}static race(e){return Promise.race(e)}static allSettled(e){return Promise.allSettled(e)}static xor(e){return Promise.allSettled(e).then(e=>{const l=e.filter(e=>"fulfilled"===e.status),r=l.length,i=e.length;if(1===r)return l[0].value;throw t("XOR_ERROR",r,i,e)})}static nand(e){return Promise.allSettled(e).then(e=>{const l=e.filter(e=>"fulfilled"===e.status),r=l.length,i=e.length;if(r===i)throw t("NAND_ERROR",r,i,e);return l.map(e=>e.value)})}static nor(e){return Promise.allSettled(e).then(e=>{const l=e.filter(e=>"fulfilled"===e.status).length,r=e.length;if(0===l)return[];throw t("NOR_ERROR",l,r,e)})}static xnor(e){return Promise.allSettled(e).then(e=>{const l=e.filter(e=>"fulfilled"===e.status),r=l.length,i=e.length;if(0===r||r===i)return l.map(e=>e.value);throw t("XNOR_ERROR",r,i,e)})}static majority(e){return Promise.allSettled(e).then(e=>{const l=e.filter(e=>"fulfilled"===e.status),r=l.length,i=e.length;if(r>i-r)return l.map(e=>e.value);throw t("MAJORITY_ERROR",r,i,e)})}static allFulfilled(e){return Promise.allSettled(e).then(e=>e.filter(e=>"fulfilled"===e.status).map(e=>e.value))}static allRejected(e){return Promise.allSettled(e).then(e=>e.filter(e=>"rejected"===e.status).map(e=>e.reason))}static createFlipFlop(e=!1){let t=e,l=null,r=Promise.resolve(t);return{getState:()=>t,set(e){return t=e,l&&(l(t),l=null),r=Promise.resolve(t),this},toggle(){return this.set(!t)},waitForChange:()=>(l||(r=new Promise(e=>{l=e})),r),waitFor(e){return t===e?Promise.resolve(t):new Promise(l=>{const r=()=>{t===e?l(t):this.waitForChange().then(r)};r()})}}}}exports.createPromiseLogic=function(e={}){const{prefix:t="",suffix:r="",rename:i={}}=e,o={and:l.and,or:l.or,race:l.race,allSettled:l.allSettled,xor:l.xor,not:l.not,nand:l.nand,nor:l.nor,xnor:l.xnor,majority:l.majority,allFulfilled:l.allFulfilled,allRejected:l.allRejected},n={};return Object.entries(o).forEach(([e,l])=>{const o=i[e]||e;n[`${t}${o}${r}`]=l}),n};
1
+ "use strict";class e extends Error{constructor(e,t,r){super(t),this.name="PromiseLogicError",this.type=e,this.results=r}}function t(t,r,l,i){return new e(t,{XOR_ERROR:`XOR condition failed: expected exactly 1 promise to fulfill, but ${r} fulfilled.`,NAND_ERROR:`NAND condition failed: all ${l} promises fulfilled (expected at least one rejection).`,NOT_ERROR:"NOT condition failed: promise resolved (expected rejection).",NOR_ERROR:`NOR condition failed: ${r} promises fulfilled (expected all rejected).`,MAJORITY_ERROR:`Majority condition failed: ${r}/${l} fulfilled (need majority).`,ALL_SUCCESSFUL_ERROR:`All successful condition failed: ${r}/${l} promises fulfilled (expected all to succeed).`,ALL_FAILED_ERROR:`All failed condition failed: ${l-r}/${l} promises rejected (expected all to fail).`}[t]||"Logic condition failed",i)}class r{constructor(e){this.promise=e}maxTimer(e){let t;return Promise.race([this.promise,new Promise((r,l)=>{t=setTimeout(()=>{l(new Error(`Promise timed out after ${e}ms`))},e)})]).finally(()=>clearTimeout(t))}then(e,t){return new r(this.promise.then(e,t))}catch(e){return new r(this.promise.catch(e))}finally(e){return new r(this.promise.finally(e))}toPromise(){return this.promise}}class l{static and(e){return new r(Promise.all(e))}static or(e){return new r(Promise.any(e))}static not(e){return new r(Promise.resolve(e).then(e=>Promise.reject(e),e=>Promise.resolve(e)).catch(e=>Promise.resolve(e)))}static race(e){return new r(Promise.race(e))}static allSettled(e){return new r(Promise.allSettled(e))}static xor(e){return new r(Promise.allSettled(e).then(e=>{const r=e.filter(e=>"fulfilled"===e.status),l=r.length,i=e.length;if(1===l)return r[0].value;throw t("XOR_ERROR",l,i,e)}))}static nand(e){return new r(Promise.allSettled(e).then(e=>{const r=e.filter(e=>"fulfilled"===e.status),l=r.length,i=e.length;if(l===i)throw t("NAND_ERROR",l,i,e);return r.map(e=>e.value)}))}static nor(e){return new r(Promise.allSettled(e).then(e=>{const r=e.filter(e=>"fulfilled"===e.status).length,l=e.length;if(0===r)return[];throw t("NOR_ERROR",r,l,e)}))}static xnor(e){return new r(Promise.allSettled(e).then(e=>{const r=e.filter(e=>"fulfilled"===e.status),l=r.length,i=e.length;if(0===l||l===i)return r.map(e=>e.value);throw t("XNOR_ERROR",l,i,e)}))}static majority(e,l={max:.5}){return new r(Promise.allSettled(e).then(e=>{const r=e.filter(e=>"fulfilled"===e.status),i=r.length,n=e.length;if(i>n*l.max)return r.map(e=>e.value);throw t("MAJORITY_ERROR",i,n,e)}))}static allFulfilled(e){return new r(Promise.allSettled(e).then(e=>e.filter(e=>"fulfilled"===e.status).map(e=>e.value)))}static allRejected(e){return new r(Promise.allSettled(e).then(e=>e.filter(e=>"rejected"===e.status).map(e=>e.reason)))}static createFlipFlop(e=!1){let t=e,r=null,l=Promise.resolve(t);return{getState:()=>t,set(e){return t=e,r&&(r(t),r=null),l=Promise.resolve(t),this},toggle(){return this.set(!t)},waitForChange:()=>(r||(l=new Promise(e=>{r=e})),l),waitFor(e){return t===e?Promise.resolve(t):new Promise(r=>{const l=()=>{t===e?r(t):this.waitForChange().then(l)};l()})}}}}exports.createPromiseLogic=function(e={}){const{prefix:t="",suffix:r="",rename:i={}}=e,n={and:l.and.bind(l),or:l.or.bind(l),race:l.race.bind(l),allSettled:l.allSettled.bind(l),xor:l.xor.bind(l),not:l.not.bind(l),nand:l.nand.bind(l),nor:l.nor.bind(l),xnor:l.xnor.bind(l),majority:l.majority.bind(l),allFulfilled:l.allFulfilled.bind(l),allRejected:l.allRejected.bind(l)},o={};return Object.entries(n).forEach(([e,l])=>{const n=i[e]||e;o[`${t}${n}${r}`]=l}),o};
@@ -1 +1 @@
1
- class e extends Error{constructor(e,t,l){super(t),this.name="PromiseLogicError",this.type=e,this.results=l}}function t(t,l,r,i){return new e(t,{XOR_ERROR:`XOR condition failed: expected exactly 1 promise to fulfill, but ${l} fulfilled.`,NAND_ERROR:`NAND condition failed: all ${r} promises fulfilled (expected at least one rejection).`,NOT_ERROR:"NOT condition failed: promise resolved (expected rejection).",NOR_ERROR:`NOR condition failed: ${l} promises fulfilled (expected all rejected).`,MAJORITY_ERROR:`Majority condition failed: ${l}/${r} fulfilled (need majority).`,ALL_SUCCESSFUL_ERROR:`All successful condition failed: ${l}/${r} promises fulfilled (expected all to succeed).`,ALL_FAILED_ERROR:`All failed condition failed: ${r-l}/${r} promises rejected (expected all to fail).`}[t]||"Logic condition failed",i)}class l{static and(e){return Promise.all(e)}static or(e){return Promise.any(e)}static not(e){return Promise.resolve(e).then(e=>Promise.reject(e),e=>Promise.resolve(e)).catch(e=>Promise.resolve(e))}static race(e){return Promise.race(e)}static allSettled(e){return Promise.allSettled(e)}static xor(e){return Promise.allSettled(e).then(e=>{const l=e.filter(e=>"fulfilled"===e.status),r=l.length,i=e.length;if(1===r)return l[0].value;throw t("XOR_ERROR",r,i,e)})}static nand(e){return Promise.allSettled(e).then(e=>{const l=e.filter(e=>"fulfilled"===e.status),r=l.length,i=e.length;if(r===i)throw t("NAND_ERROR",r,i,e);return l.map(e=>e.value)})}static nor(e){return Promise.allSettled(e).then(e=>{const l=e.filter(e=>"fulfilled"===e.status).length,r=e.length;if(0===l)return[];throw t("NOR_ERROR",l,r,e)})}static xnor(e){return Promise.allSettled(e).then(e=>{const l=e.filter(e=>"fulfilled"===e.status),r=l.length,i=e.length;if(0===r||r===i)return l.map(e=>e.value);throw t("XNOR_ERROR",r,i,e)})}static majority(e){return Promise.allSettled(e).then(e=>{const l=e.filter(e=>"fulfilled"===e.status),r=l.length,i=e.length;if(r>i-r)return l.map(e=>e.value);throw t("MAJORITY_ERROR",r,i,e)})}static allFulfilled(e){return Promise.allSettled(e).then(e=>e.filter(e=>"fulfilled"===e.status).map(e=>e.value))}static allRejected(e){return Promise.allSettled(e).then(e=>e.filter(e=>"rejected"===e.status).map(e=>e.reason))}static createFlipFlop(e=!1){let t=e,l=null,r=Promise.resolve(t);return{getState:()=>t,set(e){return t=e,l&&(l(t),l=null),r=Promise.resolve(t),this},toggle(){return this.set(!t)},waitForChange:()=>(l||(r=new Promise(e=>{l=e})),r),waitFor(e){return t===e?Promise.resolve(t):new Promise(l=>{const r=()=>{t===e?l(t):this.waitForChange().then(r)};r()})}}}}function r(e={}){const{prefix:t="",suffix:r="",rename:i={}}=e,n={and:l.and,or:l.or,race:l.race,allSettled:l.allSettled,xor:l.xor,not:l.not,nand:l.nand,nor:l.nor,xnor:l.xnor,majority:l.majority,allFulfilled:l.allFulfilled,allRejected:l.allRejected},o={};return Object.entries(n).forEach(([e,l])=>{const n=i[e]||e;o[`${t}${n}${r}`]=l}),o}export{r as createPromiseLogic};
1
+ class e extends Error{constructor(e,t,r){super(t),this.name="PromiseLogicError",this.type=e,this.results=r}}function t(t,r,l,i){return new e(t,{XOR_ERROR:`XOR condition failed: expected exactly 1 promise to fulfill, but ${r} fulfilled.`,NAND_ERROR:`NAND condition failed: all ${l} promises fulfilled (expected at least one rejection).`,NOT_ERROR:"NOT condition failed: promise resolved (expected rejection).",NOR_ERROR:`NOR condition failed: ${r} promises fulfilled (expected all rejected).`,MAJORITY_ERROR:`Majority condition failed: ${r}/${l} fulfilled (need majority).`,ALL_SUCCESSFUL_ERROR:`All successful condition failed: ${r}/${l} promises fulfilled (expected all to succeed).`,ALL_FAILED_ERROR:`All failed condition failed: ${l-r}/${l} promises rejected (expected all to fail).`}[t]||"Logic condition failed",i)}class r{constructor(e){this.promise=e}maxTimer(e){let t;return Promise.race([this.promise,new Promise((r,l)=>{t=setTimeout(()=>{l(new Error(`Promise timed out after ${e}ms`))},e)})]).finally(()=>clearTimeout(t))}then(e,t){return new r(this.promise.then(e,t))}catch(e){return new r(this.promise.catch(e))}finally(e){return new r(this.promise.finally(e))}toPromise(){return this.promise}}class l{static and(e){return new r(Promise.all(e))}static or(e){return new r(Promise.any(e))}static not(e){return new r(Promise.resolve(e).then(e=>Promise.reject(e),e=>Promise.resolve(e)).catch(e=>Promise.resolve(e)))}static race(e){return new r(Promise.race(e))}static allSettled(e){return new r(Promise.allSettled(e))}static xor(e){return new r(Promise.allSettled(e).then(e=>{const r=e.filter(e=>"fulfilled"===e.status),l=r.length,i=e.length;if(1===l)return r[0].value;throw t("XOR_ERROR",l,i,e)}))}static nand(e){return new r(Promise.allSettled(e).then(e=>{const r=e.filter(e=>"fulfilled"===e.status),l=r.length,i=e.length;if(l===i)throw t("NAND_ERROR",l,i,e);return r.map(e=>e.value)}))}static nor(e){return new r(Promise.allSettled(e).then(e=>{const r=e.filter(e=>"fulfilled"===e.status).length,l=e.length;if(0===r)return[];throw t("NOR_ERROR",r,l,e)}))}static xnor(e){return new r(Promise.allSettled(e).then(e=>{const r=e.filter(e=>"fulfilled"===e.status),l=r.length,i=e.length;if(0===l||l===i)return r.map(e=>e.value);throw t("XNOR_ERROR",l,i,e)}))}static majority(e,l={max:.5}){return new r(Promise.allSettled(e).then(e=>{const r=e.filter(e=>"fulfilled"===e.status),i=r.length,n=e.length;if(i>n*l.max)return r.map(e=>e.value);throw t("MAJORITY_ERROR",i,n,e)}))}static allFulfilled(e){return new r(Promise.allSettled(e).then(e=>e.filter(e=>"fulfilled"===e.status).map(e=>e.value)))}static allRejected(e){return new r(Promise.allSettled(e).then(e=>e.filter(e=>"rejected"===e.status).map(e=>e.reason)))}static createFlipFlop(e=!1){let t=e,r=null,l=Promise.resolve(t);return{getState:()=>t,set(e){return t=e,r&&(r(t),r=null),l=Promise.resolve(t),this},toggle(){return this.set(!t)},waitForChange:()=>(r||(l=new Promise(e=>{r=e})),l),waitFor(e){return t===e?Promise.resolve(t):new Promise(r=>{const l=()=>{t===e?r(t):this.waitForChange().then(l)};l()})}}}}function i(e={}){const{prefix:t="",suffix:r="",rename:i={}}=e,n={and:l.and.bind(l),or:l.or.bind(l),race:l.race.bind(l),allSettled:l.allSettled.bind(l),xor:l.xor.bind(l),not:l.not.bind(l),nand:l.nand.bind(l),nor:l.nor.bind(l),xnor:l.xnor.bind(l),majority:l.majority.bind(l),allFulfilled:l.allFulfilled.bind(l),allRejected:l.allRejected.bind(l)},o={};return Object.entries(n).forEach(([e,l])=>{const n=i[e]||e;o[`${t}${n}${r}`]=l}),o}export{i as createPromiseLogic};
package/dist/index.cjs.js CHANGED
@@ -1 +1 @@
1
- "use strict";class e extends Error{constructor(e,t,l){super(t),this.name="PromiseLogicError",this.type=e,this.results=l}}function t(t,l,r,i){return new e(t,{XOR_ERROR:`XOR condition failed: expected exactly 1 promise to fulfill, but ${l} fulfilled.`,NAND_ERROR:`NAND condition failed: all ${r} promises fulfilled (expected at least one rejection).`,NOT_ERROR:"NOT condition failed: promise resolved (expected rejection).",NOR_ERROR:`NOR condition failed: ${l} promises fulfilled (expected all rejected).`,MAJORITY_ERROR:`Majority condition failed: ${l}/${r} fulfilled (need majority).`,ALL_SUCCESSFUL_ERROR:`All successful condition failed: ${l}/${r} promises fulfilled (expected all to succeed).`,ALL_FAILED_ERROR:`All failed condition failed: ${r-l}/${r} promises rejected (expected all to fail).`}[t]||"Logic condition failed",i)}class l{static and(e){return Promise.all(e)}static or(e){return Promise.any(e)}static not(e){return Promise.resolve(e).then(e=>Promise.reject(e),e=>Promise.resolve(e)).catch(e=>Promise.resolve(e))}static race(e){return Promise.race(e)}static allSettled(e){return Promise.allSettled(e)}static xor(e){return Promise.allSettled(e).then(e=>{const l=e.filter(e=>"fulfilled"===e.status),r=l.length,i=e.length;if(1===r)return l[0].value;throw t("XOR_ERROR",r,i,e)})}static nand(e){return Promise.allSettled(e).then(e=>{const l=e.filter(e=>"fulfilled"===e.status),r=l.length,i=e.length;if(r===i)throw t("NAND_ERROR",r,i,e);return l.map(e=>e.value)})}static nor(e){return Promise.allSettled(e).then(e=>{const l=e.filter(e=>"fulfilled"===e.status).length,r=e.length;if(0===l)return[];throw t("NOR_ERROR",l,r,e)})}static xnor(e){return Promise.allSettled(e).then(e=>{const l=e.filter(e=>"fulfilled"===e.status),r=l.length,i=e.length;if(0===r||r===i)return l.map(e=>e.value);throw t("XNOR_ERROR",r,i,e)})}static majority(e){return Promise.allSettled(e).then(e=>{const l=e.filter(e=>"fulfilled"===e.status),r=l.length,i=e.length;if(r>i-r)return l.map(e=>e.value);throw t("MAJORITY_ERROR",r,i,e)})}static allFulfilled(e){return Promise.allSettled(e).then(e=>e.filter(e=>"fulfilled"===e.status).map(e=>e.value))}static allRejected(e){return Promise.allSettled(e).then(e=>e.filter(e=>"rejected"===e.status).map(e=>e.reason))}static createFlipFlop(e=!1){let t=e,l=null,r=Promise.resolve(t);return{getState:()=>t,set(e){return t=e,l&&(l(t),l=null),r=Promise.resolve(t),this},toggle(){return this.set(!t)},waitForChange:()=>(l||(r=new Promise(e=>{l=e})),r),waitFor(e){return t===e?Promise.resolve(t):new Promise(l=>{const r=()=>{t===e?l(t):this.waitForChange().then(r)};r()})}}}}exports.PromiseLogic=l,exports.PromiseLogicError=e,exports.createLogicError=t,exports.createPromiseLogic=function(e={}){const{prefix:t="",suffix:r="",rename:i={}}=e,o={and:l.and,or:l.or,race:l.race,allSettled:l.allSettled,xor:l.xor,not:l.not,nand:l.nand,nor:l.nor,xnor:l.xnor,majority:l.majority,allFulfilled:l.allFulfilled,allRejected:l.allRejected},s={};return Object.entries(o).forEach(([e,l])=>{const o=i[e]||e;s[`${t}${o}${r}`]=l}),s};
1
+ "use strict";class e extends Error{constructor(e,t,r){super(t),this.name="PromiseLogicError",this.type=e,this.results=r}}function t(t,r,l,i){return new e(t,{XOR_ERROR:`XOR condition failed: expected exactly 1 promise to fulfill, but ${r} fulfilled.`,NAND_ERROR:`NAND condition failed: all ${l} promises fulfilled (expected at least one rejection).`,NOT_ERROR:"NOT condition failed: promise resolved (expected rejection).",NOR_ERROR:`NOR condition failed: ${r} promises fulfilled (expected all rejected).`,MAJORITY_ERROR:`Majority condition failed: ${r}/${l} fulfilled (need majority).`,ALL_SUCCESSFUL_ERROR:`All successful condition failed: ${r}/${l} promises fulfilled (expected all to succeed).`,ALL_FAILED_ERROR:`All failed condition failed: ${l-r}/${l} promises rejected (expected all to fail).`}[t]||"Logic condition failed",i)}class r{constructor(e){this.promise=e}maxTimer(e){let t;return Promise.race([this.promise,new Promise((r,l)=>{t=setTimeout(()=>{l(new Error(`Promise timed out after ${e}ms`))},e)})]).finally(()=>clearTimeout(t))}then(e,t){return new r(this.promise.then(e,t))}catch(e){return new r(this.promise.catch(e))}finally(e){return new r(this.promise.finally(e))}toPromise(){return this.promise}}class l{static and(e){return new r(Promise.all(e))}static or(e){return new r(Promise.any(e))}static not(e){return new r(Promise.resolve(e).then(e=>Promise.reject(e),e=>Promise.resolve(e)).catch(e=>Promise.resolve(e)))}static race(e){return new r(Promise.race(e))}static allSettled(e){return new r(Promise.allSettled(e))}static xor(e){return new r(Promise.allSettled(e).then(e=>{const r=e.filter(e=>"fulfilled"===e.status),l=r.length,i=e.length;if(1===l)return r[0].value;throw t("XOR_ERROR",l,i,e)}))}static nand(e){return new r(Promise.allSettled(e).then(e=>{const r=e.filter(e=>"fulfilled"===e.status),l=r.length,i=e.length;if(l===i)throw t("NAND_ERROR",l,i,e);return r.map(e=>e.value)}))}static nor(e){return new r(Promise.allSettled(e).then(e=>{const r=e.filter(e=>"fulfilled"===e.status).length,l=e.length;if(0===r)return[];throw t("NOR_ERROR",r,l,e)}))}static xnor(e){return new r(Promise.allSettled(e).then(e=>{const r=e.filter(e=>"fulfilled"===e.status),l=r.length,i=e.length;if(0===l||l===i)return r.map(e=>e.value);throw t("XNOR_ERROR",l,i,e)}))}static majority(e,l={max:.5}){return new r(Promise.allSettled(e).then(e=>{const r=e.filter(e=>"fulfilled"===e.status),i=r.length,n=e.length;if(i>n*l.max)return r.map(e=>e.value);throw t("MAJORITY_ERROR",i,n,e)}))}static allFulfilled(e){return new r(Promise.allSettled(e).then(e=>e.filter(e=>"fulfilled"===e.status).map(e=>e.value)))}static allRejected(e){return new r(Promise.allSettled(e).then(e=>e.filter(e=>"rejected"===e.status).map(e=>e.reason)))}static createFlipFlop(e=!1){let t=e,r=null,l=Promise.resolve(t);return{getState:()=>t,set(e){return t=e,r&&(r(t),r=null),l=Promise.resolve(t),this},toggle(){return this.set(!t)},waitForChange:()=>(r||(l=new Promise(e=>{r=e})),l),waitFor(e){return t===e?Promise.resolve(t):new Promise(r=>{const l=()=>{t===e?r(t):this.waitForChange().then(l)};l()})}}}}exports.PromiseLogic=l,exports.PromiseLogicError=e,exports.createLogicError=t,exports.createPromiseLogic=function(e={}){const{prefix:t="",suffix:r="",rename:i={}}=e,n={and:l.and.bind(l),or:l.or.bind(l),race:l.race.bind(l),allSettled:l.allSettled.bind(l),xor:l.xor.bind(l),not:l.not.bind(l),nand:l.nand.bind(l),nor:l.nor.bind(l),xnor:l.xnor.bind(l),majority:l.majority.bind(l),allFulfilled:l.allFulfilled.bind(l),allRejected:l.allRejected.bind(l)},o={};return Object.entries(n).forEach(([e,l])=>{const n=i[e]||e;o[`${t}${n}${r}`]=l}),o};
package/dist/index.esm.js CHANGED
@@ -1 +1 @@
1
- class e extends Error{constructor(e,t,l){super(t),this.name="PromiseLogicError",this.type=e,this.results=l}}function t(t,l,r,i){return new e(t,{XOR_ERROR:`XOR condition failed: expected exactly 1 promise to fulfill, but ${l} fulfilled.`,NAND_ERROR:`NAND condition failed: all ${r} promises fulfilled (expected at least one rejection).`,NOT_ERROR:"NOT condition failed: promise resolved (expected rejection).",NOR_ERROR:`NOR condition failed: ${l} promises fulfilled (expected all rejected).`,MAJORITY_ERROR:`Majority condition failed: ${l}/${r} fulfilled (need majority).`,ALL_SUCCESSFUL_ERROR:`All successful condition failed: ${l}/${r} promises fulfilled (expected all to succeed).`,ALL_FAILED_ERROR:`All failed condition failed: ${r-l}/${r} promises rejected (expected all to fail).`}[t]||"Logic condition failed",i)}class l{static and(e){return Promise.all(e)}static or(e){return Promise.any(e)}static not(e){return Promise.resolve(e).then(e=>Promise.reject(e),e=>Promise.resolve(e)).catch(e=>Promise.resolve(e))}static race(e){return Promise.race(e)}static allSettled(e){return Promise.allSettled(e)}static xor(e){return Promise.allSettled(e).then(e=>{const l=e.filter(e=>"fulfilled"===e.status),r=l.length,i=e.length;if(1===r)return l[0].value;throw t("XOR_ERROR",r,i,e)})}static nand(e){return Promise.allSettled(e).then(e=>{const l=e.filter(e=>"fulfilled"===e.status),r=l.length,i=e.length;if(r===i)throw t("NAND_ERROR",r,i,e);return l.map(e=>e.value)})}static nor(e){return Promise.allSettled(e).then(e=>{const l=e.filter(e=>"fulfilled"===e.status).length,r=e.length;if(0===l)return[];throw t("NOR_ERROR",l,r,e)})}static xnor(e){return Promise.allSettled(e).then(e=>{const l=e.filter(e=>"fulfilled"===e.status),r=l.length,i=e.length;if(0===r||r===i)return l.map(e=>e.value);throw t("XNOR_ERROR",r,i,e)})}static majority(e){return Promise.allSettled(e).then(e=>{const l=e.filter(e=>"fulfilled"===e.status),r=l.length,i=e.length;if(r>i-r)return l.map(e=>e.value);throw t("MAJORITY_ERROR",r,i,e)})}static allFulfilled(e){return Promise.allSettled(e).then(e=>e.filter(e=>"fulfilled"===e.status).map(e=>e.value))}static allRejected(e){return Promise.allSettled(e).then(e=>e.filter(e=>"rejected"===e.status).map(e=>e.reason))}static createFlipFlop(e=!1){let t=e,l=null,r=Promise.resolve(t);return{getState:()=>t,set(e){return t=e,l&&(l(t),l=null),r=Promise.resolve(t),this},toggle(){return this.set(!t)},waitForChange:()=>(l||(r=new Promise(e=>{l=e})),r),waitFor(e){return t===e?Promise.resolve(t):new Promise(l=>{const r=()=>{t===e?l(t):this.waitForChange().then(r)};r()})}}}}function r(e={}){const{prefix:t="",suffix:r="",rename:i={}}=e,n={and:l.and,or:l.or,race:l.race,allSettled:l.allSettled,xor:l.xor,not:l.not,nand:l.nand,nor:l.nor,xnor:l.xnor,majority:l.majority,allFulfilled:l.allFulfilled,allRejected:l.allRejected},o={};return Object.entries(n).forEach(([e,l])=>{const n=i[e]||e;o[`${t}${n}${r}`]=l}),o}export{l as PromiseLogic,e as PromiseLogicError,t as createLogicError,r as createPromiseLogic};
1
+ class e extends Error{constructor(e,t,r){super(t),this.name="PromiseLogicError",this.type=e,this.results=r}}function t(t,r,l,i){return new e(t,{XOR_ERROR:`XOR condition failed: expected exactly 1 promise to fulfill, but ${r} fulfilled.`,NAND_ERROR:`NAND condition failed: all ${l} promises fulfilled (expected at least one rejection).`,NOT_ERROR:"NOT condition failed: promise resolved (expected rejection).",NOR_ERROR:`NOR condition failed: ${r} promises fulfilled (expected all rejected).`,MAJORITY_ERROR:`Majority condition failed: ${r}/${l} fulfilled (need majority).`,ALL_SUCCESSFUL_ERROR:`All successful condition failed: ${r}/${l} promises fulfilled (expected all to succeed).`,ALL_FAILED_ERROR:`All failed condition failed: ${l-r}/${l} promises rejected (expected all to fail).`}[t]||"Logic condition failed",i)}class r{constructor(e){this.promise=e}maxTimer(e){let t;return Promise.race([this.promise,new Promise((r,l)=>{t=setTimeout(()=>{l(new Error(`Promise timed out after ${e}ms`))},e)})]).finally(()=>clearTimeout(t))}then(e,t){return new r(this.promise.then(e,t))}catch(e){return new r(this.promise.catch(e))}finally(e){return new r(this.promise.finally(e))}toPromise(){return this.promise}}class l{static and(e){return new r(Promise.all(e))}static or(e){return new r(Promise.any(e))}static not(e){return new r(Promise.resolve(e).then(e=>Promise.reject(e),e=>Promise.resolve(e)).catch(e=>Promise.resolve(e)))}static race(e){return new r(Promise.race(e))}static allSettled(e){return new r(Promise.allSettled(e))}static xor(e){return new r(Promise.allSettled(e).then(e=>{const r=e.filter(e=>"fulfilled"===e.status),l=r.length,i=e.length;if(1===l)return r[0].value;throw t("XOR_ERROR",l,i,e)}))}static nand(e){return new r(Promise.allSettled(e).then(e=>{const r=e.filter(e=>"fulfilled"===e.status),l=r.length,i=e.length;if(l===i)throw t("NAND_ERROR",l,i,e);return r.map(e=>e.value)}))}static nor(e){return new r(Promise.allSettled(e).then(e=>{const r=e.filter(e=>"fulfilled"===e.status).length,l=e.length;if(0===r)return[];throw t("NOR_ERROR",r,l,e)}))}static xnor(e){return new r(Promise.allSettled(e).then(e=>{const r=e.filter(e=>"fulfilled"===e.status),l=r.length,i=e.length;if(0===l||l===i)return r.map(e=>e.value);throw t("XNOR_ERROR",l,i,e)}))}static majority(e,l={max:.5}){return new r(Promise.allSettled(e).then(e=>{const r=e.filter(e=>"fulfilled"===e.status),i=r.length,n=e.length;if(i>n*l.max)return r.map(e=>e.value);throw t("MAJORITY_ERROR",i,n,e)}))}static allFulfilled(e){return new r(Promise.allSettled(e).then(e=>e.filter(e=>"fulfilled"===e.status).map(e=>e.value)))}static allRejected(e){return new r(Promise.allSettled(e).then(e=>e.filter(e=>"rejected"===e.status).map(e=>e.reason)))}static createFlipFlop(e=!1){let t=e,r=null,l=Promise.resolve(t);return{getState:()=>t,set(e){return t=e,r&&(r(t),r=null),l=Promise.resolve(t),this},toggle(){return this.set(!t)},waitForChange:()=>(r||(l=new Promise(e=>{r=e})),l),waitFor(e){return t===e?Promise.resolve(t):new Promise(r=>{const l=()=>{t===e?r(t):this.waitForChange().then(l)};l()})}}}}function i(e={}){const{prefix:t="",suffix:r="",rename:i={}}=e,n={and:l.and.bind(l),or:l.or.bind(l),race:l.race.bind(l),allSettled:l.allSettled.bind(l),xor:l.xor.bind(l),not:l.not.bind(l),nand:l.nand.bind(l),nor:l.nor.bind(l),xnor:l.xnor.bind(l),majority:l.majority.bind(l),allFulfilled:l.allFulfilled.bind(l),allRejected:l.allRejected.bind(l)},o={};return Object.entries(n).forEach(([e,l])=>{const n=i[e]||e;o[`${t}${n}${r}`]=l}),o}export{l as PromiseLogic,e as PromiseLogicError,t as createLogicError,i as createPromiseLogic};
@@ -1 +1 @@
1
- "use strict";class e{filterResults(e,t){return"fulfilled"===t?e.filter(e=>"fulfilled"===e.status).map(e=>e.value):e.filter(e=>"rejected"===e.status).map(e=>e.reason)}countFulfilled(e){return e.filter(e=>"fulfilled"===e.status).length}}class t extends e{async execute(e){return Promise.any(e)}}class l extends e{async execute(e){try{return Promise.all(e)}catch(e){throw new u("AND_ERROR","AND gate failed",[e])}}}let r=class extends Error{constructor(e,t,l){super(t),this.type=e,this.results=l,this.name="PromiseLogicError"}};function s(e,t,l,s){return new r(e,{XOR_ERROR:`XOR condition failed: expected exactly 1 promise to fulfill, but ${t} fulfilled.`,NAND_ERROR:`NAND condition failed: all ${l} promises fulfilled (expected at least one rejection).`,NOR_ERROR:`NOR condition failed: ${t} promises fulfilled (expected all rejected).`,XNOR_ERROR:`XNOR condition failed: ${t}/${l} promises fulfilled (expected all or none).`,MAJORITY_ERROR:`Majority condition failed: ${t}/${l} fulfilled (need majority).`,ALL_SUCCESSFUL_ERROR:`All successful condition failed: ${t}/${l} promises fulfilled (expected all to succeed).`,ALL_FAILED_ERROR:`All failed condition failed: ${l-t}/${l} promises rejected (expected all to fail).`}[e]||"Logic condition failed",s)}class i extends e{async execute(e){const t=await Promise.allSettled(e),l=this.filterResults(t,"fulfilled"),r=l.length,i=t.length;if(r>i-r)return l;throw s("MAJORITY_ERROR",r,i,t)}}class n extends e{async execute(e){const t=await Promise.allSettled(e),l=this.filterResults(t,"fulfilled"),r=l.length,i=t.length;if(r===i)throw s("NAND_ERROR",r,i,t);return l}}class a extends e{async execute(e){const t=await Promise.allSettled(e),l=this.filterResults(t,"fulfilled").length,r=t.length;if(0===l)return[];throw s("NOR_ERROR",l,r,t)}}class o extends e{async execute(e){const t=await Promise.allSettled(e),l=this.filterResults(t,"fulfilled"),r=l.length,i=t.length;if(0===r||r===i)return l;throw s("XNOR_ERROR",r,i,t)}}class c extends e{async execute(e){const t=await Promise.allSettled(e),l=t.filter(e=>"fulfilled"===e.status),r=l.length,i=t.length;if(1===r)return l[0].value;throw s("XOR_ERROR",r,i,t)}}class u extends Error{constructor(e,t,l){super(t),this.type=e,this.results=l,this.name="PromiseLogicError"}}class d{static get gates(){return{and:new l,or:new t,xor:new c,nand:new n,nor:new a,xnor:new o,majority:new i}}static and(e){return this.gates.and.execute(e)}static or(e){return this.gates.or.execute(e)}static xor(e){return this.gates.xor.execute(e)}static nand(e){return this.gates.nand.execute(e)}static nor(e){return this.gates.nor.execute(e)}static xnor(e){return this.gates.xnor.execute(e)}static majority(e){return this.gates.majority.execute(e)}static allFulfilled(e){return Promise.allSettled(e).then(e=>e.filter(e=>"fulfilled"===e.status).map(e=>e.value))}static allRejected(e){return Promise.allSettled(e).then(e=>e.filter(e=>"rejected"===e.status).map(e=>e.reason))}static not(e){return Promise.resolve(e).then(e=>Promise.reject(e),e=>Promise.resolve(e))}static race(e){return Promise.race(e)}static allSettled(e){return Promise.allSettled(e)}static createFlipFlop(e=!1){let t=e,l=null,r=Promise.resolve(t);const s=()=>(l||(r=new Promise(e=>{l=e})),r);return{getState:()=>t,set:async e=>(t=e,l&&(l(t),l=null),r=Promise.resolve(t),t),async toggle(){return this.set(!t)},waitForChange:s,waitFor:e=>t===e?Promise.resolve(t):new Promise(l=>{const r=()=>{t===e?l(t):s().then(r)};r()})}}}exports.createPromiseLogic=function(e={}){const{prefix:t="",suffix:l="",rename:r={}}=e,s={and:d.and,or:d.or,not:d.not,race:d.race,allSettled:d.allSettled,xor:d.xor,nand:d.nand,nor:d.nor,xnor:d.xnor,majority:d.majority,allFulfilled:d.allFulfilled,allRejected:d.allRejected},i={};return Object.entries(s).forEach(([e,s])=>{const n=r[e]||e;i[`${t}${n}${l}`]=s}),i};
1
+ "use strict";class e{filterFulfilledResults(e){return e.filter(e=>"fulfilled"===e.status).map(e=>e.value)}filterRejectedResults(e){return e.filter(e=>"rejected"===e.status).map(e=>e.reason)}countFulfilled(e){return e.filter(e=>"fulfilled"===e.status).length}}class t extends e{async execute(e){return Promise.any(e)}}class l extends Error{constructor(e,t,l){super(t),this.type=e,this.results=l,this.name="PromiseLogicError"}}function r(e,t,r,s){return new l(e,{XOR_ERROR:`XOR condition failed: expected exactly 1 promise to fulfill, but ${t} fulfilled.`,NAND_ERROR:`NAND condition failed: all ${r} promises fulfilled (expected at least one rejection).`,NOR_ERROR:`NOR condition failed: ${t} promises fulfilled (expected all rejected).`,XNOR_ERROR:`XNOR condition failed: ${t}/${r} promises fulfilled (expected all or none).`,MAJORITY_ERROR:`Majority condition failed: ${t}/${r} fulfilled (need majority).`,ALL_SUCCESSFUL_ERROR:`All successful condition failed: ${t}/${r} promises fulfilled (expected all to succeed).`,ALL_FAILED_ERROR:`All failed condition failed: ${r-t}/${r} promises rejected (expected all to fail).`}[e]||"Logic condition failed",s)}class s extends e{async execute(e){try{return Promise.all(e)}catch(e){throw new l("AND_ERROR","AND gate failed",[e])}}}class i extends e{async execute(e,t){const l=await Promise.allSettled(e),s=this.filterFulfilledResults(l),i=s.length,n=l.length;if(i>n*t.max)return s;throw r("MAJORITY_ERROR",i,n,l)}}class n extends e{async execute(e){const t=await Promise.allSettled(e),l=this.filterFulfilledResults(t),s=l.length,i=t.length;if(s===i)throw r("NAND_ERROR",s,i,t);return l}}class a extends e{async execute(e){const t=await Promise.allSettled(e),l=this.filterFulfilledResults(t).length,s=t.length;if(0===l)return[];throw r("NOR_ERROR",l,s,t)}}class o extends e{async execute(e){const t=await Promise.allSettled(e),l=this.filterFulfilledResults(t),s=l.length,i=t.length;if(0===s||s===i)return l;throw r("XNOR_ERROR",s,i,t)}}class c extends e{async execute(e){const t=await Promise.allSettled(e),l=this.filterFulfilledResults(t),s=l.length,i=t.length;if(1===s)return l[0];throw r("XOR_ERROR",s,i,t)}}class u{constructor(e){this.promise=e}maxTimer(e){let t;return Promise.race([this.promise,new Promise((l,r)=>{t=setTimeout(()=>{r(new Error(`Promise timed out after ${e}ms,${this.promise}`))},e)})]).finally(()=>clearTimeout(t))}then(e,t){return new u(this.promise.then(e,t))}catch(e){return new u(this.promise.catch(e))}finally(e){return new u(this.promise.finally(e))}toPromise(){return this.promise}}class d{static get gates(){return{and:new s,or:new t,xor:new c,nand:new n,nor:new a,xnor:new o,majority:new i}}static and(e){return new u(this.gates.and.execute(e))}static or(e){return new u(this.gates.or.execute(e))}static xor(e){return new u(this.gates.xor.execute(e))}static nand(e){return new u(this.gates.nand.execute(e))}static nor(e){return new u(this.gates.nor.execute(e))}static xnor(e){return new u(this.gates.xnor.execute(e))}static majority(e,t={max:.5}){return new u(this.gates.majority.execute(e,t))}static allFulfilled(e){return new u(Promise.allSettled(e).then(e=>e.filter(e=>"fulfilled"===e.status).map(e=>e.value)))}static allRejected(e){return new u(Promise.allSettled(e).then(e=>e.filter(e=>"rejected"===e.status).map(e=>e.reason)))}static not(e){return new u(Promise.resolve(e).then(e=>Promise.reject(e),e=>Promise.resolve(e)))}static race(e){return new u(Promise.race(e))}static allSettled(e){return new u(Promise.allSettled(e))}static createFlipFlop(e=!1){let t=e,l=null,r=Promise.resolve(t);const s=()=>(l||(r=new Promise(e=>{l=e})),r);return{getState:()=>t,set:async e=>(t=e,l&&(l(t),l=null),r=Promise.resolve(t),t),async toggle(){return this.set(!t)},waitForChange:s,waitFor:e=>t===e?Promise.resolve(t):new Promise(l=>{const r=()=>{t===e?l(t):s().then(r)};r()})}}}exports.createPromiseLogic=function(e={}){const{prefix:t="",suffix:l="",rename:r={}}=e,s={and:d.and.bind(d),or:d.or.bind(d),not:d.not.bind(d),race:d.race.bind(d),allSettled:d.allSettled.bind(d),xor:d.xor.bind(d),nand:d.nand.bind(d),nor:d.nor.bind(d),xnor:d.xnor.bind(d),majority:d.majority.bind(d),allFulfilled:d.allFulfilled.bind(d),allRejected:d.allRejected.bind(d)},i={};return Object.entries(s).forEach(([e,s])=>{const n=r[e]||e;i[`${t}${n}${l}`]=s}),i};
@@ -1 +1 @@
1
- class e{filterResults(e,t){return"fulfilled"===t?e.filter(e=>"fulfilled"===e.status).map(e=>e.value):e.filter(e=>"rejected"===e.status).map(e=>e.reason)}countFulfilled(e){return e.filter(e=>"fulfilled"===e.status).length}}class t extends e{async execute(e){return Promise.any(e)}}class l extends e{async execute(e){try{return Promise.all(e)}catch(e){throw new u("AND_ERROR","AND gate failed",[e])}}}let r=class extends Error{constructor(e,t,l){super(t),this.type=e,this.results=l,this.name="PromiseLogicError"}};function s(e,t,l,s){return new r(e,{XOR_ERROR:`XOR condition failed: expected exactly 1 promise to fulfill, but ${t} fulfilled.`,NAND_ERROR:`NAND condition failed: all ${l} promises fulfilled (expected at least one rejection).`,NOR_ERROR:`NOR condition failed: ${t} promises fulfilled (expected all rejected).`,XNOR_ERROR:`XNOR condition failed: ${t}/${l} promises fulfilled (expected all or none).`,MAJORITY_ERROR:`Majority condition failed: ${t}/${l} fulfilled (need majority).`,ALL_SUCCESSFUL_ERROR:`All successful condition failed: ${t}/${l} promises fulfilled (expected all to succeed).`,ALL_FAILED_ERROR:`All failed condition failed: ${l-t}/${l} promises rejected (expected all to fail).`}[e]||"Logic condition failed",s)}class i extends e{async execute(e){const t=await Promise.allSettled(e),l=this.filterResults(t,"fulfilled"),r=l.length,i=t.length;if(r>i-r)return l;throw s("MAJORITY_ERROR",r,i,t)}}class n extends e{async execute(e){const t=await Promise.allSettled(e),l=this.filterResults(t,"fulfilled"),r=l.length,i=t.length;if(r===i)throw s("NAND_ERROR",r,i,t);return l}}class a extends e{async execute(e){const t=await Promise.allSettled(e),l=this.filterResults(t,"fulfilled").length,r=t.length;if(0===l)return[];throw s("NOR_ERROR",l,r,t)}}class o extends e{async execute(e){const t=await Promise.allSettled(e),l=this.filterResults(t,"fulfilled"),r=l.length,i=t.length;if(0===r||r===i)return l;throw s("XNOR_ERROR",r,i,t)}}class c extends e{async execute(e){const t=await Promise.allSettled(e),l=t.filter(e=>"fulfilled"===e.status),r=l.length,i=t.length;if(1===r)return l[0].value;throw s("XOR_ERROR",r,i,t)}}class u extends Error{constructor(e,t,l){super(t),this.type=e,this.results=l,this.name="PromiseLogicError"}}class d{static get gates(){return{and:new l,or:new t,xor:new c,nand:new n,nor:new a,xnor:new o,majority:new i}}static and(e){return this.gates.and.execute(e)}static or(e){return this.gates.or.execute(e)}static xor(e){return this.gates.xor.execute(e)}static nand(e){return this.gates.nand.execute(e)}static nor(e){return this.gates.nor.execute(e)}static xnor(e){return this.gates.xnor.execute(e)}static majority(e){return this.gates.majority.execute(e)}static allFulfilled(e){return Promise.allSettled(e).then(e=>e.filter(e=>"fulfilled"===e.status).map(e=>e.value))}static allRejected(e){return Promise.allSettled(e).then(e=>e.filter(e=>"rejected"===e.status).map(e=>e.reason))}static not(e){return Promise.resolve(e).then(e=>Promise.reject(e),e=>Promise.resolve(e))}static race(e){return Promise.race(e)}static allSettled(e){return Promise.allSettled(e)}static createFlipFlop(e=!1){let t=e,l=null,r=Promise.resolve(t);const s=()=>(l||(r=new Promise(e=>{l=e})),r);return{getState:()=>t,set:async e=>(t=e,l&&(l(t),l=null),r=Promise.resolve(t),t),async toggle(){return this.set(!t)},waitForChange:s,waitFor:e=>t===e?Promise.resolve(t):new Promise(l=>{const r=()=>{t===e?l(t):s().then(r)};r()})}}}function f(e={}){const{prefix:t="",suffix:l="",rename:r={}}=e,s={and:d.and,or:d.or,not:d.not,race:d.race,allSettled:d.allSettled,xor:d.xor,nand:d.nand,nor:d.nor,xnor:d.xnor,majority:d.majority,allFulfilled:d.allFulfilled,allRejected:d.allRejected},i={};return Object.entries(s).forEach(([e,s])=>{const n=r[e]||e;i[`${t}${n}${l}`]=s}),i}export{f as createPromiseLogic};
1
+ class e{filterFulfilledResults(e){return e.filter(e=>"fulfilled"===e.status).map(e=>e.value)}filterRejectedResults(e){return e.filter(e=>"rejected"===e.status).map(e=>e.reason)}countFulfilled(e){return e.filter(e=>"fulfilled"===e.status).length}}class t extends e{async execute(e){return Promise.any(e)}}class l extends Error{constructor(e,t,l){super(t),this.type=e,this.results=l,this.name="PromiseLogicError"}}function r(e,t,r,n){return new l(e,{XOR_ERROR:`XOR condition failed: expected exactly 1 promise to fulfill, but ${t} fulfilled.`,NAND_ERROR:`NAND condition failed: all ${r} promises fulfilled (expected at least one rejection).`,NOR_ERROR:`NOR condition failed: ${t} promises fulfilled (expected all rejected).`,XNOR_ERROR:`XNOR condition failed: ${t}/${r} promises fulfilled (expected all or none).`,MAJORITY_ERROR:`Majority condition failed: ${t}/${r} fulfilled (need majority).`,ALL_SUCCESSFUL_ERROR:`All successful condition failed: ${t}/${r} promises fulfilled (expected all to succeed).`,ALL_FAILED_ERROR:`All failed condition failed: ${r-t}/${r} promises rejected (expected all to fail).`}[e]||"Logic condition failed",n)}class n extends e{async execute(e){try{return Promise.all(e)}catch(e){throw new l("AND_ERROR","AND gate failed",[e])}}}class s extends e{async execute(e,t){const l=await Promise.allSettled(e),n=this.filterFulfilledResults(l),s=n.length,i=l.length;if(s>i*t.max)return n;throw r("MAJORITY_ERROR",s,i,l)}}class i extends e{async execute(e){const t=await Promise.allSettled(e),l=this.filterFulfilledResults(t),n=l.length,s=t.length;if(n===s)throw r("NAND_ERROR",n,s,t);return l}}class a extends e{async execute(e){const t=await Promise.allSettled(e),l=this.filterFulfilledResults(t).length,n=t.length;if(0===l)return[];throw r("NOR_ERROR",l,n,t)}}class o extends e{async execute(e){const t=await Promise.allSettled(e),l=this.filterFulfilledResults(t),n=l.length,s=t.length;if(0===n||n===s)return l;throw r("XNOR_ERROR",n,s,t)}}class c extends e{async execute(e){const t=await Promise.allSettled(e),l=this.filterFulfilledResults(t),n=l.length,s=t.length;if(1===n)return l[0];throw r("XOR_ERROR",n,s,t)}}class u{constructor(e){this.promise=e}maxTimer(e){let t;return Promise.race([this.promise,new Promise((l,r)=>{t=setTimeout(()=>{r(new Error(`Promise timed out after ${e}ms,${this.promise}`))},e)})]).finally(()=>clearTimeout(t))}then(e,t){return new u(this.promise.then(e,t))}catch(e){return new u(this.promise.catch(e))}finally(e){return new u(this.promise.finally(e))}toPromise(){return this.promise}}class d{static get gates(){return{and:new n,or:new t,xor:new c,nand:new i,nor:new a,xnor:new o,majority:new s}}static and(e){return new u(this.gates.and.execute(e))}static or(e){return new u(this.gates.or.execute(e))}static xor(e){return new u(this.gates.xor.execute(e))}static nand(e){return new u(this.gates.nand.execute(e))}static nor(e){return new u(this.gates.nor.execute(e))}static xnor(e){return new u(this.gates.xnor.execute(e))}static majority(e,t={max:.5}){return new u(this.gates.majority.execute(e,t))}static allFulfilled(e){return new u(Promise.allSettled(e).then(e=>e.filter(e=>"fulfilled"===e.status).map(e=>e.value)))}static allRejected(e){return new u(Promise.allSettled(e).then(e=>e.filter(e=>"rejected"===e.status).map(e=>e.reason)))}static not(e){return new u(Promise.resolve(e).then(e=>Promise.reject(e),e=>Promise.resolve(e)))}static race(e){return new u(Promise.race(e))}static allSettled(e){return new u(Promise.allSettled(e))}static createFlipFlop(e=!1){let t=e,l=null,r=Promise.resolve(t);const n=()=>(l||(r=new Promise(e=>{l=e})),r);return{getState:()=>t,set:async e=>(t=e,l&&(l(t),l=null),r=Promise.resolve(t),t),async toggle(){return this.set(!t)},waitForChange:n,waitFor:e=>t===e?Promise.resolve(t):new Promise(l=>{const r=()=>{t===e?l(t):n().then(r)};r()})}}}function f(e={}){const{prefix:t="",suffix:l="",rename:r={}}=e,n={and:d.and.bind(d),or:d.or.bind(d),not:d.not.bind(d),race:d.race.bind(d),allSettled:d.allSettled.bind(d),xor:d.xor.bind(d),nand:d.nand.bind(d),nor:d.nor.bind(d),xnor:d.xnor.bind(d),majority:d.majority.bind(d),allFulfilled:d.allFulfilled.bind(d),allRejected:d.allRejected.bind(d)},s={};return Object.entries(n).forEach(([e,n])=>{const i=r[e]||e;s[`${t}${i}${l}`]=n}),s}export{f as createPromiseLogic};
@@ -1 +1 @@
1
- "use strict";class e{filterResults(e,t){return"fulfilled"===t?e.filter(e=>"fulfilled"===e.status).map(e=>e.value):e.filter(e=>"rejected"===e.status).map(e=>e.reason)}countFulfilled(e){return e.filter(e=>"fulfilled"===e.status).length}}class t extends e{async execute(e){return Promise.any(e)}}class l extends e{async execute(e){try{return Promise.all(e)}catch(e){throw new u("AND_ERROR","AND gate failed",[e])}}}let r=class extends Error{constructor(e,t,l){super(t),this.type=e,this.results=l,this.name="PromiseLogicError"}};function s(e,t,l,s){return new r(e,{XOR_ERROR:`XOR condition failed: expected exactly 1 promise to fulfill, but ${t} fulfilled.`,NAND_ERROR:`NAND condition failed: all ${l} promises fulfilled (expected at least one rejection).`,NOR_ERROR:`NOR condition failed: ${t} promises fulfilled (expected all rejected).`,XNOR_ERROR:`XNOR condition failed: ${t}/${l} promises fulfilled (expected all or none).`,MAJORITY_ERROR:`Majority condition failed: ${t}/${l} fulfilled (need majority).`,ALL_SUCCESSFUL_ERROR:`All successful condition failed: ${t}/${l} promises fulfilled (expected all to succeed).`,ALL_FAILED_ERROR:`All failed condition failed: ${l-t}/${l} promises rejected (expected all to fail).`}[e]||"Logic condition failed",s)}class i extends e{async execute(e){const t=await Promise.allSettled(e),l=this.filterResults(t,"fulfilled"),r=l.length,i=t.length;if(r>i-r)return l;throw s("MAJORITY_ERROR",r,i,t)}}class n extends e{async execute(e){const t=await Promise.allSettled(e),l=this.filterResults(t,"fulfilled"),r=l.length,i=t.length;if(r===i)throw s("NAND_ERROR",r,i,t);return l}}class a extends e{async execute(e){const t=await Promise.allSettled(e),l=this.filterResults(t,"fulfilled").length,r=t.length;if(0===l)return[];throw s("NOR_ERROR",l,r,t)}}class o extends e{async execute(e){const t=await Promise.allSettled(e),l=this.filterResults(t,"fulfilled"),r=l.length,i=t.length;if(0===r||r===i)return l;throw s("XNOR_ERROR",r,i,t)}}class c extends e{async execute(e){const t=await Promise.allSettled(e),l=t.filter(e=>"fulfilled"===e.status),r=l.length,i=t.length;if(1===r)return l[0].value;throw s("XOR_ERROR",r,i,t)}}class u extends Error{constructor(e,t,l){super(t),this.type=e,this.results=l,this.name="PromiseLogicError"}}class d{static get gates(){return{and:new l,or:new t,xor:new c,nand:new n,nor:new a,xnor:new o,majority:new i}}static and(e){return this.gates.and.execute(e)}static or(e){return this.gates.or.execute(e)}static xor(e){return this.gates.xor.execute(e)}static nand(e){return this.gates.nand.execute(e)}static nor(e){return this.gates.nor.execute(e)}static xnor(e){return this.gates.xnor.execute(e)}static majority(e){return this.gates.majority.execute(e)}static allFulfilled(e){return Promise.allSettled(e).then(e=>e.filter(e=>"fulfilled"===e.status).map(e=>e.value))}static allRejected(e){return Promise.allSettled(e).then(e=>e.filter(e=>"rejected"===e.status).map(e=>e.reason))}static not(e){return Promise.resolve(e).then(e=>Promise.reject(e),e=>Promise.resolve(e))}static race(e){return Promise.race(e)}static allSettled(e){return Promise.allSettled(e)}static createFlipFlop(e=!1){let t=e,l=null,r=Promise.resolve(t);const s=()=>(l||(r=new Promise(e=>{l=e})),r);return{getState:()=>t,set:async e=>(t=e,l&&(l(t),l=null),r=Promise.resolve(t),t),async toggle(){return this.set(!t)},waitForChange:s,waitFor:e=>t===e?Promise.resolve(t):new Promise(l=>{const r=()=>{t===e?l(t):s().then(r)};r()})}}}exports.PromiseLogic=d,exports.PromiseLogicError=u,exports.createPromiseLogic=function(e={}){const{prefix:t="",suffix:l="",rename:r={}}=e,s={and:d.and,or:d.or,not:d.not,race:d.race,allSettled:d.allSettled,xor:d.xor,nand:d.nand,nor:d.nor,xnor:d.xnor,majority:d.majority,allFulfilled:d.allFulfilled,allRejected:d.allRejected},i={};return Object.entries(s).forEach(([e,s])=>{const n=r[e]||e;i[`${t}${n}${l}`]=s}),i};
1
+ "use strict";class e{filterFulfilledResults(e){return e.filter(e=>"fulfilled"===e.status).map(e=>e.value)}filterRejectedResults(e){return e.filter(e=>"rejected"===e.status).map(e=>e.reason)}countFulfilled(e){return e.filter(e=>"fulfilled"===e.status).length}}class t extends e{async execute(e){return Promise.any(e)}}class r extends Error{constructor(e,t,r){super(t),this.type=e,this.results=r,this.name="PromiseLogicError"}}function l(e,t,l,s){return new r(e,{XOR_ERROR:`XOR condition failed: expected exactly 1 promise to fulfill, but ${t} fulfilled.`,NAND_ERROR:`NAND condition failed: all ${l} promises fulfilled (expected at least one rejection).`,NOR_ERROR:`NOR condition failed: ${t} promises fulfilled (expected all rejected).`,XNOR_ERROR:`XNOR condition failed: ${t}/${l} promises fulfilled (expected all or none).`,MAJORITY_ERROR:`Majority condition failed: ${t}/${l} fulfilled (need majority).`,ALL_SUCCESSFUL_ERROR:`All successful condition failed: ${t}/${l} promises fulfilled (expected all to succeed).`,ALL_FAILED_ERROR:`All failed condition failed: ${l-t}/${l} promises rejected (expected all to fail).`}[e]||"Logic condition failed",s)}class s extends e{async execute(e){try{return Promise.all(e)}catch(e){throw new r("AND_ERROR","AND gate failed",[e])}}}class i extends e{async execute(e,t){const r=await Promise.allSettled(e),s=this.filterFulfilledResults(r),i=s.length,n=r.length;if(i>n*t.max)return s;throw l("MAJORITY_ERROR",i,n,r)}}class n extends e{async execute(e){const t=await Promise.allSettled(e),r=this.filterFulfilledResults(t),s=r.length,i=t.length;if(s===i)throw l("NAND_ERROR",s,i,t);return r}}class a extends e{async execute(e){const t=await Promise.allSettled(e),r=this.filterFulfilledResults(t).length,s=t.length;if(0===r)return[];throw l("NOR_ERROR",r,s,t)}}class o extends e{async execute(e){const t=await Promise.allSettled(e),r=this.filterFulfilledResults(t),s=r.length,i=t.length;if(0===s||s===i)return r;throw l("XNOR_ERROR",s,i,t)}}class c extends e{async execute(e){const t=await Promise.allSettled(e),r=this.filterFulfilledResults(t),s=r.length,i=t.length;if(1===s)return r[0];throw l("XOR_ERROR",s,i,t)}}class u{constructor(e){this.promise=e}maxTimer(e){let t;return Promise.race([this.promise,new Promise((r,l)=>{t=setTimeout(()=>{l(new Error(`Promise timed out after ${e}ms,${this.promise}`))},e)})]).finally(()=>clearTimeout(t))}then(e,t){return new u(this.promise.then(e,t))}catch(e){return new u(this.promise.catch(e))}finally(e){return new u(this.promise.finally(e))}toPromise(){return this.promise}}class d{static get gates(){return{and:new s,or:new t,xor:new c,nand:new n,nor:new a,xnor:new o,majority:new i}}static and(e){return new u(this.gates.and.execute(e))}static or(e){return new u(this.gates.or.execute(e))}static xor(e){return new u(this.gates.xor.execute(e))}static nand(e){return new u(this.gates.nand.execute(e))}static nor(e){return new u(this.gates.nor.execute(e))}static xnor(e){return new u(this.gates.xnor.execute(e))}static majority(e,t={max:.5}){return new u(this.gates.majority.execute(e,t))}static allFulfilled(e){return new u(Promise.allSettled(e).then(e=>e.filter(e=>"fulfilled"===e.status).map(e=>e.value)))}static allRejected(e){return new u(Promise.allSettled(e).then(e=>e.filter(e=>"rejected"===e.status).map(e=>e.reason)))}static not(e){return new u(Promise.resolve(e).then(e=>Promise.reject(e),e=>Promise.resolve(e)))}static race(e){return new u(Promise.race(e))}static allSettled(e){return new u(Promise.allSettled(e))}static createFlipFlop(e=!1){let t=e,r=null,l=Promise.resolve(t);const s=()=>(r||(l=new Promise(e=>{r=e})),l);return{getState:()=>t,set:async e=>(t=e,r&&(r(t),r=null),l=Promise.resolve(t),t),async toggle(){return this.set(!t)},waitForChange:s,waitFor:e=>t===e?Promise.resolve(t):new Promise(r=>{const l=()=>{t===e?r(t):s().then(l)};l()})}}}exports.PromiseLogic=d,exports.PromiseLogicError=r,exports.createPromiseLogic=function(e={}){const{prefix:t="",suffix:r="",rename:l={}}=e,s={and:d.and.bind(d),or:d.or.bind(d),not:d.not.bind(d),race:d.race.bind(d),allSettled:d.allSettled.bind(d),xor:d.xor.bind(d),nand:d.nand.bind(d),nor:d.nor.bind(d),xnor:d.xnor.bind(d),majority:d.majority.bind(d),allFulfilled:d.allFulfilled.bind(d),allRejected:d.allRejected.bind(d)},i={};return Object.entries(s).forEach(([e,s])=>{const n=l[e]||e;i[`${t}${n}${r}`]=s}),i};
@@ -1 +1 @@
1
- class e{filterResults(e,t){return"fulfilled"===t?e.filter(e=>"fulfilled"===e.status).map(e=>e.value):e.filter(e=>"rejected"===e.status).map(e=>e.reason)}countFulfilled(e){return e.filter(e=>"fulfilled"===e.status).length}}class t extends e{async execute(e){return Promise.any(e)}}class l extends e{async execute(e){try{return Promise.all(e)}catch(e){throw new u("AND_ERROR","AND gate failed",[e])}}}let r=class extends Error{constructor(e,t,l){super(t),this.type=e,this.results=l,this.name="PromiseLogicError"}};function s(e,t,l,s){return new r(e,{XOR_ERROR:`XOR condition failed: expected exactly 1 promise to fulfill, but ${t} fulfilled.`,NAND_ERROR:`NAND condition failed: all ${l} promises fulfilled (expected at least one rejection).`,NOR_ERROR:`NOR condition failed: ${t} promises fulfilled (expected all rejected).`,XNOR_ERROR:`XNOR condition failed: ${t}/${l} promises fulfilled (expected all or none).`,MAJORITY_ERROR:`Majority condition failed: ${t}/${l} fulfilled (need majority).`,ALL_SUCCESSFUL_ERROR:`All successful condition failed: ${t}/${l} promises fulfilled (expected all to succeed).`,ALL_FAILED_ERROR:`All failed condition failed: ${l-t}/${l} promises rejected (expected all to fail).`}[e]||"Logic condition failed",s)}class i extends e{async execute(e){const t=await Promise.allSettled(e),l=this.filterResults(t,"fulfilled"),r=l.length,i=t.length;if(r>i-r)return l;throw s("MAJORITY_ERROR",r,i,t)}}class n extends e{async execute(e){const t=await Promise.allSettled(e),l=this.filterResults(t,"fulfilled"),r=l.length,i=t.length;if(r===i)throw s("NAND_ERROR",r,i,t);return l}}class a extends e{async execute(e){const t=await Promise.allSettled(e),l=this.filterResults(t,"fulfilled").length,r=t.length;if(0===l)return[];throw s("NOR_ERROR",l,r,t)}}class o extends e{async execute(e){const t=await Promise.allSettled(e),l=this.filterResults(t,"fulfilled"),r=l.length,i=t.length;if(0===r||r===i)return l;throw s("XNOR_ERROR",r,i,t)}}class c extends e{async execute(e){const t=await Promise.allSettled(e),l=t.filter(e=>"fulfilled"===e.status),r=l.length,i=t.length;if(1===r)return l[0].value;throw s("XOR_ERROR",r,i,t)}}class u extends Error{constructor(e,t,l){super(t),this.type=e,this.results=l,this.name="PromiseLogicError"}}class d{static get gates(){return{and:new l,or:new t,xor:new c,nand:new n,nor:new a,xnor:new o,majority:new i}}static and(e){return this.gates.and.execute(e)}static or(e){return this.gates.or.execute(e)}static xor(e){return this.gates.xor.execute(e)}static nand(e){return this.gates.nand.execute(e)}static nor(e){return this.gates.nor.execute(e)}static xnor(e){return this.gates.xnor.execute(e)}static majority(e){return this.gates.majority.execute(e)}static allFulfilled(e){return Promise.allSettled(e).then(e=>e.filter(e=>"fulfilled"===e.status).map(e=>e.value))}static allRejected(e){return Promise.allSettled(e).then(e=>e.filter(e=>"rejected"===e.status).map(e=>e.reason))}static not(e){return Promise.resolve(e).then(e=>Promise.reject(e),e=>Promise.resolve(e))}static race(e){return Promise.race(e)}static allSettled(e){return Promise.allSettled(e)}static createFlipFlop(e=!1){let t=e,l=null,r=Promise.resolve(t);const s=()=>(l||(r=new Promise(e=>{l=e})),r);return{getState:()=>t,set:async e=>(t=e,l&&(l(t),l=null),r=Promise.resolve(t),t),async toggle(){return this.set(!t)},waitForChange:s,waitFor:e=>t===e?Promise.resolve(t):new Promise(l=>{const r=()=>{t===e?l(t):s().then(r)};r()})}}}function f(e={}){const{prefix:t="",suffix:l="",rename:r={}}=e,s={and:d.and,or:d.or,not:d.not,race:d.race,allSettled:d.allSettled,xor:d.xor,nand:d.nand,nor:d.nor,xnor:d.xnor,majority:d.majority,allFulfilled:d.allFulfilled,allRejected:d.allRejected},i={};return Object.entries(s).forEach(([e,s])=>{const n=r[e]||e;i[`${t}${n}${l}`]=s}),i}export{d as PromiseLogic,u as PromiseLogicError,f as createPromiseLogic};
1
+ class e{filterFulfilledResults(e){return e.filter(e=>"fulfilled"===e.status).map(e=>e.value)}filterRejectedResults(e){return e.filter(e=>"rejected"===e.status).map(e=>e.reason)}countFulfilled(e){return e.filter(e=>"fulfilled"===e.status).length}}class t extends e{async execute(e){return Promise.any(e)}}class l extends Error{constructor(e,t,l){super(t),this.type=e,this.results=l,this.name="PromiseLogicError"}}function r(e,t,r,n){return new l(e,{XOR_ERROR:`XOR condition failed: expected exactly 1 promise to fulfill, but ${t} fulfilled.`,NAND_ERROR:`NAND condition failed: all ${r} promises fulfilled (expected at least one rejection).`,NOR_ERROR:`NOR condition failed: ${t} promises fulfilled (expected all rejected).`,XNOR_ERROR:`XNOR condition failed: ${t}/${r} promises fulfilled (expected all or none).`,MAJORITY_ERROR:`Majority condition failed: ${t}/${r} fulfilled (need majority).`,ALL_SUCCESSFUL_ERROR:`All successful condition failed: ${t}/${r} promises fulfilled (expected all to succeed).`,ALL_FAILED_ERROR:`All failed condition failed: ${r-t}/${r} promises rejected (expected all to fail).`}[e]||"Logic condition failed",n)}class n extends e{async execute(e){try{return Promise.all(e)}catch(e){throw new l("AND_ERROR","AND gate failed",[e])}}}class s extends e{async execute(e,t){const l=await Promise.allSettled(e),n=this.filterFulfilledResults(l),s=n.length,i=l.length;if(s>i*t.max)return n;throw r("MAJORITY_ERROR",s,i,l)}}class i extends e{async execute(e){const t=await Promise.allSettled(e),l=this.filterFulfilledResults(t),n=l.length,s=t.length;if(n===s)throw r("NAND_ERROR",n,s,t);return l}}class a extends e{async execute(e){const t=await Promise.allSettled(e),l=this.filterFulfilledResults(t).length,n=t.length;if(0===l)return[];throw r("NOR_ERROR",l,n,t)}}class o extends e{async execute(e){const t=await Promise.allSettled(e),l=this.filterFulfilledResults(t),n=l.length,s=t.length;if(0===n||n===s)return l;throw r("XNOR_ERROR",n,s,t)}}class c extends e{async execute(e){const t=await Promise.allSettled(e),l=this.filterFulfilledResults(t),n=l.length,s=t.length;if(1===n)return l[0];throw r("XOR_ERROR",n,s,t)}}class u{constructor(e){this.promise=e}maxTimer(e){let t;return Promise.race([this.promise,new Promise((l,r)=>{t=setTimeout(()=>{r(new Error(`Promise timed out after ${e}ms,${this.promise}`))},e)})]).finally(()=>clearTimeout(t))}then(e,t){return new u(this.promise.then(e,t))}catch(e){return new u(this.promise.catch(e))}finally(e){return new u(this.promise.finally(e))}toPromise(){return this.promise}}class d{static get gates(){return{and:new n,or:new t,xor:new c,nand:new i,nor:new a,xnor:new o,majority:new s}}static and(e){return new u(this.gates.and.execute(e))}static or(e){return new u(this.gates.or.execute(e))}static xor(e){return new u(this.gates.xor.execute(e))}static nand(e){return new u(this.gates.nand.execute(e))}static nor(e){return new u(this.gates.nor.execute(e))}static xnor(e){return new u(this.gates.xnor.execute(e))}static majority(e,t={max:.5}){return new u(this.gates.majority.execute(e,t))}static allFulfilled(e){return new u(Promise.allSettled(e).then(e=>e.filter(e=>"fulfilled"===e.status).map(e=>e.value)))}static allRejected(e){return new u(Promise.allSettled(e).then(e=>e.filter(e=>"rejected"===e.status).map(e=>e.reason)))}static not(e){return new u(Promise.resolve(e).then(e=>Promise.reject(e),e=>Promise.resolve(e)))}static race(e){return new u(Promise.race(e))}static allSettled(e){return new u(Promise.allSettled(e))}static createFlipFlop(e=!1){let t=e,l=null,r=Promise.resolve(t);const n=()=>(l||(r=new Promise(e=>{l=e})),r);return{getState:()=>t,set:async e=>(t=e,l&&(l(t),l=null),r=Promise.resolve(t),t),async toggle(){return this.set(!t)},waitForChange:n,waitFor:e=>t===e?Promise.resolve(t):new Promise(l=>{const r=()=>{t===e?l(t):n().then(r)};r()})}}}function f(e={}){const{prefix:t="",suffix:l="",rename:r={}}=e,n={and:d.and.bind(d),or:d.or.bind(d),not:d.not.bind(d),race:d.race.bind(d),allSettled:d.allSettled.bind(d),xor:d.xor.bind(d),nand:d.nand.bind(d),nor:d.nor.bind(d),xnor:d.xnor.bind(d),majority:d.majority.bind(d),allFulfilled:d.allFulfilled.bind(d),allRejected:d.allRejected.bind(d)},s={};return Object.entries(n).forEach(([e,n])=>{const i=r[e]||e;s[`${t}${i}${l}`]=n}),s}export{d as PromiseLogic,l as PromiseLogicError,f as createPromiseLogic};
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "promise-logic",
3
- "version": "2.5.7",
3
+ "version": "2.6.2",
4
4
  "description": "Compose promises with logic gate semantics (AND, OR, XOR, NAND, NOR, XNOR, Majority). Forget APIs, remember logic.",
5
5
  "type": "module",
6
6
  "main": "dist/index.cjs.js",
@@ -31,7 +31,8 @@
31
31
  "scripts": {
32
32
  "build": "cross-env NODE_ENV=production rollup -c",
33
33
  "dev": "cross-env NODE_ENV=development rollup -c -w",
34
- "test": "jest --config jest.config.cjs",
34
+ "test": "node src/test/test.js",
35
+ "test:jest": "jest --config jest.config.cjs",
35
36
  "test:watch": "jest --watch --config jest.config.cjs",
36
37
  "test:coverage": "jest --coverage --config jest.config.cjs",
37
38
  "lint": "eslint src test",