Skip to content

Instantly share code, notes, and snippets.

@agisrh
Last active March 17, 2023 03:29
Show Gist options
  • Save agisrh/4c48301a215f949d93e2039cf34ce305 to your computer and use it in GitHub Desktop.
Save agisrh/4c48301a215f949d93e2039cf34ce305 to your computer and use it in GitHub Desktop.
Flutter Function Library
/*** 1. Regex Validation Email ***/
String email = '[email protected]';
bool isValidEmail = RegExp(r"^[a-zA-Z0-9.a-zA-Z0-9.!#$%&'*+-/=?^_`{|}~]+@[a-zA-Z0-9]+\.[a-zA-Z]+").hasMatch(email);
/*** 2. Hide Partial Email Address ***/
String email = "[email protected]";
int leftChar = 1; // num of left character to show
int rightChar = 2; // num of right character to show
int hideChar = email.split("@")[0].length - rightChar;
String result = email.replaceRange(leftChar, hideChar, '*' * (hideChar-leftChar));
// Result : j****[email protected]
/*** 3. Word First Capital ***/
String word = 'John Doe';
String result = word[0].toUpperCase()+word.substring(1).toLowerCase();
// Result : John doe
/*** 4. Proper ***/
String word = 'john doe';
String result = word.split(" ").map((str) => str[0].toUpperCase()+str.substring(1).toLowerCase()).join(" ");
// Result : John Doe
/*** 5. Get Inital Word ***/
String word = 'john doe';
String result = word.trim().split(' ').map((l) => l[0].toUpperCase()).join();
// Result : JD
/*** 6. Get inital word only 2 digit ***/
String word = 'Michael John Doe';
String result = word.trim().split(' ').map((l) => l[0].toUpperCase()).take(2).join();
// Result : MJ
/*** 7. Word Propper ***/
String wordPropper({required String text, List<String>? except}) {
final _spaceRegex = RegExp(r"\s+");
String cleanSpace = text.trim().replaceAll(_spaceRegex, " ");
String result = cleanSpace.split(" ").map((str) => except!=null ? except.contains(str) ? str : str[0].toUpperCase()+str.substring(1).toLowerCase() : str[0].toUpperCase()+str.substring(1).toLowerCase()).join(" ");
return result;
}
ex : wordPropper(text:"bank BRI DKI jakarta", except: ['DKI', 'BRI']);
// Result : Bank BRI DKI Jakarta
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment