From 7ec5118598655d496a95472f8bae7ffb3cf3d65f Mon Sep 17 00:00:00 2001 From: Sambo Chea Date: Tue, 1 Jun 2021 23:03:54 +0700 Subject: [PATCH] Task: Add text formatter functions and updated for configurable from the latest functions with future and async support and add tests and examples --- CHANGELOG.md | 3 +- LICENSE | 21 +++++++ example/cubetiq_text_formatter_example.dart | 14 +++++ lib/src/configurable/configurable.dart | 22 +++++++- .../configurable/configuration_provider.dart | 10 ++++ .../mutable_configurable_provider.dart | 30 +++++++++- .../simple_configuration_provider.dart | 2 +- lib/src/configurable/system_config.dart | 31 +++++++++++ lib/src/text/text_formatter.dart | 55 +++++++++++++++++++ lib/text.dart | 3 + pubspec.yaml | 1 + test/cubetiq_text_formatter_test.dart | 15 +++++ 12 files changed, 202 insertions(+), 5 deletions(-) create mode 100644 LICENSE create mode 100644 example/cubetiq_text_formatter_example.dart create mode 100644 lib/src/text/text_formatter.dart create mode 100644 lib/text.dart create mode 100644 test/cubetiq_text_formatter_test.dart diff --git a/CHANGELOG.md b/CHANGELOG.md index effe43c..93f1d8d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,4 @@ ## 1.0.0 -- Initial version. +- Configurable +- Text Formatter diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..09896ff --- /dev/null +++ b/LICENSE @@ -0,0 +1,21 @@ +MIT License + +Copyright (c) 2021 CUBETIQ Solution + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. diff --git a/example/cubetiq_text_formatter_example.dart b/example/cubetiq_text_formatter_example.dart new file mode 100644 index 0000000..bbeac3a --- /dev/null +++ b/example/cubetiq_text_formatter_example.dart @@ -0,0 +1,14 @@ +import 'package:cubetiq/text.dart'; + +void main(List args) { + var text1 = 'Hello, {0}, then do this it by {1}!'; + var text2 = 'Hello, {firstName}, then do this it by {lastName}!'; + var result1 = TextFormatter(text1).format(['Sambo', 'Chea']); + var result2 = TextFormatter(text2).decorate({ + 'firstName': 'Sambo', + 'lastName': 'Chea', + }); + + print('Result 1 => $result1'); + print('Result 2 => $result2'); +} diff --git a/lib/src/configurable/configurable.dart b/lib/src/configurable/configurable.dart index f3ff3ea..00eaa8b 100644 --- a/lib/src/configurable/configurable.dart +++ b/lib/src/configurable/configurable.dart @@ -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 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 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 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 removeConfigAsync(String key) async => + await SystemConfig.removeAsync(key); diff --git a/lib/src/configurable/configuration_provider.dart b/lib/src/configurable/configuration_provider.dart index 8b3883f..308df7c 100644 --- a/lib/src/configurable/configuration_provider.dart +++ b/lib/src/configurable/configuration_provider.dart @@ -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 containsKeyAsync(String key) async { + return Future.value(containsKey(key)); + } + + /// get or null for async function + Future getOrNullAsync(String key, {String? defaultValue}) async { + return Future.value(getOrNull(key, defaultValue: defaultValue)); + } } diff --git a/lib/src/configurable/mutable_configurable_provider.dart b/lib/src/configurable/mutable_configurable_provider.dart index 707a281..f280ce8 100644 --- a/lib/src/configurable/mutable_configurable_provider.dart +++ b/lib/src/configurable/mutable_configurable_provider.dart @@ -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 configs); + void setAll(Map 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 setAsync(String key, String? value) async { + set(key, value); + } + + /// Allow to set all values into config values in async function + Future setAllAsync(Map configs) async { + configs.forEach((key, value) { + setAsync(key, value); + }); + } + + /// Allow to remove the key from config values in async function + Future removeAsync(String key) async { + remove(key); + } + + /// Allow to remove all values from config values in async function + Future removeAllAsync() async { + removeAll(); + } } diff --git a/lib/src/configurable/simple_configuration_provider.dart b/lib/src/configurable/simple_configuration_provider.dart index 769f2ca..a6ac468 100644 --- a/lib/src/configurable/simple_configuration_provider.dart +++ b/lib/src/configurable/simple_configuration_provider.dart @@ -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 _configs = {}; @override diff --git a/lib/src/configurable/system_config.dart b/lib/src/configurable/system_config.dart index 2ebd241..78d7c02 100644 --- a/lib/src/configurable/system_config.dart +++ b/lib/src/configurable/system_config.dart @@ -30,8 +30,15 @@ class SystemConfig { return getProvider().getOrNull(key, defaultValue: defaultValue); } + static Future getOrNullAsync(String key, {String? defaultValue}) { + return getProvider().getOrNullAsync(key, defaultValue: defaultValue); + } + static bool containsKey(String key) => getProvider().containsKey(key); + static Future 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 setAsync(String key, String? value) async { + await _getMutableConfigurationProvider().setAsync(key, value); + } + static void setAll(Map values) { _getMutableConfigurationProvider().setAll(values); } + + static Future setAllAsync(Map values) async { + await _getMutableConfigurationProvider().setAllAsync(values); + } + + static void remove(String key) { + _getMutableConfigurationProvider().remove(key); + } + + static Future removeAsync(String key) async { + await _getMutableConfigurationProvider().removeAsync(key); + } + + static void removeAll() { + _getMutableConfigurationProvider().removeAll(); + } + + static Future removeAllAsync() async { + await _getMutableConfigurationProvider().removeAllAsync(); + } } diff --git a/lib/src/text/text_formatter.dart b/lib/src/text/text_formatter.dart new file mode 100644 index 0000000..a3871f1 --- /dev/null +++ b/lib/src/text/text_formatter.dart @@ -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 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 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; + } +} diff --git a/lib/text.dart b/lib/text.dart new file mode 100644 index 0000000..1c00d45 --- /dev/null +++ b/lib/text.dart @@ -0,0 +1,3 @@ +library cubetiq; + +export 'src/text/text_formatter.dart'; diff --git a/pubspec.yaml b/pubspec.yaml index 32692a3..3f5f4f9 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -2,6 +2,7 @@ name: cubetiq description: CUBETIQ Dart Shared is functions, utils, and extensions for developers. version: 1.0.0 homepage: https://www.cubetiqs.com +repository: https://git.cubetiqs.com/CUBETIQ/cubetiq_dart_shared.git environment: sdk: '>=2.12.0 <3.0.0' diff --git a/test/cubetiq_text_formatter_test.dart b/test/cubetiq_text_formatter_test.dart new file mode 100644 index 0000000..b7a9939 --- /dev/null +++ b/test/cubetiq_text_formatter_test.dart @@ -0,0 +1,15 @@ +import 'package:cubetiq/text.dart'; +import 'package:test/test.dart'; + +void main() { + test('text formatter function format', () { + var text1 = 'Hello, {0}!'; + var result1 = TextFormatter(text1).format(['Sambo']); + + var text2 = 'Hello, {name}!'; + var result2 = TextFormatter(text2).decorate({'name': 'Chea'}); + + expect('Hello, Sambo!', result1); + expect('Hello, Chea!', result2); + }); +}