From 2ced2f1a9abb7084670668eb2699da53fffa2e33 Mon Sep 17 00:00:00 2001 From: Yami An Date: Fri, 31 Mar 2023 14:09:14 +0700 Subject: [PATCH] v1.0.5 --- YANLib/YANList.cs | 61 +++++++++++++++++++++++++++++++++++++++++++--- YANLib/YANModel.cs | 17 +++++++++++-- 2 files changed, 73 insertions(+), 5 deletions(-) diff --git a/YANLib/YANList.cs b/YANLib/YANList.cs index 64e418ef..3a21901f 100644 --- a/YANLib/YANList.cs +++ b/YANLib/YANList.cs @@ -1,4 +1,4 @@ -using static System.Math; +using static System.Nullable; namespace YANLib; @@ -13,9 +13,64 @@ public static partial class YANList /// An of s, where each inner list has a maximum size of . public static IEnumerable> ChunkBySize(this List srcs, int chunkSize) { - for (var i = 0; i < srcs?.Count; i += chunkSize) + var cnt = srcs?.Count ?? 0; + if (srcs == null || cnt < 1 || chunkSize < 1) { - yield return srcs.GetRange(i, Min(chunkSize, srcs.Count - i)); + yield break; + } + for (var i = 0; i < cnt; i += chunkSize) + { + yield return srcs.GetRange(i, i + chunkSize > cnt ? cnt - i : chunkSize); + } + } + + /// + /// Cleans a given by removing null or whitespace elements depending on the type of T. + /// + /// The type of elements in the list. + /// The source list to be cleaned. + /// A new that contains only non-null or non-whitespace elements. + public static IList? Clean(this IList srcs) + { + if (srcs?.Count > 0) + { + var t = typeof(T); + if (t.IsClass || GetUnderlyingType(t) != null) + { + return srcs.ClnPrcYld().ToList(); + } + } + return srcs; + } + + /// + /// Cleans a given list string by removing null or whitespace elements, returning a new list that contains only non-null and non-whitespace elements. + /// + /// The source list to be cleaned. + /// A new list string that contains only non-null or non-whitespace elements, or null if the input list is null. + public static IList? Clean(this IList srcs) => srcs?.Count > 0 ? srcs.ClnPrcYld().ToList() : srcs; + + // Clean process yield + private static IEnumerable ClnPrcYld(this IList srcs) + { + for (var i = 0; i < srcs?.Count; i++) + { + if (srcs[i] != null) + { + yield return srcs[i]; + } + } + } + + // Clean process yield + private static IEnumerable ClnPrcYld(this IList srcs) + { + for (var i = 0; i < srcs?.Count; i++) + { + if (!string.IsNullOrWhiteSpace(srcs[i])) + { + yield return srcs[i]; + } } } } diff --git a/YANLib/YANModel.cs b/YANLib/YANModel.cs index d83a5515..4dee5c69 100644 --- a/YANLib/YANModel.cs +++ b/YANLib/YANModel.cs @@ -1,4 +1,5 @@ using System.Collections; +using System.Reflection; using static System.Reflection.BindingFlags; namespace YANLib; @@ -18,7 +19,7 @@ public static partial class YANModel { if (mdl != null) { - var props = typeof(T).GetProperties(Public | Instance).Where(p => p.CanRead && p.CanWrite); + var props = typeof(T).GetProperties(Public | Instance).ChgTzPropPrcYld(); foreach (var prop in props) { var val = prop?.GetValue(mdl); @@ -32,7 +33,7 @@ public static partial class YANModel { prop?.SetValue(mdl, ChangeTimeZoneAllProperties(val, tzSrc, tzDst)); } - else if (val.GetType().IsGenericType && val?.GetType()?.GetGenericTypeDefinition() == typeof(List<>)) + else if (val.GetType().IsGenericType && val?.GetType()?.GetGenericTypeDefinition() == typeof(IList<>)) { var list = (IList)val; for (var i = 0; i < list?.Count; i++) @@ -45,4 +46,16 @@ public static partial class YANModel } return mdl; } + + // Change time zone properties process yield + private static IEnumerable ChgTzPropPrcYld(this PropertyInfo[] arrProp) + { + for (var i = 0; i < arrProp.Length; i++) + { + if (arrProp[i].CanRead && arrProp[i].CanWrite) + { + yield return arrProp[i]; + } + } + } }