defered-branch 1.0.1 β 1.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 +70 -36
- package/dist/index.d.ts +12 -3
- package/dist/index.mjs +1 -1
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -1,17 +1,17 @@
|
|
|
1
1
|
# defered-branch π¦
|
|
2
2
|
|
|
3
|
-
A tiny TypeScript/JavaScript
|
|
4
|
-
|
|
3
|
+
A tiny TypeScript/JavaScript utility for deferred branching logic.
|
|
4
|
+
It provides a small, chainable API to register conditional branches and two kinds of "no-match" handlers: one that runs immediately when no branch was matched, and another that runs later when you call `run()`.
|
|
5
5
|
|
|
6
6
|
For more awesome packages, check out [my homepageπ](https://baendlorel.github.io/?repoType=npm)
|
|
7
7
|
|
|
8
|
-
##
|
|
8
|
+
## Highlights β¨
|
|
9
9
|
|
|
10
|
-
- Chainable API for
|
|
10
|
+
- Chainable API for registering branches
|
|
11
11
|
- Only the last matched branch is executed
|
|
12
|
-
-
|
|
13
|
-
-
|
|
14
|
-
-
|
|
12
|
+
- Immediate `nomatch` handler (runs instantly when no branch matched)
|
|
13
|
+
- Deferred `deferedNomatch` handler (runs when `run()` is invoked)
|
|
14
|
+
- TypeScript declarations included
|
|
15
15
|
|
|
16
16
|
## Installation π¦
|
|
17
17
|
|
|
@@ -21,55 +21,89 @@ pnpm add defered-branch
|
|
|
21
21
|
npm install defered-branch
|
|
22
22
|
```
|
|
23
23
|
|
|
24
|
-
##
|
|
24
|
+
## Export / Import π
|
|
25
|
+
|
|
26
|
+
The package exports a factory named `deferedBranch` (note the spelling):
|
|
27
|
+
|
|
28
|
+
```ts
|
|
29
|
+
import { deferedBranch } from 'defered-branch';
|
|
30
|
+
```
|
|
31
|
+
|
|
32
|
+
## Basic Usage
|
|
33
|
+
|
|
34
|
+
```ts
|
|
35
|
+
import { deferedBranch } from 'defered-branch';
|
|
36
|
+
|
|
37
|
+
// register branches - only the last matched branch will run
|
|
38
|
+
const b = deferedBranch()
|
|
39
|
+
.add(false, () => 'A')
|
|
40
|
+
.add(true, () => 'B');
|
|
41
|
+
|
|
42
|
+
console.log(b.run()); // 'B'
|
|
43
|
+
```
|
|
44
|
+
|
|
45
|
+
## nomatch vs deferedNomatch
|
|
46
|
+
|
|
47
|
+
There are two ways to handle the case when no branch matches:
|
|
48
|
+
|
|
49
|
+
- `nomatch(handler: Fn)` β runs the handler immediately if no branch has been matched yet. Note: if `nomatch` runs the handler immediately, later calls to `add(true, ...)` will not retroactively affect that call.
|
|
50
|
+
- `deferedNomatch(handler: Fn)` β registers a handler that will be executed when `run()` is called and no branch matched.
|
|
51
|
+
|
|
52
|
+
Examples:
|
|
25
53
|
|
|
26
54
|
```ts
|
|
27
|
-
import {
|
|
55
|
+
import { deferedBranch } from 'defered-branch';
|
|
56
|
+
|
|
57
|
+
// nomatch runs immediately when invoked if no branch matched
|
|
58
|
+
deferedBranch()
|
|
59
|
+
.add(false, () => 'x')
|
|
60
|
+
.nomatch(() => console.log('immediate nomatch')) // prints now
|
|
61
|
+
.add(true, () => console.log('will not be printed')); // ignored for the already-called nomatch
|
|
28
62
|
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
.add(
|
|
32
|
-
.
|
|
33
|
-
.run();
|
|
63
|
+
// deferedNomatch runs when run() is called
|
|
64
|
+
const b2 = deferedBranch()
|
|
65
|
+
.add(false, () => 'x')
|
|
66
|
+
.deferedNomatch(() => 'deferred fallback');
|
|
34
67
|
|
|
35
|
-
console.log(
|
|
68
|
+
console.log(b2.run()); // 'deferred fallback'
|
|
36
69
|
```
|
|
37
70
|
|
|
38
71
|
## API Reference π
|
|
39
72
|
|
|
40
|
-
|
|
73
|
+
All functions accept a `Fn` type: `type Fn = (...args: unknown[]) => unknown`.
|
|
41
74
|
|
|
42
|
-
|
|
75
|
+
### `deferedBranch(): DeferBranch`
|
|
43
76
|
|
|
44
|
-
|
|
77
|
+
Create a new `DeferBranch` instance.
|
|
45
78
|
|
|
46
|
-
|
|
47
|
-
Multiple calls will override the previous matched branch.
|
|
79
|
+
### `add(condition: boolean, branch: Fn): DeferBranch`
|
|
48
80
|
|
|
49
|
-
|
|
81
|
+
Register a branch. If `condition` is true the provided `branch` becomes the current matched branch. Later `add(true, ...)` calls will override the matched branch.
|
|
50
82
|
|
|
51
|
-
|
|
83
|
+
Throws `TypeError` if `branch` is not a function.
|
|
52
84
|
|
|
53
|
-
### `
|
|
85
|
+
### `nomatch(handler: Fn): DeferBranch`
|
|
54
86
|
|
|
55
|
-
|
|
56
|
-
Returns `undefined` if neither is set.
|
|
87
|
+
If no branch has been matched when this is called, the `handler` is invoked immediately. This is useful for side-effects that should run right away when a branch is absent. Returns the instance for chaining. Throws `TypeError` when `handler` is not a function.
|
|
57
88
|
|
|
58
|
-
|
|
89
|
+
### `deferedNomatch(handler: Fn): DeferBranch`
|
|
59
90
|
|
|
60
|
-
|
|
61
|
-
const branch = deferBranch()
|
|
62
|
-
.add(false, () => 'A')
|
|
63
|
-
.add(false, () => 'B')
|
|
64
|
-
.nomatch(() => 'Fallback!');
|
|
91
|
+
Register a handler to be executed by `run()` if no branch matched. Unlike `nomatch`, this does not run immediately β it defers execution until `run()`.
|
|
65
92
|
|
|
66
|
-
|
|
67
|
-
|
|
93
|
+
### `run(): unknown`
|
|
94
|
+
|
|
95
|
+
Execute the matched branch and return its value. If no branch matched but a `deferedNomatch` handler was set, returns the handler's return value. Returns `undefined` if neither exists.
|
|
96
|
+
|
|
97
|
+
## Types & Distribution
|
|
98
|
+
|
|
99
|
+
- Types are included (`"types": "./dist/index.d.ts"` in package.json).
|
|
100
|
+
- The package entrypoint is an ESM module at `./dist/index.mjs`.
|
|
68
101
|
|
|
69
102
|
## Error Handling β οΈ
|
|
70
103
|
|
|
71
|
-
-
|
|
104
|
+
- `TypeError` is thrown if a non-function is passed as a branch or handler.
|
|
72
105
|
|
|
73
|
-
## License
|
|
106
|
+
## Author & License
|
|
74
107
|
|
|
75
|
-
|
|
108
|
+
- Author: Kasukabe Tsumugi (<futami16237@gmail.com>)
|
|
109
|
+
- License: MIT
|
package/dist/index.d.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
declare class DeferBranch {
|
|
2
2
|
private _branch;
|
|
3
|
-
private
|
|
3
|
+
private _nomatch;
|
|
4
4
|
/**
|
|
5
5
|
* Add a new entry
|
|
6
6
|
* - will **override** the previous matched branch
|
|
@@ -11,22 +11,31 @@ declare class DeferBranch {
|
|
|
11
11
|
add(condition: boolean, branch: Fn): DeferBranch;
|
|
12
12
|
/**
|
|
13
13
|
* When no branch matched, run this handler
|
|
14
|
+
* - This will be called **instantly** and **ignores later matched branches**
|
|
14
15
|
* @param handler handle the exhausted case
|
|
15
16
|
* @returns this
|
|
16
17
|
*/
|
|
17
18
|
nomatch(handler: Fn): DeferBranch;
|
|
19
|
+
/**
|
|
20
|
+
* When no branch matched, run this handler
|
|
21
|
+
* - Will be called when `run()` is called
|
|
22
|
+
* @param handler handle the exhausted case
|
|
23
|
+
* @returns this
|
|
24
|
+
*/
|
|
25
|
+
deferedNomatch(handler: Fn): DeferBranch;
|
|
18
26
|
/**
|
|
19
27
|
* If some branch matched, return its returnValue
|
|
20
28
|
* - if no branch matched, return the nomatch handler returnValue
|
|
21
29
|
* - if no branch matched and no nomatch handler, return undefined
|
|
30
|
+
* @param args arguments to pass to the matched branch or nomatch handler
|
|
22
31
|
*/
|
|
23
|
-
run(): unknown;
|
|
32
|
+
run(...args: unknown[]): unknown;
|
|
24
33
|
}
|
|
25
34
|
/**
|
|
26
35
|
* ## About
|
|
27
36
|
* @package DeferedBranch
|
|
28
37
|
* @author Kasukabe Tsumugi <futami16237@gmail.com>
|
|
29
|
-
* @version 1.0
|
|
38
|
+
* @version 1.2.0 (Last Update: 2025.09.19 08:00:16.406)
|
|
30
39
|
* @license MIT
|
|
31
40
|
* @link https://github.com/baendlorel/defered-branch
|
|
32
41
|
* @description Sometimes we need to judge something with multiple cases, but the main job should be done later. So we have Defered Branch!
|
package/dist/index.mjs
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
class t{t=null;
|
|
1
|
+
class t{t=null;o=null;add(t,n){if("function"!=typeof n)throw new TypeError("Branch must be a function");return t&&(this.t=n),this}nomatch(t){if("function"!=typeof t)throw new TypeError("Branch must be a function");return this.t||t(),this}deferedNomatch(t){if("function"!=typeof t)throw new TypeError("Branch must be a function");return this.o=t,this}run(...t){return this.t?this.t(...t):this.o?this.o(...t):void 0}}const n=()=>new t;export{n as deferedBranch};
|