From 887c7b66f4291bc03c2da130386c25e071cde710 Mon Sep 17 00:00:00 2001 From: kkt4 Date: Sun, 17 Jul 2022 12:01:24 +0700 Subject: [PATCH 1/2] add word capitalize --- lib/src/string.dart | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/lib/src/string.dart b/lib/src/string.dart index 050d589..2368e32 100644 --- a/lib/src/string.dart +++ b/lib/src/string.dart @@ -24,6 +24,15 @@ extension StringCapitalizeExtension on String { } } +extension StringWordCapitalize on String { + String wordFormatCapitalize() { + final result = split(' '); + final buffer = StringBuffer(); + buffer.write(result.map((e) => e.capitalize()).join(' ')); + return buffer.toString(); + } +} + extension StringDecapitalizeExtension on String { /// Returns a copy of this string having its first letter lowercased, or the /// original string, if it's empty or already starts with a lower case letter. From 22601b51f9be92d128eed56e9bc62823d36097a4 Mon Sep 17 00:00:00 2001 From: kkt4 Date: Sun, 17 Jul 2022 12:01:43 +0700 Subject: [PATCH 2/2] add test for word capitalize --- test/string_test.dart | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/test/string_test.dart b/test/string_test.dart index a6888ef..4def2a8 100644 --- a/test/string_test.dart +++ b/test/string_test.dart @@ -13,6 +13,14 @@ void main() { expect('Test'.capitalize(), 'Test'); }); + test('Word Capitalize', () { + expect('lorem ipsum'.wordFormatCapitalize(), 'Lorem Ipsum'); + expect('lorem'.wordFormatCapitalize(), 'Lorem'); + expect('123'.wordFormatCapitalize(), '123'); + expect(''.wordFormatCapitalize(), ''); + expect('123 123 a123'.wordFormatCapitalize(), '123 123 A123'); + }); + test('.decapitalize()', () { expect(''.decapitalize(), ''); expect('123'.decapitalize(), '123');