Author.fromString constructor

Author.fromString(
  1. String value
)

Creates a new Author object from an string

Is able to parse values like author name (author@email.com) or author@email.com (author name)

Implementation

factory Author.fromString(String value) {
  final match = _nameEmailRx.firstMatch(value);
  var name = '';
  var email = '';

  if (match == null) {
    if (_isEmailRx.hasMatch(value)) {
      email = value.trim();
    } else {
      name = value.trim();
    }
  } else {
    final group1 = match.group(1) ?? '';
    if (_isEmailRx.hasMatch(group1)) {
      name = (match.group(2) ?? '').trim();
      email = group1.trim();
    } else {
      name = group1.trim();
      email = (match.group(2) ?? '').trim();
    }
  }

  return Author(name: name, email: email);
}