Skip to content

Commit e2a81c8

Browse files
Capitalized Method (#51)
* Added upperCaseFirst method. * Added tests. * Fixed the format. * Simplified. * Remove unecessary method and test for nullables. * Improved the format. * Changed the name of the method and improved the code. * Added testing of non ASCII first character and updated the tests and docstring. * Fixed the format. * Added empty line. * renamed the method. * Code clean-up. * renamed.
1 parent 43708f2 commit e2a81c8

File tree

2 files changed

+62
-0
lines changed

2 files changed

+62
-0
lines changed

lib/string_basics.dart

+36
Original file line numberDiff line numberDiff line change
@@ -200,6 +200,42 @@ extension StringBasics on String {
200200
}
201201
return stringBuffer.toString();
202202
}
203+
204+
/// Returns a string with the first character in upper case.
205+
///
206+
/// This method can capitalize first character
207+
/// that is either alphabetic or accented.
208+
///
209+
/// If the first character is not alphabetic then return the same string.
210+
/// If [this] is empty, returns and empty string.
211+
///
212+
/// Example:
213+
/// ```dart
214+
/// final foo = 'bar';
215+
/// final baz = foo.capitalizeFirst(); // 'Bar'
216+
///
217+
/// // accented first character
218+
/// final og = 'éfoo';
219+
/// final capitalized = og.capitalizeFirst() // 'Éfoo'
220+
///
221+
/// // non alphabetic first character
222+
/// final foo1 = '1bar';
223+
/// final baz1 = foo1.capitalizeFirst(); // '1bar'
224+
///
225+
/// final test = '';
226+
/// final result = test.capitalizeFirst(); // ''
227+
/// ```
228+
String capitalize() {
229+
if (this.isEmpty) return '';
230+
231+
// trim this string first
232+
final trimmed = this.trimLeft();
233+
234+
// convert the first character to upper case
235+
final firstCharacter = trimmed[0].toUpperCase();
236+
237+
return trimmed.replaceRange(0, 1, firstCharacter);
238+
}
203239
}
204240

205241
extension NullableStringBasics on String? {

test/string_basics_test.dart

+26
Original file line numberDiff line numberDiff line change
@@ -352,4 +352,30 @@ void main() {
352352
expect('a'.isNotNullOrBlank, isTrue);
353353
});
354354
});
355+
356+
group('capitalize', () {
357+
test('returns a new string with the first character in upper case', () {
358+
expect('foo'.capitalize(), 'Foo');
359+
expect('hello World'.capitalize(), 'Hello World');
360+
});
361+
362+
test(
363+
'returns a new string with the first non-ASCII unicode character'
364+
'or accented characters in upper case', () {
365+
expect('éfoo'.capitalize(), 'Éfoo');
366+
});
367+
368+
test(
369+
'returns same string if the first character is not an'
370+
'alphabet character', () {
371+
expect('1bravo'.capitalize(), '1bravo');
372+
expect('2nd'.capitalize(), '2nd');
373+
expect('ßfoo'.capitalize(), 'ßfoo');
374+
expect('文foo'.capitalize(), '文foo');
375+
});
376+
377+
test('returns empty if the string is empty', () {
378+
expect(''.capitalize(), '');
379+
});
380+
});
355381
}

0 commit comments

Comments
 (0)