diff --git a/lib/src/text/extension.dart b/lib/src/text/extension.dart new file mode 100644 index 0000000..b838c6a --- /dev/null +++ b/lib/src/text/extension.dart @@ -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 args) => StringUtils.textFormat(this, args); + + String? decorator(Map 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); +} diff --git a/lib/src/text/functions.dart b/lib/src/text/functions.dart new file mode 100644 index 0000000..0ff2f36 --- /dev/null +++ b/lib/src/text/functions.dart @@ -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 args) => + TextFormatter(text).format(args); + + /// Text decorator with custom key/value params + static String? decorator(String? text, Map 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(); + } +} diff --git a/lib/text.dart b/lib/text.dart index 1c00d45..49fd584 100644 --- a/lib/text.dart +++ b/lib/text.dart @@ -1,3 +1,5 @@ library cubetiq; export 'src/text/text_formatter.dart'; +export 'src/text/extension.dart'; +export 'src/text/functions.dart';