2021-06-01 23:35:56 +07:00
|
|
|
import 'text_formatter.dart';
|
|
|
|
|
|
|
|
class StringUtils {
|
|
|
|
/// Format number with precision
|
|
|
|
static String format(double n, [int precision = 2]) {
|
|
|
|
return n.toStringAsFixed(n.truncateToDouble() == n ? precision : precision);
|
|
|
|
}
|
|
|
|
|
2021-06-07 17:50:04 +07:00
|
|
|
/// Format number from String with precision
|
|
|
|
static String formatFromString(String? text, [int precision = 2]) {
|
|
|
|
text ??= '0.0';
|
|
|
|
var n = double.parse(text);
|
|
|
|
return n.toStringAsFixed(n.truncateToDouble() == n ? precision : precision);
|
|
|
|
}
|
|
|
|
|
2021-06-17 15:25:40 +07:00
|
|
|
/// Text formatter with custom args
|
|
|
|
static TextFormatter textFormatter(String? text, {bool translate = false}) =>
|
|
|
|
TextFormatter(text).translate(translate: translate);
|
|
|
|
|
2021-06-01 23:35:56 +07:00
|
|
|
/// Text format with custom args
|
2021-06-17 15:25:40 +07:00
|
|
|
static String? textFormat(String? text,
|
|
|
|
{List<dynamic>? args, bool translate = false}) =>
|
|
|
|
textFormatter(text, translate: translate).format(args: args);
|
2021-06-01 23:35:56 +07:00
|
|
|
|
|
|
|
/// Text decorator with custom key/value params
|
2021-06-17 15:25:40 +07:00
|
|
|
static String? decorator(String? text,
|
|
|
|
{Map<String, dynamic>? params, bool translate = false}) =>
|
|
|
|
textFormatter(text, translate: translate).decorate(params: params);
|
2021-06-01 23:35:56 +07:00
|
|
|
|
|
|
|
static String? asLowerCaseThenTrim(String? text) =>
|
|
|
|
text?.toLowerCase().trim();
|
|
|
|
static String? asUpperCaseThenTrim(String? text) =>
|
|
|
|
text?.toUpperCase().trim();
|
|
|
|
|
|
|
|
/// Check string is null or empty text
|
|
|
|
static bool isNullOrEmpty(String? text) {
|
|
|
|
if (text == null) return true;
|
|
|
|
return text.isEmpty;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Check string is null or empty or blank text
|
|
|
|
static bool isNullOrEmptyOrBlank(String? text) {
|
|
|
|
if (isNullOrEmpty(text)) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return text!.trim().isEmpty;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Check string pair is equals or not with ignore-case and trim
|
|
|
|
static bool equalsIgnoreCase(String? a, String? b, {trim = false}) {
|
|
|
|
if (a == b) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (trim == true) {
|
|
|
|
a = asLowerCaseThenTrim(a);
|
|
|
|
b = asLowerCaseThenTrim(b);
|
|
|
|
}
|
|
|
|
|
|
|
|
return a?.toLowerCase() == b?.toLowerCase();
|
|
|
|
}
|
|
|
|
}
|