Skip to content

Commit f725dc5

Browse files
Filip Johanssonfippli
authored andcommitted
refactoring and better error
1 parent 45445b3 commit f725dc5

File tree

5 files changed

+15
-26
lines changed

5 files changed

+15
-26
lines changed

src/asyncChain.js

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,15 +10,17 @@
1010
*/
1111
export 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
};

src/containsAsyncFunction.js

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,2 @@
1-
// Check if an array of functions contains an async function
2-
3-
const filter = (func) => func.constructor.name === "AsyncFunction";
4-
51
export const containsAsyncFunction = (functions) =>
6-
functions.filter(filter).length > 0;
2+
functions.map((fn) => fn.constructor.name === "AsyncFunction").includes(true);

src/index.js

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff 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)) {

src/syncChain.js

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,15 +10,17 @@
1010
*/
1111
export 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
};

src/validFunctions.js

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,5 @@
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);

0 commit comments

Comments
 (0)