Created
April 17, 2021 19:14
-
-
Save sayrer/b4ab156cb746a38f6c73d0ae519b2dda to your computer and use it in GitHub Desktop.
I'd use an Array function, maybe every()
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* Original code from https://seanbarry.dev/posts/switch-true-pattern: | |
switch (true) { | |
case !isDefined(user): | |
throw new Error("User must be defined."); | |
case !isString(user.firstName): | |
throw new Error("User's first name must be a string"); | |
case !isValidEmail(user.email): | |
throw new Error("User's email address must be a valid email address"); | |
case !isValidPhoneNumber(user.number): | |
throw new Error("User's phone number must be a valid phone number"); | |
// any number of other validation cases here | |
default: | |
return user; | |
} | |
*/ | |
function isDefined(user) { | |
return true; | |
} | |
function isString(user) { | |
return true; | |
} | |
function isValidEmail(user) { | |
return true; | |
} | |
function isValidPhoneNumber(user) { | |
return true; | |
} | |
let user = {}; | |
let validation_cases = [isDefined, isString, isValidEmail, isValidPhoneNumber]; | |
let passed = validation_cases.every((check) => check(user)); | |
console.log("passed: ", passed); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment