suggestCommand static method

String? suggestCommand(
  1. String name,
  2. Iterable<String> known
)

The known command closest to name (at most two edits for longer names, one for short ones; swapped neighbours count as one), or null when nothing is close.

Implementation

static String? suggestCommand(String name, Iterable<String> known) {
  final limit = name.length <= 4 ? 1 : 2;
  String? best;
  var bestDistance = limit + 1;
  for (final k in known) {
    if ((k.length - name.length).abs() > limit) continue;
    final d = _distance(name, k, limit);
    if (d < bestDistance ||
        (d == bestDistance && best != null && k.length < best.length)) {
      best = k;
      bestDistance = d;
    }
  }
  return bestDistance <= limit ? best : null;
}