Task: Add text formatter functions and updated for configurable from the latest

functions with future and async support and add tests and examples
This commit is contained in:
2021-06-01 23:03:54 +07:00
parent 65e6aac985
commit 7ec5118598
12 changed files with 202 additions and 5 deletions

View File

@@ -4,6 +4,11 @@ import 'system_config.dart';
String? getConfigOrNull(String key, {String? defaultValue}) =>
SystemConfig.getOrNull(key, defaultValue: defaultValue);
/// get config or null from system config in async
Future<String?> getConfigOrNullAsync(String key,
{String? defaultValue}) async =>
await SystemConfig.getOrNullAsync(key, defaultValue: defaultValue);
/// get config with non-null from system config
String getConfig(String key, {String? defaultValue}) =>
SystemConfig.get(key, defaultValue: defaultValue);
@@ -11,5 +16,20 @@ String getConfig(String key, {String? defaultValue}) =>
/// check has config key or not from system config
bool hasConfigkey(String key) => SystemConfig.containsKey(key);
/// set config into memory
/// check has config key or not from system config in async function
Future<bool> hasConfigkeyAsync(String key) async =>
await SystemConfig.containsKeyAsync(key);
/// set config into memory or custom provider
void setConfig(String key, String? value) => SystemConfig.set(key, value);
/// set config into memory or custom provider in async function
Future<void> setConfigAsync(String key, String? value) async =>
await SystemConfig.setAsync(key, value);
/// remove config into memory or custom provider
void removeConfig(String key) => SystemConfig.remove(key);
/// remove config into memory or custom provider in async function
Future<void> removeConfigAsync(String key) async =>
await SystemConfig.removeAsync(key);

View File

@@ -8,4 +8,14 @@ abstract class ConfigurationProvider {
/// get or null in implemented classes
String? getOrNull(String key, {String? defaultValue});
/// check has key or not for async function
Future<bool> containsKeyAsync(String key) async {
return Future.value(containsKey(key));
}
/// get or null for async function
Future<String?> getOrNullAsync(String key, {String? defaultValue}) async {
return Future.value(getOrNull(key, defaultValue: defaultValue));
}
}

View File

@@ -4,16 +4,42 @@ import 'configuration_provider.dart';
///
/// @author sombochea
/// @since 1.0.3
abstract class MutableConfigurationProvider implements ConfigurationProvider {
abstract class MutableConfigurationProvider extends ConfigurationProvider {
/// Allow to set the value into config values
void set(String key, String? value);
/// Allow to set all values into config values
void setAll(Map<String, String?> configs);
void setAll(Map<String, String?> configs) {
configs.forEach((key, value) {
set(key, value);
});
}
/// Allow to remove the key from config values
void remove(String key);
/// Allow to remove all values from config values
void removeAll();
/// Allow to set the value into config values in async function
Future<void> setAsync(String key, String? value) async {
set(key, value);
}
/// Allow to set all values into config values in async function
Future<void> setAllAsync(Map<String, String?> configs) async {
configs.forEach((key, value) {
setAsync(key, value);
});
}
/// Allow to remove the key from config values in async function
Future<void> removeAsync(String key) async {
remove(key);
}
/// Allow to remove all values from config values in async function
Future<void> removeAllAsync() async {
removeAll();
}
}

View File

@@ -4,7 +4,7 @@ import 'mutable_configurable_provider.dart';
///
/// @author sombochea
/// @since 1.0.0
class SimpleConfigurationProvider implements MutableConfigurationProvider {
class SimpleConfigurationProvider extends MutableConfigurationProvider {
final Map<String, String?> _configs = {};
@override

View File

@@ -30,8 +30,15 @@ class SystemConfig {
return getProvider().getOrNull(key, defaultValue: defaultValue);
}
static Future<String?> getOrNullAsync(String key, {String? defaultValue}) {
return getProvider().getOrNullAsync(key, defaultValue: defaultValue);
}
static bool containsKey(String key) => getProvider().containsKey(key);
static Future<bool> containsKeyAsync(String key) =>
getProvider().containsKeyAsync(key);
/// Convert to mutable configuration provider
static MutableConfigurationProvider _getMutableConfigurationProvider() {
if (_provider is MutableConfigurationProvider) {
@@ -46,7 +53,31 @@ class SystemConfig {
_getMutableConfigurationProvider().set(key, value);
}
static Future<void> setAsync(String key, String? value) async {
await _getMutableConfigurationProvider().setAsync(key, value);
}
static void setAll(Map<String, String?> values) {
_getMutableConfigurationProvider().setAll(values);
}
static Future<void> setAllAsync(Map<String, String?> values) async {
await _getMutableConfigurationProvider().setAllAsync(values);
}
static void remove(String key) {
_getMutableConfigurationProvider().remove(key);
}
static Future<void> removeAsync(String key) async {
await _getMutableConfigurationProvider().removeAsync(key);
}
static void removeAll() {
_getMutableConfigurationProvider().removeAll();
}
static Future<void> removeAllAsync() async {
await _getMutableConfigurationProvider().removeAllAsync();
}
}

View File

@@ -0,0 +1,55 @@
/// Text Formatter
///
/// @author sombochea
/// @since 1.0.0
class TextFormatter {
String? text;
TextFormatter(String? text) {
this.text = text;
}
String? format(List<dynamic> args) {
if (text == null) {
return null;
}
if (args.isEmpty) {
return text;
}
var msg = text;
args.asMap().forEach((index, element) {
var _replaced = '';
if (element != null) {
_replaced = element.toString();
}
msg = msg?.replaceAll('{$index}', _replaced);
});
return msg;
}
String? decorate(Map<String, dynamic> params) {
if (text == null) {
return null;
}
if (params.isEmpty) {
return text;
}
var msg = text;
params.forEach((index, element) {
var _replaced = '';
if (element != null) {
_replaced = element.toString();
}
msg = msg?.replaceAll('{$index}', _replaced);
});
return msg;
}
}