sanitizeName method

String sanitizeName(
  1. String productionName, [
  2. bool allLower = true
])
inherited

Sanitize a name to be in correct way to be used as an identifier

Implementation

String sanitizeName(String productionName, [bool allLower = true]) {
  final buffer = StringBuffer();
  var lastCharacter = 0;
  var index = 0;
  for (var character in productionName.runes) {
    if (StringHelper.isWhitespace(character) ||
        StringHelper.isNewline(character)) {
      buffer.write('_');
    } else if (StringHelper.isAlphabetic(character) ||
        StringHelper.isUnderline(character) ||
        (StringHelper.isNumber(character) && index > 0)) {
      if (StringHelper.isLower(lastCharacter) &&
          !StringHelper.isLower(character)) {
        buffer.write('_');
      }
      if (allLower && !StringHelper.isLower(character)) {
        final temp = String.fromCharCode(character);
        character = temp.toLowerCase().codeUnitAt(0);
      }
      buffer.writeCharCode(character);
    }
    lastCharacter = character;
    index += 1;
  }
  return buffer.toString();
}