findLevenshteinDistance function

FindLevenshteinDistanceResult? findLevenshteinDistance(
  1. String password,
  2. Map<String, int> rankedDictionary,
  3. int threshold
)

Implementation

FindLevenshteinDistanceResult? findLevenshteinDistance(
  String password,
  Map<String, int> rankedDictionary,
  int threshold,
) {
  try {
    int foundDistance = 0;

    String found = rankedDictionary.keys.firstWhere((entry) {
      final usedThreshold = getUsedThreshold(password, entry, threshold);
      if ((password.length - entry.length).abs() > usedThreshold) {
        return false;
      }
      final foundEntryDistance = levenshtein(password, entry);
      final isInThreshold = foundEntryDistance <= usedThreshold;

      if (isInThreshold) {
        foundDistance = foundEntryDistance;
      }
      return isInThreshold;
    });

    return FindLevenshteinDistanceResult(
      levenshteinDistance: foundDistance,
      levenshteinDistanceEntry: found,
    );
  } on StateError {
    return null;
  }
}