2024-08-26 17:49:05 +00:00
|
|
|
import 'dart:math';
|
|
|
|
import 'package:uuid/uuid.dart';
|
|
|
|
|
|
|
|
abstract class PrimitiveUtils {
|
|
|
|
static bool containsTextInBracket(String matcher, String text) {
|
2024-08-27 06:48:31 +00:00
|
|
|
final allMatches = RegExp(r'(?<=\().+?(?=\))').allMatches(matcher);
|
2024-08-26 17:49:05 +00:00
|
|
|
if (allMatches.isEmpty) return false;
|
|
|
|
return allMatches
|
|
|
|
.map((e) => e.group(0))
|
|
|
|
.every((match) => match?.contains(text) ?? false);
|
|
|
|
}
|
|
|
|
|
|
|
|
static final Random _random = Random();
|
|
|
|
static T getRandomElement<T>(List<T> list) {
|
|
|
|
return list[_random.nextInt(list.length)];
|
|
|
|
}
|
|
|
|
|
|
|
|
static const uuid = Uuid();
|
|
|
|
|
|
|
|
static String toReadableNumber(double num) {
|
|
|
|
if (num > 999 && num < 99999) {
|
2024-08-27 06:48:31 +00:00
|
|
|
return '${(num / 1000).toStringAsFixed(0)}K';
|
2024-08-26 17:49:05 +00:00
|
|
|
} else if (num > 99999 && num < 999999) {
|
2024-08-27 06:48:31 +00:00
|
|
|
return '${(num / 1000).toStringAsFixed(0)}K';
|
2024-08-26 17:49:05 +00:00
|
|
|
} else if (num > 999999 && num < 999999999) {
|
2024-08-27 06:48:31 +00:00
|
|
|
return '${(num / 1000000).toStringAsFixed(0)}M';
|
2024-08-26 17:49:05 +00:00
|
|
|
} else if (num > 999999999) {
|
2024-08-27 06:48:31 +00:00
|
|
|
return '${(num / 1000000000).toStringAsFixed(0)}B';
|
2024-08-26 17:49:05 +00:00
|
|
|
} else {
|
|
|
|
return num.toStringAsFixed(0);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static Future<T> raceMultiple<T>(
|
|
|
|
Future<T> Function() inner, {
|
|
|
|
Duration timeout = const Duration(milliseconds: 2500),
|
|
|
|
int retryCount = 4,
|
|
|
|
}) async {
|
|
|
|
return Future.any(
|
|
|
|
List.generate(retryCount, (i) {
|
|
|
|
if (i == 0) return inner();
|
|
|
|
return Future.delayed(
|
|
|
|
Duration(milliseconds: timeout.inMilliseconds * i),
|
|
|
|
inner,
|
|
|
|
);
|
|
|
|
}),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
static String toSafeFileName(String str) {
|
|
|
|
return str.replaceAll(RegExp(r'[/\?%*:|"<>]'), ' ');
|
|
|
|
}
|
|
|
|
}
|