diff --git a/docs/CHANGELOG.md b/docs/CHANGELOG.md index b982bf0..ba4b507 100644 --- a/docs/CHANGELOG.md +++ b/docs/CHANGELOG.md @@ -1,23 +1,23 @@ -### **v.1.0.1.0823** +### **v1.0.1.0823** -> Was fixed tests and was added validator for input source.
-> Was added `ToEnum` from the string.
-> Was added check methods `IsTypeOfNullableInt`, `IsTypeOfFloatingPoint`, `IsTypeOfNullableFloatingPoint` from property type. -### **v.1.0.1.1842** +### **v1.0.1.1842** -> Was added new byte extension: `ToStringFromByteUnicode`, `ToHexByte`.
-> Was added new string extension: `ToBytesUnicode`. -### **v.1.0.2.1107** +### **v1.0.2.1107** -> Was added new DateTime extension: `StartOfWeek`, `EndOfWeek`, `StartOfMonth`, `EndOfMonth`, `StartOfPreviousMonth`, `EndOfPreviousMonth`, `StartOfYear`, `EndOfYear`, `DaysInMonth`, `DaysInYear`, `GetIso8601WeekOfYear`.
-> Was added new string extension: `ReplaceExact`.
-> Was added new Exception extension: `GetFullError`.
-> Was added new ExpandoObject extension: `AddProperty`, `UpdateValue`, `GetValue`. -### **v.1.0.2.1457** +### **v1.0.2.1457** -> Was added new string extension: `IfNullOrWhiteSpace`, `IfNullOrEmpty`.
-> Was added new TExtensions extension: `IfNotNull`. -### **v.1.0.3.0** +### **v1.0.3.0** -> Was renamed `Utils` to `GeneralUtils`.
-> Was added new `EnumerateUtils` with methods: `FromTo`, `FromTo` and `PowersOf`.
-> Was added new `DirectoryHelper` with methods: `CreateDirectory`, `CopyDirectory` and `DeleteDirectory`.
@@ -42,34 +42,34 @@ -> Was added new `EnumerableExtensions` with methods: `Replace`, `Join`, `IsLast`, `IsFirst`, `GetDifferences`, `ContainsAny`, `AnyStartWith` (input: IEnumerable, string), `ToObservableCollection`, `Randomize`, `Transpose`, `ToCollection`, `Combinations`, `ToDataTable`, `ToDataTableDynamic`, `IsNullOrEmptyEnumerable`, `WithIndex`, `ListToString`, `CloneCollection`, `NotNull`.
-> Was added new `ArrayExtensions` with methods: `IndexOf`.
-### **v.1.0.3.1101** +### **v1.0.3.1101** -> Was added new object extension: `SerializeToString` .
-> Was added new string extension: `DeserializeToObject` .
-> Was added new T extension: `SerializeToXmlDoc` .
-### **v.1.0.4.1925** +### **v1.0.4.1925** -> Added support for net framework.
-### **v.1.0.5.1849** +### **v1.0.5.1849** -> Added string extension `Contains`.
-> Added int/long extension `IsLessZero`.
-> Was added new Type extension: `GetStringPropertyNames`, `GetStringPropertyInfos`, `GetPropertyInfos`.
-### **v.1.0.5.2131** +### **v1.0.5.2131** -> Added string extension `ParseToInt`, `ParseNullableInt`, `TryParseInt`.
-> Added list extension `ActionForEach`.
-### **v.1.0.6.1341** +### **v1.0.6.1341** -> Update vulnerable library version.
-### **v.1.0.7.0535** +### **v1.0.7.0535** -> Add in directory helper new methods: `FileCount` x3, `DirectoryFileCount`.
-### **v.1.0.8.0638** +### **v1.0.8.0638** -> Added string extension `GetHashSha512String`, `FromSpaceSeparatedString`, `IsMissing`, `IsNullOrEmpty`, `AddQueryString`, `AddHashFragment`, `GetOrigin`, `Obfuscate`.
-> Added Enumerable extension `ToSpaceSeparatedString`, `HasDuplicates`, `GetDuplicates`.
-### **v.1.0.9.2108** +### **v1.0.9.2108** -> Added object extensions `ThrowIfArgNull`, `ThrowArgIfNull`.
-> Added bool extensions `IsTrue`, `IsFalse`.
-> Added null check extensions `IsNotNull`, `IsDbNull`.
@@ -77,26 +77,31 @@ -> Adjust validation for input params at some methods. -> Small code refactor. -### **v.1.0.10.2315** +### **v1.0.10.2315** -> Update lib version. Add option to sign the new version of the files.
-> Small code refactor. -### **v.1.0.11.1319** +### **v1.0.11.1319** -> Fix wrong modification.
-### **v.1.0.12.1447** +### **v1.0.12.1447** -> Add IDataReader extensions to convert object in specific type.
-### **v.1.0.13.8399** +### **v1.0.13.8399** -> Add excel column name generator `GetExcelColumnName`.
-> Adjust method modifier for `GetDuplicates`.
-> Fix tests. -### **v.1.0.14.6517** +### **v1.0.14.6517** -> Fix some enums extensions.
--> Add new methods (`AppendTo`, `GetPropertiesInfoFromSource`) in 'TExtensions'. +-> Add new methods (`AppendTo`, `GetPropertiesInfoFromSource`) in `TExtensions`. -### **v.1.1.0.0** +### **v1.1.0.0** -> Remove unused packages.
-> Downgrade some package versions to cover target frameworks.
--> Fix some warnings and disposable objects.
\ No newline at end of file +-> Fix some warnings and disposable objects.
+ +### **v1.1.1.7310** +-> Adjust and clean up code execution.
+-> Reorganize typeparam extensions.
+-> Add new typeparam extensions: `IfIsNull`, `IfIsNotNull`, `IfIsNullOrFuncIsTrue`, `IfIsNullAndFuncIsTrue`, `IfFuncIsTrue`, `IfFuncIsFalse`, `IfFunc`, `IfNull`, `IfNotNull`.
\ No newline at end of file diff --git a/src/DomainCommonExtensions/ArraysExtensions/ArrayExtensions.cs b/src/DomainCommonExtensions/ArraysExtensions/ArrayExtensions.cs index 2fa2744..ac3d01c 100644 --- a/src/DomainCommonExtensions/ArraysExtensions/ArrayExtensions.cs +++ b/src/DomainCommonExtensions/ArraysExtensions/ArrayExtensions.cs @@ -14,10 +14,6 @@ // // *********************************************************************** -#region U S A G E S - -#endregion - namespace DomainCommonExtensions.ArraysExtensions { /// diff --git a/src/DomainCommonExtensions/ArraysExtensions/DictionaryExtensions.cs b/src/DomainCommonExtensions/ArraysExtensions/DictionaryExtensions.cs index cca3c5a..6811bc4 100644 --- a/src/DomainCommonExtensions/ArraysExtensions/DictionaryExtensions.cs +++ b/src/DomainCommonExtensions/ArraysExtensions/DictionaryExtensions.cs @@ -19,6 +19,7 @@ using System; using System.Collections; using System.Collections.Generic; +using DomainCommonExtensions.DataTypeExtensions; #endregion @@ -42,7 +43,7 @@ public static Dictionary AddRange(this Dictionary(); foreach (var item in newItems) - if (context.ContainsKey(item.Key)) + if (context.ContainsKey(item.Key).IsTrue()) context[item.Key] = item.Value; else context.Add(item.Key, item.Value); @@ -62,7 +63,7 @@ public static Dictionary RemoveKeys(this Dictionary< IEnumerable keys) { foreach (var key in keys) - if (dict.ContainsKey(key)) + if (dict.ContainsKey(key).IsTrue()) dict.Remove(key); return dict; @@ -94,7 +95,7 @@ public static int IndexOf(this IDictionary dictionary, object value) public static string GetStringOrDefault(this Dictionary dictionary, string key, string @default = default) { - if (dictionary.ContainsKey(key)) return dictionary[key]; + if (dictionary.ContainsKey(key).IsTrue()) return dictionary[key]; return @default; } @@ -110,7 +111,7 @@ public static string GetStringOrDefault(this Dictionary dictiona public static Guid GetGuidOrDefault(this Dictionary dictionary, string key, Guid @default = default) { - if (dictionary.ContainsKey(key)) + if (dictionary.ContainsKey(key).IsTrue()) if (Guid.TryParse(dictionary[key], out var value)) return value; @@ -128,7 +129,7 @@ public static Guid GetGuidOrDefault(this Dictionary dictionary, public static int GetIntOrDefault(this Dictionary dictionary, string key, int @default = default) { - if (dictionary.ContainsKey(key)) + if (dictionary.ContainsKey(key).IsTrue()) if (int.TryParse(dictionary[key], out var value)) return value; @@ -146,7 +147,7 @@ public static int GetIntOrDefault(this Dictionary dictionary, st public static bool GetBoolOrDefault(this Dictionary dictionary, string key, bool @default = default) { - if (dictionary.ContainsKey(key)) + if (dictionary.ContainsKey(key).IsTrue()) if (bool.TryParse(dictionary[key], out var value)) return value; diff --git a/src/DomainCommonExtensions/ArraysExtensions/EnumerableExtensions.cs b/src/DomainCommonExtensions/ArraysExtensions/EnumerableExtensions.cs index cf85129..c8690c8 100644 --- a/src/DomainCommonExtensions/ArraysExtensions/EnumerableExtensions.cs +++ b/src/DomainCommonExtensions/ArraysExtensions/EnumerableExtensions.cs @@ -23,6 +23,8 @@ using System.Text; using DomainCommonExtensions.CommonExtensions; using System.Data; +using DomainCommonExtensions.CommonExtensions.TypeParam; +using DomainCommonExtensions.DataTypeExtensions; namespace DomainCommonExtensions.ArraysExtensions { @@ -194,9 +196,9 @@ public static IEnumerable Randomize(this IEnumerable target) public static IEnumerable> Transpose(this IEnumerable> collection) { var values = collection?.ToList() ?? new List>(); - if (!values.Any()) + if (values.IsNullOrEmptyEnumerable()) return values; - if (!values.First().Any()) + if (values.First().IsNullOrEmptyEnumerable()) return Transpose(values.Skip(1)); var x = values.First().First(); @@ -240,8 +242,8 @@ public static Collection ToCollection(this IEnumerable enumerable) public static IEnumerable> Combinations(this IEnumerable source, int select, bool repetition = false) { - Contract.Requires(source != null); - Contract.Requires(select >= 0); + Contract.Requires(source.IsNotNull()); + Contract.Requires(select.IsGreaterThanOrEqualZero()); var enumerable = source.ToList(); @@ -351,7 +353,7 @@ public static string ListToString(this IEnumerable list, string delimiter) // ReSharper disable PossibleMultipleEnumeration var result = new StringBuilder(); - if (!list.IsNull() && list.Any()) + if (list.IsNotNull() && list.Any()) { var notFirst = false; foreach (var item in list) diff --git a/src/DomainCommonExtensions/CommonExtensions/ExceptionExtensions.cs b/src/DomainCommonExtensions/CommonExtensions/ExceptionExtensions.cs index 0af32d2..fda741c 100644 --- a/src/DomainCommonExtensions/CommonExtensions/ExceptionExtensions.cs +++ b/src/DomainCommonExtensions/CommonExtensions/ExceptionExtensions.cs @@ -68,7 +68,7 @@ public static string GetFullError(this Exception ex, bool showCallStack = true) /// public static Exception WithData(this Exception ex, object dataKey, object dataValue) { - if (!ex.Data.IsNull()) ex.Data[dataKey] = dataValue; + if (ex.Data.IsNotNull()) ex.Data[dataKey] = dataValue; return ex; } diff --git a/src/DomainCommonExtensions/CommonExtensions/ExpandoObjectExtensions.cs b/src/DomainCommonExtensions/CommonExtensions/ExpandoObjectExtensions.cs index 8f807cc..5b6a5f3 100644 --- a/src/DomainCommonExtensions/CommonExtensions/ExpandoObjectExtensions.cs +++ b/src/DomainCommonExtensions/CommonExtensions/ExpandoObjectExtensions.cs @@ -17,6 +17,7 @@ using System.Collections.Generic; using System.Dynamic; using System.Linq; +using DomainCommonExtensions.DataTypeExtensions; namespace DomainCommonExtensions.CommonExtensions { @@ -37,7 +38,7 @@ public static void AddProperty(this ExpandoObject expando, string propertyName, { // ExpandoObject supports IDictionary so we can extend it like this var expandoDict = expando as IDictionary; - if (expandoDict.ContainsKey(propertyName)) + if (expandoDict.ContainsKey(propertyName).IsTrue()) expandoDict[propertyName] = propertyValue; else expandoDict.Add(propertyName, propertyValue); @@ -53,7 +54,7 @@ public static void AddProperty(this ExpandoObject expando, string propertyName, public static void UpdateValue(this ExpandoObject expando, string propertyName, object propertyValue) { var map = (IDictionary)expando; - if (map.ContainsKey(propertyName)) + if (map.ContainsKey(propertyName).IsTrue()) map[propertyName] = propertyValue; foreach (var val in map.Values) { diff --git a/src/DomainCommonExtensions/CommonExtensions/ExpressionExtensions.cs b/src/DomainCommonExtensions/CommonExtensions/ExpressionExtensions.cs index c7b623e..dea21a2 100644 --- a/src/DomainCommonExtensions/CommonExtensions/ExpressionExtensions.cs +++ b/src/DomainCommonExtensions/CommonExtensions/ExpressionExtensions.cs @@ -58,7 +58,6 @@ public static Expression> AndAlso( Expression.Invoke(expr2, param)), param); } - /// /// Convert all expressions to 'AND' clause /// diff --git a/src/DomainCommonExtensions/CommonExtensions/FilesExtensions.cs b/src/DomainCommonExtensions/CommonExtensions/FilesExtensions.cs index fd178c0..5a128eb 100644 --- a/src/DomainCommonExtensions/CommonExtensions/FilesExtensions.cs +++ b/src/DomainCommonExtensions/CommonExtensions/FilesExtensions.cs @@ -17,6 +17,7 @@ #region U S A G E S using System.IO; +using DomainCommonExtensions.DataTypeExtensions; #endregion @@ -41,7 +42,7 @@ public static bool IsFileInUse(this string path) using var fs = new FileStream(path, FileMode.OpenOrCreate); var canWrite = fs.CanWrite; - return canWrite != true; + return canWrite.IsFalse(); } catch (IOException) { diff --git a/src/DomainCommonExtensions/CommonExtensions/NullExtensions.cs b/src/DomainCommonExtensions/CommonExtensions/NullExtensions.cs index c1d1ecc..f6f6c04 100644 --- a/src/DomainCommonExtensions/CommonExtensions/NullExtensions.cs +++ b/src/DomainCommonExtensions/CommonExtensions/NullExtensions.cs @@ -40,13 +40,13 @@ public static bool IsNull(this object obj) /// /// Check if KeyValue is null /// - /// - /// + /// + /// /// /// - public static bool IsNull(this KeyValuePair source) + public static bool IsNull(this KeyValuePair source) { - return source.Equals(default(KeyValuePair)); + return source.Equals(default(KeyValuePair)); } /// diff --git a/src/DomainCommonExtensions/CommonExtensions/TypeParam/TypeParamActionExtensions.cs b/src/DomainCommonExtensions/CommonExtensions/TypeParam/TypeParamActionExtensions.cs new file mode 100644 index 0000000..6cbe05b --- /dev/null +++ b/src/DomainCommonExtensions/CommonExtensions/TypeParam/TypeParamActionExtensions.cs @@ -0,0 +1,74 @@ +// *********************************************************************** +// Assembly : RzR.Shared.Extensions.DomainCommonExtensions +// Author : RzR +// Created On : 2024-02-13 19:42 +// +// Last Modified By : RzR +// Last Modified On : 2024-02-13 20:02 +// *********************************************************************** +// +// Copyright (c) RzR. All rights reserved. +// +// +// +// +// *********************************************************************** + +#region U S A G E S + +using System; + +#endregion + +namespace DomainCommonExtensions.CommonExtensions.TypeParam +{ + /// ------------------------------------------------------------------------------------------------- + /// + /// T type extensions. + /// + /// ================================================================================================= + public static partial class TExtensions + { + /// ------------------------------------------------------------------------------------------------- + /// + /// Return data if not null. + /// + /// + /// Thrown when one or more required arguments are null. + /// + /// Input type. + /// Source data. + /// Action. + /// ================================================================================================= + public static void IfNotNull(this TInput source, Action action) + { + if (action.IsNull()) + throw new ArgumentNullException(nameof(action)); + + if (source.IsNull()) + return; + + action(source); + } + + /// ------------------------------------------------------------------------------------------------- + /// + /// Return data if null. + /// + /// + /// Thrown when one or more required arguments are null. + /// + /// Input type. + /// Source data. + /// Action. + /// ================================================================================================= + public static void IfNull(this TInput source, Action action) + { + if (action.IsNull()) + throw new ArgumentNullException(nameof(action)); + + if (source.IsNull()) + action(source); + } + } +} \ No newline at end of file diff --git a/src/DomainCommonExtensions/CommonExtensions/TExtensions.cs b/src/DomainCommonExtensions/CommonExtensions/TypeParam/TypeParamExtensions.cs similarity index 72% rename from src/DomainCommonExtensions/CommonExtensions/TExtensions.cs rename to src/DomainCommonExtensions/CommonExtensions/TypeParam/TypeParamExtensions.cs index 66dbeb8..eb47bea 100644 --- a/src/DomainCommonExtensions/CommonExtensions/TExtensions.cs +++ b/src/DomainCommonExtensions/CommonExtensions/TypeParam/TypeParamExtensions.cs @@ -16,7 +16,6 @@ #region U S A G E S -using DomainCommonExtensions.DataTypeExtensions; using System; using System.Collections.Generic; using System.IO; @@ -26,19 +25,20 @@ using System.Runtime.Serialization.Formatters.Binary; using System.Xml; using CodeSource; +using DomainCommonExtensions.DataTypeExtensions; #pragma warning disable SCS0007 #endregion -namespace DomainCommonExtensions.CommonExtensions +namespace DomainCommonExtensions.CommonExtensions.TypeParam { // ReSharper disable once InconsistentNaming /// /// T type extensions /// /// - public static class TExtensions + public static partial class TExtensions { /// /// Cloning an object @@ -65,63 +65,6 @@ public static T Clone(this T source) } } - /// - /// Return data if not null - /// - /// Source data - /// Action - /// - /// Input type - /// Result type - /// - public static TResult IfNotNull(this TInput source, Func action) - { - if (action.IsNull()) - throw new ArgumentNullException(nameof(action)); - - return !source.IsNull() - ? action(source) - : default; - } - - /// - /// Return data if not null - /// - /// Source data - /// Action - /// Default result value - /// - /// Input type - /// Result type - /// - public static TResult IfNotNull(this TInput source, Func action, TResult defaultValue) - { - if (action.IsNull()) - throw new ArgumentNullException(nameof(action)); - - return !source.IsNull() - ? action(source) - : defaultValue; - } - - /// - /// Return data if not null - /// - /// Source data - /// Action - /// Input type - /// - public static void IfNotNull(this TInput source, Action action) - { - if (action.IsNull()) - throw new ArgumentNullException(nameof(action)); - - if (source.IsNull()) - return; - - action(source); - } - /// /// Serialize source data to XML document /// @@ -185,5 +128,43 @@ public static IList GetPropertiesInfoFromSource(this TSou } catch { return null; } } + + /// ------------------------------------------------------------------------------------------------- + /// + /// A TSource extension method that if is null. + /// + /// Type of the source. + /// Source data. + /// Default result value. + /// + /// A TSource. + /// + /// ================================================================================================= + public static TSource IfIsNull(this TSource source, TSource defaultValue) + { + if (source.IsNull()) + return defaultValue; + + return source; + } + + /// ------------------------------------------------------------------------------------------------- + /// + /// A TSource extension method that if is not null. + /// + /// Type of the source. + /// Source data. + /// Default result value. + /// + /// A TSource. + /// + /// ================================================================================================= + public static TSource IfIsNotNull(this TSource source, TSource defaultValue) + { + if (source.IsNotNull()) + return defaultValue; + + return source; + } } } \ No newline at end of file diff --git a/src/DomainCommonExtensions/CommonExtensions/TypeParam/TypeParamFuncExtensions.cs b/src/DomainCommonExtensions/CommonExtensions/TypeParam/TypeParamFuncExtensions.cs new file mode 100644 index 0000000..18c5412 --- /dev/null +++ b/src/DomainCommonExtensions/CommonExtensions/TypeParam/TypeParamFuncExtensions.cs @@ -0,0 +1,325 @@ +// *********************************************************************** +// Assembly : RzR.Shared.Extensions.DomainCommonExtensions +// Author : RzR +// Created On : 2024-02-13 19:30 +// +// Last Modified By : RzR +// Last Modified On : 2024-02-13 19:35 +// *********************************************************************** +// +// Copyright (c) RzR. All rights reserved. +// +// +// +// +// *********************************************************************** + +#region U S A G E S + +using System; +using DomainCommonExtensions.DataTypeExtensions; + +#endregion + +namespace DomainCommonExtensions.CommonExtensions.TypeParam +{ + /// ------------------------------------------------------------------------------------------------- + /// + /// T type extensions. + /// + /// ================================================================================================= + public static partial class TExtensions + { + /// ------------------------------------------------------------------------------------------------- + /// + /// Return data if not null. + /// + /// + /// Thrown when one or more required arguments are null. + /// + /// Input type. + /// Result type. + /// Source data. + /// Function. + /// + /// A TResult. + /// + /// ================================================================================================= + public static TResult IfNotNull(this TInput source, Func func) + { + if (func.IsNull()) + throw new ArgumentNullException(nameof(func)); + + return source.IsNotNull() + ? func(source) + : default; + } + + /// ------------------------------------------------------------------------------------------------- + /// + /// Return data if not null. + /// + /// + /// Thrown when one or more required arguments are null. + /// + /// Input type. + /// Result type. + /// Source data. + /// Function. + /// Default result value. + /// + /// A TResult. + /// + /// ================================================================================================= + public static TResult IfNotNull(this TInput source, Func func, + TResult defaultValue) + { + if (func.IsNull()) + throw new ArgumentNullException(nameof(func)); + + return source.IsNotNull() + ? func(source) + : defaultValue; + } + + /// ------------------------------------------------------------------------------------------------- + /// + /// Return data if null. + /// + /// Type of the source. + /// Source data. + /// Default result value. + /// The function. + /// + /// A TSource. + /// + /// ================================================================================================= + public static TSource IfNull(this TSource source, TSource defaultValue, Func func) + { + if (func.IsNull()) + throw new ArgumentNullException(nameof(func)); + + if (source.IsNull() && func.Invoke().IsTrue()) + return defaultValue; + + return source; + } + + /// ------------------------------------------------------------------------------------------------- + /// + /// Return data if not null. + /// + /// Type of the source. + /// Source data. + /// Default result value. + /// The function. + /// + /// A TResult. + /// + /// ================================================================================================= + public static TSource IfNotNull(this TSource source, TSource defaultValue, Func func) + { + if (func.IsNull()) + throw new ArgumentNullException(nameof(func)); + + if (source.IsNotNull() && func.Invoke().IsTrue()) + return defaultValue; + + return source; + } + + /// ------------------------------------------------------------------------------------------------- + /// + /// Return data if not null. + /// + /// Type of the source. + /// Source data. + /// Default result value. + /// The function. + /// True if is or operator, false if not. + /// + /// A TResult. + /// + /// ================================================================================================= + public static TSource IfNotNull(this TSource source, TSource defaultValue, Func func, bool isOrOperator) + { + if (func.IsNull()) + throw new ArgumentNullException(nameof(func)); + + if (isOrOperator.IsTrue()) + { + if (source.IsNotNull() || func.Invoke().IsTrue()) + return defaultValue; + } + else + { + if (source.IsNotNull() && func.Invoke().IsTrue()) + return defaultValue; + } + + return source; + } + + /// ------------------------------------------------------------------------------------------------- + /// + /// Return data if null. + /// + /// Type of the source. + /// Source data. + /// Default result value. + /// The function. + /// True if is or operator, false if not. + /// True to function result. + /// + /// A TSource. + /// + /// ================================================================================================= + public static TSource IfNull(this TSource source, TSource defaultValue, Func func, bool isOrOperator, bool funcResult) + { + if (func.IsNull()) + throw new ArgumentNullException(nameof(func)); + + if (isOrOperator.IsTrue()) + { + if (source.IsNull() || func.Invoke() == funcResult) + return defaultValue; + } + else + { + if (source.IsNull() && func.Invoke() == funcResult) + return defaultValue; + } + + return source; + } + + /// ------------------------------------------------------------------------------------------------- + /// + /// A TSource extension method that if is null or function is true. + /// + /// Type of the source. + /// Source data. + /// Default result value. + /// The function. + /// + /// A TSource. + /// + /// ================================================================================================= + public static TSource IfIsNullOrFuncIsTrue(this TSource source, TSource defaultValue, Func func) + { + if (func.IsNull()) + throw new ArgumentNullException(nameof(func)); + + if (source.IsNull() || func.Invoke().IsTrue()) + return defaultValue; + + return source; + } + + /// ------------------------------------------------------------------------------------------------- + /// + /// A TSource extension method that if is null and function is true. + /// + /// + /// Thrown when one or more required arguments are null. + /// + /// Type of the source. + /// Source data. + /// Default result value. + /// The function. + /// + /// A TSource. + /// + /// ================================================================================================= + public static TSource IfIsNullAndFuncIsTrue(this TSource source, TSource defaultValue, Func func) + { + if (func.IsNull()) + throw new ArgumentNullException(nameof(func)); + + if (source.IsNull() && func.Invoke().IsTrue()) + return defaultValue; + + return source; + } + + /// ------------------------------------------------------------------------------------------------- + /// + /// A TSource extension method that if function is true. + /// + /// + /// Thrown when one or more required arguments are null. + /// + /// Type of the source. + /// Source data. + /// Default result value. + /// The function. + /// + /// A TSource. + /// + /// ================================================================================================= + public static TSource IfFuncIsTrue(this TSource source, TSource defaultValue, Func func) + { + if (func.IsNull()) + throw new ArgumentNullException(nameof(func)); + + if (func.Invoke().IsTrue()) + return defaultValue; + + return source; + } + + /// ------------------------------------------------------------------------------------------------- + /// + /// A TSource extension method that if function is false. + /// + /// + /// Thrown when one or more required arguments are null. + /// + /// Type of the source. + /// Source data. + /// Default result value. + /// The function. + /// + /// A TSource. + /// + /// ================================================================================================= + public static TSource IfFuncIsFalse(this TSource source, TSource defaultValue, Func func) + { + if (func.IsNull()) + throw new ArgumentNullException(nameof(func)); + + if (func.Invoke().IsFalse()) + return defaultValue; + + return source; + } + + /// ------------------------------------------------------------------------------------------------- + /// + /// A TSource extension method that if function. + /// + /// + /// Thrown when one or more required arguments are null. + /// + /// Type of the source. + /// Source data. + /// Default result value. + /// The function. + /// True to function result. + /// + /// A TSource. + /// + /// ================================================================================================= + public static TSource IfFunc(this TSource source, TSource defaultValue, Func func, + bool funcResult) + { + if (func.IsNull()) + throw new ArgumentNullException(nameof(func)); + + if (func.Invoke() == funcResult) + return defaultValue; + + return source; + } + } +} \ No newline at end of file diff --git a/src/DomainCommonExtensions/DataTypeExtensions/DateTimeExtensions.cs b/src/DomainCommonExtensions/DataTypeExtensions/DateTimeExtensions.cs index f4e4738..3efb42b 100644 --- a/src/DomainCommonExtensions/DataTypeExtensions/DateTimeExtensions.cs +++ b/src/DomainCommonExtensions/DataTypeExtensions/DateTimeExtensions.cs @@ -169,7 +169,7 @@ public static int CalculateAge(this DateTime input) /// /// Current DateTime value /// Return DateTime.Now in case when - /// source + /// input /// is null /// /// diff --git a/src/DomainCommonExtensions/DataTypeExtensions/DecimalExtensions.cs b/src/DomainCommonExtensions/DataTypeExtensions/DecimalExtensions.cs index 541c4b7..13e9d93 100644 --- a/src/DomainCommonExtensions/DataTypeExtensions/DecimalExtensions.cs +++ b/src/DomainCommonExtensions/DataTypeExtensions/DecimalExtensions.cs @@ -32,7 +32,7 @@ public static class DecimalExtensions /// public static bool IsNullOrZero(this decimal? value) { - return value.IsNull() || value == 0; + return value.IsNull() || value.IsZero(); } /// @@ -46,6 +46,17 @@ public static bool IsZero(this decimal value) return value == 0; } + /// + /// Check if the value is == 0 + /// + /// Input value to check + /// + /// + public static bool IsZero(this decimal? value) + { + return value == 0; + } + /// /// Check if the value is less or equal with 0 /// diff --git a/src/DomainCommonExtensions/DataTypeExtensions/EnumExtensions.cs b/src/DomainCommonExtensions/DataTypeExtensions/EnumExtensions.cs index fe2545e..944aa66 100644 --- a/src/DomainCommonExtensions/DataTypeExtensions/EnumExtensions.cs +++ b/src/DomainCommonExtensions/DataTypeExtensions/EnumExtensions.cs @@ -19,6 +19,7 @@ using System; using System.Collections.Generic; using System.Linq; +using DomainCommonExtensions.CommonExtensions; #if NET45_OR_GREATER || NET || NETSTANDARD1_0_OR_GREATER using System.ComponentModel; @@ -44,7 +45,7 @@ public static class EnumExtensions /// public static Dictionary GetEnumDefinition(this Type enumType) { - if (!enumType.IsEnum) throw new Exception("Non valid enum"); + if (enumType.IsEnum.IsFalse()) throw new Exception("Non valid enum"); return Enum.GetNames(enumType).ToDictionary(x => (int)Enum.Parse(enumType, x), x => x); } @@ -97,7 +98,7 @@ public static T ToEnumMemberValue(this string str) where T : struct, Enum, IC /// public static int ToInt(this T source) where T : Enum, IConvertible { - if (!typeof(T).IsEnum) + if (typeof(T).IsEnum.IsFalse()) throw new ArgumentException("T must be an enumerated type"); return (int)(IConvertible)source; @@ -112,7 +113,7 @@ public static int ToInt(this T source) where T : Enum, IConvertible /// public static string ToString(this T source) where T : Enum, IConvertible { - if (!typeof(T).IsEnum) + if (typeof(T).IsEnum.IsFalse()) throw new ArgumentException("T must be an enumerated type"); return (string)(IConvertible)source; @@ -146,7 +147,7 @@ public static string GetDescription(this Enum value, bool returnEmpty = false) /// public static T GetEnumValue(this string str) where T : struct, Enum, IConvertible { - if (!typeof(T).IsEnum) throw new Exception("T must be an Enumeration type."); + if (typeof(T).IsEnum.IsFalse()) throw new Exception("T must be an Enumeration type."); var val = ((T[])Enum.GetValues(typeof(T)))[0]; if (!string.IsNullOrEmpty(str)) foreach (var enumValue in (T[])Enum.GetValues(typeof(T))) @@ -168,7 +169,7 @@ public static T GetEnumValue(this string str) where T : struct, Enum, IConver /// public static T GetEnumValue(this int intValue) where T : struct, Enum, IConvertible { - if (!typeof(T).IsEnum) throw new Exception("T must be an Enumeration type."); + if (typeof(T).IsEnum.IsFalse()) throw new Exception("T must be an Enumeration type."); var val = ((T[])Enum.GetValues(typeof(T)))[0]; foreach (var enumValue in (T[])Enum.GetValues(typeof(T))) @@ -193,10 +194,10 @@ public static string GetDisplayName(this Enum value) var type = value.GetType(); var memberInfo = type.GetMember(value.ToString()).FirstOrDefault(); - if (memberInfo != null) + if (memberInfo.IsNotNull()) { var attribute = (DisplayAttribute) - memberInfo.GetCustomAttributes(typeof(DisplayAttribute), false) + memberInfo!.GetCustomAttributes(typeof(DisplayAttribute), false) .FirstOrDefault(); return attribute?.Name ?? value.ToString(); } @@ -214,10 +215,10 @@ public static string GetDisplayDescription(this Enum value) var type = value.GetType(); var memberInfo = type.GetMember(value.ToString()).FirstOrDefault(); - if (memberInfo != null) + if (memberInfo.IsNotNull()) { var attribute = (DisplayAttribute) - memberInfo.GetCustomAttributes(typeof(DisplayAttribute), false) + memberInfo!.GetCustomAttributes(typeof(DisplayAttribute), false) .FirstOrDefault(); return attribute?.Description ?? value.ToString(); } diff --git a/src/DomainCommonExtensions/DataTypeExtensions/IntExtensions.cs b/src/DomainCommonExtensions/DataTypeExtensions/IntExtensions.cs index 17c433e..553c04c 100644 --- a/src/DomainCommonExtensions/DataTypeExtensions/IntExtensions.cs +++ b/src/DomainCommonExtensions/DataTypeExtensions/IntExtensions.cs @@ -34,7 +34,7 @@ public static class IntExtensions /// public static bool IsNullOrZero(this int? value) { - return value.IsNull() || value == 0; + return value.IsNull() || value.IsZero(); } /// @@ -48,6 +48,17 @@ public static bool IsZero(this int value) return value == 0; } + /// + /// Check if the value is == 0 + /// + /// Input value to check + /// + /// + public static bool IsZero(this int? value) + { + return value == 0; + } + /// /// Check if the value is less or equal with 0 /// diff --git a/src/DomainCommonExtensions/DataTypeExtensions/LongExtensions.cs b/src/DomainCommonExtensions/DataTypeExtensions/LongExtensions.cs index 09744d3..9a4b8e3 100644 --- a/src/DomainCommonExtensions/DataTypeExtensions/LongExtensions.cs +++ b/src/DomainCommonExtensions/DataTypeExtensions/LongExtensions.cs @@ -46,6 +46,17 @@ public static bool IsZero(this long value) return value == 0; } + /// + /// Check if the value is == 0 + /// + /// Input value to check + /// + /// + public static bool IsZero(this long? value) + { + return value == 0; + } + /// /// Check if the value is less or equal with 0 /// diff --git a/src/DomainCommonExtensions/DataTypeExtensions/ObjectExtensions.cs b/src/DomainCommonExtensions/DataTypeExtensions/ObjectExtensions.cs index c2af395..dc9b4e3 100644 --- a/src/DomainCommonExtensions/DataTypeExtensions/ObjectExtensions.cs +++ b/src/DomainCommonExtensions/DataTypeExtensions/ObjectExtensions.cs @@ -70,6 +70,7 @@ public static bool TryCast(this object obj, out T result) if (obj is T) { result = (T)obj; + return true; } diff --git a/src/shared/GeneralAssemblyInfo.cs b/src/shared/GeneralAssemblyInfo.cs index 694b53e..0a4ebe6 100644 --- a/src/shared/GeneralAssemblyInfo.cs +++ b/src/shared/GeneralAssemblyInfo.cs @@ -47,6 +47,6 @@ [assembly: NeutralResourcesLanguage("en-US", UltimateResourceFallbackLocation.MainAssembly)] #endif -[assembly: AssemblyVersion("1.1.0.0")] -[assembly: AssemblyFileVersion("1.1.0.0")] -[assembly: AssemblyInformationalVersion("1.1.0.0")] +[assembly: AssemblyVersion("1.1.1.7310")] +[assembly: AssemblyFileVersion("1.1.1.7310")] +[assembly: AssemblyInformationalVersion("1.1.1.7310")] diff --git a/src/tests/DataTypeTests/DataTypeTests.csproj b/src/tests/DataTypeTests/DataTypeTests.csproj index 5fec6aa..d552f06 100644 --- a/src/tests/DataTypeTests/DataTypeTests.csproj +++ b/src/tests/DataTypeTests/DataTypeTests.csproj @@ -25,6 +25,10 @@ + + + + diff --git a/src/tests/DataTypeTests/Models/TempModel.cs b/src/tests/DataTypeTests/Models/TempModel.cs new file mode 100644 index 0000000..083e287 --- /dev/null +++ b/src/tests/DataTypeTests/Models/TempModel.cs @@ -0,0 +1,25 @@ +// *********************************************************************** +// Assembly : RzR.Shared.Extensions.DataTypeTests +// Author : RzR +// Created On : 2024-02-13 18:20 +// +// Last Modified By : RzR +// Last Modified On : 2024-02-13 18:20 +// *********************************************************************** +// +// Copyright (c) RzR. All rights reserved. +// +// +// +// +// *********************************************************************** + +namespace DataTypeTests.Models +{ + public class TempModel + { + public int Id { get; set; } + public string Name { get; set; } + public string Code { get; set; } + } +} \ No newline at end of file diff --git a/src/tests/DataTypeTests/TypeParamTests.cs b/src/tests/DataTypeTests/TypeParamTests.cs new file mode 100644 index 0000000..2ae40da --- /dev/null +++ b/src/tests/DataTypeTests/TypeParamTests.cs @@ -0,0 +1,163 @@ +// *********************************************************************** +// Assembly : RzR.Shared.Extensions.DataTypeTests +// Author : RzR +// Created On : 2024-02-13 17:01 +// +// Last Modified By : RzR +// Last Modified On : 2024-02-13 19:41 +// *********************************************************************** +// +// Copyright (c) RzR. All rights reserved. +// +// +// +// +// *********************************************************************** + +#region U S A G E S + +using DataTypeTests.Models; +using DomainCommonExtensions.CommonExtensions; +using DomainCommonExtensions.CommonExtensions.TypeParam; +using Microsoft.VisualStudio.TestTools.UnitTesting; + +#endregion + +namespace DataTypeTests +{ + [TestClass] + public class TypeParamTests + { + [DataRow(null, 0, 0)] + [DataRow(null, null, null)] + [DataRow(1, 0, 1)] + [DataRow(0, 0, 0)] + [TestMethod] + public void IfIsNull_NullableInt_Test(int? sourceValue, int? defaultValue, int? exceptedResult) + { + var test = sourceValue.IfIsNull(defaultValue); + + Assert.AreEqual(exceptedResult, test); + } + + [DataRow(null, 0, 0)] + [DataRow(null, null, null)] + [DataRow(1, 0, 1)] + [DataRow(0, 0, 0)] + [TestMethod] + public void IfIsNull_NullableDecimal_Test(int? sourceValue, int? defaultValue, int? exceptedResult) + { + var test = ((decimal?)sourceValue).IfIsNull(defaultValue); + + Assert.AreEqual(exceptedResult, test); + } + + [DataRow(null, 0D, 0D)] + [DataRow(1D, 0D, 1D)] + [DataRow(0D, 0D, 0D)] + [DataRow(null, null, null)] + [TestMethod] + public void IfIsNull_NullableDouble_Test(double? sourceValue, double? defaultValue, double? exceptedResult) + { + var test = sourceValue.IfIsNull(defaultValue); + + Assert.AreEqual(exceptedResult, test); + } + + [DataRow(null, 0D, 0D)] + [DataRow(1D, 0D, 1D)] + [DataRow(0D, 0D, 0D)] + [TestMethod] + public void IfIsNull_Double_Test(double? sourceValue, double defaultValue, double exceptedResult) + { + var test = sourceValue.IfIsNull(defaultValue); + + Assert.AreEqual(exceptedResult, test); + } + + [TestMethod] + public void IfIsNull_ClassModel_Test() + { + var testModel = new TempModel(); + var test = testModel.IfIsNull(null); + Assert.AreEqual(testModel, test); + + var test1 = ((TempModel)null).IfIsNull(null); + Assert.AreEqual(null, test1); + + var testModel2 = new TempModel(); + var test2 = ((TempModel)null).IfIsNull(testModel2); + Assert.AreEqual(testModel2, test2); + } + + [TestMethod] + public void IfIsNulOrFunc_Null_Test() + { + int? i = null; + + var test = i.IfIsNullOrFuncIsTrue(0, () => i == 0); + + Assert.AreEqual(0, test); + } + + [TestMethod] + public void IfIsNulOrFunc_Zero_Equal_Zero_Test() + { + int? i = 0; + + var test = i.IfIsNullOrFuncIsTrue(-1, () => i == 0); + + Assert.AreEqual(-1, test); + } + + [TestMethod] + public void IfIsNulOrFunc_One_Equal_Zero_False_Test() + { + int? i = 1; + + var test = i.IfIsNullOrFuncIsTrue(0, () => i == 0); + + Assert.AreEqual(1, test); + } + + [TestMethod] + public void IfIsNulOrFunc_Null_Func_True_Test() + { + int? i = null; + + var test = i.IfFunc(0, () => i != 0 && i.IsNull(), true); + + Assert.AreEqual(0, test); + } + + [TestMethod] + public void IfIsNulOrFunc_Null_Func_False_Test() + { + int? i = null; + + var test = i.IfFunc(0, () => i != 0 && i.IsNull(), false); + + Assert.AreEqual(null, test); + } + + [TestMethod] + public void IfIsNulOrFunc_1_Func_True_Test() + { + int? i = 1; + + var test = i.IfFunc(0, () => i != 0 && i.IsNull(), true); + + Assert.AreEqual(1, test); + } + + [TestMethod] + public void IfIsNulOrFunc_1_Func_False_Test() + { + int? i = 1; + + var test = i.IfFunc(0, () => i != 0 && i.IsNull(), false); + + Assert.AreEqual(0, test); + } + } +} \ No newline at end of file