TF-1233 Fix error out of range in create avatar icon

This commit is contained in:
Dat PHAM HOANG
2022-12-07 15:20:03 +07:00
committed by Dat H. Pham
parent 1a0cb26f01
commit e70fa04779
2 changed files with 92 additions and 25 deletions
@@ -1,7 +1,9 @@
import 'package:core/utils/app_logger.dart';
extension StringExtension on String { extension StringExtension on String {
String get firstLetterToUpperCase { String get firstLetterToUpperCase {
try {
final listWord = split(' '); final listWord = split(' ');
if (listWord.length > 1) { if (listWord.length > 1) {
final regexLetter = RegExp("([A-Za-z])"); final regexLetter = RegExp("([A-Za-z])");
@@ -27,9 +29,13 @@ extension StringExtension on String {
? '${firstLetter.toUpperCase()}${secondLetter.toUpperCase()}' ? '${firstLetter.toUpperCase()}${secondLetter.toUpperCase()}'
: ''; : '';
} else { } else {
final firstLetter = listMatch[0].group(0); final firstLetter = substring(0, length > 1 ? 2 : 1);
return firstLetter != null ? firstLetter.toUpperCase() : ''; return firstLetter.toUpperCase();
} }
} }
} catch (e) {
logError('StringExtension::firstLetterToUpperCase(): $e');
return '';
}
} }
} }
@@ -0,0 +1,61 @@
import 'package:core/presentation/extensions/string_extension.dart';
import 'package:flutter_test/flutter_test.dart';
void main() {
group("firstLetterToUpperCase()", () {
test("from 2 digits number", () {
const twentyThree = '23';
expect(twentyThree.firstLetterToUpperCase, equals('23'));
});
test("from 3 digits number", () {
const twentyThreeThree = '233';
expect(twentyThreeThree.firstLetterToUpperCase, equals('23'));
});
test("from 1 digit number", () {
const two = '2';
expect(two.firstLetterToUpperCase, equals('2'));
});
test("from 1 character", () {
const two = 'A';
expect(two.firstLetterToUpperCase, equals('A'));
});
test("from name with multiple words", () {
const name = 'Grant big';
expect(name.firstLetterToUpperCase, equals('GB'));
});
test("from name with one word", () {
const name = 'Grant';
expect(name.firstLetterToUpperCase, equals('GR'));
});
test("from name mix with number and word", () {
const name = '23 Grant';
expect(name.firstLetterToUpperCase, equals('GG'));
});
test("from name mix with word and number", () {
const name = 'Grant 23';
expect(name.firstLetterToUpperCase, equals('GG'));
});
test("from empty string", () {
const name = '';
expect(name.firstLetterToUpperCase, equals(''));
});
test("from special string", () {
const name = '&^%';
expect(name.firstLetterToUpperCase, equals('&^'));
});
test("from multiple special string", () {
const name = '&^% *()^';
expect(name.firstLetterToUpperCase, equals(''));
});
});
}