Skip to content

Commit ef35069

Browse files
committed
🔧 Refactor toTitleCase method to handle consecutive whitespace correctly in both nullable and non-nullable string extensions
Closes #48
1 parent 5ca7246 commit ef35069

3 files changed

Lines changed: 3 additions & 2 deletions

File tree

‎lib/src/nullable_string_extensions.dart‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -812,7 +812,7 @@ extension MiscExtensionsNullable on String? {
812812
return this;
813813
}
814814

815-
var words = this!.trim().toLowerCase().split(' ');
815+
var words = this!.trim().toLowerCase().split(RegExp(r'\s+'));
816816
for (var i = 0; i < words.length; i++) {
817817
words[i] = words[i].substring(0, 1).toUpperCase() + words[i].substring(1);
818818
}

‎lib/src/string_extensions.dart‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -781,7 +781,7 @@ extension MiscExtensionsNonNullable on String {
781781
return this;
782782
}
783783

784-
var words = this.trim().toLowerCase().split(' ');
784+
var words = this.trim().toLowerCase().split(RegExp(r'\s+'));
785785
for (var i = 0; i < words.length; i++) {
786786
words[i] = words[i].substring(0, 1).toUpperCase() + words[i].substring(1);
787787
}

‎test/string_extensions_test.dart‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ void main() {
5757
expect(' '.toTitleCase, ' ');
5858
expect(' '.toTitleCase, ' ');
5959
expect(null.toTitleCase, null);
60+
expect('title case'.toTitleCase, 'Title Case');
6061
},
6162
);
6263
test(

0 commit comments

Comments
 (0)