normalizeShareCode function

String? normalizeShareCode(
  1. String input
)

Normalises user input to the 10 canonical symbols (upper case, O → 0, I/L → 1, hyphens and whitespace dropped), or null if it contains a character outside the alphabet or has the wrong length.

Implementation

String? normalizeShareCode(String input) {
  final buf = StringBuffer();
  for (final rune in input.toUpperCase().runes) {
    final ch = String.fromCharCode(rune);
    if (ch == '-' || ch.trim().isEmpty) continue;
    final mapped = switch (ch) {
      'O' => '0',
      'I' || 'L' => '1',
      _ => ch,
    };
    if (!crockfordAlphabet.contains(mapped)) return null;
    buf.write(mapped);
  }
  final s = buf.toString();
  return s.length == 10 ? s : null;
}