Need to simplify this function; all inputs are 0/1:
export const handleStepComplete = (userSave: number, concur: number, signature: number) => {
if (userSave === 0 && concur === 0 && signature === 0) {
return {completed: false, active: true, error: false};
}
if ((userSave === 1 && concur === 0 && signature === 0) ||
(userSave === 1 && concur === 1 && signature === 0)) {
return {completed: true, active: true, error: false};
}
if (userSave === 1 && concur === 0 && signature === 1) {
return {completed: false, active: false, error: true};
}
};
CodePudding user response:
Just a draft, but you could structure it something like this.
export const handleStepComplete = (userSave: number, concur: number, signature: number) => {
return {
completed: (userSave && concur && signature),
active: (userSave && !signature && !concur),
error: (signature && !concur)
}
};
This does not handle all cases, and neither does your code. The logic is probably not correct too, but you get the point and can build on it.
CodePudding user response:
export const handleStepComplete = (userSave: number, concur: number, signature: number) => {
if (userSave === 0 && concur === 0 && signature === 0) {
return {completed: false, active: true, error: false};
}
if(userSave === 1 && concur === 0 || 1 && signature === 0){
return {completed: true, active: true, error: false};
}
if (userSave === 1 && concur === 0 && signature === 1) {
return {completed: false, active: false, error: true};
}
}
try this, I use or condition in the second if check, your two condition is now in one condition
CodePudding user response:
This is the shortest I could come up with
const handleStepComplete = (userSave, concur, signature) => {
return {completed: Boolean(signature) ? false : Boolean(userSave),
active: Boolean(signature) ? false : !Boolean(concur),
error: Boolean(signature)};
};
const result = handleStepComplete(0,0,0);
console.log(result)
CodePudding user response:
what do you think of this ? i have removed the types for params for it to work here in the comment section ... and added all possible cases that i can think of ..
const handleStepComplete1 = (userSave, concur, signature) => {
return {
completed: (userSave === 1 && concur === 0 && signature === 0) ||
(userSave === 1 && concur === 1 && signature === 0),
active: (userSave === 0 && concur === 0 && signature === 0) || (userSave === 1 && concur === 0 && signature === 0) || (userSave === 1 && concur === 1 && signature === 0),
error: (userSave === 1 && concur === 0 && signature === 1)
};
};
const handleStepComplete2 = (userSave, concur, signature) => ({
completed: !!(userSave && !signature),
active: !!(!(!userSave && concur) && !signature),
error: !!(userSave && !concur && signature),
});
console.log(handleStepComplete1(0,0,0), handleStepComplete2(0,0,0))
console.log(handleStepComplete1(1,0,0), handleStepComplete2(1,0,0))
console.log(handleStepComplete1(0,1,0), handleStepComplete2(0,1,0))
console.log(handleStepComplete1(0,0,1), handleStepComplete2(0,0,1))
console.log(handleStepComplete1(0,1,1), handleStepComplete2(0,1,1))
console.log(handleStepComplete1(1,1,1), handleStepComplete2(1,1,1))
console.log(handleStepComplete1(1,1,0), handleStepComplete2(1,1,0))
console.log(handleStepComplete1(1,0,1), handleStepComplete2(1,0,1))