Skip to content

Instantly share code, notes, and snippets.

@navanathjadhav
Created June 11, 2022 12:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save navanathjadhav/c3fcfd2ace2c7eea96af3a536b4d2ff1 to your computer and use it in GitHub Desktop.
Save navanathjadhav/c3fcfd2ace2c7eea96af3a536b4d2ff1 to your computer and use it in GitHub Desktop.
Traditional Switch Case / Else if
/**
* This method will validate user's status
* @param user user data
*/
function validateStatus(user) {
let response = {};
/*
* User status check: return response as per user status
*/
switch (user.status) {
//////////////////////////////
// Active //
//////////////////////////////
case "Active":
response = { message: "Login successful", user: user };
break;
//////////////////////////////
// Inactive //
//////////////////////////////
case "Inactive":
response = { message: "User is inactive", user: undefined };
break;
//////////////////////////////
// Invited //
//////////////////////////////
case "Invited":
response = { message: "User is not activated yet", user: undefined };
break;
//////////////////////////////
// Blocked //
//////////////////////////////
case "Blocked":
response = { message: "User is blocked", user: undefined };
break;
//////////////////////////////
// Invalid Status //
//////////////////////////////
default:
response = { message: "User with invalid status", user: undefined };
break;
}
/*
* Return response
*/
return response;
}
/**
* This method will validate user's role
* @param user user data
*/
function validateRole(user) {
let response = {};
if (user.role === "Admin") {
response = { message: "Full Access granted", user: user };
} else if (user.role === "Teacher") {
response = { message: "Limited Access granted", user: user };
} else if (user.role === "Student") {
response = { message: "Access denied", user: undefined };
}
/*
* Return response
*/
return response;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment