serial-task 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 +3 -2
- package/dist/index.d.ts +56 -74
- package/dist/index.mjs +1 -1
- package/package.json +4 -24
package/README.md
CHANGED
|
@@ -21,6 +21,7 @@ pnpm add serial-task
|
|
|
21
21
|
## 🎯 Quick Start
|
|
22
22
|
|
|
23
23
|
> Note: For async functions(tasks/resultWrapper/conditions), use `createSerialTaskAsync` instead.
|
|
24
|
+
> Note: Relies on native `Promise.try` for async tasks, which is currently only supported in Node.js 20+ and modern browsers. For older environments, consider using a polyfill or transpiler that supports this feature.
|
|
24
25
|
|
|
25
26
|
```typescript
|
|
26
27
|
import { createSerialTask } from 'serial-task';
|
|
@@ -62,8 +63,7 @@ Creates a sync/async serial task function.
|
|
|
62
63
|
- **skipCondition?**: `function` - Function that determines when to skip a task (default: `() => false`)
|
|
63
64
|
- **resultWrapper?**: `function` - Function that transforms input between tasks, default(means the first task gets original args, subsequent tasks get the last return value):
|
|
64
65
|
```ts
|
|
65
|
-
(_task: Fn, index: number, _tasks: Fn[], args: unknown[], lastReturn: unknown) =>
|
|
66
|
-
index === 0 ? args : [lastReturn];
|
|
66
|
+
(_task: Fn, index: number, _tasks: Fn[], args: unknown[], lastReturn: unknown) => (index === 0 ? args : [lastReturn]);
|
|
67
67
|
```
|
|
68
68
|
|
|
69
69
|
#### Returns
|
|
@@ -312,6 +312,7 @@ const dynamicTask = createSerialTask({
|
|
|
312
312
|
## 🔄 Async Support
|
|
313
313
|
|
|
314
314
|
For async functions, use `createSerialTaskAsync`:
|
|
315
|
+
`createSerialTaskAsync` uses native `Promise.try`.
|
|
315
316
|
|
|
316
317
|
```typescript
|
|
317
318
|
import { createSerialTaskAsync } from 'serial-task';
|
package/dist/index.d.ts
CHANGED
|
@@ -1,55 +1,3 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* ## Usage
|
|
3
|
-
* Use this when you have async functions in tasks, conditions or result wrapper
|
|
4
|
-
*
|
|
5
|
-
* Creates an async serial task function that executes a series of functions in order
|
|
6
|
-
* - all given functions(`options.tasks`) will be called in order
|
|
7
|
-
* - generated task function will have the same length as the first task function
|
|
8
|
-
* - you can appoint generated task function's name by `options.name`
|
|
9
|
-
* - **Strongly Recommended**: all task functions have same input type and output type
|
|
10
|
-
* - returned function.length will be the same as the first task function's length
|
|
11
|
-
* @param opts Options for creating a serial task, details in `SerialTaskOptions`
|
|
12
|
-
* @returns a funtcion that executes the tasks in order, returns `TaskReturn<OriginalReturn>`
|
|
13
|
-
*
|
|
14
|
-
* ## About
|
|
15
|
-
* @package SerialTask
|
|
16
|
-
* @author Kasukabe Tsumugi <futami16237@gmail.com>
|
|
17
|
-
* @version 1.0.0 (Last Update: 2025.08.22 17:05:02.346)
|
|
18
|
-
* @license MIT
|
|
19
|
-
* @link https://github.com/baendlorel/serial-task
|
|
20
|
-
* @description Put a list of functions in and get a composed task function. Similar to functional programming's compose (function composition), but with more fine-grained and precise control, and the generated task incurs almost no runtime overhead. Supports both synchronous and asynchronous functions.
|
|
21
|
-
* @copyright Copyright (c) 2025 Kasukabe Tsumugi. All rights reserved.
|
|
22
|
-
*/
|
|
23
|
-
declare function createSerialTaskAsync<F extends Fn>(opts: SerialTaskOptions<F>): TaskifyAsync<F>;
|
|
24
|
-
|
|
25
|
-
/**
|
|
26
|
-
* ## Usage
|
|
27
|
-
* **DO NOT Use** this when you have **async functions** in tasks, conditions or result wrapper
|
|
28
|
-
*
|
|
29
|
-
* Creates a serial task function that executes a series of functions in order.
|
|
30
|
-
* - all given functions(`options.tasks`) will be called in order
|
|
31
|
-
* - the returned value will be `options.resultWrapper`ed then passed to the next task
|
|
32
|
-
* - generated task function will have the same length as the first task function
|
|
33
|
-
* - you can appoint generated task function's name by `options.name`
|
|
34
|
-
* - **Strongly Recommended**: all task functions have same input type and output type
|
|
35
|
-
* - returned function.length will be the same as the first task function's length
|
|
36
|
-
* @param opts Options for creating a serial task, details in `SerialTaskOptions`
|
|
37
|
-
* @returns a funtcion that executes the tasks in order, returns `TaskReturn<OriginalReturn>`
|
|
38
|
-
*
|
|
39
|
-
* ## About
|
|
40
|
-
* @package SerialTask
|
|
41
|
-
* @author Kasukabe Tsumugi <futami16237@gmail.com>
|
|
42
|
-
* @version 1.0.0 (Last Update: 2025.08.22 17:05:02.346)
|
|
43
|
-
* @license MIT
|
|
44
|
-
* @link https://github.com/baendlorel/serial-task
|
|
45
|
-
* @description Put a list of functions in and get a composed task function. Similar to functional programming's compose (function composition), but with more fine-grained and precise control, and the generated task incurs almost no runtime overhead. Supports both synchronous and asynchronous functions.
|
|
46
|
-
* @copyright Copyright (c) 2025 Kasukabe Tsumugi. All rights reserved.
|
|
47
|
-
*/
|
|
48
|
-
declare function createSerialTask<F extends Fn>(opts: SerialTaskOptions<F>): Taskify<F>;
|
|
49
|
-
|
|
50
|
-
export { createSerialTask, createSerialTaskAsync };
|
|
51
|
-
|
|
52
|
-
// # from: src/global.d.ts
|
|
53
1
|
/* eslint-disable @typescript-eslint/no-explicit-any */
|
|
54
2
|
type Fn = (...args: any[]) => any;
|
|
55
3
|
|
|
@@ -134,13 +82,7 @@ interface SerialTaskOptions<F extends Fn> {
|
|
|
134
82
|
* }
|
|
135
83
|
* ```
|
|
136
84
|
*/
|
|
137
|
-
resultWrapper?: (
|
|
138
|
-
task: F,
|
|
139
|
-
index: number,
|
|
140
|
-
tasks: F[],
|
|
141
|
-
args: Parameters<F>,
|
|
142
|
-
lastReturn: ReturnType<F>
|
|
143
|
-
) => unknown[];
|
|
85
|
+
resultWrapper?: (task: F, index: number, tasks: F[], args: Parameters<F>, lastReturn: ReturnType<F>) => unknown[];
|
|
144
86
|
|
|
145
87
|
/**
|
|
146
88
|
* Break the loop and return the last result immediately when this function returns `true`
|
|
@@ -153,13 +95,7 @@ interface SerialTaskOptions<F extends Fn> {
|
|
|
153
95
|
* @param args input value of the whole serial task
|
|
154
96
|
* @param lastReturn returned value of the last task function
|
|
155
97
|
*/
|
|
156
|
-
breakCondition?: (
|
|
157
|
-
task: F,
|
|
158
|
-
index: number,
|
|
159
|
-
tasks: F[],
|
|
160
|
-
args: Parameters<F>,
|
|
161
|
-
lastReturn: ReturnType<F>
|
|
162
|
-
) => boolean;
|
|
98
|
+
breakCondition?: (task: F, index: number, tasks: F[], args: Parameters<F>, lastReturn: ReturnType<F>) => boolean;
|
|
163
99
|
|
|
164
100
|
/**
|
|
165
101
|
* Give `true` to skip this task item
|
|
@@ -172,13 +108,59 @@ interface SerialTaskOptions<F extends Fn> {
|
|
|
172
108
|
* @param args input value of the whole serial task
|
|
173
109
|
* @param lastReturn returned value of the last task function
|
|
174
110
|
*/
|
|
175
|
-
skipCondition?: (
|
|
176
|
-
task: F,
|
|
177
|
-
index: number,
|
|
178
|
-
tasks: F[],
|
|
179
|
-
args: Parameters<F>,
|
|
180
|
-
lastReturn: ReturnType<F>
|
|
181
|
-
) => boolean;
|
|
111
|
+
skipCondition?: (task: F, index: number, tasks: F[], args: Parameters<F>, lastReturn: ReturnType<F>) => boolean;
|
|
182
112
|
}
|
|
183
113
|
|
|
184
|
-
|
|
114
|
+
/**
|
|
115
|
+
* ## Usage
|
|
116
|
+
* Use this when you have async functions in tasks, conditions or result wrapper
|
|
117
|
+
*
|
|
118
|
+
* Creates an async serial task function that executes a series of functions in order
|
|
119
|
+
* - all given functions(`options.tasks`) will be called in order
|
|
120
|
+
* - generated task function will have the same length as the first task function
|
|
121
|
+
* - you can appoint generated task function's name by `options.name`
|
|
122
|
+
* - **Strongly Recommended**: all task functions have same input type and output type
|
|
123
|
+
* - returned function.length will be the same as the first task function's length
|
|
124
|
+
* @param opts Options for creating a serial task, details in `SerialTaskOptions`
|
|
125
|
+
* @returns a funtcion that executes the tasks in order, returns `TaskReturn<OriginalReturn>`
|
|
126
|
+
*
|
|
127
|
+
* ## About
|
|
128
|
+
* @package SerialTask
|
|
129
|
+
* @author Kasukabe Tsumugi <futami16237@gmail.com>
|
|
130
|
+
* @version 1.2.0 (Last Update: 2026.02.13 16:27:40.250)
|
|
131
|
+
* @license MIT
|
|
132
|
+
* @link https://github.com/baendlorel/serial-task
|
|
133
|
+
* @link https://baendlorel.github.io/ Welcome to my site!
|
|
134
|
+
* @description Put a list of functions in and get a composed task function. Similar to functional programming's compose (function composition), but with more fine-grained and precise control, and the generated task incurs almost no runtime overhead. Supports both synchronous and asynchronous functions.
|
|
135
|
+
* @copyright Copyright (c) 2026 Kasukabe Tsumugi. All rights reserved.
|
|
136
|
+
*/
|
|
137
|
+
declare function createSerialTaskAsync<F extends Fn>(opts: SerialTaskOptions<F>): TaskifyAsync<F>;
|
|
138
|
+
|
|
139
|
+
/**
|
|
140
|
+
* ## Usage
|
|
141
|
+
* **DO NOT Use** this when you have **async functions** in tasks, conditions or result wrapper
|
|
142
|
+
*
|
|
143
|
+
* Creates a serial task function that executes a series of functions in order.
|
|
144
|
+
* - all given functions(`options.tasks`) will be called in order
|
|
145
|
+
* - the returned value will be `options.resultWrapper`ed then passed to the next task
|
|
146
|
+
* - generated task function will have the same length as the first task function
|
|
147
|
+
* - you can appoint generated task function's name by `options.name`
|
|
148
|
+
* - **Strongly Recommended**: all task functions have same input type and output type
|
|
149
|
+
* - returned function.length will be the same as the first task function's length
|
|
150
|
+
* @param opts Options for creating a serial task, details in `SerialTaskOptions`
|
|
151
|
+
* @returns a funtcion that executes the tasks in order, returns `TaskReturn<OriginalReturn>`
|
|
152
|
+
*
|
|
153
|
+
* ## About
|
|
154
|
+
* @package SerialTask
|
|
155
|
+
* @author Kasukabe Tsumugi <futami16237@gmail.com>
|
|
156
|
+
* @version 1.2.0 (Last Update: 2026.02.13 16:27:40.250)
|
|
157
|
+
* @license MIT
|
|
158
|
+
* @link https://github.com/baendlorel/serial-task
|
|
159
|
+
* @link https://baendlorel.github.io/ Welcome to my site!
|
|
160
|
+
* @description Put a list of functions in and get a composed task function. Similar to functional programming's compose (function composition), but with more fine-grained and precise control, and the generated task incurs almost no runtime overhead. Supports both synchronous and asynchronous functions.
|
|
161
|
+
* @copyright Copyright (c) 2026 Kasukabe Tsumugi. All rights reserved.
|
|
162
|
+
*/
|
|
163
|
+
declare function createSerialTask<F extends Fn>(opts: SerialTaskOptions<F>): Taskify<F>;
|
|
164
|
+
|
|
165
|
+
export { createSerialTask, createSerialTaskAsync };
|
|
166
|
+
export type { SerialTaskOptions, TaskReturn, Taskify, TaskifyAsync };
|
package/dist/index.mjs
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
const
|
|
1
|
+
const e=Reflect.defineProperty,t=Array.isArray,r=()=>!1,n=r,o=(e,t,r,n,o)=>0===t?n:[o];function i(e){if("object"!=typeof e||null===e)throw new TypeError("[SerialTask] 'options' must be an object");const{name:i="",tasks:a,breakCondition:s=r,skipCondition:u=n,resultWrapper:l=o}=e;if("string"!=typeof i)throw new TypeError("[SerialTask] 'name' must be a string or omitted");if(!t(a)||a.some(e=>"function"!=typeof e))throw new TypeError("[SerialTask] 'tasks' must be a function array");if("function"!=typeof s)throw new TypeError("[SerialTask] 'breakCondition' must be a function or omitted");if("function"!=typeof u)throw new TypeError("[SerialTask] 'skipCondition' must be a function or omitted");if("function"!=typeof l)throw new TypeError("[SerialTask] 'resultWrapper' must be a function or omitted");return{name:i,tasks:a,breakCondition:s,skipCondition:u,resultWrapper:l}}function a(t){const{name:r,tasks:n,breakCondition:o,skipCondition:a,resultWrapper:s}=i(t);if(0===n.length){const t=()=>({value:void 0,results:[],trivial:!0,breakAt:-1,skipped:[]});return e(t,"name",{value:r,configurable:!0}),t}const u=async function(...e){let t;const r=new Array(n.length);let i=-1;const u=[];for(let l=0;l<n.length;l++){const f=n[l],c=await Promise.try(s,f,l,n,e,t);if(await Promise.try(o,f,l,n,e,t)){i=l;break}await Promise.try(a,f,l,n,e,t)?u.push(l):(t=await Promise.try(f,...c),r[l]=t)}return{value:t,results:r,trivial:!1,breakAt:i,skipped:u}};return r&&e(u,"name",{value:r,configurable:!0}),e(u,"length",{value:n[0].length,configurable:!0}),u}function s(t){const{name:r,tasks:n,breakCondition:o,skipCondition:a,resultWrapper:s}=i(t);if(0===n.length){const t=()=>({value:void 0,results:[],trivial:!0,breakAt:-1,skipped:[]});return e(t,"name",{value:r,configurable:!0}),t}const u=function(...e){let t;const r=new Array(n.length);let i=-1;const u=[];for(let l=0;l<n.length;l++){const f=n[l],c=s(f,l,n,e,t);if(o(f,l,n,e,t)){i=l;break}a(f,l,n,e,t)?u.push(l):(t=f.apply(null,c),r[l]=t)}return{value:t,results:r,trivial:!1,breakAt:i,skipped:u}};return r&&e(u,"name",{value:r,configurable:!0}),e(u,"length",{value:n[0].length,configurable:!0}),u}export{s as createSerialTask,a as createSerialTaskAsync};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "serial-task",
|
|
3
|
-
"version": "1.0
|
|
3
|
+
"version": "1.2.0",
|
|
4
4
|
"author": {
|
|
5
5
|
"name": "Kasukabe Tsumugi",
|
|
6
6
|
"email": "futami16237@gmail.com"
|
|
@@ -29,32 +29,12 @@
|
|
|
29
29
|
"javascript"
|
|
30
30
|
],
|
|
31
31
|
"scripts": {
|
|
32
|
+
"lines": "clear && find ./src -type f \\( -name \"*.ts\" -o -name \"*.tsx\" -o -name \"*.js\" -o -name \"*.mjs\" -o -name \"*.cjs\" -o -name \"*.cpp\" -o -name \"*.h\" -o -name \"*.py\" -o -name \"*.css\" -o -name \"*.rs\" -o -name \"*.rc\" \\) | xargs wc -l",
|
|
32
33
|
"test": "clear & vitest",
|
|
33
34
|
"lint": "oxlint .",
|
|
34
35
|
"cover": "clear & vitest --coverage",
|
|
35
|
-
"build": "
|
|
36
|
+
"build": "tsx ./.scripts/rollup.ts"
|
|
36
37
|
},
|
|
37
38
|
"license": "MIT",
|
|
38
|
-
"devDependencies": {
|
|
39
|
-
"@babel/plugin-proposal-decorators": "^7.28.0",
|
|
40
|
-
"@babel/preset-env": "^7.28.3",
|
|
41
|
-
"@rollup/plugin-alias": "^5.1.1",
|
|
42
|
-
"@rollup/plugin-babel": "^6.0.4",
|
|
43
|
-
"@rollup/plugin-commonjs": "^28.0.6",
|
|
44
|
-
"@rollup/plugin-node-resolve": "^16.0.1",
|
|
45
|
-
"@rollup/plugin-replace": "^6.0.2",
|
|
46
|
-
"@rollup/plugin-terser": "^0.4.4",
|
|
47
|
-
"@rollup/plugin-typescript": "^12.1.4",
|
|
48
|
-
"@types/node": "^24.3.0",
|
|
49
|
-
"@vitest/coverage-v8": "^3.2.4",
|
|
50
|
-
"oxlint": "^1.12.0",
|
|
51
|
-
"prettier": "^3.6.2",
|
|
52
|
-
"rimraf": "^6.0.1",
|
|
53
|
-
"rollup": "^4.47.1",
|
|
54
|
-
"rollup-plugin-dts": "^6.2.3",
|
|
55
|
-
"rollup-plugin-dts-merger": "^1.2.3",
|
|
56
|
-
"tslib": "^2.8.1",
|
|
57
|
-
"typescript": "^5.9.2",
|
|
58
|
-
"vitest": "^3.2.4"
|
|
59
|
-
}
|
|
39
|
+
"devDependencies": {}
|
|
60
40
|
}
|