format method

String format(
  1. String pattern
)

Formats the full name as desired.

Which pattern to use to format it? string format

  • 'short': typical first + last name
  • 'long': birth name (without prefix and suffix)
  • 'public': first name combined with the last name's initial.
  • 'official': official document format

char format

  • 'b': birth name
  • 'B': capitalized birth name
  • 'f': first name
  • 'F': capitalized first name
  • 'l': last name (official)
  • 'L': capitalized last name
  • 'm': middle names
  • 'M': capitalized middle names
  • 'o': official document format
  • 'O': official document format in capital letters
  • 'p': prefix
  • 'P': capitalized prefix
  • 's': suffix
  • 'S': capitalized suffix

punctuations

  • '.': period
  • ',': comma
  • ' ': space
  • '-': hyphen
  • '_': underscore
  • '$': an escape character to select only the initial of the next char.

Given the name Joe Jim Smith, call format with the pattern string.

  • format('l f') => 'Smith Joe'
  • format('L, f') => 'SMITH, Joe'
  • format('short') => 'Joe Smith'
  • format() => 'SMITH, Joe Jim'
  • format(r'f $l.') => 'Joe S.'.

Do note that the escape character is only valid for the birth name parts: first, middle, and last names.

Implementation

String format(String pattern) {
  if (pattern == 'short') return short;
  if (pattern == 'long') return long;
  if (pattern == 'public') return public;
  if (pattern == 'official') pattern = 'o';

  var group = ''; // set of chars.
  final formatted = <String>[];
  for (var c in pattern.chars) {
    if (!kAllowedTokens.contains(c)) {
      throw NotAllowedException(
        source: full,
        operation: 'format',
        message: 'unsupported character <$c> from $pattern.',
      );
    }
    group += c;
    if (c == r'$') continue;
    formatted.add(_map(group) ?? '');
    group = '';
  }
  return formatted.join().trim();
}