sanitizeVisibility function

String sanitizeVisibility(
  1. String code
)

Sanitize the visibility in sea miles to get macth with the regular expresion of visibility in METAR like reports.

Args: code (String): the code or section to sanitize.

Returns: String: the sanitized code or section.

Implementation

String sanitizeVisibility(String code) {
  final regex = RegExp(r'\s(?<int>\d+)\s(?<frac>\d/\dSM)\s?');

  for (var i = 0; i < 3; i++) {
    if (regex.hasMatch(code)) {
      final match = regex.firstMatch(code)!;
      code = code.replaceFirst(
        regex,
        ' ${match.namedGroup('int')}_${match.namedGroup('frac')} ',
      );
    }
  }

  return code;
}