parseEmailAndName property

Map<String, String>? get parseEmailAndName

Implementation

Map<String, String>? get parseEmailAndName {
  // Use a regular expression to capture the name and email parts
  final RegExp regExp = RegExp(r'^(.*?)(?:\s*<\s*([^>]+)\s*>)?$');
  final match = regExp.firstMatch(this.trim());

  if (match != null) {
    String? name = match.group(1)?.trim();
    String? email = match.group(2)?.trim();

    // If no name is provided and the input is a valid email, use the part before '@' as the name
    if (name == null || name.isEmpty) {
      // Directly validate the input as an email if no '<' and '>' are found
      final RegExp emailDirectRegExp = RegExp(r'^\S+@\S+\.\S+$');
      if (emailDirectRegExp.hasMatch(this.trim())) {
        email = this.trim();
        name = email.substring(0, email.indexOf('@'));
      }
    }

    // Check if the email is valid using a regular expression
    if (email != null && _isEmailValid(email)) {
      // Remove any leading or trailing whitespaces
      name = name?.replaceAll(RegExp(r'\s+$'), '');

      return {'fullName': name ?? '', 'email': email};
    }
  }

  return null;
}