toLowerCaseUnderscore static method

String toLowerCaseUnderscore(
  1. String s, {
  2. bool simple = false,
})

Transforms s to lower-case using underscore (_) before the upper-case letters.

  • If simple is true removes non-letters and non-digits.

Implementation

static String toLowerCaseUnderscore(String s, {bool simple = false}) {
  if (simple) {
    s = s.replaceAll(_regexpNotLettersAndDigits, '_');
  }

  var str = StringBuffer();

  String? prevChar;

  for (var rune in s.runes) {
    var char = String.fromCharCode(rune);

    if (prevChar != null && isUpperCase(char) && isLetterOrDigit(char)) {
      if (char != '_' && prevChar != '_') {
        str.write('_');
      }
    }

    str.write(char.toLowerCase());

    prevChar = char;
  }

  return str.toString();
}