Skip to content

Commit

Permalink
refactoring and better error
Browse files Browse the repository at this point in the history
  • Loading branch information
fippli committed Mar 1, 2022
1 parent 45445b3 commit f725dc5
Show file tree
Hide file tree
Showing 5 changed files with 15 additions and 26 deletions.
8 changes: 5 additions & 3 deletions src/asyncChain.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,17 @@
*/
export const asyncChain = async (input, ...functions) => {
try {
const [nextFunction] = functions;
const [nextFunction, ...nextFunctions] = functions;
const nextValue = await nextFunction(input);

if (functions.length === 1) {
return nextValue;
}

return await asyncChain(nextValue, ...functions.slice(1));
return await asyncChain(nextValue, ...nextFunctions);
} catch (error) {
throw `Failed to chain ${functions} with input ${input}. ERROR: ${error.message}`;
throw new Error(`Failed to chain ${functions} with input ${input}.`, {
cause: error,
});
}
};
6 changes: 1 addition & 5 deletions src/containsAsyncFunction.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,2 @@
// Check if an array of functions contains an async function

const filter = (func) => func.constructor.name === "AsyncFunction";

export const containsAsyncFunction = (functions) =>
functions.filter(filter).length > 0;
functions.map((fn) => fn.constructor.name === "AsyncFunction").includes(true);
6 changes: 2 additions & 4 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,13 @@ export const chain = (...parameters) => {
return null;
}

const [nextValue] = parameters;
const [nextValue, ...functions] = parameters;
if (parameters.length === 1) {
return nextValue;
}

const functions = parameters.slice(1);

if (!validFunctions(functions)) {
throw new Error(`Invalid function argument.`);
throw new Error("Invalid function argument.");
}

if (containsAsyncFunction(functions)) {
Expand Down
8 changes: 5 additions & 3 deletions src/syncChain.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,17 @@
*/
export const syncChain = (input, ...functions) => {
try {
const [nextFunction] = functions;
const [nextFunction, ...nextFunctions] = functions;
const nextValue = nextFunction(input);

if (functions.length === 1) {
return nextValue;
}

return syncChain(nextValue, ...functions.slice(1));
return syncChain(nextValue, ...nextFunctions);
} catch (error) {
throw `Failed to chain ${functions} with input ${input}. ERROR: ${error.message}`;
throw new Error(`Failed to chain ${functions} with input ${input}.`, {
cause: error,
});
}
};
13 changes: 2 additions & 11 deletions src/validFunctions.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,5 @@
* Check if the input is an array containing functions
* @param {*} functions
*/
export const validFunctions = (functions) => {
const allAreFunctions = functions
.map((fn) => (typeof fn === "function" ? 1 : 0))
.reduce((sum, value) => sum * value, 1);

if (allAreFunctions === 0) {
return false;
}

return true;
};
export const validFunctions = (functions) =>
!functions.map((fn) => typeof fn === "function").includes(false);

0 comments on commit f725dc5

Please sign in to comment.