38 lines
987 B
Dart
38 lines
987 B
Dart
bool isValidUsername({required String name}) {
|
|
if (name.isEmpty) {
|
|
return false;
|
|
}
|
|
return RegExp(r".+\@.+\..+").hasMatch(cleanUpUsername(name: name));
|
|
}
|
|
|
|
String cleanUpUsername({required String name}) {
|
|
name = name.replaceAll(" ", "");
|
|
if (name.isNotEmpty) {
|
|
if (name[0] == "@") {
|
|
name = name.substring(1);
|
|
}
|
|
}
|
|
return name;
|
|
}
|
|
|
|
Uri urlFromUsername({required String name}) {
|
|
name = cleanUpUsername(name: name);
|
|
return Uri.parse("https://${name.substring(name.indexOf("@") + 1)}");
|
|
}
|
|
|
|
String userFromUsername({required String name}) {
|
|
name = cleanUpUsername(name: name);
|
|
return name.substring(0, name.indexOf("@"));
|
|
}
|
|
|
|
// A fully qualified and valid username for example (@)hello@world.com
|
|
// The first @ is ignored
|
|
class Username {
|
|
late final String user;
|
|
late final Uri url;
|
|
Username(String username) {
|
|
assert(isValidUsername(name: username));
|
|
user = userFromUsername(name: username);
|
|
url = urlFromUsername(name: username);
|
|
}
|
|
}
|