defered-branch 1.0.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/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2025 kasukabe tsumugi
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,75 @@
1
+ # defered-branch 🚦
2
+
3
+ A tiny TypeScript/JavaScript library for deferred branching logic.
4
+ Easily manage conditional execution and fallback handlers in a clean, chainable way!
5
+
6
+ For more awesome packages, check out [my homepage💛](https://baendlorel.github.io/?repoType=npm)
7
+
8
+ ## Features ✨
9
+
10
+ - Chainable API for adding branches
11
+ - Only the last matched branch is executed
12
+ - Custom handler for unmatched cases
13
+ - TypeScript support
14
+ - Lightweight and dependency-free
15
+
16
+ ## Installation 📦
17
+
18
+ ```bash
19
+ pnpm add defered-branch
20
+ # or
21
+ npm install defered-branch
22
+ ```
23
+
24
+ ## Usage 🚀
25
+
26
+ ```ts
27
+ import { deferBranch } from 'defered-branch';
28
+
29
+ const result = deferBranch()
30
+ .add(false, () => 'not this')
31
+ .add(true, () => 'this branch runs!')
32
+ .nomatch(() => 'no branch matched')
33
+ .run();
34
+
35
+ console.log(result); // 'this branch runs!'
36
+ ```
37
+
38
+ ## API Reference 📚
39
+
40
+ ### `deferBranch(): DeferBranch`
41
+
42
+ Creates a new branch handler.
43
+
44
+ ### `add(condition: boolean, branch: () => any): this`
45
+
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.
48
+
49
+ ### `nomatch(handler: () => any): this`
50
+
51
+ Sets a handler to run if no branch matches.
52
+
53
+ ### `run(): any`
54
+
55
+ Executes the matched branch, or the nomatch handler if none matched.
56
+ Returns `undefined` if neither is set.
57
+
58
+ ## Example 🧩
59
+
60
+ ```ts
61
+ const branch = deferBranch()
62
+ .add(false, () => 'A')
63
+ .add(false, () => 'B')
64
+ .nomatch(() => 'Fallback!');
65
+
66
+ console.log(branch.run()); // 'Fallback!'
67
+ ```
68
+
69
+ ## Error Handling ⚠️
70
+
71
+ - Throws `TypeError` if branch or nomatch handler is not a function.
72
+
73
+ ## License 📄
74
+
75
+ MIT
@@ -0,0 +1,38 @@
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;
22
+ }
23
+ /**
24
+ * ## About
25
+ * @package DeferedBranch
26
+ * @author Kasukabe Tsumugi <futami16237@gmail.com>
27
+ * @version 1.0.0 (Last Update: 2025.09.19 07:27:30.174)
28
+ * @license MIT
29
+ * @link https://github.com/baendlorel/defered-branch
30
+ * @description Sometimes we need to judge something with multiple cases, but the main job should be done later. So we have Defered Branch!
31
+ * @copyright Copyright (c) 2025 Kasukabe Tsumugi. All rights reserved.
32
+ */
33
+ declare const deferBranch: () => DeferBranch;
34
+
35
+ export { deferBranch };
36
+
37
+ // # from: src/global.d.ts
38
+ type Fn = (...args: unknown[]) => unknown;
package/dist/index.mjs ADDED
@@ -0,0 +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};
package/package.json ADDED
@@ -0,0 +1,58 @@
1
+ {
2
+ "name": "defered-branch",
3
+ "version": "1.0.0",
4
+ "author": {
5
+ "name": "Kasukabe Tsumugi",
6
+ "email": "futami16237@gmail.com"
7
+ },
8
+ "purpose": "npm",
9
+ "description": "Sometimes we need to judge something with multiple cases, but the main job should be done later. So we have Defered Branch!",
10
+ "description_zh": "有时候我们需要根据多个条件来判断某件事,但主要工作应该在之后完成。所以我们有了延迟分支!",
11
+ "type": "module",
12
+ "module": "./dist/index.mjs",
13
+ "types": "./dist/index.d.ts",
14
+ "exports": {
15
+ "types": "./dist/index.d.ts",
16
+ "import": "./dist/index.mjs",
17
+ "default": "./dist/index.mjs"
18
+ },
19
+ "files": [
20
+ "dist"
21
+ ],
22
+ "homepage": "https://github.com/baendlorel/defered-branch#readme",
23
+ "repository": {
24
+ "type": "git",
25
+ "url": "https://github.com/baendlorel/defered-branch"
26
+ },
27
+ "keywords": [
28
+ "branch",
29
+ "assert",
30
+ "typescript",
31
+ "javascript"
32
+ ],
33
+ "scripts": {
34
+ "test": "clear & vitest",
35
+ "lint": "oxlint .",
36
+ "cover": "clear & vitest --coverage",
37
+ "build": "node ./scripts/rollup.mjs"
38
+ },
39
+ "license": "MIT",
40
+ "devDependencies": {
41
+ "@rollup/plugin-alias": "^5.1.1",
42
+ "@rollup/plugin-node-resolve": "^16.0.1",
43
+ "@rollup/plugin-replace": "^6.0.2",
44
+ "@rollup/plugin-terser": "^0.4.4",
45
+ "@rollup/plugin-typescript": "^12.1.4",
46
+ "@types/node": "^24.5.2",
47
+ "@vitest/coverage-v8": "^3.2.4",
48
+ "oxlint": "^1.16.0",
49
+ "prettier": "^3.6.2",
50
+ "rimraf": "^6.0.1",
51
+ "rollup": "^4.50.2",
52
+ "rollup-plugin-dts": "^6.2.3",
53
+ "rollup-plugin-dts-merger": "^1.2.4",
54
+ "tslib": "^2.8.1",
55
+ "typescript": "^5.9.2",
56
+ "vitest": "^3.2.4"
57
+ }
58
+ }