nextLetter method

String nextLetter()

Implementation

String nextLetter() {
  String s = this.toLowerCase();
  if (s == "z") {
    return String.fromCharCode(s.codeUnitAt(0) - 25) + String.fromCharCode(s.codeUnitAt(0) - 25); // AA or aa
  } else {
    var lastChar = s.substring(s.length - 1);
    var sub = s.substring(0, s.length - 1);
    if (lastChar == "z") {
      // If a string of length > 1 ends in Z/z,
      // increment the string (excluding the last Z/z) recursively,
      // and append A/a (depending on casing) to it
      return sub.nextLetter() + 'a';
    } else {
      // (take till last char) append with (increment last char)
      return sub + String.fromCharCode(lastChar.codeUnitAt(0) + 1);
    }
  }
}