defered-branch 1.0.0 β†’ 1.1.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 CHANGED
@@ -1,17 +1,17 @@
1
1
  # defered-branch 🚦
2
2
 
3
- A tiny TypeScript/JavaScript library for deferred branching logic.
4
- Easily manage conditional execution and fallback handlers in a clean, chainable way!
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
- ## Features ✨
8
+ ## Highlights ✨
9
9
 
10
- - Chainable API for adding branches
10
+ - Chainable API for registering branches
11
11
  - Only the last matched branch is executed
12
- - Custom handler for unmatched cases
13
- - TypeScript support
14
- - Lightweight and dependency-free
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
- ## Usage πŸš€
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 { deferBranch } from 'defered-branch';
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
- const result = deferBranch()
30
- .add(false, () => 'not this')
31
- .add(true, () => 'this branch runs!')
32
- .nomatch(() => 'no branch matched')
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(result); // 'this branch runs!'
68
+ console.log(b2.run()); // 'deferred fallback'
36
69
  ```
37
70
 
38
71
  ## API Reference πŸ“š
39
72
 
40
- ### `deferBranch(): DeferBranch`
73
+ All functions accept a `Fn` type: `type Fn = (...args: unknown[]) => unknown`.
41
74
 
42
- Creates a new branch handler.
75
+ ### `deferedBranch(): DeferBranch`
43
76
 
44
- ### `add(condition: boolean, branch: () => any): this`
77
+ Create a new `DeferBranch` instance.
45
78
 
46
- Adds a branch. If `condition` is true, this branch will be set as the one to run.
47
- Multiple calls will override the previous matched branch.
79
+ ### `add(condition: boolean, branch: Fn): DeferBranch`
48
80
 
49
- ### `nomatch(handler: () => any): this`
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
- Sets a handler to run if no branch matches.
83
+ Throws `TypeError` if `branch` is not a function.
52
84
 
53
- ### `run(): any`
85
+ ### `nomatch(handler: Fn): DeferBranch`
54
86
 
55
- Executes the matched branch, or the nomatch handler if none matched.
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
- ## Example 🧩
89
+ ### `deferedNomatch(handler: Fn): DeferBranch`
59
90
 
60
- ```ts
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
- console.log(branch.run()); // 'Fallback!'
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
- - Throws `TypeError` if branch or nomatch handler is not a function.
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
- MIT
108
+ - Author: Kasukabe Tsumugi (<futami16237@gmail.com>)
109
+ - License: MIT
package/dist/index.d.ts CHANGED
@@ -1,38 +1,48 @@
1
1
  declare class DeferBranch {
2
- /**
3
- * Add a new entry
4
- * - will **override** the previous matched branch
5
- * @param condition the condition to match
6
- * @param branch the branch to run when matched
7
- * @returns this
8
- */
9
- add(condition: boolean, branch: Fn): DeferBranch;
10
- /**
11
- * When no branch matched, run this handler
12
- * @param handler handle the exhausted case
13
- * @returns this
14
- */
15
- nomatch(handler: Fn): DeferBranch;
16
- /**
17
- * If some branch matched, return its returnValue
18
- * - if no branch matched, return the nomatch handler returnValue
19
- * - if no branch matched and no nomatch handler, return undefined
20
- */
21
- run(): unknown;
2
+ private _branch;
3
+ private _nomatch;
4
+ /**
5
+ * Add a new entry
6
+ * - will **override** the previous matched branch
7
+ * @param condition the condition to match
8
+ * @param branch the branch to run when matched
9
+ * @returns this
10
+ */
11
+ add(condition: boolean, branch: Fn): DeferBranch;
12
+ /**
13
+ * When no branch matched, run this handler
14
+ * - This will be called **instantly** and **ignores later matched branches**
15
+ * @param handler handle the exhausted case
16
+ * @returns this
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;
26
+ /**
27
+ * If some branch matched, return its returnValue
28
+ * - if no branch matched, return the nomatch handler returnValue
29
+ * - if no branch matched and no nomatch handler, return undefined
30
+ */
31
+ run(): unknown;
22
32
  }
23
33
  /**
24
34
  * ## About
25
35
  * @package DeferedBranch
26
36
  * @author Kasukabe Tsumugi <futami16237@gmail.com>
27
- * @version 1.0.0 (Last Update: 2025.09.19 07:27:30.174)
37
+ * @version 1.1.0 (Last Update: 2025.09.19 07:53:15.059)
28
38
  * @license MIT
29
39
  * @link https://github.com/baendlorel/defered-branch
30
40
  * @description Sometimes we need to judge something with multiple cases, but the main job should be done later. So we have Defered Branch!
31
41
  * @copyright Copyright (c) 2025 Kasukabe Tsumugi. All rights reserved.
32
42
  */
33
- declare const deferBranch: () => DeferBranch;
43
+ declare const deferedBranch: () => DeferBranch;
34
44
 
35
- export { deferBranch };
45
+ export { deferedBranch };
36
46
 
37
47
  // # from: src/global.d.ts
38
48
  type Fn = (...args: unknown[]) => unknown;
package/dist/index.mjs CHANGED
@@ -1 +1 @@
1
- class t{t=null;h=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.h=t,this}run(){return this.t?this.t():this.h?this.h():void 0}}const n=()=>new t;export{n as deferBranch};
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(){return this.t?this.t():this.o?this.o():void 0}}const n=()=>new t;export{n as deferedBranch};
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "defered-branch",
3
- "version": "1.0.0",
3
+ "version": "1.1.0",
4
4
  "author": {
5
5
  "name": "Kasukabe Tsumugi",
6
6
  "email": "futami16237@gmail.com"