Jonny Borges
Committed by GitHub

Merge pull request #2547 from anasfik/master

capitalizeAllWordsFirstLetter() method to capitalize only first letter in String words
... ... @@ -124,4 +124,8 @@ extension GetStringUtils on String {
final path = startsWith('/') ? this : '/$this';
return GetUtils.createPath(path, segments);
}
/// capitalize only first letter in String words to upper case
String capitalizeAllWordsFirstLetter() =>
GetUtils.capitalizeAllWordsFirstLetter(this);
}
... ...
... ... @@ -595,6 +595,44 @@ class GetUtils {
return numericOnlyStr;
}
/// Capitalize only the first letter of each word in a string
/// Example: getx will make it easy => Getx Will Make It Easy
/// Example 2 : this is an example text => This Is An Example Text
static String capitalizeAllWordsFirstLetter(String s) {
String lowerCasedString = s.toLowerCase();
String stringWithoutExtraSpaces = lowerCasedString.trim();
if (stringWithoutExtraSpaces.isEmpty) {
return "";
}
if (stringWithoutExtraSpaces.length == 1) {
return stringWithoutExtraSpaces.toUpperCase();
}
List<String> stringWordsList = stringWithoutExtraSpaces.split(" ");
List<String> capitalizedWordsFirstLetter = stringWordsList
.map(
(word) {
if (word.trim().isEmpty) return "";
return word.trim();
},
)
.where(
(word) => word != "",
)
.map(
(word) {
if (word.startsWith(RegExp(r'[\n\t\r]'))) {
return word;
}
return word[0].toUpperCase() + word.substring(1).toLowerCase();
},
)
.toList();
String finalResult = capitalizedWordsFirstLetter.join(" ");
return finalResult;
}
static bool hasMatch(String? value, String pattern) {
return (value == null) ? false : RegExp(pattern).hasMatch(value);
}
... ...
... ... @@ -16,6 +16,42 @@ void main() {
expect(text.isNum, false);
});
test('var.capitalizeAllWordsFirstLetter()', () {
final List<String> sentences = [
"getx",
"this is an example sentence",
"this is an example sentence with a number 5",
"this is an example sentence with a number 5 and a special character #",
"this is an example sentence with a number 5 and a special character # and b letter C",
" emm, lemme think !",
"Bro, $letters is a good word",
"THIS IS A SENTENCE WITH ALL CAPITAL LETTERS",
""
];
expect(text.capitalizeAllWordsFirstLetter(), "Oi");
expect(digit.capitalizeAllWordsFirstLetter(), "5");
expect(specialCaracters.capitalizeAllWordsFirstLetter(), "#\$!%@");
expect(alphaNumeric.capitalizeAllWordsFirstLetter(), "123asd");
expect(numbers.capitalizeAllWordsFirstLetter(), "123");
expect(letters.capitalizeAllWordsFirstLetter(), "Foo");
expect(sentences[0].capitalizeAllWordsFirstLetter(), "Getx");
expect(sentences[1].capitalizeAllWordsFirstLetter(),
"This Is An Example Sentence");
expect(sentences[2].capitalizeAllWordsFirstLetter(),
"This Is An Example Sentence With A Number 5");
expect(sentences[3].capitalizeAllWordsFirstLetter(),
"This Is An Example Sentence With A Number 5 And A Special Character #");
expect(sentences[4].capitalizeAllWordsFirstLetter(),
"This Is An Example Sentence With A Number 5 And A Special Character # And B Letter C");
expect(
sentences[5].capitalizeAllWordsFirstLetter(), "Emm, Lemme Think !");
expect(sentences[6].capitalizeAllWordsFirstLetter(),
"Bro, Foo Is A Good Word");
expect(sentences[7].capitalizeAllWordsFirstLetter(),
"This Is A Sentence With All Capital Letters");
expect(sentences[8].capitalizeAllWordsFirstLetter(), "");
});
test('var.isNumericOnly', () {
expect(numbers.isNumericOnly, true);
expect(letters.isNumericOnly, false);
... ...