defered-branch 2.1.2 β 2.2.0
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 +92 -53
- package/dist/index.d.ts +144 -16
- package/dist/index.mjs +1 -1
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -1,18 +1,16 @@
|
|
|
1
1
|
# Defered Branch πΏ
|
|
2
2
|
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
> Sometimes we need to judge something with multiple cases, but the main job should be done later. So we have Defered Branch! β¨
|
|
3
|
+
A lightweight library providing a if-else like branch, but with deferred execution. You can evaluate conditions now and execute the matched branch later.
|
|
6
4
|
|
|
7
|
-
|
|
5
|
+
For more awesome packages, check out [my homepageπ](https://baendlorel.github.io/?repoType=npm)
|
|
8
6
|
|
|
9
7
|
## π Features
|
|
10
8
|
|
|
11
|
-
- π― **
|
|
12
|
-
- π **Type Safe**: Full TypeScript support with
|
|
9
|
+
- π― **Four Execution Patterns**: Single/all-match Γ immediate/dynamic logic
|
|
10
|
+
- π **Type Safe**: Full TypeScript support with type inference
|
|
13
11
|
- πͺΆ **Lightweight**: Zero dependencies, minimal footprint
|
|
14
|
-
- π§© **Flexible**: Works with any function signature
|
|
15
|
-
- π¦ **
|
|
12
|
+
- π§© **Flexible**: Works with any function signature
|
|
13
|
+
- π¦ **Generic**: Customizable branch, condition, and nomatch handler types
|
|
16
14
|
|
|
17
15
|
## π¦ Installation
|
|
18
16
|
|
|
@@ -22,51 +20,35 @@ npm install defered-branch
|
|
|
22
20
|
pnpm add defered-branch
|
|
23
21
|
```
|
|
24
22
|
|
|
25
|
-
## π
|
|
23
|
+
## π API Overview
|
|
26
24
|
|
|
27
|
-
### `deferedBranch` -
|
|
25
|
+
### `deferedBranch` - Single-Match Immediate πββοΈ
|
|
28
26
|
|
|
29
|
-
|
|
27
|
+
Evaluate conditions once, execute first match immediately.
|
|
30
28
|
|
|
31
|
-
|
|
32
|
-
- Static condition evaluation
|
|
29
|
+
**Use cases:** One-time conditional flows, static condition evaluation
|
|
33
30
|
|
|
34
|
-
### `deferedBranchDynamic` - Reusable
|
|
31
|
+
### `deferedBranchDynamic` - Single-Match Reusable π
|
|
35
32
|
|
|
36
|
-
|
|
33
|
+
Reusable branching logic, execute first match dynamically.
|
|
37
34
|
|
|
38
|
-
|
|
39
|
-
- Repeated conditional logic with dynamic data
|
|
40
|
-
- State-dependent operations that change over time
|
|
35
|
+
**Use cases:** Event handlers with varying states, repeated conditional logic
|
|
41
36
|
|
|
42
|
-
|
|
37
|
+
### `deferedBranchAll` - Multi-Match Immediate π―
|
|
43
38
|
|
|
44
|
-
|
|
39
|
+
Evaluate conditions once, execute all matches immediately.
|
|
45
40
|
|
|
46
|
-
|
|
47
|
-
import { deferedBranch } from 'defered-branch';
|
|
41
|
+
**Use cases:** Notification systems, validation rules, multiple event listeners
|
|
48
42
|
|
|
49
|
-
|
|
50
|
-
// 1. Create instance
|
|
51
|
-
const branch = deferedBranch();
|
|
52
|
-
|
|
53
|
-
// 2. Add conditional branches
|
|
54
|
-
branch
|
|
55
|
-
.add(user.role === 'admin', () => 'Access granted to admin panel')
|
|
56
|
-
.add(user.role === 'user', () => 'Access granted to user dashboard')
|
|
57
|
-
.add(user.role === 'guest', () => 'Limited access granted');
|
|
43
|
+
### `deferedBranchAllDynamic` - Multi-Match Reusable π
|
|
58
44
|
|
|
59
|
-
|
|
60
|
-
branch.nomatch(() => 'Access denied');
|
|
45
|
+
Reusable branching logic, execute all matches dynamically.
|
|
61
46
|
|
|
62
|
-
|
|
47
|
+
**Use cases:** Plugin systems, middleware chains, observer patterns
|
|
63
48
|
|
|
64
|
-
|
|
65
|
-
const result = branch.run();
|
|
66
|
-
console.log(result); // Output based on user.role
|
|
67
|
-
```
|
|
49
|
+
## π Usage Examples
|
|
68
50
|
|
|
69
|
-
###
|
|
51
|
+
### Basic `deferedBranch`
|
|
70
52
|
|
|
71
53
|
```typescript
|
|
72
54
|
const calculator = deferedBranch<(a: number, b: number) => number>();
|
|
@@ -82,7 +64,7 @@ calculator
|
|
|
82
64
|
const result = calculator.run(10, 5); // Returns calculated result
|
|
83
65
|
```
|
|
84
66
|
|
|
85
|
-
###
|
|
67
|
+
### Reusable Logic with `deferedBranchDynamic`
|
|
86
68
|
|
|
87
69
|
```typescript
|
|
88
70
|
import { deferedBranchDynamic } from 'defered-branch';
|
|
@@ -118,7 +100,17 @@ statusHandler.predicate(); // Re-evaluates with new status
|
|
|
118
100
|
statusHandler.run('Status updated');
|
|
119
101
|
```
|
|
120
102
|
|
|
121
|
-
###
|
|
103
|
+
### deferedBranchAll for Multi-Match Execution
|
|
104
|
+
|
|
105
|
+
_Same as deferedBranch_
|
|
106
|
+
|
|
107
|
+
### deferedBranchAllDynamic
|
|
108
|
+
|
|
109
|
+
_Same as deferedBranchDynamic_
|
|
110
|
+
|
|
111
|
+
### Real-world Example
|
|
112
|
+
|
|
113
|
+
#### Theme Switching
|
|
122
114
|
|
|
123
115
|
```typescript
|
|
124
116
|
const themeHandler = deferedBranchDynamic<() => void>();
|
|
@@ -156,6 +148,41 @@ function onConfigChange() {
|
|
|
156
148
|
}
|
|
157
149
|
```
|
|
158
150
|
|
|
151
|
+
#### `h` function
|
|
152
|
+
|
|
153
|
+
[This is a simplied example from package **KT.js**](https://www.npmjs.com/package/kt.js)
|
|
154
|
+
|
|
155
|
+
In this section, we can see that element creation separates judgement and dealing of `attr` and `content`. With out this package, we should judge 2 times and the code would be too long for one .ts file.
|
|
156
|
+
|
|
157
|
+
```typescript
|
|
158
|
+
const attrBranch = deferedBranchDynamic<BranchFn, NoMatchFn, PredicateFn>()
|
|
159
|
+
.add((_, attr) => typeof attr === 'string', attrIsString)
|
|
160
|
+
.add((_, attr) => typeof attr === 'object' && attr !== null, attrIsObject)
|
|
161
|
+
.nomatch(invalidAttrHandler);
|
|
162
|
+
const contentBranch = __Same_As_AttrBranch__;
|
|
163
|
+
|
|
164
|
+
function h<T extends HTMLTag>(
|
|
165
|
+
tag: T,
|
|
166
|
+
attr: RawAttribute = '',
|
|
167
|
+
content: RawContent = ''
|
|
168
|
+
): HTMLElement<T> {
|
|
169
|
+
if (typeof tag !== 'string') {
|
|
170
|
+
throw new TypeError('[__NAME__:h] tagName must be a string.');
|
|
171
|
+
}
|
|
172
|
+
attrBranch.predicate(null, attr);
|
|
173
|
+
contentBranch.predicate(null, content);
|
|
174
|
+
|
|
175
|
+
// create element
|
|
176
|
+
const el = document.createElement(tag) as HTMLElement<T>;
|
|
177
|
+
// ... some element enhancement logic ...
|
|
178
|
+
|
|
179
|
+
attrBranch.run(element, attr);
|
|
180
|
+
contentBranch.run(element, content);
|
|
181
|
+
|
|
182
|
+
// ... other logic ...
|
|
183
|
+
}
|
|
184
|
+
```
|
|
185
|
+
|
|
159
186
|
## π§ API Reference
|
|
160
187
|
|
|
161
188
|
### deferedBranch()
|
|
@@ -199,9 +226,28 @@ Creates a new dynamic deferred branch instance for reusable logic patterns.
|
|
|
199
226
|
- Sets a fallback handler for when no predicates match
|
|
200
227
|
- Executes during `.predicate()` evaluation
|
|
201
228
|
|
|
229
|
+
##### `.predicate(...args): void`
|
|
230
|
+
|
|
231
|
+
- Evaluates all conditions to find the first match
|
|
232
|
+
- Call this before `.run()` to update the matched branch
|
|
233
|
+
- Essential for dynamic behavior
|
|
234
|
+
|
|
235
|
+
##### `.run(...args): any`
|
|
236
|
+
|
|
237
|
+
- Executes the currently matched branch
|
|
238
|
+
- Returns the function's return value or `undefined`
|
|
239
|
+
|
|
240
|
+
### deferedBranchAll()
|
|
241
|
+
|
|
242
|
+
_Same as deferedBranch_
|
|
243
|
+
|
|
244
|
+
### deferedBranchAllDynamic()
|
|
245
|
+
|
|
246
|
+
_Same as deferedBranchDynamic_
|
|
247
|
+
|
|
202
248
|
## π§Ύ Type Annotation
|
|
203
249
|
|
|
204
|
-
The factory functions `deferedBranch` and `
|
|
250
|
+
The factory functions `deferedBranch`, `deferedBranchDynamic`, `deferedBranchAll`, and `deferedBranchAllDynamic` are generic helpers. You can provide explicit type parameters to describe the branch function signature and the optional `nomatch` handler.
|
|
205
251
|
|
|
206
252
|
Below is an example using `deferedBranch`:
|
|
207
253
|
|
|
@@ -256,17 +302,6 @@ calc.predicate();
|
|
|
256
302
|
const result = calc.run(2, 3); // typed as number | undefined
|
|
257
303
|
```
|
|
258
304
|
|
|
259
|
-
##### `.predicate(...args): void`
|
|
260
|
-
|
|
261
|
-
- Evaluates all conditions to find the first match
|
|
262
|
-
- Call this before `.run()` to update the matched branch
|
|
263
|
-
- Essential for dynamic behavior
|
|
264
|
-
|
|
265
|
-
##### `.run(...args): any`
|
|
266
|
-
|
|
267
|
-
- Executes the currently matched branch
|
|
268
|
-
- Returns the function's return value or `undefined`
|
|
269
|
-
|
|
270
305
|
## π License
|
|
271
306
|
|
|
272
307
|
MIT License - see [LICENSE](LICENSE) file for details.
|
|
@@ -275,6 +310,10 @@ MIT License - see [LICENSE](LICENSE) file for details.
|
|
|
275
310
|
|
|
276
311
|
Contributions are welcome! Please feel free to submit issues and pull requests.
|
|
277
312
|
|
|
313
|
+
## π¦ Trivia
|
|
314
|
+
|
|
315
|
+
[**KT.js**](https://www.npmjs.com/package/kt.js) is the reason of this package's birth. It uses `deferedBranchDynamic` internally to handle attribute and content processing in a clean, reusable way.
|
|
316
|
+
|
|
278
317
|
---
|
|
279
318
|
|
|
280
319
|
Made with β€οΈ by [Kasukabe Tsumugi](https://github.com/baendlorel) (´q⒠ᡠβ’q`) β‘
|
package/dist/index.d.ts
CHANGED
|
@@ -1,6 +1,5 @@
|
|
|
1
|
-
declare class
|
|
1
|
+
declare class DeferedBranch<BranchFn extends AnyFn, NoMatchFn extends AnyFn> {
|
|
2
2
|
private _branch;
|
|
3
|
-
private _nomatch;
|
|
4
3
|
/**
|
|
5
4
|
* Add a new entry
|
|
6
5
|
* - **First truthy first served**.
|
|
@@ -15,19 +14,16 @@ declare class DeferBranch<BranchFn extends AnyFn, NoMatchFn extends AnyFn> {
|
|
|
15
14
|
* @param handler handle the exhausted case
|
|
16
15
|
* @returns this
|
|
17
16
|
*/
|
|
18
|
-
nomatch(handler:
|
|
17
|
+
nomatch(handler: NoMatchFn): this;
|
|
19
18
|
/**
|
|
20
|
-
*
|
|
21
|
-
* - if no branch matched, return the nomatch handler's returnValue
|
|
22
|
-
* - if no branch matched and no nomatch handler, return undefined
|
|
23
|
-
*
|
|
24
|
-
* > Note: Although args are for the matched branch, they will also be passed to nomatch handler
|
|
19
|
+
* Execute the first matched branch in order of addition
|
|
25
20
|
* @param args arguments to pass to the matched branch or nomatch handler
|
|
21
|
+
* @returns the return value of the matched branch or void
|
|
26
22
|
*/
|
|
27
|
-
run(...args: Parameters<BranchFn>): ReturnType<BranchFn> |
|
|
23
|
+
run(...args: Parameters<BranchFn>): ReturnType<BranchFn> | void;
|
|
28
24
|
}
|
|
29
25
|
|
|
30
|
-
declare class
|
|
26
|
+
declare class DeferedBranchDynamic<BranchFn extends AnyFn, NoMatchFn extends AnyFn, ConditionFn extends AnyFn = Predicate<BranchFn>> {
|
|
31
27
|
/**
|
|
32
28
|
* & Compress conditions and branches into a single array since they are paired
|
|
33
29
|
*/
|
|
@@ -53,12 +49,69 @@ declare class DeferBranchDynamic<BranchFn extends AnyFn, NoMatchFn extends AnyFn
|
|
|
53
49
|
*/
|
|
54
50
|
predicate(...args: Parameters<ConditionFn>): void;
|
|
55
51
|
/**
|
|
56
|
-
*
|
|
52
|
+
* Execute the first matched branch in order of addition
|
|
57
53
|
* @param args arguments to pass to the matched branch
|
|
54
|
+
* @returns the return value of the matched branch or void
|
|
55
|
+
*/
|
|
56
|
+
run(...args: Parameters<BranchFn>): ReturnType<BranchFn> | undefined;
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
declare class DeferedBranchAllDynamic<BranchFn extends AnyFn, NoMatchFn extends AnyFn, ConditionFn extends AnyFn = Predicate<BranchFn>> {
|
|
60
|
+
/**
|
|
61
|
+
* & Compress conditions and branches into a single array since they are paired
|
|
62
|
+
*/
|
|
63
|
+
private _condranches;
|
|
64
|
+
private _branches;
|
|
65
|
+
private _nomatch;
|
|
66
|
+
/**
|
|
67
|
+
* Add a new entry
|
|
68
|
+
* - **All truthy branches will be executed**.
|
|
69
|
+
* @param condition the condition to match
|
|
70
|
+
* @param branch the branch to run when matched
|
|
71
|
+
* @returns this
|
|
72
|
+
*/
|
|
73
|
+
add(condition: ConditionFn, branch: BranchFn): this;
|
|
74
|
+
/**
|
|
75
|
+
* When calling `this.predicate` and no branch matched, run this handler
|
|
76
|
+
* @param handler handle the exhausted case
|
|
77
|
+
* @returns this
|
|
78
|
+
*/
|
|
79
|
+
nomatch(handler: AnyFn): this;
|
|
80
|
+
/**
|
|
81
|
+
* Run the conditions to find all matched branches
|
|
82
|
+
*/
|
|
83
|
+
predicate(...args: Parameters<ConditionFn>): void;
|
|
84
|
+
/**
|
|
85
|
+
* Execute all matched branches in order of addition
|
|
86
|
+
* @param args arguments to pass to the matched branches
|
|
58
87
|
*/
|
|
59
88
|
run(...args: Parameters<BranchFn>): ReturnType<BranchFn> | undefined;
|
|
60
89
|
}
|
|
61
90
|
|
|
91
|
+
declare class DeferedBranchAll<BranchFn extends AnyFn, NoMatchFn extends AnyFn> {
|
|
92
|
+
private _branches;
|
|
93
|
+
/**
|
|
94
|
+
* Add a new entry
|
|
95
|
+
* - **All truthy branches will be executed**.
|
|
96
|
+
* @param condition the condition to match
|
|
97
|
+
* @param branch the branch to run when matched
|
|
98
|
+
* @returns this
|
|
99
|
+
*/
|
|
100
|
+
add(condition: boolean, branch: BranchFn): this;
|
|
101
|
+
/**
|
|
102
|
+
* Register a handler to be called when no branch matched
|
|
103
|
+
* - This will be called **instantly and ignores later matched branches**
|
|
104
|
+
* @param handler handle the exhausted case
|
|
105
|
+
* @returns this
|
|
106
|
+
*/
|
|
107
|
+
nomatch(handler: NoMatchFn): this;
|
|
108
|
+
/**
|
|
109
|
+
* Execute all matched branches in order of addition
|
|
110
|
+
* @param args arguments to pass to the matched branch or nomatch handler
|
|
111
|
+
*/
|
|
112
|
+
run(...args: Parameters<BranchFn>): void;
|
|
113
|
+
}
|
|
114
|
+
|
|
62
115
|
/**
|
|
63
116
|
* ## Steps to use
|
|
64
117
|
* 1. const a = deferedBranch()
|
|
@@ -85,14 +138,14 @@ declare class DeferBranchDynamic<BranchFn extends AnyFn, NoMatchFn extends AnyFn
|
|
|
85
138
|
* ## About
|
|
86
139
|
* @package DeferedBranch
|
|
87
140
|
* @author Kasukabe Tsumugi <futami16237@gmail.com>
|
|
88
|
-
* @version 2.
|
|
141
|
+
* @version 2.2.0 (Last Update: 2025.09.21 16:49:49.136)
|
|
89
142
|
* @license MIT
|
|
90
143
|
* @link https://github.com/baendlorel/defered-branch
|
|
91
144
|
* @link https://baendlorel.github.io/ Welcome to my site!
|
|
92
145
|
* @description Sometimes we need to judge something with multiple cases, but the main job should be done later. So we have Defered Branch!
|
|
93
146
|
* @copyright Copyright (c) 2025 Kasukabe Tsumugi. All rights reserved.
|
|
94
147
|
*/
|
|
95
|
-
declare const deferedBranch: <BranchFn extends AnyFn = AnyFn, NoMatchFn extends AnyFn = AnyFn>() =>
|
|
148
|
+
declare const deferedBranch: <BranchFn extends AnyFn = AnyFn, NoMatchFn extends AnyFn = AnyFn>() => DeferedBranch<BranchFn, NoMatchFn>;
|
|
96
149
|
/**
|
|
97
150
|
* ## Steps to use
|
|
98
151
|
* 1. const a = deferedBranchDynamic()
|
|
@@ -126,16 +179,91 @@ declare const deferedBranch: <BranchFn extends AnyFn = AnyFn, NoMatchFn extends
|
|
|
126
179
|
* ## About
|
|
127
180
|
* @package DeferedBranch
|
|
128
181
|
* @author Kasukabe Tsumugi <futami16237@gmail.com>
|
|
129
|
-
* @version 2.
|
|
182
|
+
* @version 2.2.0 (Last Update: 2025.09.21 16:49:49.136)
|
|
183
|
+
* @license MIT
|
|
184
|
+
* @link https://github.com/baendlorel/defered-branch
|
|
185
|
+
* @link https://baendlorel.github.io/ Welcome to my site!
|
|
186
|
+
* @description Sometimes we need to judge something with multiple cases, but the main job should be done later. So we have Defered Branch!
|
|
187
|
+
* @copyright Copyright (c) 2025 Kasukabe Tsumugi. All rights reserved.
|
|
188
|
+
*/
|
|
189
|
+
declare const deferedBranchDynamic: <BranchFn extends AnyFn = AnyFn, NoMatchFn extends AnyFn = AnyFn, ConditionFn extends AnyFn = Predicate<BranchFn>>() => DeferedBranchDynamic<BranchFn, NoMatchFn, ConditionFn>;
|
|
190
|
+
/**
|
|
191
|
+
* ## Steps to use
|
|
192
|
+
* 1. const a = deferedBranchAll()
|
|
193
|
+
* 2. add branches by a.add(condition, branch)
|
|
194
|
+
* 3. (optional) add defered nomatch handler, or add and run at the same time by a.nomatch(handler)
|
|
195
|
+
* 4. ...run some other logic
|
|
196
|
+
* 5. run all matched branches by a.run();
|
|
197
|
+
*
|
|
198
|
+
* ## Type Annotation (optional)
|
|
199
|
+
* ```ts
|
|
200
|
+
* type BranchFn = (a: number) => void;
|
|
201
|
+
* type NoMatchFn = () => void;
|
|
202
|
+
* const a = deferedBranchAll<BranchFn, NoMatchFn>();
|
|
203
|
+
*
|
|
204
|
+
* // then we have restrictions:
|
|
205
|
+
* a.add(true, someFn satisfies BranchFn);
|
|
206
|
+
* a.nomatch(otherFn satisfies NoMatchFn);
|
|
207
|
+
*
|
|
208
|
+
* // some other logic
|
|
209
|
+
*
|
|
210
|
+
* a.run(...args); // input args are restricted to Parameters<BranchFn>
|
|
211
|
+
* ```
|
|
212
|
+
*
|
|
213
|
+
* ## About
|
|
214
|
+
* @package DeferedBranch
|
|
215
|
+
* @author Kasukabe Tsumugi <futami16237@gmail.com>
|
|
216
|
+
* @version 2.2.0 (Last Update: 2025.09.21 16:49:49.136)
|
|
217
|
+
* @license MIT
|
|
218
|
+
* @link https://github.com/baendlorel/defered-branch
|
|
219
|
+
* @link https://baendlorel.github.io/ Welcome to my site!
|
|
220
|
+
* @description Sometimes we need to judge something with multiple cases, but the main job should be done later. So we have Defered Branch!
|
|
221
|
+
* @copyright Copyright (c) 2025 Kasukabe Tsumugi. All rights reserved.
|
|
222
|
+
*/
|
|
223
|
+
declare const deferedBranchAll: <BranchFn extends AnyFn = AnyFn, NoMatchFn extends AnyFn = AnyFn>() => DeferedBranchAll<BranchFn, NoMatchFn>;
|
|
224
|
+
/**
|
|
225
|
+
* ## Steps to use
|
|
226
|
+
* 1. const a = deferedBranchAllDynamic()
|
|
227
|
+
* 2. add branches by a.add(condition, branch)
|
|
228
|
+
* 3. add nomatch handler by a.nomatch(handler)
|
|
229
|
+
* 4. call a.predicate(...) to find all matched branches
|
|
230
|
+
* 5. ...run some other logic
|
|
231
|
+
* 6. run all matched branches by a.run();
|
|
232
|
+
*
|
|
233
|
+
* ## Type Annotation (optional)
|
|
234
|
+
* ```ts
|
|
235
|
+
* type BranchFn = (a: number) => void;
|
|
236
|
+
* type NoMatchFn = () => void;
|
|
237
|
+
* // optional, you can customize the condition function type
|
|
238
|
+
* type ConditionFn = Predicate<BranchFn>;
|
|
239
|
+
* const a = deferedBranchAllDynamic<BranchFn, NoMatchFn, ConditionFn>();
|
|
240
|
+
*
|
|
241
|
+
* // then we have restrictions:
|
|
242
|
+
* a.add(someFn satisfies ConditionFn, secondFn satisfies BranchFn);
|
|
243
|
+
* a.nomatch(thirdFn satisfies NoMatchFn);
|
|
244
|
+
*
|
|
245
|
+
* // some other logic
|
|
246
|
+
*
|
|
247
|
+
* a.predicate(...args);
|
|
248
|
+
*
|
|
249
|
+
* // some other logic
|
|
250
|
+
*
|
|
251
|
+
* a.run(...args); // input args are restricted to Parameters<BranchFn>
|
|
252
|
+
* ```
|
|
253
|
+
*
|
|
254
|
+
* ## About
|
|
255
|
+
* @package DeferedBranch
|
|
256
|
+
* @author Kasukabe Tsumugi <futami16237@gmail.com>
|
|
257
|
+
* @version 2.2.0 (Last Update: 2025.09.21 16:49:49.136)
|
|
130
258
|
* @license MIT
|
|
131
259
|
* @link https://github.com/baendlorel/defered-branch
|
|
132
260
|
* @link https://baendlorel.github.io/ Welcome to my site!
|
|
133
261
|
* @description Sometimes we need to judge something with multiple cases, but the main job should be done later. So we have Defered Branch!
|
|
134
262
|
* @copyright Copyright (c) 2025 Kasukabe Tsumugi. All rights reserved.
|
|
135
263
|
*/
|
|
136
|
-
declare const
|
|
264
|
+
declare const deferedBranchAllDynamic: <BranchFn extends AnyFn = AnyFn, NoMatchFn extends AnyFn = AnyFn, ConditionFn extends AnyFn = Predicate<BranchFn>>() => DeferedBranchAllDynamic<BranchFn, NoMatchFn, ConditionFn>;
|
|
137
265
|
|
|
138
|
-
export { deferedBranch, deferedBranchDynamic };
|
|
266
|
+
export { deferedBranch, deferedBranchAll, deferedBranchAllDynamic, deferedBranchDynamic };
|
|
139
267
|
|
|
140
268
|
// # from: src/global.d.ts
|
|
141
269
|
type AnyFn = (...args: any[]) => any;
|
package/dist/index.mjs
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
class
|
|
1
|
+
class t{t=null;add(t,n){if("function"!=typeof n)throw new TypeError("DeferBranch: branch must be a function");return t&&!this.t&&(this.t=n),this}nomatch(t){if("function"!=typeof t)throw new TypeError("DeferBranch: branch must be a function");return this.t||t(),this}run(...t){if(this.t)return this.t.apply(null,t)}}class n{i=[];t=null;h=null;add(t,n){if("function"!=typeof t)throw new TypeError("DeferBranch: condition must be a function");if("function"!=typeof n)throw new TypeError("DeferBranch: branch must be a function");return this.i.push(t,n),this}nomatch(t){if("function"!=typeof t)throw new TypeError("DeferBranch: handler must be a function");return this.h=t,this}predicate(...t){const n=this.i.length;for(let t=0;t<n;t+=2)if(this.i[t]())return void(this.t=this.i[t+1]);this.h&&this.h.apply(this,t)}run(...t){if(this.t)return this.t.apply(null,t)}}class r{i=[];o=[];h=null;add(t,n){if("function"!=typeof t)throw new TypeError("DeferBranch: condition must be a function");if("function"!=typeof n)throw new TypeError("DeferBranch: branch must be a function");return this.i.push(t,n),this}nomatch(t){if("function"!=typeof t)throw new TypeError("DeferBranch: handler must be a function");return this.h=t,this}predicate(...t){const n=this.i.length;for(let t=0;t<n;t+=2)this.i[t]()&&this.o.push(this.i[t+1]);this.h&&0===this.o.length&&this.h.apply(this,t)}run(...t){const n=this.o.length;if(0!==n)for(let r=0;r<n;r++)this.o[r].apply(null,t)}}class e{o=[];add(t,n){if("function"!=typeof n)throw new TypeError("DeferBranch: branch must be a function");return t&&this.o.push(n),this}nomatch(t){if("function"!=typeof t)throw new TypeError("DeferBranch: branch must be a function");return 0===this.o.length&&t(),this}run(...t){const n=this.o.length;if(0!==n)for(let r=0;r<n;r++)this.o[r].apply(null,t)}}const i=()=>new t,h=()=>new n,o=()=>new e,s=()=>new r;export{i as deferedBranch,o as deferedBranchAll,s as deferedBranchAllDynamic,h as deferedBranchDynamic};
|