sanitizeWindshear function

String sanitizeWindshear(
  1. String code
)

Sanitize the windshear in to get macth with the regular expresion of windshear in METAR like reports.

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

Returns: String: the sanitized code or section.

Implementation

String sanitizeWindshear(String code) {
  code = code.replaceFirst(RegExp(r'WS\sALL\sRWY'), 'WS_ALL_RWY');

  final regex = RegExp(r'WS\sR(WY)?(?<name>\d{2}[CLR]?)');
  for (var i = 1; i <= 3; i++) {
    if (regex.hasMatch(code)) {
      final match = regex.firstMatch(code);
      code = code.replaceFirst(
        regex,
        'WS_R${match!.namedGroup("name")}',
      );
    }
  }

  return code;
}