Skip to content

Commit

Permalink
231222
Browse files Browse the repository at this point in the history
  • Loading branch information
Tynab committed Dec 22, 2023
1 parent 650fa24 commit b21b256
Show file tree
Hide file tree
Showing 114 changed files with 2,402 additions and 4,201 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using BenchmarkDotNet.Attributes;
using YANLib.Benchmarks.Models;
using YANLib.Core;
using static Newtonsoft.Json.JsonConvert;
using static System.Guid;
using static System.Threading.Tasks.Parallel;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using BenchmarkDotNet.Attributes;
using YANLib.Benchmarks.Models;
using YANLib.Core;
using static Newtonsoft.Json.JsonConvert;
using static System.Guid;
using static System.Threading.Tasks.Parallel;
Expand Down
6 changes: 3 additions & 3 deletions lib/YANLib/YANBool.cs → lib/YANLib/Core/YANBool.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
using static System.Linq.Enumerable;
using static YANLib.YANNum;
using static YANLib.Core.YANNum;

namespace YANLib;
namespace YANLib.Core;

public static partial class YANBool
{
Expand All @@ -20,7 +20,7 @@ public static bool ToBool(this object? val, object? dfltVal = null)
}
catch
{
return dfltVal is not null && dfltVal.ToBool();
return dfltVal.IsNotNull() && dfltVal.ToBool();
}
}

Expand Down
6 changes: 3 additions & 3 deletions lib/YANLib/YANBytes.cs → lib/YANLib/Core/YANBytes.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
using static System.Text.Encoding;

namespace YANLib;
namespace YANLib.Core;

public static partial class YANBytes
{
Expand All @@ -11,7 +11,7 @@ public static partial class YANBytes
/// </summary>
/// <param name="val">The object to be converted to a byte array. Can be <see langword="null"/>.</param>
/// <returns>A byte array representing the UTF-8 encoded serialization of the input object, or <see langword="null"/> if the input object is <see langword="null"/>.</returns>
public static byte[]? ToByteArray(this object? val) => val is null ? default : UTF8.GetBytes(val.StandardSerialize() ?? string.Empty);
public static byte[]? ToByteArray(this object? val) => val.IsNull() ? default : UTF8.GetBytes(val.StandardSerialize() ?? string.Empty);

/// <summary>
/// Converts a collection of objects to an enumerable collection of byte arrays.
Expand Down Expand Up @@ -47,7 +47,7 @@ public static partial class YANBytes
/// </summary>
/// <param name="arr">The byte array to be converted. Can be <see langword="null"/>.</param>
/// <returns>The object of type <typeparamref name="T"/> represented by the deserialized byte array, or the default value of type <typeparamref name="T"/> if the byte array is <see langword="null"/>.</returns>
public static T? FromByteArray<T>(this byte[]? arr) => arr is null ? default : UTF8.GetString(arr).Deserialize<T>();
public static T? FromByteArray<T>(this byte[]? arr) => arr.IsNull() ? default : UTF8.GetString(arr).Deserialize<T>();

/// <summary>
/// Converts a collection of byte arrays to an enumerable collection of objects of type <typeparamref name="T"/>.
Expand Down
40 changes: 2 additions & 38 deletions lib/YANLib/YANDateTime.cs → lib/YANLib/Core/YANDateTime.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,12 @@
using static System.Globalization.DateTimeStyles;
using static System.Linq.Enumerable;
using static System.Math;
using static YANLib.YANNum;
using static YANLib.Core.YANNum;

namespace YANLib;
namespace YANLib.Core;

public static partial class YANDateTime
{
/// <summary>
/// Converts the specified string representation of a date and time to its <see cref="DateTime"/> equivalent, using an optional format and default value.
/// If the string is null, empty, or white space, returns the default value.
/// If a format is provided, it attempts to parse the string using the specified format; otherwise, it uses standard date and time parsing.
/// </summary>
/// <param name="str">The string to be converted to <see cref="DateTime"/>. Can be null or white space.</param>
/// <param name="fmt">The format of the input string. Can be null or white space.</param>
/// <param name="dfltVal">The default <see cref="DateTime"/> value to return if parsing fails.</param>
/// <returns>The <see cref="DateTime"/> equivalent of the input string, or the default value if parsing fails.</returns>
public static DateTime ToDateTime(this string? str, string? fmt = null, DateTime dfltVal = default) => str.IsWhiteSpaceOrNull()
? dfltVal
: fmt.IsWhiteSpaceOrNull()
Expand All @@ -29,41 +20,14 @@ public static DateTime ToDateTime(this string? str, string? fmt = null, DateTime
? dt
: dfltVal;

/// <summary>
/// Converts a collection of string representations of dates and times to their respective <see cref="DateTime"/> equivalents, using an optional format and default value.
/// If the collection is null or empty, returns null.
/// Each string in the collection is converted to a <see cref="DateTime"/> value; if conversion fails, the default value is used.
/// </summary>
/// <param name="strs">The collection of strings to be converted to <see cref="DateTime"/>. Can be null.</param>
/// <param name="fmt">The format of the input strings. Can be null or white space.</param>
/// <param name="dfltVal">The default <see cref="DateTime"/> value to return if parsing fails.</param>
/// <returns>An enumerable collection of <see cref="DateTime"/> values representing the converted results, or the default value for strings that fail to convert.</returns>
public static IEnumerable<DateTime>? ToDateTimes(this IEnumerable<string?>? strs, string? fmt = null, DateTime dfltVal = default) => strs.IsEmptyOrNull()
? default
: strs.Select(x => x.ToDateTime(fmt, dfltVal));

/// <summary>
/// Converts a collection (ICollection) of string representations of dates and times to their respective <see cref="DateTime"/> equivalents, using an optional format and default value.
/// If the collection is null or empty, returns null.
/// Each string in the collection is converted to a <see cref="DateTime"/> value; if conversion fails, the default value is used.
/// </summary>
/// <param name="strs">The ICollection of strings to be converted to <see cref="DateTime"/>. Can be null.</param>
/// <param name="fmt">The format of the input strings. Can be null or white space.</param>
/// <param name="dfltVal">The default <see cref="DateTime"/> value to return if parsing fails.</param>
/// <returns>An enumerable collection of <see cref="DateTime"/> values representing the converted results, or the default value for strings that fail to convert.</returns>
public static IEnumerable<DateTime>? ToDateTimes(this ICollection<string?>? strs, string? fmt = null, DateTime dfltVal = default) => strs.IsEmptyOrNull()
? default
: strs.Select(x => x.ToDateTime(fmt, dfltVal));

/// <summary>
/// Converts an array of string representations of dates and times to their respective <see cref="DateTime"/> equivalents, using an optional format and default value.
/// If the array is null or empty, returns null.
/// Each string in the array is converted to a <see cref="DateTime"/> value; if conversion fails, the default value is used.
/// </summary>
/// <param name="strs">The array of strings to be converted to <see cref="DateTime"/>. Can be null.</param>
/// <param name="fmt">The format of the input strings. Can be null or white space.</param>
/// <param name="dfltVal">The default <see cref="DateTime"/> value to return if parsing fails.</param>
/// <returns>An enumerable collection of <see cref="DateTime"/> values representing the converted results, or the default value for strings that fail to convert.</returns>
public static IEnumerable<DateTime>? ToDateTimes(this string?[]? strs, string? fmt = null, DateTime dfltVal = default) => strs.IsEmptyOrNull()
? default
: strs.Select(x => x.ToDateTime(fmt, dfltVal));
Expand Down
8 changes: 1 addition & 7 deletions lib/YANLib/YANEnum.cs → lib/YANLib/Core/YANEnum.cs
Original file line number Diff line number Diff line change
@@ -1,15 +1,9 @@
using static System.Enum;

namespace YANLib;
namespace YANLib.Core;

public static partial class YANEnum
{
/// <summary>
/// Converts the specified string to its enumeration equivalent of type <typeparamref name="T"/>.
/// If the string is null, white-space, or does not correspond to an enumeration value of type <typeparamref name="T"/>, returns the default value of <typeparamref name="T"/>.
/// </summary>
/// <param name="val">The string to be converted to an enumeration value of type <typeparamref name="T"/>. Can be <see langword="null"/>.</param>
/// <returns>The enumeration value of type <typeparamref name="T"/> represented by the string, or the default value of <typeparamref name="T"/> if the conversion fails.</returns>
public static T? ToEnum<T>(this string? val) where T : struct => val.IsWhiteSpaceOrNull() ? default : TryParse<T>(val, out var result) ? result : default;

/// <summary>
Expand Down
186 changes: 186 additions & 0 deletions lib/YANLib/Core/YANEnumerable.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,186 @@
using System.Diagnostics.CodeAnalysis;
using static System.Math;
using static System.Nullable;

namespace YANLib.Core;

public static partial class YANEnumerable
{
/// <summary>
/// Divides a collection of elements into chunks of a specified size.
/// If the source collection is <see langword="null"/> or empty, or if the chunk size is zero, yields no results.
/// </summary>
/// <param name="srcs">The source collection of elements to be chunked. Can be <see langword="null"/>.</param>
/// <param name="chunkSize">The size of each chunk. Can be <see langword="null"/>, resulting in a default size of 0.</param>
/// <returns>An enumerable collection of lists, each containing a chunk of elements from the source collection.</returns>
public static IEnumerable<List<T?>> ChunkBySize<T>(this IEnumerable<T?>? srcs, object? chunkSize)
{
var size = (int)chunkSize.ToUint();

if (srcs.IsEmptyOrNull() || size is 0)
{
yield break;
}

var srcsList = srcs.ToList();
var cnt = srcsList.Count;

for (var i = 0; i < cnt; i += size)
{
yield return srcsList.GetRange(i, Min(size, cnt - i));
}
}

/// <summary>
/// Divides a collection (ICollection) of elements into chunks of a specified size.
/// If the source collection is <see langword="null"/> or empty, or if the chunk size is zero, yields no results.
/// </summary>
/// <param name="srcs">The source ICollection of elements to be chunked. Can be <see langword="null"/>.</param>
/// <param name="chunkSize">The size of each chunk. Can be <see langword="null"/>, resulting in a default size of 0.</param>
/// <returns>An enumerable collection of lists, each containing a chunk of elements from the source collection.</returns>
public static IEnumerable<List<T?>> ChunkBySize<T>(this ICollection<T?>? srcs, object? chunkSize)
{
var size = (int)chunkSize.ToUint();

if (srcs.IsEmptyOrNull() || size is 0)
{
yield break;
}

var cnt = srcs.Count;

for (var i = 0; i < cnt; i += size)
{
yield return srcs.ToList().GetRange(i, Min(size, cnt - i));
}
}

/// <summary>
/// Divides an array of elements into chunks of a specified size.
/// If the source array is <see langword="null"/> or empty, or if the chunk size is zero, yields no results.
/// </summary>
/// <param name="srcs">The source array of elements to be chunked. Can be <see langword="null"/>.</param>
/// <param name="chunkSize">The size of each chunk. Can be <see langword="null"/>, resulting in a default size of 0.</param>
/// <returns>An enumerable collection of lists, each containing a chunk of elements from the source array.</returns>
public static IEnumerable<List<T?>> ChunkBySize<T>(this T?[]? srcs, object? chunkSize)
{
var size = (int)chunkSize.ToUint();

if (srcs.IsEmptyOrNull() || size is 0)
{
yield break;
}

var cnt = srcs.Length;

for (var i = 0; i < cnt; i += size)
{
yield return srcs.ToList().GetRange(i, Min(size, cnt - i));
}
}

/// <summary>
/// Divides a list of elements into chunks of a specified size.
/// If the source list is <see langword="null"/> or empty, or if the chunk size is zero, yields no results.
/// </summary>
/// <param name="srcs">The source list of elements to be chunked. Can be <see langword="null"/>.</param>
/// <param name="chunkSize">The size of each chunk. Can be <see langword="null"/>, resulting in a default size of 0.</param>
/// <returns>An enumerable collection of lists, each containing a chunk of elements from the source list.</returns>
public static IEnumerable<List<T?>> ChunkBySize<T>(this List<T?>? srcs, object? chunkSize)
{
var size = (int)chunkSize.ToUint();

if (srcs.IsEmptyOrNull() || size is 0)
{
yield break;
}

var cnt = srcs.Count;

for (var i = 0; i < cnt; i += size)
{
yield return srcs.GetRange(i, Min(size, cnt - i));
}
}

/// <summary>
/// Cleans a collection of nullable elements by removing <see langword="null"/> values.
/// For classes and nullable value types, <see langword="null"/> elements are removed. For non-nullable value types, no elements are removed.
/// If the source collection is <see langword="null"/> or empty, returns <see langword="null"/>.
/// </summary>
/// <param name="srcs">The source collection of nullable elements. Can be <see langword="null"/>.</param>
/// <returns>An enumerable collection with <see langword="null"/> elements removed, or <see langword="null"/> if the input collection is <see langword="null"/> or empty.</returns>
public static IEnumerable<T?>? Clean<T>(this IEnumerable<T?>? srcs)
{
if (srcs.IsEmptyOrNull())
{
return default;
}

var t = typeof(T);

return t.IsClass || GetUnderlyingType(t).IsNotNull() ? srcs.Where(x => x.IsNotNull()) : srcs;
}

/// <summary>
/// Cleans a collection (ICollection) of nullable elements by removing <see langword="null"/> values.
/// For classes and nullable value types, <see langword="null"/> elements are removed. For non-nullable value types, no elements are removed.
/// If the source collection is <see langword="null"/> or empty, returns <see langword="null"/>.
/// </summary>
/// <param name="srcs">The source ICollection of nullable elements. Can be <see langword="null"/>.</param>
/// <returns>An enumerable collection with <see langword="null"/> elements removed, or <see langword="null"/> if the input ICollection is <see langword="null"/> or empty.</returns>
public static IEnumerable<T?>? Clean<T>(this ICollection<T?>? srcs)
{
if (srcs.IsEmptyOrNull())
{
return default;
}

var t = typeof(T);

return t.IsClass || GetUnderlyingType(t).IsNotNull() ? srcs.Where(x => x.IsNotNull()) : srcs;
}

/// <summary>
/// Cleans an array of nullable elements by removing <see langword="null"/> values.
/// For classes and nullable value types, <see langword="null"/> elements are removed. For non-nullable value types, no elements are removed.
/// If the source array is <see langword="null"/> or empty, returns <see langword="null"/>.
/// </summary>
/// <param name="srcs">The source array of nullable elements. Can be <see langword="null"/>.</param>
/// <returns>An enumerable collection with <see langword="null"/> elements removed, or <see langword="null"/> if the input array is <see langword="null"/> or empty.</returns>
public static IEnumerable<T?>? Clean<T>(this T?[]? srcs)
{
if (srcs.IsEmptyOrNull())
{
return default;
}

var t = typeof(T);

return t.IsClass || GetUnderlyingType(t).IsNotNull() ? srcs.Where(x => x.IsNotNull()) : srcs;
}

/// <summary>
/// Cleans a collection of strings by removing <see langword="null"/> or white-space strings.
/// If the source collection is <see langword="null"/> or empty, returns <see langword="null"/>.
/// </summary>
/// <param name="srcs">The source collection of strings. Can be <see langword="null"/>.</param>
/// <returns>An enumerable collection with <see langword="null"/> or white-space strings removed, or <see langword="null"/> if the input collection is <see langword="null"/> or empty.</returns>
public static IEnumerable<string?>? Clean(this IEnumerable<string?>? srcs) => srcs.IsEmptyOrNull() ? default : srcs.Where(x => x.IsNotWhiteSpaceAndNull());

/// <summary>
/// Cleans a collection (ICollection) of strings by removing <see langword="null"/> or white-space strings.
/// If the source collection is <see langword="null"/> or empty, returns <see langword="null"/>.
/// </summary>
/// <param name="srcs">The source ICollection of strings. Can be <see langword="null"/>.</param>
/// <returns>An enumerable collection with <see langword="null"/> or white-space strings removed, or <see langword="null"/> if the input ICollection is <see langword="null"/> or empty.</returns>
public static IEnumerable<string?>? Clean(this ICollection<string?>? srcs) => srcs.IsEmptyOrNull() ? default : srcs.Where(x => x.IsNotWhiteSpaceAndNull());

/// <summary>
/// Cleans an array of strings by removing <see langword="null"/> or white-space strings.
/// If the source array is <see langword="null"/> or empty, returns <see langword="null"/>.
/// </summary>
/// <param name="srcs">The source array of strings. Can be <see langword="null"/>.</param>
/// <returns>An enumerable collection with <see langword="null"/> or white-space strings removed, or <see langword="null"/> if the input array is <see langword="null"/> or empty.</returns>
public static IEnumerable<string?>? Clean(this string?[]? srcs) => srcs.IsEmptyOrNull() ? default : srcs.Where(x => x.IsNotWhiteSpaceAndNull());
}
Loading

0 comments on commit b21b256

Please sign in to comment.