File tree Expand file tree Collapse file tree 5 files changed +15
-26
lines changed Expand file tree Collapse file tree 5 files changed +15
-26
lines changed Original file line number Diff line number Diff line change 1010 */
1111export const asyncChain = async ( input , ...functions ) => {
1212 try {
13- const [ nextFunction ] = functions ;
13+ const [ nextFunction , ... nextFunctions ] = functions ;
1414 const nextValue = await nextFunction ( input ) ;
1515
1616 if ( functions . length === 1 ) {
1717 return nextValue ;
1818 }
1919
20- return await asyncChain ( nextValue , ...functions . slice ( 1 ) ) ;
20+ return await asyncChain ( nextValue , ...nextFunctions ) ;
2121 } catch ( error ) {
22- throw `Failed to chain ${ functions } with input ${ input } . ERROR: ${ error . message } ` ;
22+ throw new Error ( `Failed to chain ${ functions } with input ${ input } .` , {
23+ cause : error ,
24+ } ) ;
2325 }
2426} ;
Original file line number Diff line number Diff line change 1- // Check if an array of functions contains an async function
2-
3- const filter = ( func ) => func . constructor . name === "AsyncFunction" ;
4-
51export const containsAsyncFunction = ( functions ) =>
6- functions . filter ( filter ) . length > 0 ;
2+ functions . map ( ( fn ) => fn . constructor . name === "AsyncFunction" ) . includes ( true ) ;
Original file line number Diff line number Diff line change @@ -8,15 +8,13 @@ export const chain = (...parameters) => {
88 return null ;
99 }
1010
11- const [ nextValue ] = parameters ;
11+ const [ nextValue , ... functions ] = parameters ;
1212 if ( parameters . length === 1 ) {
1313 return nextValue ;
1414 }
1515
16- const functions = parameters . slice ( 1 ) ;
17-
1816 if ( ! validFunctions ( functions ) ) {
19- throw new Error ( ` Invalid function argument.` ) ;
17+ throw new Error ( " Invalid function argument." ) ;
2018 }
2119
2220 if ( containsAsyncFunction ( functions ) ) {
Original file line number Diff line number Diff line change 1010 */
1111export const syncChain = ( input , ...functions ) => {
1212 try {
13- const [ nextFunction ] = functions ;
13+ const [ nextFunction , ... nextFunctions ] = functions ;
1414 const nextValue = nextFunction ( input ) ;
1515
1616 if ( functions . length === 1 ) {
1717 return nextValue ;
1818 }
1919
20- return syncChain ( nextValue , ...functions . slice ( 1 ) ) ;
20+ return syncChain ( nextValue , ...nextFunctions ) ;
2121 } catch ( error ) {
22- throw `Failed to chain ${ functions } with input ${ input } . ERROR: ${ error . message } ` ;
22+ throw new Error ( `Failed to chain ${ functions } with input ${ input } .` , {
23+ cause : error ,
24+ } ) ;
2325 }
2426} ;
Original file line number Diff line number Diff line change 22 * Check if the input is an array containing functions
33 * @param {* } functions
44 */
5- export const validFunctions = ( functions ) => {
6- const allAreFunctions = functions
7- . map ( ( fn ) => ( typeof fn === "function" ? 1 : 0 ) )
8- . reduce ( ( sum , value ) => sum * value , 1 ) ;
9-
10- if ( allAreFunctions === 0 ) {
11- return false ;
12- }
13-
14- return true ;
15- } ;
5+ export const validFunctions = ( functions ) =>
6+ ! functions . map ( ( fn ) => typeof fn === "function" ) . includes ( false ) ;
You can’t perform that action at this time.
0 commit comments