Skip to content

Instantly share code, notes, and snippets.

@folaoluwafemi
Created May 10, 2023 14:31
Show Gist options
  • Save folaoluwafemi/0abf71ddb216c47e7848516219ed3393 to your computer and use it in GitHub Desktop.
Save folaoluwafemi/0abf71ddb216c47e7848516219ed3393 to your computer and use it in GitHub Desktop.
avian-epoch-8139
void main() {
print('compute line height ${computeTextSizeFromText('''
font-family: Poppins;
font-size: 25px;
font-weight: 500;
line-height: 38px;
letter-spacing: 0em;
text-align: left;
''')}',);
}
double computeTextSizeFromText(String text) {
final List<String> lines = text.split('\n');
late String lineHeight;
late String fontSize;
for (String line in lines) {
line = line.trim();
final List<String> keyValue = line.split(':');
if (keyValue.first.contains('font-size')) {
fontSize = keyValue.last.trim().removeAll(';').removeAll('px');
}
if (keyValue.first.contains('line-height')) {
lineHeight = keyValue.last.trim().removeAll(';').removeAll('px');
}
}
return double.parse(lineHeight)/double.parse(fontSize);
}
extension StringExtension on String {
String toFirstUpperCase() {
if (isEmpty) return '';
if (length == 1) return toUpperCase();
final List<String> charsList = chars;
final String first = charsList.first;
final String remainingChars = charsList.join().replaceFirst(first, '');
return '${first.toUpperCase()}$remainingChars';
}
String get cleanLower => trim().toLowerCase();
String get cleanUpper => trim().toUpperCase();
String removeAll(String pattern) {
return replaceAll(pattern, '');
}
String removeAtLast(String pattern) {
if (chars.last == pattern) {
return substring(0, length - 1);
}
return this;
}
String removeAllAtLast(List<String> patterns) {
String result = this;
for (final String pattern in patterns) {
result = result.removeAtLast(pattern);
return result;
}
return result;
}
List<String> get words => split(' ');
String toEachFirstUpperCase() {
if (isEmpty) return '';
if (length == 1) return toUpperCase();
final List<String> upperWords = words.map((e) {
return e.toFirstUpperCase();
}).toList();
return upperWords.join(' ');
}
List<String> get chars => split('');
}
extension NullableStringExtension on String? {
bool get isNullOrEmpty => this?.isEmpty ?? true;
bool get isNotNullOrEmpty => this?.isNotEmpty ?? false;
String? get nullIfEmpty => isNullOrEmpty ? null : this;
String get emptyIfNull => this == null ? '' : this!;
String? get cleanL => this?.trim().toLowerCase();
}
final List<String> upperAndLowerAlphabets = [
'A',
'B',
'C',
'D',
'E',
'F',
'G',
'H',
'I',
'J',
'K',
'L',
'M',
'N',
'O',
'P',
'Q',
'R',
'S',
'T',
'U',
'V',
'W',
'X',
'Y',
'Z',
//lower
'a',
'b',
'c',
'd',
'e',
'f',
'g',
'h',
'i',
'j',
'k',
'l',
'm',
'n',
'o',
'p',
'q',
'r',
's',
't',
'u',
'v',
'w',
'x',
'y',
'z'
];
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment