alterToString static method

String alterToString(
  1. String alterText
)

Parse alter text to a string of one or two sharps/flats.

Args: alter_text: A string representation of an integer number of semitones.

Returns: A string, one of 'bb', 'b', '#', '##', or the empty string.

Raises: ChordSymbolParseError: If alter_text cannot be parsed to an integer, or if the integer is not a valid number of semitones between -2 and 2 inclusive.

Implementation

static String alterToString(String alterText) {
  // Parse alter text to an integer number of semitones.
  try {
    final alterSemitones = int.parse(alterText);
    // Visual alter representation
    switch (alterSemitones) {
      case -2:
        return 'bb';
      case -1:
        return 'b';
      case 0:
        return '';
      case 1:
        return '#';
      case 2:
        return '##';
      default:
        throw XmlParserException('Invalid alter: $alterSemitones');
    }
  } catch (e) {
    throw XmlParserException('Non-integer alter: $alterText');
  }
}