suffix method

String suffix(
  1. String previousRank,
  2. String nextRank
)

It creates the part of the ranking when one of the neighbors is bigger than the other, i.e. at least one of the two parameters (previousRank or nextRank) will always be empty, when it contains a wildcard, it has to preserve it.

Implementation

String suffix(String previousRank, String nextRank) {
  if (previousRank.isEmpty && nextRank.isEmpty) return alphabet.mean();

  // Contains wildcard
  if (previousRank.contains(wildcard) || nextRank.contains(wildcard)) {
    if (previousRank.isEmpty) {
      // Previous is empty

      var response = nextRank.substring(0, nextRank.length - 1);

      if (nextRank.last() == alphabet.first()) {
        return response + wildcard + alphabet.mean();
      }
      response += alphabet.between(
        alphabet.first(),
        nextRank.last(),
      );

      return response;
    } else {
      // Next is empty

      var response = previousRank.substring(0, previousRank.length - 1);

      response += alphabet.between(previousRank.last(), alphabet.last());

      return response;
    }
  }

  var _prev =
      (previousRank.isEmpty) ? alphabet.first() : previousRank.first();
  var _next = (nextRank.isEmpty) ? alphabet.last() : nextRank.first();

  return alphabet.between(_prev, _next);
}