closestMatch function

String? closestMatch(
  1. String input,
  2. Iterable<String> options
)

The candidate in options closest to input, when close enough to be a likely typo.

Implementation

String? closestMatch(String input, Iterable<String> options) {
  String? best;
  var bestDistance = 1 << 30;
  for (final option in options) {
    final distance = _editDistance(input.toLowerCase(), option.toLowerCase());
    if (distance < bestDistance) {
      best = option;
      bestDistance = distance;
    }
  }
  final limit = input.length <= 4 ? 1 : (input.length <= 8 ? 2 : 3);
  return bestDistance <= limit ? best : null;
}