2025-09-29 23:01:09 +05:30
|
|
|
import 'dart:convert';
|
|
|
|
|
|
2026-06-10 11:05:07 +01:00
|
|
|
import 'package:diacritic/diacritic.dart' as diacritic;
|
|
|
|
|
|
2023-11-09 16:19:53 +00:00
|
|
|
extension StringExtension on String {
|
|
|
|
|
String capitalize() {
|
2025-07-29 00:34:03 +05:30
|
|
|
return split(" ").map((str) => str.isEmpty ? str : str[0].toUpperCase() + str.substring(1)).join(" ");
|
2023-11-09 16:19:53 +00:00
|
|
|
}
|
2026-05-18 21:52:42 +05:30
|
|
|
|
|
|
|
|
String? get nullIfEmpty => isEmpty ? null : this;
|
2026-06-10 11:05:07 +01:00
|
|
|
|
|
|
|
|
String removeDiacritics() => diacritic.removeDiacritics(this);
|
2023-11-09 16:19:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
extension DurationExtension on String {
|
2026-04-21 05:28:11 +02:00
|
|
|
/// Parses and returns the string of format HH:MM:SS.ffffff as a duration object else null
|
2023-11-09 16:19:53 +00:00
|
|
|
Duration? toDuration() {
|
|
|
|
|
try {
|
2026-04-21 05:28:11 +02:00
|
|
|
final parts = split(':');
|
|
|
|
|
final hours = double.parse(parts[0]).toInt();
|
|
|
|
|
final minutes = double.parse(parts[1]).toInt();
|
|
|
|
|
final secondsParts = parts[2].split('.');
|
|
|
|
|
final seconds = int.parse(secondsParts[0]);
|
|
|
|
|
final milliseconds = secondsParts.length > 1 ? (double.parse('0.${secondsParts[1]}') * 1000).round() : 0;
|
|
|
|
|
return Duration(hours: hours, minutes: minutes, seconds: seconds, milliseconds: milliseconds);
|
2023-11-09 16:19:53 +00:00
|
|
|
} catch (e) {
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
double toDouble() {
|
|
|
|
|
return double.parse(this);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int toInt() {
|
|
|
|
|
return int.parse(this);
|
|
|
|
|
}
|
|
|
|
|
}
|
2025-09-29 23:01:09 +05:30
|
|
|
|
|
|
|
|
Map<String, dynamic>? tryJsonDecode(dynamic json) {
|
|
|
|
|
try {
|
|
|
|
|
return jsonDecode(json) as Map<String, dynamic>;
|
|
|
|
|
} catch (e) {
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
}
|