makeValidPubPackageName function
Converts any string to a valid pub package name to only use basic Latin
letters and Arabic digits: a-z0-9_
.
Implementation
String makeValidPubPackageName(String name) {
final onlyValidLetters =
name.replaceAll(RegExp(r'\W'), '_').replaceAll('__', '_');
if (onlyValidLetters.isNotEmpty) {
final firstLetter = onlyValidLetters[0];
// Set first letter to 'n' if it's a digit
if (firstLetter.matches(RegExp(r'\d'))) {
return 'n$onlyValidLetters';
}
}
return onlyValidLetters;
}