Skip to content

Commit

Permalink
v1.0.5
Browse files Browse the repository at this point in the history
  • Loading branch information
Tynab committed Mar 31, 2023
1 parent 9443cf0 commit 2ced2f1
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 5 deletions.
61 changes: 58 additions & 3 deletions YANLib/YANList.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using static System.Math;
using static System.Nullable;

namespace YANLib;

Expand All @@ -13,9 +13,64 @@ public static partial class YANList
/// <returns>An <see cref="IEnumerable{T}"/> of <see cref="List{T}"/>s, where each inner list has a maximum size of <paramref name="chunkSize"/>.</returns>
public static IEnumerable<List<T>> ChunkBySize<T>(this List<T> 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);
}
}

/// <summary>
/// Cleans a given <see cref="IList{T}"/> by removing null or whitespace elements depending on the type of T.
/// </summary>
/// <typeparam name="T">The type of elements in the list.</typeparam>
/// <param name="srcs">The source list to be cleaned.</param>
/// <returns>A new <see cref="IList{T}"/> that contains only non-null or non-whitespace elements.</returns>
public static IList<T>? Clean<T>(this IList<T> srcs)
{
if (srcs?.Count > 0)
{
var t = typeof(T);
if (t.IsClass || GetUnderlyingType(t) != null)
{
return srcs.ClnPrcYld().ToList();
}
}
return srcs;
}

/// <summary>
/// Cleans a given list string by removing null or whitespace elements, returning a new list that contains only non-null and non-whitespace elements.
/// </summary>
/// <param name="srcs">The source list to be cleaned.</param>
/// <returns>A new list string that contains only non-null or non-whitespace elements, or null if the input list is null.</returns>
public static IList<string>? Clean(this IList<string> srcs) => srcs?.Count > 0 ? srcs.ClnPrcYld().ToList() : srcs;

// Clean process yield
private static IEnumerable<T> ClnPrcYld<T>(this IList<T> srcs)
{
for (var i = 0; i < srcs?.Count; i++)
{
if (srcs[i] != null)
{
yield return srcs[i];
}
}
}

// Clean process yield
private static IEnumerable<string> ClnPrcYld(this IList<string> srcs)
{
for (var i = 0; i < srcs?.Count; i++)
{
if (!string.IsNullOrWhiteSpace(srcs[i]))
{
yield return srcs[i];
}
}
}
}
17 changes: 15 additions & 2 deletions YANLib/YANModel.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Collections;
using System.Reflection;
using static System.Reflection.BindingFlags;

namespace YANLib;
Expand All @@ -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);
Expand All @@ -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++)
Expand All @@ -45,4 +46,16 @@ public static partial class YANModel
}
return mdl;
}

// Change time zone properties process yield
private static IEnumerable<PropertyInfo> ChgTzPropPrcYld(this PropertyInfo[] arrProp)
{
for (var i = 0; i < arrProp.Length; i++)
{
if (arrProp[i].CanRead && arrProp[i].CanWrite)
{
yield return arrProp[i];
}
}
}
}

0 comments on commit 2ced2f1

Please sign in to comment.