defered-branch 2.1.2 β 2.2.1
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 +103 -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,27 @@
|
|
|
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
|
|
14
|
+
|
|
15
|
+
## β²οΈ Change Log (Symplified)
|
|
16
|
+
|
|
17
|
+
### 2.2.1
|
|
18
|
+
|
|
19
|
+
- Fix: Clear matched branch after execution to prevent stale state issues.
|
|
20
|
+
|
|
21
|
+
### 2.2.0
|
|
22
|
+
|
|
23
|
+
- Add `deferedBranchAll` and `deferedBranchAllDynamic` for multi-match scenarios.
|
|
24
|
+
- Fix documentation typos.
|
|
16
25
|
|
|
17
26
|
## π¦ Installation
|
|
18
27
|
|
|
@@ -22,51 +31,35 @@ npm install defered-branch
|
|
|
22
31
|
pnpm add defered-branch
|
|
23
32
|
```
|
|
24
33
|
|
|
25
|
-
## π
|
|
34
|
+
## π API Overview
|
|
26
35
|
|
|
27
|
-
### `deferedBranch` -
|
|
36
|
+
### `deferedBranch` - Single-Match Immediate πββοΈ
|
|
28
37
|
|
|
29
|
-
|
|
38
|
+
Evaluate conditions once, execute first match immediately.
|
|
30
39
|
|
|
31
|
-
|
|
32
|
-
- Static condition evaluation
|
|
40
|
+
**Use cases:** One-time conditional flows, static condition evaluation
|
|
33
41
|
|
|
34
|
-
### `deferedBranchDynamic` - Reusable
|
|
42
|
+
### `deferedBranchDynamic` - Single-Match Reusable π
|
|
35
43
|
|
|
36
|
-
|
|
44
|
+
Reusable branching logic, execute first match dynamically.
|
|
37
45
|
|
|
38
|
-
|
|
39
|
-
- Repeated conditional logic with dynamic data
|
|
40
|
-
- State-dependent operations that change over time
|
|
46
|
+
**Use cases:** Event handlers with varying states, repeated conditional logic
|
|
41
47
|
|
|
42
|
-
|
|
48
|
+
### `deferedBranchAll` - Multi-Match Immediate π―
|
|
43
49
|
|
|
44
|
-
|
|
50
|
+
Evaluate conditions once, execute all matches immediately.
|
|
45
51
|
|
|
46
|
-
|
|
47
|
-
import { deferedBranch } from 'defered-branch';
|
|
48
|
-
|
|
49
|
-
// Steps to use:
|
|
50
|
-
// 1. Create instance
|
|
51
|
-
const branch = deferedBranch();
|
|
52
|
+
**Use cases:** Notification systems, validation rules, multiple event listeners
|
|
52
53
|
|
|
53
|
-
|
|
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');
|
|
54
|
+
### `deferedBranchAllDynamic` - Multi-Match Reusable π
|
|
58
55
|
|
|
59
|
-
|
|
60
|
-
branch.nomatch(() => 'Access denied');
|
|
56
|
+
Reusable branching logic, execute all matches dynamically.
|
|
61
57
|
|
|
62
|
-
|
|
58
|
+
**Use cases:** Plugin systems, middleware chains, observer patterns
|
|
63
59
|
|
|
64
|
-
|
|
65
|
-
const result = branch.run();
|
|
66
|
-
console.log(result); // Output based on user.role
|
|
67
|
-
```
|
|
60
|
+
## π Usage Examples
|
|
68
61
|
|
|
69
|
-
###
|
|
62
|
+
### Basic `deferedBranch`
|
|
70
63
|
|
|
71
64
|
```typescript
|
|
72
65
|
const calculator = deferedBranch<(a: number, b: number) => number>();
|
|
@@ -82,7 +75,7 @@ calculator
|
|
|
82
75
|
const result = calculator.run(10, 5); // Returns calculated result
|
|
83
76
|
```
|
|
84
77
|
|
|
85
|
-
###
|
|
78
|
+
### Reusable Logic with `deferedBranchDynamic`
|
|
86
79
|
|
|
87
80
|
```typescript
|
|
88
81
|
import { deferedBranchDynamic } from 'defered-branch';
|
|
@@ -118,7 +111,17 @@ statusHandler.predicate(); // Re-evaluates with new status
|
|
|
118
111
|
statusHandler.run('Status updated');
|
|
119
112
|
```
|
|
120
113
|
|
|
121
|
-
###
|
|
114
|
+
### deferedBranchAll for Multi-Match Execution
|
|
115
|
+
|
|
116
|
+
_Same as deferedBranch_
|
|
117
|
+
|
|
118
|
+
### deferedBranchAllDynamic
|
|
119
|
+
|
|
120
|
+
_Same as deferedBranchDynamic_
|
|
121
|
+
|
|
122
|
+
### Real-world Example
|
|
123
|
+
|
|
124
|
+
#### Theme Switching
|
|
122
125
|
|
|
123
126
|
```typescript
|
|
124
127
|
const themeHandler = deferedBranchDynamic<() => void>();
|
|
@@ -156,6 +159,41 @@ function onConfigChange() {
|
|
|
156
159
|
}
|
|
157
160
|
```
|
|
158
161
|
|
|
162
|
+
#### `h` function
|
|
163
|
+
|
|
164
|
+
[This is a simplied example from package **KT.js**](https://www.npmjs.com/package/kt.js)
|
|
165
|
+
|
|
166
|
+
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.
|
|
167
|
+
|
|
168
|
+
```typescript
|
|
169
|
+
const attrBranch = deferedBranchDynamic<BranchFn, NoMatchFn, PredicateFn>()
|
|
170
|
+
.add((_, attr) => typeof attr === 'string', attrIsString)
|
|
171
|
+
.add((_, attr) => typeof attr === 'object' && attr !== null, attrIsObject)
|
|
172
|
+
.nomatch(invalidAttrHandler);
|
|
173
|
+
const contentBranch = __Same_As_AttrBranch__;
|
|
174
|
+
|
|
175
|
+
function h<T extends HTMLTag>(
|
|
176
|
+
tag: T,
|
|
177
|
+
attr: RawAttribute = '',
|
|
178
|
+
content: RawContent = ''
|
|
179
|
+
): HTMLElement<T> {
|
|
180
|
+
if (typeof tag !== 'string') {
|
|
181
|
+
throw new TypeError('[__NAME__:h] tagName must be a string.');
|
|
182
|
+
}
|
|
183
|
+
attrBranch.predicate(null, attr);
|
|
184
|
+
contentBranch.predicate(null, content);
|
|
185
|
+
|
|
186
|
+
// create element
|
|
187
|
+
const el = document.createElement(tag) as HTMLElement<T>;
|
|
188
|
+
// ... some element enhancement logic ...
|
|
189
|
+
|
|
190
|
+
attrBranch.run(element, attr);
|
|
191
|
+
contentBranch.run(element, content);
|
|
192
|
+
|
|
193
|
+
// ... other logic ...
|
|
194
|
+
}
|
|
195
|
+
```
|
|
196
|
+
|
|
159
197
|
## π§ API Reference
|
|
160
198
|
|
|
161
199
|
### deferedBranch()
|
|
@@ -199,9 +237,28 @@ Creates a new dynamic deferred branch instance for reusable logic patterns.
|
|
|
199
237
|
- Sets a fallback handler for when no predicates match
|
|
200
238
|
- Executes during `.predicate()` evaluation
|
|
201
239
|
|
|
240
|
+
##### `.predicate(...args): void`
|
|
241
|
+
|
|
242
|
+
- Evaluates all conditions to find the first match
|
|
243
|
+
- Call this before `.run()` to update the matched branch
|
|
244
|
+
- Essential for dynamic behavior
|
|
245
|
+
|
|
246
|
+
##### `.run(...args): any`
|
|
247
|
+
|
|
248
|
+
- Executes the currently matched branch
|
|
249
|
+
- Returns the function's return value or `undefined`
|
|
250
|
+
|
|
251
|
+
### deferedBranchAll()
|
|
252
|
+
|
|
253
|
+
_Same as deferedBranch_
|
|
254
|
+
|
|
255
|
+
### deferedBranchAllDynamic()
|
|
256
|
+
|
|
257
|
+
_Same as deferedBranchDynamic_
|
|
258
|
+
|
|
202
259
|
## π§Ύ Type Annotation
|
|
203
260
|
|
|
204
|
-
The factory functions `deferedBranch` and `
|
|
261
|
+
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
262
|
|
|
206
263
|
Below is an example using `deferedBranch`:
|
|
207
264
|
|
|
@@ -256,17 +313,6 @@ calc.predicate();
|
|
|
256
313
|
const result = calc.run(2, 3); // typed as number | undefined
|
|
257
314
|
```
|
|
258
315
|
|
|
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
316
|
## π License
|
|
271
317
|
|
|
272
318
|
MIT License - see [LICENSE](LICENSE) file for details.
|
|
@@ -275,6 +321,10 @@ MIT License - see [LICENSE](LICENSE) file for details.
|
|
|
275
321
|
|
|
276
322
|
Contributions are welcome! Please feel free to submit issues and pull requests.
|
|
277
323
|
|
|
324
|
+
## π¦ Trivia
|
|
325
|
+
|
|
326
|
+
[**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.
|
|
327
|
+
|
|
278
328
|
---
|
|
279
329
|
|
|
280
330
|
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.1
|
|
141
|
+
* @version 2.2.1 (Last Update: 2025.09.21 17:12:09.568)
|
|
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.1
|
|
182
|
+
* @version 2.2.1 (Last Update: 2025.09.21 17:12:09.568)
|
|
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.1 (Last Update: 2025.09.21 17:12:09.568)
|
|
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.1 (Last Update: 2025.09.21 17:12:09.568)
|
|
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){const n=this.t.apply(null,t);return this.t=null,n}}}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){const n=this.t.apply(null,t);return this.t=null,n}}}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);this.o.length=0}}}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);this.o.length=0}}}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};
|