Task: Add extensions for string nullable and nonull functions and text export for text formatter

This commit is contained in:
Sambo Chea 2021-06-01 23:35:56 +07:00
parent 90226fe52b
commit 881aaae3ad
3 changed files with 86 additions and 0 deletions

View File

@ -0,0 +1,35 @@
import 'functions.dart';
extension StringExtensionOnNonull on String {
String? get capitalized {
return (isNotEmpty) ? '${this[0].toUpperCase()}${substring(1)}' : this;
}
}
extension StringExtensionOnNullable on String? {
String? textFormat(List<dynamic> args) => StringUtils.textFormat(this, args);
String? decorator(Map<String, dynamic> params) =>
StringUtils.decorator(this, params);
bool get isBlank {
if (this == null) return true;
return this!.trim().isEmpty;
}
bool get isNotBlank {
return !isBlank;
}
bool get isNullOrEmpty {
return StringUtils.isNullOrEmpty(this);
}
bool get isNotNullOrEmpty {
return !isNullOrEmpty;
}
bool get isNullOrEmptyOrBlank => StringUtils.isNullOrEmptyOrBlank(this);
bool get isEqualsIgnoreCase => StringUtils.equalsIgnoreCase(this, this);
}

View File

@ -0,0 +1,49 @@
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);
}
/// Text format with custom args
static String? textFormat(String? text, List<dynamic> args) =>
TextFormatter(text).format(args);
/// Text decorator with custom key/value params
static String? decorator(String? text, Map<String, dynamic> params) =>
TextFormatter(text).decorate(params);
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();
}
}

View File

@ -1,3 +1,5 @@
library cubetiq; library cubetiq;
export 'src/text/text_formatter.dart'; export 'src/text/text_formatter.dart';
export 'src/text/extension.dart';
export 'src/text/functions.dart';