[ Add, Test ] added capitalizeAllWordsFirstLetter() method that will turn the fi…
…rst letter only of words in a String to uppercase(), tested it with examples
Showing
3 changed files
with
76 additions
and
0 deletions
@@ -124,4 +124,5 @@ extension GetStringUtils on String { | @@ -124,4 +124,5 @@ extension GetStringUtils on String { | ||
124 | final path = startsWith('/') ? this : '/$this'; | 124 | final path = startsWith('/') ? this : '/$this'; |
125 | return GetUtils.createPath(path, segments); | 125 | return GetUtils.createPath(path, segments); |
126 | } | 126 | } |
127 | + String capitalizeAllWordsFirstLetter() => GetUtils.capitalizeAllWordsFirstLetter(this); | ||
127 | } | 128 | } |
@@ -595,6 +595,45 @@ class GetUtils { | @@ -595,6 +595,45 @@ class GetUtils { | ||
595 | return numericOnlyStr; | 595 | return numericOnlyStr; |
596 | } | 596 | } |
597 | 597 | ||
598 | + /// capitalize the first letter of each word in a string | ||
599 | + /// Example: your name => Your Name | ||
600 | + /// Example 2 : this is an example text=> This Is An Example Text | ||
601 | + // "emm, lemme think !", | ||
602 | + static String capitalizeAllWordsFirstLetter(String s) { | ||
603 | + String lowerCasedString = s.toLowerCase(); | ||
604 | + String stringWithoutExtraSpaces = lowerCasedString.trim(); | ||
605 | + | ||
606 | + if (stringWithoutExtraSpaces.isEmpty) { | ||
607 | + return ""; | ||
608 | + } | ||
609 | + if (stringWithoutExtraSpaces.length == 1) { | ||
610 | + return stringWithoutExtraSpaces.toUpperCase(); | ||
611 | + } | ||
612 | + | ||
613 | + List<String> stringWordsList = stringWithoutExtraSpaces.split(" "); | ||
614 | + List<String> capitalizedWordsFirstLetter = stringWordsList | ||
615 | + .map( | ||
616 | + (word) { | ||
617 | + if (word.trim().isEmpty) return ""; | ||
618 | + return word.trim(); | ||
619 | + }, | ||
620 | + ) | ||
621 | + .where( | ||
622 | + (word) => word != "", | ||
623 | + ) | ||
624 | + .map( | ||
625 | + (word) { | ||
626 | + if (word.startsWith(RegExp(r'[\n\t\r]'))) { | ||
627 | + return word; | ||
628 | + } | ||
629 | + return word[0].toUpperCase() + word.substring(1).toLowerCase(); | ||
630 | + }, | ||
631 | + ) | ||
632 | + .toList(); | ||
633 | + String finalResult = capitalizedWordsFirstLetter.join(" "); | ||
634 | + return finalResult; | ||
635 | + } | ||
636 | + | ||
598 | static bool hasMatch(String? value, String pattern) { | 637 | static bool hasMatch(String? value, String pattern) { |
599 | return (value == null) ? false : RegExp(pattern).hasMatch(value); | 638 | return (value == null) ? false : RegExp(pattern).hasMatch(value); |
600 | } | 639 | } |
@@ -16,6 +16,42 @@ void main() { | @@ -16,6 +16,42 @@ void main() { | ||
16 | expect(text.isNum, false); | 16 | expect(text.isNum, false); |
17 | }); | 17 | }); |
18 | 18 | ||
19 | + test('var.capitalizeAllWordsFirstLetter()', () { | ||
20 | + final List<String> sentences = [ | ||
21 | + "getx", | ||
22 | + "this is an example sentence", | ||
23 | + "this is an example sentence with a number 5", | ||
24 | + "this is an example sentence with a number 5 and a special character #", | ||
25 | + "this is an example sentence with a number 5 and a special character # and b letter C", | ||
26 | + " emm, lemme think !", | ||
27 | + "Bro, $letters is a good word", | ||
28 | + "THIS IS A SENTENCE WITH ALL CAPITAL LETTERS", | ||
29 | + "" | ||
30 | + ]; | ||
31 | + expect(text.capitalizeAllWordsFirstLetter(), "Oi"); | ||
32 | + expect(digit.capitalizeAllWordsFirstLetter(), "5"); | ||
33 | + expect(specialCaracters.capitalizeAllWordsFirstLetter(), "#\$!%@"); | ||
34 | + expect(alphaNumeric.capitalizeAllWordsFirstLetter(), "123asd"); | ||
35 | + expect(numbers.capitalizeAllWordsFirstLetter(), "123"); | ||
36 | + expect(letters.capitalizeAllWordsFirstLetter(), "Foo"); | ||
37 | + expect(sentences[0].capitalizeAllWordsFirstLetter(), "Getx"); | ||
38 | + expect(sentences[1].capitalizeAllWordsFirstLetter(), | ||
39 | + "This Is An Example Sentence"); | ||
40 | + expect(sentences[2].capitalizeAllWordsFirstLetter(), | ||
41 | + "This Is An Example Sentence With A Number 5"); | ||
42 | + expect(sentences[3].capitalizeAllWordsFirstLetter(), | ||
43 | + "This Is An Example Sentence With A Number 5 And A Special Character #"); | ||
44 | + expect(sentences[4].capitalizeAllWordsFirstLetter(), | ||
45 | + "This Is An Example Sentence With A Number 5 And A Special Character # And B Letter C"); | ||
46 | + expect( | ||
47 | + sentences[5].capitalizeAllWordsFirstLetter(), "Emm, Lemme Think !"); | ||
48 | + expect(sentences[6].capitalizeAllWordsFirstLetter(), | ||
49 | + "Bro, Foo Is A Good Word"); | ||
50 | + expect(sentences[7].capitalizeAllWordsFirstLetter(), | ||
51 | + "This Is A Sentence With All Capital Letters"); | ||
52 | + expect(sentences[8].capitalizeAllWordsFirstLetter(), ""); | ||
53 | + }); | ||
54 | + | ||
19 | test('var.isNumericOnly', () { | 55 | test('var.isNumericOnly', () { |
20 | expect(numbers.isNumericOnly, true); | 56 | expect(numbers.isNumericOnly, true); |
21 | expect(letters.isNumericOnly, false); | 57 | expect(letters.isNumericOnly, false); |
-
Please register or login to post a comment