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 {
|
extension StringExtension on String {
|
||||||
|
|
||||||
String get firstLetterToUpperCase {
|
String get firstLetterToUpperCase {
|
||||||
final listWord = split(' ');
|
try {
|
||||||
if (listWord.length > 1) {
|
final listWord = split(' ');
|
||||||
final regexLetter = RegExp("([A-Za-z])");
|
if (listWord.length > 1) {
|
||||||
final firstLetterOfFirstWord = regexLetter.firstMatch(listWord[0].trim())?.group(0);
|
final regexLetter = RegExp("([A-Za-z])");
|
||||||
final firstLetterOfSecondWord = regexLetter.firstMatch(listWord[1].trim())?.group(0);
|
final firstLetterOfFirstWord = regexLetter.firstMatch(listWord[0].trim())?.group(0);
|
||||||
|
final firstLetterOfSecondWord = regexLetter.firstMatch(listWord[1].trim())?.group(0);
|
||||||
|
|
||||||
if (firstLetterOfFirstWord != null && firstLetterOfSecondWord != null) {
|
if (firstLetterOfFirstWord != null && firstLetterOfSecondWord != null) {
|
||||||
return '${firstLetterOfFirstWord.toUpperCase()}${firstLetterOfSecondWord.toUpperCase()}';
|
return '${firstLetterOfFirstWord.toUpperCase()}${firstLetterOfSecondWord.toUpperCase()}';
|
||||||
} else if (firstLetterOfFirstWord != null && firstLetterOfSecondWord == null) {
|
} else if (firstLetterOfFirstWord != null && firstLetterOfSecondWord == null) {
|
||||||
return '${firstLetterOfFirstWord.toUpperCase()}${firstLetterOfFirstWord.toUpperCase()}';
|
return '${firstLetterOfFirstWord.toUpperCase()}${firstLetterOfFirstWord.toUpperCase()}';
|
||||||
} else if (firstLetterOfFirstWord == null && firstLetterOfSecondWord != null) {
|
} else if (firstLetterOfFirstWord == null && firstLetterOfSecondWord != null) {
|
||||||
return '${firstLetterOfSecondWord.toUpperCase()}${firstLetterOfSecondWord.toUpperCase()}';
|
return '${firstLetterOfSecondWord.toUpperCase()}${firstLetterOfSecondWord.toUpperCase()}';
|
||||||
|
} else {
|
||||||
|
return '';
|
||||||
|
}
|
||||||
} else {
|
} else {
|
||||||
return '';
|
final regexLetter = RegExp("([A-Za-z])");
|
||||||
}
|
final listMatch = regexLetter.allMatches(trim()).toList();
|
||||||
} else {
|
if (listMatch.length > 1) {
|
||||||
final regexLetter = RegExp("([A-Za-z])");
|
final firstLetter = listMatch[0].group(0);
|
||||||
final listMatch = regexLetter.allMatches(trim()).toList();
|
final secondLetter = listMatch[1].group(0);
|
||||||
if (listMatch.length > 1) {
|
return firstLetter != null && secondLetter != null
|
||||||
final firstLetter = listMatch[0].group(0);
|
? '${firstLetter.toUpperCase()}${secondLetter.toUpperCase()}'
|
||||||
final secondLetter = listMatch[1].group(0);
|
: '';
|
||||||
return firstLetter != null && secondLetter != null
|
} else {
|
||||||
? '${firstLetter.toUpperCase()}${secondLetter.toUpperCase()}'
|
final firstLetter = substring(0, length > 1 ? 2 : 1);
|
||||||
: '';
|
return firstLetter.toUpperCase();
|
||||||
} else {
|
}
|
||||||
final firstLetter = listMatch[0].group(0);
|
|
||||||
return firstLetter != null ? 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