TF-1233 Fix error out of range in create avatar icon
This commit is contained in:
committed by
Dat H. Pham
parent
1a0cb26f01
commit
e70fa04779
@@ -1,35 +1,41 @@
|
||||
import 'package:core/utils/app_logger.dart';
|
||||
|
||||
extension StringExtension on String {
|
||||
|
||||
String get firstLetterToUpperCase {
|
||||
final listWord = split(' ');
|
||||
if (listWord.length > 1) {
|
||||
final regexLetter = RegExp("([A-Za-z])");
|
||||
final firstLetterOfFirstWord = regexLetter.firstMatch(listWord[0].trim())?.group(0);
|
||||
final firstLetterOfSecondWord = regexLetter.firstMatch(listWord[1].trim())?.group(0);
|
||||
try {
|
||||
final listWord = split(' ');
|
||||
if (listWord.length > 1) {
|
||||
final regexLetter = RegExp("([A-Za-z])");
|
||||
final firstLetterOfFirstWord = regexLetter.firstMatch(listWord[0].trim())?.group(0);
|
||||
final firstLetterOfSecondWord = regexLetter.firstMatch(listWord[1].trim())?.group(0);
|
||||
|
||||
if (firstLetterOfFirstWord != null && firstLetterOfSecondWord != null) {
|
||||
return '${firstLetterOfFirstWord.toUpperCase()}${firstLetterOfSecondWord.toUpperCase()}';
|
||||
} else if (firstLetterOfFirstWord != null && firstLetterOfSecondWord == null) {
|
||||
return '${firstLetterOfFirstWord.toUpperCase()}${firstLetterOfFirstWord.toUpperCase()}';
|
||||
} else if (firstLetterOfFirstWord == null && firstLetterOfSecondWord != null) {
|
||||
return '${firstLetterOfSecondWord.toUpperCase()}${firstLetterOfSecondWord.toUpperCase()}';
|
||||
if (firstLetterOfFirstWord != null && firstLetterOfSecondWord != null) {
|
||||
return '${firstLetterOfFirstWord.toUpperCase()}${firstLetterOfSecondWord.toUpperCase()}';
|
||||
} else if (firstLetterOfFirstWord != null && firstLetterOfSecondWord == null) {
|
||||
return '${firstLetterOfFirstWord.toUpperCase()}${firstLetterOfFirstWord.toUpperCase()}';
|
||||
} else if (firstLetterOfFirstWord == null && firstLetterOfSecondWord != null) {
|
||||
return '${firstLetterOfSecondWord.toUpperCase()}${firstLetterOfSecondWord.toUpperCase()}';
|
||||
} else {
|
||||
return '';
|
||||
}
|
||||
} else {
|
||||
return '';
|
||||
}
|
||||
} else {
|
||||
final regexLetter = RegExp("([A-Za-z])");
|
||||
final listMatch = regexLetter.allMatches(trim()).toList();
|
||||
if (listMatch.length > 1) {
|
||||
final firstLetter = listMatch[0].group(0);
|
||||
final secondLetter = listMatch[1].group(0);
|
||||
return firstLetter != null && secondLetter != null
|
||||
? '${firstLetter.toUpperCase()}${secondLetter.toUpperCase()}'
|
||||
: '';
|
||||
} else {
|
||||
final firstLetter = listMatch[0].group(0);
|
||||
return firstLetter != null ? firstLetter.toUpperCase() : '';
|
||||
final regexLetter = RegExp("([A-Za-z])");
|
||||
final listMatch = regexLetter.allMatches(trim()).toList();
|
||||
if (listMatch.length > 1) {
|
||||
final firstLetter = listMatch[0].group(0);
|
||||
final secondLetter = listMatch[1].group(0);
|
||||
return firstLetter != null && secondLetter != null
|
||||
? '${firstLetter.toUpperCase()}${secondLetter.toUpperCase()}'
|
||||
: '';
|
||||
} else {
|
||||
final firstLetter = substring(0, length > 1 ? 2 : 1);
|
||||
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(''));
|
||||
});
|
||||
});
|
||||
}
|
||||
Reference in New Issue
Block a user