encode method

  1. @override
PhoneticEncoding? encode(
  1. String input
)
override

Encodes a string using the Soundex algorithm as configured.

Returns a PhoneticEncoding for the input String or null if the input is empty (after cleaning up).

Implementation

@override
PhoneticEncoding? encode(String input) {
  // clean up the input and convert to uppercase
  input = PhoneticUtils.clean(input, allowLatin: false);
  if (input.isEmpty) {
    return null;
  }

  List<String> parts;
  if (hyphenatedPartsEnabled) {
    parts = _splitHyphenatedParts(input);
  } else {
    parts = [input];
  }

  final iterator = parts.iterator;
  if (!iterator.moveNext()) {
    return null;
  }

  // first we encode the primary part
  final firstPart = iterator.current;
  final primary = _encode(firstPart);

  final alternates = <String>{};

  if (prefixesEnabled) {
    _addTrimmedPrefixToAlternates(alternates, firstPart);
  }

  // now go through all parts and add more alternates
  while (iterator.moveNext()) {
    final part = iterator.current;
    alternates.add(_encode(part));
    if (prefixesEnabled) {
      _addTrimmedPrefixToAlternates(alternates, part);
    }
  }

  // remove the primary if it made it into the alternate list from others
  alternates.remove(primary);

  return PhoneticEncoding(primary, alternates);
}