Created
August 21, 2020 15:15
-
-
Save khadkarajesh/c62b570235a7e601efa9eece84f83420 to your computer and use it in GitHub Desktop.
dart's string extension methods
This file contains 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
extension StringExtension on String { | |
//will smith => Will Smith | |
String capitalize() { | |
return this | |
.trim() | |
.split(" ") | |
.where((e) => e.trim().length > 0) | |
.map((e) => "${e[0].toUpperCase()}" | |
"${e.length > 1 ? e.substring(1).toLowerCase() : ""}") | |
.join(" "); | |
} | |
bool isEmail() { | |
return RegExp( | |
r"^[a-zA-Z0-9.a-zA-Z0-9.!#$%&'*+-/=?^_`{|}~]+@[a-zA-Z0-9]+\.[a-zA-Z]+") | |
.hasMatch(this); | |
} | |
/* | |
https://stackoverflow.com/questions/5142103/regex-to-validate-password-strength | |
^ Start anchor | |
(?=.*[A-Z].*[A-Z]) Ensure string has two uppercase letters. | |
(?=.*[!@#$&*]) Ensure string has one special case letter. | |
(?=.*[0-9].*[0-9]) Ensure string has two digits. | |
(?=.*[a-z].*[a-z].*[a-z]) Ensure string has three lowercase letters. | |
.{8} Ensure string is of length 8. | |
$ End anchor. | |
*/ | |
bool isValidPassword() { | |
return RegExp( | |
r"^(?=.*[A-Z].*[A-Z])(?=.*[!@#$&*])(?=.*[0-9].*[0-9])(?=.*[a-z].*[a-z].*[a-z]).{8}$") | |
.hasMatch(this); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment