defered-branch 2.2.9 → 2.2.10
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/dist/index.cjs +150 -0
- package/dist/index.cjs.map +1 -0
- package/dist/index.d.cts +244 -0
- package/dist/{index.d.ts → index.d.mts} +123 -116
- package/dist/index.mjs +150 -93
- package/dist/index.mjs.map +1 -0
- package/package.json +5 -5
package/dist/index.cjs
ADDED
|
@@ -0,0 +1,150 @@
|
|
|
1
|
+
Object.defineProperty(exports,Symbol.toStringTag,{value:`Module`});var e=class{_branch=null;add(e,t){if(typeof t!=`function`)throw TypeError(`DeferBranch: branch must be a function`);return e&&!this._branch&&(this._branch=t),this}nomatch(e){if(typeof e!=`function`)throw TypeError(`DeferBranch: branch must be a function`);return this._branch||e(),this}run(...e){if(this._branch){let t=this._branch.apply(null,e);return this._branch=null,t}}},t=class{_condranches=[];_branch=null;_nomatch=null;add(e,t){if(typeof e!=`function`)throw TypeError(`DeferBranch: condition must be a function`);if(typeof t!=`function`)throw TypeError(`DeferBranch: branch must be a function`);return this._condranches.push(e,t),this}nomatch(e){if(typeof e!=`function`)throw TypeError(`DeferBranch: handler must be a function`);return this._nomatch=e,this}predicate(...e){let t=this._condranches.length;for(let e=0;e<t;e+=2)if(this._condranches[e]()){this._branch=this._condranches[e+1];return}this._nomatch&&this._nomatch.apply(this,e)}run(...e){if(this._branch){let t=this._branch.apply(null,e);return this._branch=null,t}}},n=class{_condranches=[];_branches=[];_nomatch=null;add(e,t){if(typeof e!=`function`)throw TypeError(`DeferBranch: condition must be a function`);if(typeof t!=`function`)throw TypeError(`DeferBranch: branch must be a function`);return this._condranches.push(e,t),this}nomatch(e){if(typeof e!=`function`)throw TypeError(`DeferBranch: handler must be a function`);return this._nomatch=e,this}predicate(...e){let t=this._condranches.length;for(let e=0;e<t;e+=2)this._condranches[e]()&&this._branches.push(this._condranches[e+1]);this._nomatch&&this._branches.length===0&&this._nomatch.apply(this,e)}run(...e){let t=this._branches.length;if(t!==0){for(let n=0;n<t;n++)this._branches[n].apply(null,e);this._branches.length=0}}},r=class{_branches=[];add(e,t){if(typeof t!=`function`)throw TypeError(`DeferBranch: branch must be a function`);return e&&this._branches.push(t),this}nomatch(e){if(typeof e!=`function`)throw TypeError(`DeferBranch: branch must be a function`);return this._branches.length===0&&e(),this}run(...e){let t=this._branches.length;if(t!==0){for(let n=0;n<t;n++)this._branches[n].apply(null,e);this._branches.length=0}}};
|
|
2
|
+
/**
|
|
3
|
+
* ## Steps to use
|
|
4
|
+
* 1. const a = deferedBranch()
|
|
5
|
+
* 2. add branches by a.add(condition, branch)
|
|
6
|
+
* 3. (optional) add defered nomatch handler, or add and run at the same time by a.nomatch(handler)
|
|
7
|
+
* 4. ...run some other logic
|
|
8
|
+
* 5. run this branch by a.run();
|
|
9
|
+
*
|
|
10
|
+
* ## Type Annotation (optional)
|
|
11
|
+
* ```ts
|
|
12
|
+
* type BranchFn = (a: number) => void;
|
|
13
|
+
* type NoMatchFn = () => void;
|
|
14
|
+
* const a = deferedBranch<BranchFn, NoMatchFn>();
|
|
15
|
+
*
|
|
16
|
+
* // then we have restrictions:
|
|
17
|
+
* a.add(true, someFn satisfies BranchFn);
|
|
18
|
+
* a.nomatch(otherFn satisfies NoMatchFn);
|
|
19
|
+
*
|
|
20
|
+
* // some other logic
|
|
21
|
+
*
|
|
22
|
+
* a.run(...args); // input args are restricted to Parameters<BranchFn>
|
|
23
|
+
* ```
|
|
24
|
+
*
|
|
25
|
+
* ## About
|
|
26
|
+
* @package DeferedBranch
|
|
27
|
+
* @author Kasukabe Tsumugi <futami16237@gmail.com>
|
|
28
|
+
* @version 2.2.10 (Last Update: 2026.10.12 10:07:47.649)
|
|
29
|
+
* @license MIT
|
|
30
|
+
* @link git+https://github.com/baendlorel/tsumugi-lib.git
|
|
31
|
+
* @link https://baendlorel.github.io/ Welcome to my site!
|
|
32
|
+
* @description Sometimes we need to judge something with multiple cases, but the main job should be done later. So we have Defered Branch!
|
|
33
|
+
* @copyright Copyright (c) 2026 Kasukabe Tsumugi. All rights reserved.
|
|
34
|
+
*/
|
|
35
|
+
const i=()=>new e,a=()=>new t,o=()=>new r,s=()=>new n
|
|
36
|
+
/**
|
|
37
|
+
* ## Steps to use
|
|
38
|
+
* 1. const a = deferedBranchDynamic()
|
|
39
|
+
* 2. add branches by a.add(condition, branch)
|
|
40
|
+
* 3. add nomatch handler by a.nomatch(handler)
|
|
41
|
+
* 4. call a.predicate(...) to find the matched branch
|
|
42
|
+
* 5. ...run some other logic
|
|
43
|
+
* 6. run this branch by a.run();
|
|
44
|
+
*
|
|
45
|
+
* ## Type Annotation (optional)
|
|
46
|
+
* ```ts
|
|
47
|
+
* type BranchFn = (a: number) => void;
|
|
48
|
+
* type NoMatchFn = () => void;
|
|
49
|
+
* // optional, you can customize the condition function type
|
|
50
|
+
* type ConditionFn = Predicate<BranchFn>;
|
|
51
|
+
* const a = deferedBranch<BranchFn, NoMatchFn, ConditionFn>();
|
|
52
|
+
*
|
|
53
|
+
* // then we have restrictions:
|
|
54
|
+
* a.add(someFn satisfies ConditionFn, secondFn satisfies BranchFn);
|
|
55
|
+
* a.nomatch(thirdFn satisfies NoMatchFn);
|
|
56
|
+
*
|
|
57
|
+
* // some other logic
|
|
58
|
+
*
|
|
59
|
+
* a.predicate(...args);
|
|
60
|
+
*
|
|
61
|
+
* // some other logic
|
|
62
|
+
*
|
|
63
|
+
* a.run(...args); // input args are restricted to Parameters<BranchFn>
|
|
64
|
+
* ```
|
|
65
|
+
*
|
|
66
|
+
* ## About
|
|
67
|
+
* @package DeferedBranch
|
|
68
|
+
* @author Kasukabe Tsumugi <futami16237@gmail.com>
|
|
69
|
+
* @version 2.2.10 (Last Update: 2026.10.12 10:07:47.649)
|
|
70
|
+
* @license MIT
|
|
71
|
+
* @link git+https://github.com/baendlorel/tsumugi-lib.git
|
|
72
|
+
* @link https://baendlorel.github.io/ Welcome to my site!
|
|
73
|
+
* @description Sometimes we need to judge something with multiple cases, but the main job should be done later. So we have Defered Branch!
|
|
74
|
+
* @copyright Copyright (c) 2026 Kasukabe Tsumugi. All rights reserved.
|
|
75
|
+
*/
|
|
76
|
+
/**
|
|
77
|
+
* ## Steps to use
|
|
78
|
+
* 1. const a = deferedBranchAll()
|
|
79
|
+
* 2. add branches by a.add(condition, branch)
|
|
80
|
+
* 3. (optional) add defered nomatch handler, or add and run at the same time by a.nomatch(handler)
|
|
81
|
+
* 4. ...run some other logic
|
|
82
|
+
* 5. run all matched branches by a.run();
|
|
83
|
+
*
|
|
84
|
+
* ## Type Annotation (optional)
|
|
85
|
+
* ```ts
|
|
86
|
+
* type BranchFn = (a: number) => void;
|
|
87
|
+
* type NoMatchFn = () => void;
|
|
88
|
+
* const a = deferedBranchAll<BranchFn, NoMatchFn>();
|
|
89
|
+
*
|
|
90
|
+
* // then we have restrictions:
|
|
91
|
+
* a.add(true, someFn satisfies BranchFn);
|
|
92
|
+
* a.nomatch(otherFn satisfies NoMatchFn);
|
|
93
|
+
*
|
|
94
|
+
* // some other logic
|
|
95
|
+
*
|
|
96
|
+
* a.run(...args); // input args are restricted to Parameters<BranchFn>
|
|
97
|
+
* ```
|
|
98
|
+
*
|
|
99
|
+
* ## About
|
|
100
|
+
* @package DeferedBranch
|
|
101
|
+
* @author Kasukabe Tsumugi <futami16237@gmail.com>
|
|
102
|
+
* @version 2.2.10 (Last Update: 2026.10.12 10:07:47.649)
|
|
103
|
+
* @license MIT
|
|
104
|
+
* @link git+https://github.com/baendlorel/tsumugi-lib.git
|
|
105
|
+
* @link https://baendlorel.github.io/ Welcome to my site!
|
|
106
|
+
* @description Sometimes we need to judge something with multiple cases, but the main job should be done later. So we have Defered Branch!
|
|
107
|
+
* @copyright Copyright (c) 2026 Kasukabe Tsumugi. All rights reserved.
|
|
108
|
+
*/
|
|
109
|
+
/**
|
|
110
|
+
* ## Steps to use
|
|
111
|
+
* 1. const a = deferedBranchAllDynamic()
|
|
112
|
+
* 2. add branches by a.add(condition, branch)
|
|
113
|
+
* 3. add nomatch handler by a.nomatch(handler)
|
|
114
|
+
* 4. call a.predicate(...) to find all matched branches
|
|
115
|
+
* 5. ...run some other logic
|
|
116
|
+
* 6. run all matched branches by a.run();
|
|
117
|
+
*
|
|
118
|
+
* ## Type Annotation (optional)
|
|
119
|
+
* ```ts
|
|
120
|
+
* type BranchFn = (a: number) => void;
|
|
121
|
+
* type NoMatchFn = () => void;
|
|
122
|
+
* // optional, you can customize the condition function type
|
|
123
|
+
* type ConditionFn = Predicate<BranchFn>;
|
|
124
|
+
* const a = deferedBranchAllDynamic<BranchFn, NoMatchFn, ConditionFn>();
|
|
125
|
+
*
|
|
126
|
+
* // then we have restrictions:
|
|
127
|
+
* a.add(someFn satisfies ConditionFn, secondFn satisfies BranchFn);
|
|
128
|
+
* a.nomatch(thirdFn satisfies NoMatchFn);
|
|
129
|
+
*
|
|
130
|
+
* // some other logic
|
|
131
|
+
*
|
|
132
|
+
* a.predicate(...args);
|
|
133
|
+
*
|
|
134
|
+
* // some other logic
|
|
135
|
+
*
|
|
136
|
+
* a.run(...args); // input args are restricted to Parameters<BranchFn>
|
|
137
|
+
* ```
|
|
138
|
+
*
|
|
139
|
+
* ## About
|
|
140
|
+
* @package DeferedBranch
|
|
141
|
+
* @author Kasukabe Tsumugi <futami16237@gmail.com>
|
|
142
|
+
* @version 2.2.10 (Last Update: 2026.10.12 10:07:47.649)
|
|
143
|
+
* @license MIT
|
|
144
|
+
* @link git+https://github.com/baendlorel/tsumugi-lib.git
|
|
145
|
+
* @link https://baendlorel.github.io/ Welcome to my site!
|
|
146
|
+
* @description Sometimes we need to judge something with multiple cases, but the main job should be done later. So we have Defered Branch!
|
|
147
|
+
* @copyright Copyright (c) 2026 Kasukabe Tsumugi. All rights reserved.
|
|
148
|
+
*/
|
|
149
|
+
;exports.deferedBranch=i,exports.deferedBranchAll=o,exports.deferedBranchAllDynamic=s,exports.deferedBranchDynamic=a;
|
|
150
|
+
//# sourceMappingURL=index.cjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.cjs","names":["DeferedBranch","branch","condition","handler","args","result","DeferedBranchDynamic","len","i","DeferedBranchAllDynamic","DeferedBranchAll","deferedBranch","deferedBranchDynamic","deferedBranchAll","deferedBranchAllDynamic"],"sources":["../src/core/default.ts","../src/core/dynamic.ts","../src/core/all-dynamic.ts","../src/core/all.ts","../src/index.ts"],"sourcesContent":["import { AnyFn } from '@defered-branch/global.js';\n\nexport class DeferedBranch<BranchFn extends AnyFn, NoMatchFn extends AnyFn> {\n private _branch: BranchFn | null = null;\n\n /**\n * Add a new entry\n * - **First truthy first served**.\n * @param condition the condition to match\n * @param branch the branch to run when matched\n * @returns this\n */\n add(condition: boolean, branch: BranchFn): this {\n if (typeof branch !== 'function') {\n throw new TypeError('DeferBranch: branch must be a function');\n }\n\n // & Preventing later branches from overwriting earlier matched branch\n if (condition && !this._branch) {\n this._branch = branch;\n }\n return this;\n }\n\n /**\n * Register a handler to be called when no branch matched\n * - This will be called **instantly and ignores later matched branches**\n * @param handler handle the exhausted case\n * @returns this\n */\n nomatch(handler: NoMatchFn): this {\n if (typeof handler !== 'function') {\n throw new TypeError('DeferBranch: branch must be a function');\n }\n\n if (!this._branch) {\n handler();\n }\n\n return this;\n }\n\n /**\n * Execute the first matched branch in order of addition\n * @param args arguments to pass to the matched branch or nomatch handler\n * @returns the return value of the matched branch or void\n */\n run(...args: Parameters<BranchFn>): ReturnType<BranchFn> | void {\n if (this._branch) {\n const result = this._branch.apply(null, args);\n this._branch = null;\n return result;\n }\n }\n}\n","import { AnyFn, Predicate } from '@defered-branch/global.js';\n\nexport class DeferedBranchDynamic<\n BranchFn extends AnyFn,\n NoMatchFn extends AnyFn,\n ConditionFn extends AnyFn = Predicate<BranchFn>,\n> {\n /**\n * & Compress conditions and branches into a single array since they are paired\n */\n private _condranches: AnyFn[] = [];\n private _branch: BranchFn | null = null;\n private _nomatch: NoMatchFn | null = null;\n\n /**\n * Add a new entry\n * - **First truthy first served**.\n * @param condition the condition to match\n * @param branch the branch to run when matched\n * @returns this\n */\n add(condition: ConditionFn, branch: BranchFn): this {\n if (typeof condition !== 'function') {\n throw new TypeError('DeferBranch: condition must be a function');\n }\n if (typeof branch !== 'function') {\n throw new TypeError('DeferBranch: branch must be a function');\n }\n\n this._condranches.push(condition, branch);\n return this;\n }\n\n /**\n * When calling `this.predicate` and no branch matched, run this handler\n * @param handler handle the exhausted case\n * @returns this\n */\n nomatch(handler: AnyFn): this {\n if (typeof handler !== 'function') {\n throw new TypeError('DeferBranch: handler must be a function');\n }\n\n this._nomatch = handler as NoMatchFn;\n return this;\n }\n\n /**\n * Run the condition to find the first matched branch\n */\n predicate(...args: Parameters<ConditionFn>): void {\n const len = this._condranches.length;\n for (let i = 0; i < len; i += 2) {\n if (this._condranches[i]()) {\n this._branch = this._condranches[i + 1] as BranchFn;\n return;\n }\n }\n\n if (this._nomatch) {\n this._nomatch.apply(this, args);\n }\n }\n\n /**\n * Execute the first matched branch in order of addition\n * @param args arguments to pass to the matched branch\n * @returns the return value of the matched branch or void\n */\n run(...args: Parameters<BranchFn>): ReturnType<BranchFn> | undefined {\n if (this._branch) {\n const result = this._branch.apply(null, args);\n this._branch = null;\n return result;\n }\n }\n}\n","import { AnyFn, Predicate } from '@defered-branch/global.js';\n\nexport class DeferedBranchAllDynamic<\n BranchFn extends AnyFn,\n NoMatchFn extends AnyFn,\n ConditionFn extends AnyFn = Predicate<BranchFn>,\n> {\n /**\n * & Compress conditions and branches into a single array since they are paired\n */\n private _condranches: AnyFn[] = [];\n private _branches: BranchFn[] = [];\n private _nomatch: NoMatchFn | null = null;\n\n /**\n * Add a new entry\n * - **All truthy branches will be executed**.\n * @param condition the condition to match\n * @param branch the branch to run when matched\n * @returns this\n */\n add(condition: ConditionFn, branch: BranchFn): this {\n if (typeof condition !== 'function') {\n throw new TypeError('DeferBranch: condition must be a function');\n }\n if (typeof branch !== 'function') {\n throw new TypeError('DeferBranch: branch must be a function');\n }\n\n this._condranches.push(condition, branch);\n return this;\n }\n\n /**\n * When calling `this.predicate` and no branch matched, run this handler\n * @param handler handle the exhausted case\n * @returns this\n */\n nomatch(handler: AnyFn): this {\n if (typeof handler !== 'function') {\n throw new TypeError('DeferBranch: handler must be a function');\n }\n\n this._nomatch = handler as NoMatchFn;\n return this;\n }\n\n /**\n * Run the conditions to find all matched branches\n */\n predicate(...args: Parameters<ConditionFn>): void {\n const len = this._condranches.length;\n for (let i = 0; i < len; i += 2) {\n if (this._condranches[i]()) {\n this._branches.push(this._condranches[i + 1] as BranchFn);\n }\n }\n\n if (this._nomatch && this._branches.length === 0) {\n this._nomatch.apply(this, args);\n }\n }\n\n /**\n * Execute all matched branches in order of addition\n * @param args arguments to pass to the matched branches\n */\n run(...args: Parameters<BranchFn>): ReturnType<BranchFn> | undefined {\n const len = this._branches.length;\n if (len === 0) {\n return;\n }\n for (let i = 0; i < len; i++) {\n this._branches[i].apply(null, args);\n }\n this._branches.length = 0;\n }\n}\n","import { AnyFn } from '@defered-branch/global.js';\n\nexport class DeferedBranchAll<BranchFn extends AnyFn, NoMatchFn extends AnyFn> {\n private _branches: BranchFn[] = [];\n\n /**\n * Add a new entry\n * - **All truthy branches will be executed**.\n * @param condition the condition to match\n * @param branch the branch to run when matched\n * @returns this\n */\n add(condition: boolean, branch: BranchFn): this {\n if (typeof branch !== 'function') {\n throw new TypeError('DeferBranch: branch must be a function');\n }\n\n // & Preventing later branches from overwriting earlier matched branch\n if (condition) {\n this._branches.push(branch);\n }\n return this;\n }\n\n /**\n * Register a handler to be called when no branch matched\n * - This will be called **instantly and ignores later matched branches**\n * @param handler handle the exhausted case\n * @returns this\n */\n nomatch(handler: NoMatchFn): this {\n if (typeof handler !== 'function') {\n throw new TypeError('DeferBranch: branch must be a function');\n }\n\n if (this._branches.length === 0) {\n handler();\n }\n\n return this;\n }\n\n /**\n * Execute all matched branches in order of addition\n * @param args arguments to pass to the matched branch or nomatch handler\n */\n run(...args: Parameters<BranchFn>): void {\n const len = this._branches.length;\n if (len === 0) {\n return;\n }\n for (let i = 0; i < len; i++) {\n this._branches[i].apply(null, args);\n }\n this._branches.length = 0;\n }\n}\n","import { DeferedBranch } from './core/default.js';\nimport { DeferedBranchDynamic } from './core/dynamic.js';\nimport { DeferedBranchAllDynamic } from './core/all-dynamic.js';\nimport { DeferedBranchAll } from './core/all.js';\nimport { AnyFn, Predicate } from './global.js';\n\n/**\n * ## Steps to use\n * 1. const a = deferedBranch()\n * 2. add branches by a.add(condition, branch)\n * 3. (optional) add defered nomatch handler, or add and run at the same time by a.nomatch(handler)\n * 4. ...run some other logic\n * 5. run this branch by a.run();\n *\n * ## Type Annotation (optional)\n * ```ts\n * type BranchFn = (a: number) => void;\n * type NoMatchFn = () => void;\n * const a = deferedBranch<BranchFn, NoMatchFn>();\n *\n * // then we have restrictions:\n * a.add(true, someFn satisfies BranchFn);\n * a.nomatch(otherFn satisfies NoMatchFn);\n *\n * // some other logic\n *\n * a.run(...args); // input args are restricted to Parameters<BranchFn>\n * ```\n *\n * __PKG_INFO__\n */\nexport const deferedBranch = <BranchFn extends AnyFn = AnyFn, NoMatchFn extends AnyFn = AnyFn>() =>\n new DeferedBranch<BranchFn, NoMatchFn>();\n\n/**\n * ## Steps to use\n * 1. const a = deferedBranchDynamic()\n * 2. add branches by a.add(condition, branch)\n * 3. add nomatch handler by a.nomatch(handler)\n * 4. call a.predicate(...) to find the matched branch\n * 5. ...run some other logic\n * 6. run this branch by a.run();\n *\n * ## Type Annotation (optional)\n * ```ts\n * type BranchFn = (a: number) => void;\n * type NoMatchFn = () => void;\n * // optional, you can customize the condition function type\n * type ConditionFn = Predicate<BranchFn>;\n * const a = deferedBranch<BranchFn, NoMatchFn, ConditionFn>();\n *\n * // then we have restrictions:\n * a.add(someFn satisfies ConditionFn, secondFn satisfies BranchFn);\n * a.nomatch(thirdFn satisfies NoMatchFn);\n *\n * // some other logic\n *\n * a.predicate(...args);\n *\n * // some other logic\n *\n * a.run(...args); // input args are restricted to Parameters<BranchFn>\n * ```\n *\n * __PKG_INFO__\n */\nexport const deferedBranchDynamic = <\n BranchFn extends AnyFn = AnyFn,\n NoMatchFn extends AnyFn = AnyFn,\n ConditionFn extends AnyFn = Predicate<BranchFn>,\n>() => new DeferedBranchDynamic<BranchFn, NoMatchFn, ConditionFn>();\n\n/**\n * ## Steps to use\n * 1. const a = deferedBranchAll()\n * 2. add branches by a.add(condition, branch)\n * 3. (optional) add defered nomatch handler, or add and run at the same time by a.nomatch(handler)\n * 4. ...run some other logic\n * 5. run all matched branches by a.run();\n *\n * ## Type Annotation (optional)\n * ```ts\n * type BranchFn = (a: number) => void;\n * type NoMatchFn = () => void;\n * const a = deferedBranchAll<BranchFn, NoMatchFn>();\n *\n * // then we have restrictions:\n * a.add(true, someFn satisfies BranchFn);\n * a.nomatch(otherFn satisfies NoMatchFn);\n *\n * // some other logic\n *\n * a.run(...args); // input args are restricted to Parameters<BranchFn>\n * ```\n *\n * __PKG_INFO__\n */\nexport const deferedBranchAll = <BranchFn extends AnyFn = AnyFn, NoMatchFn extends AnyFn = AnyFn>() =>\n new DeferedBranchAll<BranchFn, NoMatchFn>();\n\n/**\n * ## Steps to use\n * 1. const a = deferedBranchAllDynamic()\n * 2. add branches by a.add(condition, branch)\n * 3. add nomatch handler by a.nomatch(handler)\n * 4. call a.predicate(...) to find all matched branches\n * 5. ...run some other logic\n * 6. run all matched branches by a.run();\n *\n * ## Type Annotation (optional)\n * ```ts\n * type BranchFn = (a: number) => void;\n * type NoMatchFn = () => void;\n * // optional, you can customize the condition function type\n * type ConditionFn = Predicate<BranchFn>;\n * const a = deferedBranchAllDynamic<BranchFn, NoMatchFn, ConditionFn>();\n *\n * // then we have restrictions:\n * a.add(someFn satisfies ConditionFn, secondFn satisfies BranchFn);\n * a.nomatch(thirdFn satisfies NoMatchFn);\n *\n * // some other logic\n *\n * a.predicate(...args);\n *\n * // some other logic\n *\n * a.run(...args); // input args are restricted to Parameters<BranchFn>\n * ```\n *\n * __PKG_INFO__\n */\nexport const deferedBranchAllDynamic = <\n BranchFn extends AnyFn = AnyFn,\n NoMatchFn extends AnyFn = AnyFn,\n ConditionFn extends AnyFn = Predicate<BranchFn>,\n>() => new DeferedBranchAllDynamic<BranchFn, NoMatchFn, ConditionFn>();\n"],"mappings":"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"}
|
package/dist/index.d.cts
ADDED
|
@@ -0,0 +1,244 @@
|
|
|
1
|
+
//#region src/global.d.ts
|
|
2
|
+
type AnyFn = (...args: any[]) => any;
|
|
3
|
+
type Predicate<Fn extends AnyFn> = (...args: Parameters<Fn>) => boolean;
|
|
4
|
+
//#endregion
|
|
5
|
+
//#region src/core/default.d.ts
|
|
6
|
+
declare class DeferedBranch<BranchFn extends AnyFn, NoMatchFn extends AnyFn> {
|
|
7
|
+
private _branch;
|
|
8
|
+
/**
|
|
9
|
+
* Add a new entry
|
|
10
|
+
* - **First truthy first served**.
|
|
11
|
+
* @param condition the condition to match
|
|
12
|
+
* @param branch the branch to run when matched
|
|
13
|
+
* @returns this
|
|
14
|
+
*/
|
|
15
|
+
add(condition: boolean, branch: BranchFn): this;
|
|
16
|
+
/**
|
|
17
|
+
* Register a handler to be called when no branch matched
|
|
18
|
+
* - This will be called **instantly and ignores later matched branches**
|
|
19
|
+
* @param handler handle the exhausted case
|
|
20
|
+
* @returns this
|
|
21
|
+
*/
|
|
22
|
+
nomatch(handler: NoMatchFn): this;
|
|
23
|
+
/**
|
|
24
|
+
* Execute the first matched branch in order of addition
|
|
25
|
+
* @param args arguments to pass to the matched branch or nomatch handler
|
|
26
|
+
* @returns the return value of the matched branch or void
|
|
27
|
+
*/
|
|
28
|
+
run(...args: Parameters<BranchFn>): ReturnType<BranchFn> | void;
|
|
29
|
+
}
|
|
30
|
+
//#endregion
|
|
31
|
+
//#region src/core/dynamic.d.ts
|
|
32
|
+
declare class DeferedBranchDynamic<BranchFn extends AnyFn, NoMatchFn extends AnyFn, ConditionFn extends AnyFn = Predicate<BranchFn>> {
|
|
33
|
+
/**
|
|
34
|
+
* & Compress conditions and branches into a single array since they are paired
|
|
35
|
+
*/
|
|
36
|
+
private _condranches;
|
|
37
|
+
private _branch;
|
|
38
|
+
private _nomatch;
|
|
39
|
+
/**
|
|
40
|
+
* Add a new entry
|
|
41
|
+
* - **First truthy first served**.
|
|
42
|
+
* @param condition the condition to match
|
|
43
|
+
* @param branch the branch to run when matched
|
|
44
|
+
* @returns this
|
|
45
|
+
*/
|
|
46
|
+
add(condition: ConditionFn, branch: BranchFn): this;
|
|
47
|
+
/**
|
|
48
|
+
* When calling `this.predicate` and no branch matched, run this handler
|
|
49
|
+
* @param handler handle the exhausted case
|
|
50
|
+
* @returns this
|
|
51
|
+
*/
|
|
52
|
+
nomatch(handler: AnyFn): this;
|
|
53
|
+
/**
|
|
54
|
+
* Run the condition to find the first matched branch
|
|
55
|
+
*/
|
|
56
|
+
predicate(...args: Parameters<ConditionFn>): void;
|
|
57
|
+
/**
|
|
58
|
+
* Execute the first matched branch in order of addition
|
|
59
|
+
* @param args arguments to pass to the matched branch
|
|
60
|
+
* @returns the return value of the matched branch or void
|
|
61
|
+
*/
|
|
62
|
+
run(...args: Parameters<BranchFn>): ReturnType<BranchFn> | undefined;
|
|
63
|
+
}
|
|
64
|
+
//#endregion
|
|
65
|
+
//#region src/core/all-dynamic.d.ts
|
|
66
|
+
declare class DeferedBranchAllDynamic<BranchFn extends AnyFn, NoMatchFn extends AnyFn, ConditionFn extends AnyFn = Predicate<BranchFn>> {
|
|
67
|
+
/**
|
|
68
|
+
* & Compress conditions and branches into a single array since they are paired
|
|
69
|
+
*/
|
|
70
|
+
private _condranches;
|
|
71
|
+
private _branches;
|
|
72
|
+
private _nomatch;
|
|
73
|
+
/**
|
|
74
|
+
* Add a new entry
|
|
75
|
+
* - **All truthy branches will be executed**.
|
|
76
|
+
* @param condition the condition to match
|
|
77
|
+
* @param branch the branch to run when matched
|
|
78
|
+
* @returns this
|
|
79
|
+
*/
|
|
80
|
+
add(condition: ConditionFn, branch: BranchFn): this;
|
|
81
|
+
/**
|
|
82
|
+
* When calling `this.predicate` and no branch matched, run this handler
|
|
83
|
+
* @param handler handle the exhausted case
|
|
84
|
+
* @returns this
|
|
85
|
+
*/
|
|
86
|
+
nomatch(handler: AnyFn): this;
|
|
87
|
+
/**
|
|
88
|
+
* Run the conditions to find all matched branches
|
|
89
|
+
*/
|
|
90
|
+
predicate(...args: Parameters<ConditionFn>): void;
|
|
91
|
+
/**
|
|
92
|
+
* Execute all matched branches in order of addition
|
|
93
|
+
* @param args arguments to pass to the matched branches
|
|
94
|
+
*/
|
|
95
|
+
run(...args: Parameters<BranchFn>): ReturnType<BranchFn> | undefined;
|
|
96
|
+
}
|
|
97
|
+
//#endregion
|
|
98
|
+
//#region src/core/all.d.ts
|
|
99
|
+
declare class DeferedBranchAll<BranchFn extends AnyFn, NoMatchFn extends AnyFn> {
|
|
100
|
+
private _branches;
|
|
101
|
+
/**
|
|
102
|
+
* Add a new entry
|
|
103
|
+
* - **All truthy branches will be executed**.
|
|
104
|
+
* @param condition the condition to match
|
|
105
|
+
* @param branch the branch to run when matched
|
|
106
|
+
* @returns this
|
|
107
|
+
*/
|
|
108
|
+
add(condition: boolean, branch: BranchFn): this;
|
|
109
|
+
/**
|
|
110
|
+
* Register a handler to be called when no branch matched
|
|
111
|
+
* - This will be called **instantly and ignores later matched branches**
|
|
112
|
+
* @param handler handle the exhausted case
|
|
113
|
+
* @returns this
|
|
114
|
+
*/
|
|
115
|
+
nomatch(handler: NoMatchFn): this;
|
|
116
|
+
/**
|
|
117
|
+
* Execute all matched branches in order of addition
|
|
118
|
+
* @param args arguments to pass to the matched branch or nomatch handler
|
|
119
|
+
*/
|
|
120
|
+
run(...args: Parameters<BranchFn>): void;
|
|
121
|
+
}
|
|
122
|
+
//#endregion
|
|
123
|
+
//#region src/index.d.ts
|
|
124
|
+
/**
|
|
125
|
+
* ## Steps to use
|
|
126
|
+
* 1. const a = deferedBranch()
|
|
127
|
+
* 2. add branches by a.add(condition, branch)
|
|
128
|
+
* 3. (optional) add defered nomatch handler, or add and run at the same time by a.nomatch(handler)
|
|
129
|
+
* 4. ...run some other logic
|
|
130
|
+
* 5. run this branch by a.run();
|
|
131
|
+
*
|
|
132
|
+
* ## Type Annotation (optional)
|
|
133
|
+
* ```ts
|
|
134
|
+
* type BranchFn = (a: number) => void;
|
|
135
|
+
* type NoMatchFn = () => void;
|
|
136
|
+
* const a = deferedBranch<BranchFn, NoMatchFn>();
|
|
137
|
+
*
|
|
138
|
+
* // then we have restrictions:
|
|
139
|
+
* a.add(true, someFn satisfies BranchFn);
|
|
140
|
+
* a.nomatch(otherFn satisfies NoMatchFn);
|
|
141
|
+
*
|
|
142
|
+
* // some other logic
|
|
143
|
+
*
|
|
144
|
+
* a.run(...args); // input args are restricted to Parameters<BranchFn>
|
|
145
|
+
* ```
|
|
146
|
+
*
|
|
147
|
+
* __PKG_INFO__
|
|
148
|
+
*/
|
|
149
|
+
declare const deferedBranch: <BranchFn extends AnyFn = AnyFn, NoMatchFn extends AnyFn = AnyFn>() => DeferedBranch<BranchFn, NoMatchFn>;
|
|
150
|
+
/**
|
|
151
|
+
* ## Steps to use
|
|
152
|
+
* 1. const a = deferedBranchDynamic()
|
|
153
|
+
* 2. add branches by a.add(condition, branch)
|
|
154
|
+
* 3. add nomatch handler by a.nomatch(handler)
|
|
155
|
+
* 4. call a.predicate(...) to find the matched branch
|
|
156
|
+
* 5. ...run some other logic
|
|
157
|
+
* 6. run this branch by a.run();
|
|
158
|
+
*
|
|
159
|
+
* ## Type Annotation (optional)
|
|
160
|
+
* ```ts
|
|
161
|
+
* type BranchFn = (a: number) => void;
|
|
162
|
+
* type NoMatchFn = () => void;
|
|
163
|
+
* // optional, you can customize the condition function type
|
|
164
|
+
* type ConditionFn = Predicate<BranchFn>;
|
|
165
|
+
* const a = deferedBranch<BranchFn, NoMatchFn, ConditionFn>();
|
|
166
|
+
*
|
|
167
|
+
* // then we have restrictions:
|
|
168
|
+
* a.add(someFn satisfies ConditionFn, secondFn satisfies BranchFn);
|
|
169
|
+
* a.nomatch(thirdFn satisfies NoMatchFn);
|
|
170
|
+
*
|
|
171
|
+
* // some other logic
|
|
172
|
+
*
|
|
173
|
+
* a.predicate(...args);
|
|
174
|
+
*
|
|
175
|
+
* // some other logic
|
|
176
|
+
*
|
|
177
|
+
* a.run(...args); // input args are restricted to Parameters<BranchFn>
|
|
178
|
+
* ```
|
|
179
|
+
*
|
|
180
|
+
* __PKG_INFO__
|
|
181
|
+
*/
|
|
182
|
+
declare const deferedBranchDynamic: <BranchFn extends AnyFn = AnyFn, NoMatchFn extends AnyFn = AnyFn, ConditionFn extends AnyFn = Predicate<BranchFn>>() => DeferedBranchDynamic<BranchFn, NoMatchFn, ConditionFn>;
|
|
183
|
+
/**
|
|
184
|
+
* ## Steps to use
|
|
185
|
+
* 1. const a = deferedBranchAll()
|
|
186
|
+
* 2. add branches by a.add(condition, branch)
|
|
187
|
+
* 3. (optional) add defered nomatch handler, or add and run at the same time by a.nomatch(handler)
|
|
188
|
+
* 4. ...run some other logic
|
|
189
|
+
* 5. run all matched branches by a.run();
|
|
190
|
+
*
|
|
191
|
+
* ## Type Annotation (optional)
|
|
192
|
+
* ```ts
|
|
193
|
+
* type BranchFn = (a: number) => void;
|
|
194
|
+
* type NoMatchFn = () => void;
|
|
195
|
+
* const a = deferedBranchAll<BranchFn, NoMatchFn>();
|
|
196
|
+
*
|
|
197
|
+
* // then we have restrictions:
|
|
198
|
+
* a.add(true, someFn satisfies BranchFn);
|
|
199
|
+
* a.nomatch(otherFn satisfies NoMatchFn);
|
|
200
|
+
*
|
|
201
|
+
* // some other logic
|
|
202
|
+
*
|
|
203
|
+
* a.run(...args); // input args are restricted to Parameters<BranchFn>
|
|
204
|
+
* ```
|
|
205
|
+
*
|
|
206
|
+
* __PKG_INFO__
|
|
207
|
+
*/
|
|
208
|
+
declare const deferedBranchAll: <BranchFn extends AnyFn = AnyFn, NoMatchFn extends AnyFn = AnyFn>() => DeferedBranchAll<BranchFn, NoMatchFn>;
|
|
209
|
+
/**
|
|
210
|
+
* ## Steps to use
|
|
211
|
+
* 1. const a = deferedBranchAllDynamic()
|
|
212
|
+
* 2. add branches by a.add(condition, branch)
|
|
213
|
+
* 3. add nomatch handler by a.nomatch(handler)
|
|
214
|
+
* 4. call a.predicate(...) to find all matched branches
|
|
215
|
+
* 5. ...run some other logic
|
|
216
|
+
* 6. run all matched branches by a.run();
|
|
217
|
+
*
|
|
218
|
+
* ## Type Annotation (optional)
|
|
219
|
+
* ```ts
|
|
220
|
+
* type BranchFn = (a: number) => void;
|
|
221
|
+
* type NoMatchFn = () => void;
|
|
222
|
+
* // optional, you can customize the condition function type
|
|
223
|
+
* type ConditionFn = Predicate<BranchFn>;
|
|
224
|
+
* const a = deferedBranchAllDynamic<BranchFn, NoMatchFn, ConditionFn>();
|
|
225
|
+
*
|
|
226
|
+
* // then we have restrictions:
|
|
227
|
+
* a.add(someFn satisfies ConditionFn, secondFn satisfies BranchFn);
|
|
228
|
+
* a.nomatch(thirdFn satisfies NoMatchFn);
|
|
229
|
+
*
|
|
230
|
+
* // some other logic
|
|
231
|
+
*
|
|
232
|
+
* a.predicate(...args);
|
|
233
|
+
*
|
|
234
|
+
* // some other logic
|
|
235
|
+
*
|
|
236
|
+
* a.run(...args); // input args are restricted to Parameters<BranchFn>
|
|
237
|
+
* ```
|
|
238
|
+
*
|
|
239
|
+
* __PKG_INFO__
|
|
240
|
+
*/
|
|
241
|
+
declare const deferedBranchAllDynamic: <BranchFn extends AnyFn = AnyFn, NoMatchFn extends AnyFn = AnyFn, ConditionFn extends AnyFn = Predicate<BranchFn>>() => DeferedBranchAllDynamic<BranchFn, NoMatchFn, ConditionFn>;
|
|
242
|
+
//#endregion
|
|
243
|
+
export { deferedBranch, deferedBranchAll, deferedBranchAllDynamic, deferedBranchDynamic };
|
|
244
|
+
//# sourceMappingURL=index.d.cts.map
|
|
@@ -1,120 +1,126 @@
|
|
|
1
|
+
//#region src/global.d.ts
|
|
1
2
|
type AnyFn = (...args: any[]) => any;
|
|
2
3
|
type Predicate<Fn extends AnyFn> = (...args: Parameters<Fn>) => boolean;
|
|
3
|
-
|
|
4
|
+
//#endregion
|
|
5
|
+
//#region src/core/default.d.ts
|
|
4
6
|
declare class DeferedBranch<BranchFn extends AnyFn, NoMatchFn extends AnyFn> {
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
7
|
+
private _branch;
|
|
8
|
+
/**
|
|
9
|
+
* Add a new entry
|
|
10
|
+
* - **First truthy first served**.
|
|
11
|
+
* @param condition the condition to match
|
|
12
|
+
* @param branch the branch to run when matched
|
|
13
|
+
* @returns this
|
|
14
|
+
*/
|
|
15
|
+
add(condition: boolean, branch: BranchFn): this;
|
|
16
|
+
/**
|
|
17
|
+
* Register a handler to be called when no branch matched
|
|
18
|
+
* - This will be called **instantly and ignores later matched branches**
|
|
19
|
+
* @param handler handle the exhausted case
|
|
20
|
+
* @returns this
|
|
21
|
+
*/
|
|
22
|
+
nomatch(handler: NoMatchFn): this;
|
|
23
|
+
/**
|
|
24
|
+
* Execute the first matched branch in order of addition
|
|
25
|
+
* @param args arguments to pass to the matched branch or nomatch handler
|
|
26
|
+
* @returns the return value of the matched branch or void
|
|
27
|
+
*/
|
|
28
|
+
run(...args: Parameters<BranchFn>): ReturnType<BranchFn> | void;
|
|
27
29
|
}
|
|
28
|
-
|
|
30
|
+
//#endregion
|
|
31
|
+
//#region src/core/dynamic.d.ts
|
|
29
32
|
declare class DeferedBranchDynamic<BranchFn extends AnyFn, NoMatchFn extends AnyFn, ConditionFn extends AnyFn = Predicate<BranchFn>> {
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
33
|
+
/**
|
|
34
|
+
* & Compress conditions and branches into a single array since they are paired
|
|
35
|
+
*/
|
|
36
|
+
private _condranches;
|
|
37
|
+
private _branch;
|
|
38
|
+
private _nomatch;
|
|
39
|
+
/**
|
|
40
|
+
* Add a new entry
|
|
41
|
+
* - **First truthy first served**.
|
|
42
|
+
* @param condition the condition to match
|
|
43
|
+
* @param branch the branch to run when matched
|
|
44
|
+
* @returns this
|
|
45
|
+
*/
|
|
46
|
+
add(condition: ConditionFn, branch: BranchFn): this;
|
|
47
|
+
/**
|
|
48
|
+
* When calling `this.predicate` and no branch matched, run this handler
|
|
49
|
+
* @param handler handle the exhausted case
|
|
50
|
+
* @returns this
|
|
51
|
+
*/
|
|
52
|
+
nomatch(handler: AnyFn): this;
|
|
53
|
+
/**
|
|
54
|
+
* Run the condition to find the first matched branch
|
|
55
|
+
*/
|
|
56
|
+
predicate(...args: Parameters<ConditionFn>): void;
|
|
57
|
+
/**
|
|
58
|
+
* Execute the first matched branch in order of addition
|
|
59
|
+
* @param args arguments to pass to the matched branch
|
|
60
|
+
* @returns the return value of the matched branch or void
|
|
61
|
+
*/
|
|
62
|
+
run(...args: Parameters<BranchFn>): ReturnType<BranchFn> | undefined;
|
|
60
63
|
}
|
|
61
|
-
|
|
64
|
+
//#endregion
|
|
65
|
+
//#region src/core/all-dynamic.d.ts
|
|
62
66
|
declare class DeferedBranchAllDynamic<BranchFn extends AnyFn, NoMatchFn extends AnyFn, ConditionFn extends AnyFn = Predicate<BranchFn>> {
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
67
|
+
/**
|
|
68
|
+
* & Compress conditions and branches into a single array since they are paired
|
|
69
|
+
*/
|
|
70
|
+
private _condranches;
|
|
71
|
+
private _branches;
|
|
72
|
+
private _nomatch;
|
|
73
|
+
/**
|
|
74
|
+
* Add a new entry
|
|
75
|
+
* - **All truthy branches will be executed**.
|
|
76
|
+
* @param condition the condition to match
|
|
77
|
+
* @param branch the branch to run when matched
|
|
78
|
+
* @returns this
|
|
79
|
+
*/
|
|
80
|
+
add(condition: ConditionFn, branch: BranchFn): this;
|
|
81
|
+
/**
|
|
82
|
+
* When calling `this.predicate` and no branch matched, run this handler
|
|
83
|
+
* @param handler handle the exhausted case
|
|
84
|
+
* @returns this
|
|
85
|
+
*/
|
|
86
|
+
nomatch(handler: AnyFn): this;
|
|
87
|
+
/**
|
|
88
|
+
* Run the conditions to find all matched branches
|
|
89
|
+
*/
|
|
90
|
+
predicate(...args: Parameters<ConditionFn>): void;
|
|
91
|
+
/**
|
|
92
|
+
* Execute all matched branches in order of addition
|
|
93
|
+
* @param args arguments to pass to the matched branches
|
|
94
|
+
*/
|
|
95
|
+
run(...args: Parameters<BranchFn>): ReturnType<BranchFn> | undefined;
|
|
92
96
|
}
|
|
93
|
-
|
|
97
|
+
//#endregion
|
|
98
|
+
//#region src/core/all.d.ts
|
|
94
99
|
declare class DeferedBranchAll<BranchFn extends AnyFn, NoMatchFn extends AnyFn> {
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
100
|
+
private _branches;
|
|
101
|
+
/**
|
|
102
|
+
* Add a new entry
|
|
103
|
+
* - **All truthy branches will be executed**.
|
|
104
|
+
* @param condition the condition to match
|
|
105
|
+
* @param branch the branch to run when matched
|
|
106
|
+
* @returns this
|
|
107
|
+
*/
|
|
108
|
+
add(condition: boolean, branch: BranchFn): this;
|
|
109
|
+
/**
|
|
110
|
+
* Register a handler to be called when no branch matched
|
|
111
|
+
* - This will be called **instantly and ignores later matched branches**
|
|
112
|
+
* @param handler handle the exhausted case
|
|
113
|
+
* @returns this
|
|
114
|
+
*/
|
|
115
|
+
nomatch(handler: NoMatchFn): this;
|
|
116
|
+
/**
|
|
117
|
+
* Execute all matched branches in order of addition
|
|
118
|
+
* @param args arguments to pass to the matched branch or nomatch handler
|
|
119
|
+
*/
|
|
120
|
+
run(...args: Parameters<BranchFn>): void;
|
|
116
121
|
}
|
|
117
|
-
|
|
122
|
+
//#endregion
|
|
123
|
+
//#region src/index.d.ts
|
|
118
124
|
/**
|
|
119
125
|
* ## Steps to use
|
|
120
126
|
* 1. const a = deferedBranch()
|
|
@@ -141,9 +147,9 @@ declare class DeferedBranchAll<BranchFn extends AnyFn, NoMatchFn extends AnyFn>
|
|
|
141
147
|
* ## About
|
|
142
148
|
* @package DeferedBranch
|
|
143
149
|
* @author Kasukabe Tsumugi <futami16237@gmail.com>
|
|
144
|
-
* @version 2.2.
|
|
150
|
+
* @version 2.2.10 (Last Update: 2026.10.12 10:07:47.649)
|
|
145
151
|
* @license MIT
|
|
146
|
-
* @link git+https://github.com/baendlorel/
|
|
152
|
+
* @link git+https://github.com/baendlorel/tsumugi-lib.git
|
|
147
153
|
* @link https://baendlorel.github.io/ Welcome to my site!
|
|
148
154
|
* @description Sometimes we need to judge something with multiple cases, but the main job should be done later. So we have Defered Branch!
|
|
149
155
|
* @copyright Copyright (c) 2026 Kasukabe Tsumugi. All rights reserved.
|
|
@@ -182,9 +188,9 @@ declare const deferedBranch: <BranchFn extends AnyFn = AnyFn, NoMatchFn extends
|
|
|
182
188
|
* ## About
|
|
183
189
|
* @package DeferedBranch
|
|
184
190
|
* @author Kasukabe Tsumugi <futami16237@gmail.com>
|
|
185
|
-
* @version 2.2.
|
|
191
|
+
* @version 2.2.10 (Last Update: 2026.10.12 10:07:47.649)
|
|
186
192
|
* @license MIT
|
|
187
|
-
* @link git+https://github.com/baendlorel/
|
|
193
|
+
* @link git+https://github.com/baendlorel/tsumugi-lib.git
|
|
188
194
|
* @link https://baendlorel.github.io/ Welcome to my site!
|
|
189
195
|
* @description Sometimes we need to judge something with multiple cases, but the main job should be done later. So we have Defered Branch!
|
|
190
196
|
* @copyright Copyright (c) 2026 Kasukabe Tsumugi. All rights reserved.
|
|
@@ -216,9 +222,9 @@ declare const deferedBranchDynamic: <BranchFn extends AnyFn = AnyFn, NoMatchFn e
|
|
|
216
222
|
* ## About
|
|
217
223
|
* @package DeferedBranch
|
|
218
224
|
* @author Kasukabe Tsumugi <futami16237@gmail.com>
|
|
219
|
-
* @version 2.2.
|
|
225
|
+
* @version 2.2.10 (Last Update: 2026.10.12 10:07:47.649)
|
|
220
226
|
* @license MIT
|
|
221
|
-
* @link git+https://github.com/baendlorel/
|
|
227
|
+
* @link git+https://github.com/baendlorel/tsumugi-lib.git
|
|
222
228
|
* @link https://baendlorel.github.io/ Welcome to my site!
|
|
223
229
|
* @description Sometimes we need to judge something with multiple cases, but the main job should be done later. So we have Defered Branch!
|
|
224
230
|
* @copyright Copyright (c) 2026 Kasukabe Tsumugi. All rights reserved.
|
|
@@ -257,13 +263,14 @@ declare const deferedBranchAll: <BranchFn extends AnyFn = AnyFn, NoMatchFn exten
|
|
|
257
263
|
* ## About
|
|
258
264
|
* @package DeferedBranch
|
|
259
265
|
* @author Kasukabe Tsumugi <futami16237@gmail.com>
|
|
260
|
-
* @version 2.2.
|
|
266
|
+
* @version 2.2.10 (Last Update: 2026.10.12 10:07:47.649)
|
|
261
267
|
* @license MIT
|
|
262
|
-
* @link git+https://github.com/baendlorel/
|
|
268
|
+
* @link git+https://github.com/baendlorel/tsumugi-lib.git
|
|
263
269
|
* @link https://baendlorel.github.io/ Welcome to my site!
|
|
264
270
|
* @description Sometimes we need to judge something with multiple cases, but the main job should be done later. So we have Defered Branch!
|
|
265
271
|
* @copyright Copyright (c) 2026 Kasukabe Tsumugi. All rights reserved.
|
|
266
272
|
*/
|
|
267
273
|
declare const deferedBranchAllDynamic: <BranchFn extends AnyFn = AnyFn, NoMatchFn extends AnyFn = AnyFn, ConditionFn extends AnyFn = Predicate<BranchFn>>() => DeferedBranchAllDynamic<BranchFn, NoMatchFn, ConditionFn>;
|
|
268
|
-
|
|
274
|
+
//#endregion
|
|
269
275
|
export { deferedBranch, deferedBranchAll, deferedBranchAllDynamic, deferedBranchDynamic };
|
|
276
|
+
//# sourceMappingURL=index.d.mts.map
|
package/dist/index.mjs
CHANGED
|
@@ -1,93 +1,150 @@
|
|
|
1
|
-
class t {
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
1
|
+
var e=class{_branch=null;add(e,t){if(typeof t!=`function`)throw TypeError(`DeferBranch: branch must be a function`);return e&&!this._branch&&(this._branch=t),this}nomatch(e){if(typeof e!=`function`)throw TypeError(`DeferBranch: branch must be a function`);return this._branch||e(),this}run(...e){if(this._branch){let t=this._branch.apply(null,e);return this._branch=null,t}}},t=class{_condranches=[];_branch=null;_nomatch=null;add(e,t){if(typeof e!=`function`)throw TypeError(`DeferBranch: condition must be a function`);if(typeof t!=`function`)throw TypeError(`DeferBranch: branch must be a function`);return this._condranches.push(e,t),this}nomatch(e){if(typeof e!=`function`)throw TypeError(`DeferBranch: handler must be a function`);return this._nomatch=e,this}predicate(...e){let t=this._condranches.length;for(let e=0;e<t;e+=2)if(this._condranches[e]()){this._branch=this._condranches[e+1];return}this._nomatch&&this._nomatch.apply(this,e)}run(...e){if(this._branch){let t=this._branch.apply(null,e);return this._branch=null,t}}},n=class{_condranches=[];_branches=[];_nomatch=null;add(e,t){if(typeof e!=`function`)throw TypeError(`DeferBranch: condition must be a function`);if(typeof t!=`function`)throw TypeError(`DeferBranch: branch must be a function`);return this._condranches.push(e,t),this}nomatch(e){if(typeof e!=`function`)throw TypeError(`DeferBranch: handler must be a function`);return this._nomatch=e,this}predicate(...e){let t=this._condranches.length;for(let e=0;e<t;e+=2)this._condranches[e]()&&this._branches.push(this._condranches[e+1]);this._nomatch&&this._branches.length===0&&this._nomatch.apply(this,e)}run(...e){let t=this._branches.length;if(t!==0){for(let n=0;n<t;n++)this._branches[n].apply(null,e);this._branches.length=0}}},r=class{_branches=[];add(e,t){if(typeof t!=`function`)throw TypeError(`DeferBranch: branch must be a function`);return e&&this._branches.push(t),this}nomatch(e){if(typeof e!=`function`)throw TypeError(`DeferBranch: branch must be a function`);return this._branches.length===0&&e(),this}run(...e){let t=this._branches.length;if(t!==0){for(let n=0;n<t;n++)this._branches[n].apply(null,e);this._branches.length=0}}};
|
|
2
|
+
/**
|
|
3
|
+
* ## Steps to use
|
|
4
|
+
* 1. const a = deferedBranch()
|
|
5
|
+
* 2. add branches by a.add(condition, branch)
|
|
6
|
+
* 3. (optional) add defered nomatch handler, or add and run at the same time by a.nomatch(handler)
|
|
7
|
+
* 4. ...run some other logic
|
|
8
|
+
* 5. run this branch by a.run();
|
|
9
|
+
*
|
|
10
|
+
* ## Type Annotation (optional)
|
|
11
|
+
* ```ts
|
|
12
|
+
* type BranchFn = (a: number) => void;
|
|
13
|
+
* type NoMatchFn = () => void;
|
|
14
|
+
* const a = deferedBranch<BranchFn, NoMatchFn>();
|
|
15
|
+
*
|
|
16
|
+
* // then we have restrictions:
|
|
17
|
+
* a.add(true, someFn satisfies BranchFn);
|
|
18
|
+
* a.nomatch(otherFn satisfies NoMatchFn);
|
|
19
|
+
*
|
|
20
|
+
* // some other logic
|
|
21
|
+
*
|
|
22
|
+
* a.run(...args); // input args are restricted to Parameters<BranchFn>
|
|
23
|
+
* ```
|
|
24
|
+
*
|
|
25
|
+
* ## About
|
|
26
|
+
* @package DeferedBranch
|
|
27
|
+
* @author Kasukabe Tsumugi <futami16237@gmail.com>
|
|
28
|
+
* @version 2.2.10 (Last Update: 2026.10.12 10:07:47.649)
|
|
29
|
+
* @license MIT
|
|
30
|
+
* @link git+https://github.com/baendlorel/tsumugi-lib.git
|
|
31
|
+
* @link https://baendlorel.github.io/ Welcome to my site!
|
|
32
|
+
* @description Sometimes we need to judge something with multiple cases, but the main job should be done later. So we have Defered Branch!
|
|
33
|
+
* @copyright Copyright (c) 2026 Kasukabe Tsumugi. All rights reserved.
|
|
34
|
+
*/
|
|
35
|
+
const i=()=>new e,a=()=>new t,o=()=>new r,s=()=>new n
|
|
36
|
+
/**
|
|
37
|
+
* ## Steps to use
|
|
38
|
+
* 1. const a = deferedBranchDynamic()
|
|
39
|
+
* 2. add branches by a.add(condition, branch)
|
|
40
|
+
* 3. add nomatch handler by a.nomatch(handler)
|
|
41
|
+
* 4. call a.predicate(...) to find the matched branch
|
|
42
|
+
* 5. ...run some other logic
|
|
43
|
+
* 6. run this branch by a.run();
|
|
44
|
+
*
|
|
45
|
+
* ## Type Annotation (optional)
|
|
46
|
+
* ```ts
|
|
47
|
+
* type BranchFn = (a: number) => void;
|
|
48
|
+
* type NoMatchFn = () => void;
|
|
49
|
+
* // optional, you can customize the condition function type
|
|
50
|
+
* type ConditionFn = Predicate<BranchFn>;
|
|
51
|
+
* const a = deferedBranch<BranchFn, NoMatchFn, ConditionFn>();
|
|
52
|
+
*
|
|
53
|
+
* // then we have restrictions:
|
|
54
|
+
* a.add(someFn satisfies ConditionFn, secondFn satisfies BranchFn);
|
|
55
|
+
* a.nomatch(thirdFn satisfies NoMatchFn);
|
|
56
|
+
*
|
|
57
|
+
* // some other logic
|
|
58
|
+
*
|
|
59
|
+
* a.predicate(...args);
|
|
60
|
+
*
|
|
61
|
+
* // some other logic
|
|
62
|
+
*
|
|
63
|
+
* a.run(...args); // input args are restricted to Parameters<BranchFn>
|
|
64
|
+
* ```
|
|
65
|
+
*
|
|
66
|
+
* ## About
|
|
67
|
+
* @package DeferedBranch
|
|
68
|
+
* @author Kasukabe Tsumugi <futami16237@gmail.com>
|
|
69
|
+
* @version 2.2.10 (Last Update: 2026.10.12 10:07:47.649)
|
|
70
|
+
* @license MIT
|
|
71
|
+
* @link git+https://github.com/baendlorel/tsumugi-lib.git
|
|
72
|
+
* @link https://baendlorel.github.io/ Welcome to my site!
|
|
73
|
+
* @description Sometimes we need to judge something with multiple cases, but the main job should be done later. So we have Defered Branch!
|
|
74
|
+
* @copyright Copyright (c) 2026 Kasukabe Tsumugi. All rights reserved.
|
|
75
|
+
*/
|
|
76
|
+
/**
|
|
77
|
+
* ## Steps to use
|
|
78
|
+
* 1. const a = deferedBranchAll()
|
|
79
|
+
* 2. add branches by a.add(condition, branch)
|
|
80
|
+
* 3. (optional) add defered nomatch handler, or add and run at the same time by a.nomatch(handler)
|
|
81
|
+
* 4. ...run some other logic
|
|
82
|
+
* 5. run all matched branches by a.run();
|
|
83
|
+
*
|
|
84
|
+
* ## Type Annotation (optional)
|
|
85
|
+
* ```ts
|
|
86
|
+
* type BranchFn = (a: number) => void;
|
|
87
|
+
* type NoMatchFn = () => void;
|
|
88
|
+
* const a = deferedBranchAll<BranchFn, NoMatchFn>();
|
|
89
|
+
*
|
|
90
|
+
* // then we have restrictions:
|
|
91
|
+
* a.add(true, someFn satisfies BranchFn);
|
|
92
|
+
* a.nomatch(otherFn satisfies NoMatchFn);
|
|
93
|
+
*
|
|
94
|
+
* // some other logic
|
|
95
|
+
*
|
|
96
|
+
* a.run(...args); // input args are restricted to Parameters<BranchFn>
|
|
97
|
+
* ```
|
|
98
|
+
*
|
|
99
|
+
* ## About
|
|
100
|
+
* @package DeferedBranch
|
|
101
|
+
* @author Kasukabe Tsumugi <futami16237@gmail.com>
|
|
102
|
+
* @version 2.2.10 (Last Update: 2026.10.12 10:07:47.649)
|
|
103
|
+
* @license MIT
|
|
104
|
+
* @link git+https://github.com/baendlorel/tsumugi-lib.git
|
|
105
|
+
* @link https://baendlorel.github.io/ Welcome to my site!
|
|
106
|
+
* @description Sometimes we need to judge something with multiple cases, but the main job should be done later. So we have Defered Branch!
|
|
107
|
+
* @copyright Copyright (c) 2026 Kasukabe Tsumugi. All rights reserved.
|
|
108
|
+
*/
|
|
109
|
+
/**
|
|
110
|
+
* ## Steps to use
|
|
111
|
+
* 1. const a = deferedBranchAllDynamic()
|
|
112
|
+
* 2. add branches by a.add(condition, branch)
|
|
113
|
+
* 3. add nomatch handler by a.nomatch(handler)
|
|
114
|
+
* 4. call a.predicate(...) to find all matched branches
|
|
115
|
+
* 5. ...run some other logic
|
|
116
|
+
* 6. run all matched branches by a.run();
|
|
117
|
+
*
|
|
118
|
+
* ## Type Annotation (optional)
|
|
119
|
+
* ```ts
|
|
120
|
+
* type BranchFn = (a: number) => void;
|
|
121
|
+
* type NoMatchFn = () => void;
|
|
122
|
+
* // optional, you can customize the condition function type
|
|
123
|
+
* type ConditionFn = Predicate<BranchFn>;
|
|
124
|
+
* const a = deferedBranchAllDynamic<BranchFn, NoMatchFn, ConditionFn>();
|
|
125
|
+
*
|
|
126
|
+
* // then we have restrictions:
|
|
127
|
+
* a.add(someFn satisfies ConditionFn, secondFn satisfies BranchFn);
|
|
128
|
+
* a.nomatch(thirdFn satisfies NoMatchFn);
|
|
129
|
+
*
|
|
130
|
+
* // some other logic
|
|
131
|
+
*
|
|
132
|
+
* a.predicate(...args);
|
|
133
|
+
*
|
|
134
|
+
* // some other logic
|
|
135
|
+
*
|
|
136
|
+
* a.run(...args); // input args are restricted to Parameters<BranchFn>
|
|
137
|
+
* ```
|
|
138
|
+
*
|
|
139
|
+
* ## About
|
|
140
|
+
* @package DeferedBranch
|
|
141
|
+
* @author Kasukabe Tsumugi <futami16237@gmail.com>
|
|
142
|
+
* @version 2.2.10 (Last Update: 2026.10.12 10:07:47.649)
|
|
143
|
+
* @license MIT
|
|
144
|
+
* @link git+https://github.com/baendlorel/tsumugi-lib.git
|
|
145
|
+
* @link https://baendlorel.github.io/ Welcome to my site!
|
|
146
|
+
* @description Sometimes we need to judge something with multiple cases, but the main job should be done later. So we have Defered Branch!
|
|
147
|
+
* @copyright Copyright (c) 2026 Kasukabe Tsumugi. All rights reserved.
|
|
148
|
+
*/
|
|
149
|
+
;export{i as deferedBranch,o as deferedBranchAll,s as deferedBranchAllDynamic,a as deferedBranchDynamic};
|
|
150
|
+
//# sourceMappingURL=index.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.mjs","names":["DeferedBranch","branch","condition","handler","args","result","DeferedBranchDynamic","len","i","DeferedBranchAllDynamic","DeferedBranchAll","deferedBranch","deferedBranchDynamic","deferedBranchAll","deferedBranchAllDynamic"],"sources":["../src/core/default.ts","../src/core/dynamic.ts","../src/core/all-dynamic.ts","../src/core/all.ts","../src/index.ts"],"sourcesContent":["import { AnyFn } from '@defered-branch/global.js';\n\nexport class DeferedBranch<BranchFn extends AnyFn, NoMatchFn extends AnyFn> {\n private _branch: BranchFn | null = null;\n\n /**\n * Add a new entry\n * - **First truthy first served**.\n * @param condition the condition to match\n * @param branch the branch to run when matched\n * @returns this\n */\n add(condition: boolean, branch: BranchFn): this {\n if (typeof branch !== 'function') {\n throw new TypeError('DeferBranch: branch must be a function');\n }\n\n // & Preventing later branches from overwriting earlier matched branch\n if (condition && !this._branch) {\n this._branch = branch;\n }\n return this;\n }\n\n /**\n * Register a handler to be called when no branch matched\n * - This will be called **instantly and ignores later matched branches**\n * @param handler handle the exhausted case\n * @returns this\n */\n nomatch(handler: NoMatchFn): this {\n if (typeof handler !== 'function') {\n throw new TypeError('DeferBranch: branch must be a function');\n }\n\n if (!this._branch) {\n handler();\n }\n\n return this;\n }\n\n /**\n * Execute the first matched branch in order of addition\n * @param args arguments to pass to the matched branch or nomatch handler\n * @returns the return value of the matched branch or void\n */\n run(...args: Parameters<BranchFn>): ReturnType<BranchFn> | void {\n if (this._branch) {\n const result = this._branch.apply(null, args);\n this._branch = null;\n return result;\n }\n }\n}\n","import { AnyFn, Predicate } from '@defered-branch/global.js';\n\nexport class DeferedBranchDynamic<\n BranchFn extends AnyFn,\n NoMatchFn extends AnyFn,\n ConditionFn extends AnyFn = Predicate<BranchFn>,\n> {\n /**\n * & Compress conditions and branches into a single array since they are paired\n */\n private _condranches: AnyFn[] = [];\n private _branch: BranchFn | null = null;\n private _nomatch: NoMatchFn | null = null;\n\n /**\n * Add a new entry\n * - **First truthy first served**.\n * @param condition the condition to match\n * @param branch the branch to run when matched\n * @returns this\n */\n add(condition: ConditionFn, branch: BranchFn): this {\n if (typeof condition !== 'function') {\n throw new TypeError('DeferBranch: condition must be a function');\n }\n if (typeof branch !== 'function') {\n throw new TypeError('DeferBranch: branch must be a function');\n }\n\n this._condranches.push(condition, branch);\n return this;\n }\n\n /**\n * When calling `this.predicate` and no branch matched, run this handler\n * @param handler handle the exhausted case\n * @returns this\n */\n nomatch(handler: AnyFn): this {\n if (typeof handler !== 'function') {\n throw new TypeError('DeferBranch: handler must be a function');\n }\n\n this._nomatch = handler as NoMatchFn;\n return this;\n }\n\n /**\n * Run the condition to find the first matched branch\n */\n predicate(...args: Parameters<ConditionFn>): void {\n const len = this._condranches.length;\n for (let i = 0; i < len; i += 2) {\n if (this._condranches[i]()) {\n this._branch = this._condranches[i + 1] as BranchFn;\n return;\n }\n }\n\n if (this._nomatch) {\n this._nomatch.apply(this, args);\n }\n }\n\n /**\n * Execute the first matched branch in order of addition\n * @param args arguments to pass to the matched branch\n * @returns the return value of the matched branch or void\n */\n run(...args: Parameters<BranchFn>): ReturnType<BranchFn> | undefined {\n if (this._branch) {\n const result = this._branch.apply(null, args);\n this._branch = null;\n return result;\n }\n }\n}\n","import { AnyFn, Predicate } from '@defered-branch/global.js';\n\nexport class DeferedBranchAllDynamic<\n BranchFn extends AnyFn,\n NoMatchFn extends AnyFn,\n ConditionFn extends AnyFn = Predicate<BranchFn>,\n> {\n /**\n * & Compress conditions and branches into a single array since they are paired\n */\n private _condranches: AnyFn[] = [];\n private _branches: BranchFn[] = [];\n private _nomatch: NoMatchFn | null = null;\n\n /**\n * Add a new entry\n * - **All truthy branches will be executed**.\n * @param condition the condition to match\n * @param branch the branch to run when matched\n * @returns this\n */\n add(condition: ConditionFn, branch: BranchFn): this {\n if (typeof condition !== 'function') {\n throw new TypeError('DeferBranch: condition must be a function');\n }\n if (typeof branch !== 'function') {\n throw new TypeError('DeferBranch: branch must be a function');\n }\n\n this._condranches.push(condition, branch);\n return this;\n }\n\n /**\n * When calling `this.predicate` and no branch matched, run this handler\n * @param handler handle the exhausted case\n * @returns this\n */\n nomatch(handler: AnyFn): this {\n if (typeof handler !== 'function') {\n throw new TypeError('DeferBranch: handler must be a function');\n }\n\n this._nomatch = handler as NoMatchFn;\n return this;\n }\n\n /**\n * Run the conditions to find all matched branches\n */\n predicate(...args: Parameters<ConditionFn>): void {\n const len = this._condranches.length;\n for (let i = 0; i < len; i += 2) {\n if (this._condranches[i]()) {\n this._branches.push(this._condranches[i + 1] as BranchFn);\n }\n }\n\n if (this._nomatch && this._branches.length === 0) {\n this._nomatch.apply(this, args);\n }\n }\n\n /**\n * Execute all matched branches in order of addition\n * @param args arguments to pass to the matched branches\n */\n run(...args: Parameters<BranchFn>): ReturnType<BranchFn> | undefined {\n const len = this._branches.length;\n if (len === 0) {\n return;\n }\n for (let i = 0; i < len; i++) {\n this._branches[i].apply(null, args);\n }\n this._branches.length = 0;\n }\n}\n","import { AnyFn } from '@defered-branch/global.js';\n\nexport class DeferedBranchAll<BranchFn extends AnyFn, NoMatchFn extends AnyFn> {\n private _branches: BranchFn[] = [];\n\n /**\n * Add a new entry\n * - **All truthy branches will be executed**.\n * @param condition the condition to match\n * @param branch the branch to run when matched\n * @returns this\n */\n add(condition: boolean, branch: BranchFn): this {\n if (typeof branch !== 'function') {\n throw new TypeError('DeferBranch: branch must be a function');\n }\n\n // & Preventing later branches from overwriting earlier matched branch\n if (condition) {\n this._branches.push(branch);\n }\n return this;\n }\n\n /**\n * Register a handler to be called when no branch matched\n * - This will be called **instantly and ignores later matched branches**\n * @param handler handle the exhausted case\n * @returns this\n */\n nomatch(handler: NoMatchFn): this {\n if (typeof handler !== 'function') {\n throw new TypeError('DeferBranch: branch must be a function');\n }\n\n if (this._branches.length === 0) {\n handler();\n }\n\n return this;\n }\n\n /**\n * Execute all matched branches in order of addition\n * @param args arguments to pass to the matched branch or nomatch handler\n */\n run(...args: Parameters<BranchFn>): void {\n const len = this._branches.length;\n if (len === 0) {\n return;\n }\n for (let i = 0; i < len; i++) {\n this._branches[i].apply(null, args);\n }\n this._branches.length = 0;\n }\n}\n","import { DeferedBranch } from './core/default.js';\nimport { DeferedBranchDynamic } from './core/dynamic.js';\nimport { DeferedBranchAllDynamic } from './core/all-dynamic.js';\nimport { DeferedBranchAll } from './core/all.js';\nimport { AnyFn, Predicate } from './global.js';\n\n/**\n * ## Steps to use\n * 1. const a = deferedBranch()\n * 2. add branches by a.add(condition, branch)\n * 3. (optional) add defered nomatch handler, or add and run at the same time by a.nomatch(handler)\n * 4. ...run some other logic\n * 5. run this branch by a.run();\n *\n * ## Type Annotation (optional)\n * ```ts\n * type BranchFn = (a: number) => void;\n * type NoMatchFn = () => void;\n * const a = deferedBranch<BranchFn, NoMatchFn>();\n *\n * // then we have restrictions:\n * a.add(true, someFn satisfies BranchFn);\n * a.nomatch(otherFn satisfies NoMatchFn);\n *\n * // some other logic\n *\n * a.run(...args); // input args are restricted to Parameters<BranchFn>\n * ```\n *\n * __PKG_INFO__\n */\nexport const deferedBranch = <BranchFn extends AnyFn = AnyFn, NoMatchFn extends AnyFn = AnyFn>() =>\n new DeferedBranch<BranchFn, NoMatchFn>();\n\n/**\n * ## Steps to use\n * 1. const a = deferedBranchDynamic()\n * 2. add branches by a.add(condition, branch)\n * 3. add nomatch handler by a.nomatch(handler)\n * 4. call a.predicate(...) to find the matched branch\n * 5. ...run some other logic\n * 6. run this branch by a.run();\n *\n * ## Type Annotation (optional)\n * ```ts\n * type BranchFn = (a: number) => void;\n * type NoMatchFn = () => void;\n * // optional, you can customize the condition function type\n * type ConditionFn = Predicate<BranchFn>;\n * const a = deferedBranch<BranchFn, NoMatchFn, ConditionFn>();\n *\n * // then we have restrictions:\n * a.add(someFn satisfies ConditionFn, secondFn satisfies BranchFn);\n * a.nomatch(thirdFn satisfies NoMatchFn);\n *\n * // some other logic\n *\n * a.predicate(...args);\n *\n * // some other logic\n *\n * a.run(...args); // input args are restricted to Parameters<BranchFn>\n * ```\n *\n * __PKG_INFO__\n */\nexport const deferedBranchDynamic = <\n BranchFn extends AnyFn = AnyFn,\n NoMatchFn extends AnyFn = AnyFn,\n ConditionFn extends AnyFn = Predicate<BranchFn>,\n>() => new DeferedBranchDynamic<BranchFn, NoMatchFn, ConditionFn>();\n\n/**\n * ## Steps to use\n * 1. const a = deferedBranchAll()\n * 2. add branches by a.add(condition, branch)\n * 3. (optional) add defered nomatch handler, or add and run at the same time by a.nomatch(handler)\n * 4. ...run some other logic\n * 5. run all matched branches by a.run();\n *\n * ## Type Annotation (optional)\n * ```ts\n * type BranchFn = (a: number) => void;\n * type NoMatchFn = () => void;\n * const a = deferedBranchAll<BranchFn, NoMatchFn>();\n *\n * // then we have restrictions:\n * a.add(true, someFn satisfies BranchFn);\n * a.nomatch(otherFn satisfies NoMatchFn);\n *\n * // some other logic\n *\n * a.run(...args); // input args are restricted to Parameters<BranchFn>\n * ```\n *\n * __PKG_INFO__\n */\nexport const deferedBranchAll = <BranchFn extends AnyFn = AnyFn, NoMatchFn extends AnyFn = AnyFn>() =>\n new DeferedBranchAll<BranchFn, NoMatchFn>();\n\n/**\n * ## Steps to use\n * 1. const a = deferedBranchAllDynamic()\n * 2. add branches by a.add(condition, branch)\n * 3. add nomatch handler by a.nomatch(handler)\n * 4. call a.predicate(...) to find all matched branches\n * 5. ...run some other logic\n * 6. run all matched branches by a.run();\n *\n * ## Type Annotation (optional)\n * ```ts\n * type BranchFn = (a: number) => void;\n * type NoMatchFn = () => void;\n * // optional, you can customize the condition function type\n * type ConditionFn = Predicate<BranchFn>;\n * const a = deferedBranchAllDynamic<BranchFn, NoMatchFn, ConditionFn>();\n *\n * // then we have restrictions:\n * a.add(someFn satisfies ConditionFn, secondFn satisfies BranchFn);\n * a.nomatch(thirdFn satisfies NoMatchFn);\n *\n * // some other logic\n *\n * a.predicate(...args);\n *\n * // some other logic\n *\n * a.run(...args); // input args are restricted to Parameters<BranchFn>\n * ```\n *\n * __PKG_INFO__\n */\nexport const deferedBranchAllDynamic = <\n BranchFn extends AnyFn = AnyFn,\n NoMatchFn extends AnyFn = AnyFn,\n ConditionFn extends AnyFn = Predicate<BranchFn>,\n>() => new DeferedBranchAllDynamic<BranchFn, NoMatchFn, ConditionFn>();\n"],"mappings":"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"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "defered-branch",
|
|
3
|
-
"version": "2.2.
|
|
3
|
+
"version": "2.2.10",
|
|
4
4
|
"author": {
|
|
5
5
|
"name": "Kasukabe Tsumugi",
|
|
6
6
|
"email": "futami16237@gmail.com"
|
|
@@ -19,10 +19,10 @@
|
|
|
19
19
|
"files": [
|
|
20
20
|
"dist"
|
|
21
21
|
],
|
|
22
|
-
"homepage": "https://github.com/baendlorel/
|
|
22
|
+
"homepage": "https://github.com/baendlorel/tsumugi-lib/tree/main/packages/defered-branch#readme",
|
|
23
23
|
"repository": {
|
|
24
24
|
"type": "git",
|
|
25
|
-
"url": "git+https://github.com/baendlorel/
|
|
25
|
+
"url": "git+https://github.com/baendlorel/tsumugi-lib.git",
|
|
26
26
|
"directory": "packages/defered-branch"
|
|
27
27
|
},
|
|
28
28
|
"keywords": [
|
|
@@ -33,6 +33,6 @@
|
|
|
33
33
|
],
|
|
34
34
|
"license": "MIT",
|
|
35
35
|
"bugs": {
|
|
36
|
-
"url": "https://github.com/baendlorel/
|
|
36
|
+
"url": "https://github.com/baendlorel/tsumugi-lib/issues"
|
|
37
37
|
}
|
|
38
|
-
}
|
|
38
|
+
}
|