Skip to content

Commit

Permalink
230519
Browse files Browse the repository at this point in the history
  • Loading branch information
Tynab committed May 19, 2023
1 parent f2257d8 commit c55a3b7
Show file tree
Hide file tree
Showing 6 changed files with 204 additions and 156 deletions.
52 changes: 52 additions & 0 deletions lib/YANLib/Ultimate/YANJson.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
namespace YANLib.Ultimate;

public static partial class YANJson
{
public static IEnumerable<string> Serializes<T>(this IEnumerable<T> mdls)
{
if (mdls is null || !mdls.Any())
{
yield break;
}
foreach (var mdl in mdls)
{
yield return mdl.Serialize();
}
}

public static IEnumerable<string> CamelSerializes<T>(this IEnumerable<T> mdls)
{
if (mdls is null || !mdls.Any())
{
yield break;
}
foreach (var mdl in mdls)
{
yield return mdl.CamelSerialize();
}
}

public static IEnumerable<T?> StandardDeserializes<T>(this IEnumerable<string> strs)
{
if (strs is null || !strs.Any())
{
yield break;
}
foreach (var str in strs)
{
yield return str.StandardDeserialize<T>();
}
}

public static IEnumerable<T?> Deserializes<T>(this IEnumerable<string> strs)
{
if (strs is null || !strs.Any())
{
yield break;
}
foreach (var str in strs)
{
yield return str.Deserialize<T>();
}
}
}
76 changes: 76 additions & 0 deletions lib/YANLib/Ultimate/YANNum.Byte.Nullable.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
namespace YANLib.Ultimate;

public static partial class YANNum
{
public static IEnumerable<byte> ToByte<T>(this IEnumerable<T?> nums) where T : struct
{
if (nums is null || !nums.Any())
{
yield break;
}
foreach (var num in nums)
{
yield return num.ToByte();
}
}

public static IEnumerable<byte> ToByte<T>(this IEnumerable<string> strs, T? dfltVal) where T : struct
{
if (strs is null || !strs.Any())
{
yield break;
}
foreach (var str in strs)
{
yield return str.ToByte(dfltVal);
}
}

public static IEnumerable<byte> GenerateRandomBytes<T1, T2, T>(T1? min, T2 max, T size) where T1 : struct where T2 : struct where T : struct
{
for (var i = 0ul; i < size.ToUlong(); i++)
{
yield return YANLib.YANNum.GenerateRandomByte(min, max);
}
}

public static IEnumerable<byte> GenerateRandomBytes<T1, T2, T>(T1? min, T2 max, T? size) where T1 : struct where T2 : struct where T : struct
{
for (var i = 0ul; i < size.ToUlong(); i++)
{
yield return YANLib.YANNum.GenerateRandomByte(min, max);
}
}

public static IEnumerable<byte> GenerateRandomBytes<T1, T2, T>(T1 min, T2? max, T size) where T1 : struct where T2 : struct where T : struct
{
for (var i = 0ul; i < size.ToUlong(); i++)
{
yield return YANLib.YANNum.GenerateRandomByte(min, max);
}
}

public static IEnumerable<byte> GenerateRandomBytes<T1, T2, T>(T1 min, T2? max, T? size) where T1 : struct where T2 : struct where T : struct
{
for (var i = 0ul; i < size.ToUlong(); i++)
{
yield return YANLib.YANNum.GenerateRandomByte(min, max);
}
}

public static IEnumerable<byte> GenerateRandomBytes<T1, T2, T>(T1? min, T2? max, T size) where T1 : struct where T2 : struct where T : struct
{
for (var i = 0ul; i < size.ToUlong(); i++)
{
yield return YANLib.YANNum.GenerateRandomByte(min, max);
}
}

public static IEnumerable<byte> GenerateRandomBytes<T1, T2, T>(T1? min, T2? max, T? size) where T1 : struct where T2 : struct where T : struct
{
for (var i = 0ul; i < size.ToUlong(); i++)
{
yield return YANLib.YANNum.GenerateRandomByte(min, max);
}
}
}
48 changes: 48 additions & 0 deletions lib/YANLib/Ultimate/YANNum.Byte.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
namespace YANLib.Ultimate;

public static partial class YANNum
{
public static IEnumerable<byte> ToByte<T>(this IEnumerable<T> nums) where T : struct
{
if (nums is null || !nums.Any())
{
yield break;
}
foreach (var num in nums)
{
yield return num.ToByte();
}
}

public static IEnumerable<byte> ToByte(this IEnumerable<string> strs)
{
if (strs is null || !strs.Any())
{
yield break;
}
foreach (var str in strs)
{
yield return YANLib.YANNum.ToByte(str);
}
}

public static IEnumerable<byte> ToByte<T>(this IEnumerable<string> strs, T dfltVal) where T : struct
{
if (strs is null || !strs.Any())
{
yield break;
}
foreach (var str in strs)
{
yield return str.ToByte(dfltVal);
}
}

public static IEnumerable<byte> GenerateRandomBytes<T1, T2, T>(T1 min, T2 max, T size) where T1 : struct where T2 : struct where T : struct
{
for (var i = 0ul; i < size.ToUlong(); i++)
{
yield return YANLib.YANNum.GenerateRandomByte(min, max);
}
}
}
56 changes: 8 additions & 48 deletions lib/YANLib/YANJson.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,43 +5,23 @@ namespace YANLib;

public static partial class YANJson
{
private static readonly JsonSerializerOptions Pnsi = new()
private static readonly JsonSerializerOptions IsPropNameCaseInsJsonSerializerOpt = new()
{
PropertyNameCaseInsensitive = true
};

private static readonly JsonSerializerOptions CamelCasePnp = new()
private static readonly JsonSerializerOptions PropNamingPolWkCaseJsonSerializerOpt = new()
{
PropertyNamingPolicy = CamelCase
};

public static string Serialize<T>(this T mdl) => JsonSerializer.Serialize(mdl);

public static IEnumerable<string> Serializes<T>(this IEnumerable<T> mdls)
{
if (mdls is null || !mdls.Any())
{
yield break;
}
foreach (var mdl in mdls)
{
yield return mdl.Serialize();
}
}
public static IEnumerable<string>? Serializes<T>(this IEnumerable<T> mdls) => mdls is null || !mdls.Any() ? default : mdls.Select(m => m.Serialize());

public static string CamelSerialize<T>(this T mdl) => JsonSerializer.Serialize(mdl, CamelCasePnp);
public static string CamelSerialize<T>(this T mdl) => JsonSerializer.Serialize(mdl, PropNamingPolWkCaseJsonSerializerOpt);

public static IEnumerable<string> CamelSerializes<T>(this IEnumerable<T> mdls)
{
if (mdls is null || !mdls.Any())
{
yield break;
}
foreach (var mdl in mdls)
{
yield return mdl.CamelSerialize();
}
}
public static IEnumerable<string>? CamelSerializes<T>(this IEnumerable<T> mdls) => mdls is null || !mdls.Any() ? default : mdls.Select(m => m.CamelSerialize());

public static T? StandardDeserialize<T>(this string str)
{
Expand All @@ -55,39 +35,19 @@ public static IEnumerable<string> CamelSerializes<T>(this IEnumerable<T> mdls)
}
}

public static IEnumerable<T?> StandardDeserializes<T>(this IEnumerable<string> strs)
{
if (strs is null || !strs.Any())
{
yield break;
}
foreach (var str in strs)
{
yield return str.StandardDeserialize<T>();
}
}
public static IEnumerable<T?>? StandardDeserializes<T>(this IEnumerable<string> strs) => strs is null || !strs.Any() ? default : strs.Select(s => s.StandardDeserialize<T>());

public static T? Deserialize<T>(this string str)
{
try
{
return JsonSerializer.Deserialize<T>(str, Pnsi);
return JsonSerializer.Deserialize<T>(str, IsPropNameCaseInsJsonSerializerOpt);
}
catch
{
return default;
}
}

public static IEnumerable<T?> Deserializes<T>(this IEnumerable<string> strs)
{
if (strs is null || !strs.Any())
{
yield break;
}
foreach (var str in strs)
{
yield return str.Deserialize<T>();
}
}
public static IEnumerable<T?>? Deserializes<T>(this IEnumerable<string> strs) => strs is null || !strs.Any() ? default : strs.Select(s => s.Deserialize<T>());
}
78 changes: 12 additions & 66 deletions lib/YANLib/YANNum.Byte.Nullable.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
namespace YANLib;
using static System.Linq.Enumerable;

namespace YANLib;

public static partial class YANNum
{

public static byte ToByte<T>(this T? num) where T : struct
{
try
Expand All @@ -15,85 +17,29 @@ public static byte ToByte<T>(this T? num) where T : struct
}
}

public static IEnumerable<byte> ToByte<T>(this IEnumerable<T?> nums) where T : struct
{
if (nums is null || !nums.Any())
{
yield break;
}
foreach (var num in nums)
{
yield return num.ToByte();
}
}
public static IEnumerable<byte>? ToByte<T>(this IEnumerable<T?> nums) where T : struct => nums is null || !nums.Any() ? default : nums.Select(n => n.ToByte());

public static byte ToByte<T>(this string str, T? dfltVal) where T : struct => dfltVal.HasValue ? str.ToByte(dfltVal.Value) : default;

public static IEnumerable<byte> ToByte<T>(this IEnumerable<string> strs, T? dfltVal) where T : struct
{
if (strs is null || !strs.Any())
{
yield break;
}
foreach (var num in strs)
{
yield return num.ToByte(dfltVal);
}
}
public static IEnumerable<byte>? ToByte<T>(this IEnumerable<string> strs, T? dfltVal) where T : struct => strs is null || !strs.Any() ? default : strs.Select(s => s.ToByte(dfltVal));

public static byte GenerateRandomByte<T1, T2>(T1? min, T2 max) where T1 : struct where T2 : struct => min.HasValue ? GenerateRandomByte(min.Value, max) : default;

public static IEnumerable<byte> GenerateRandomBytes<T1, T2, T>(T1? min, T2 max, T size) where T1 : struct where T2 : struct where T : struct
{
for (var i = 0ul; i < size.ToUlong(); i++)
{
yield return GenerateRandomByte(min, max);
}
}
public static IEnumerable<byte> GenerateRandomBytes<T1, T2, T>(T1? min, T2 max, T size) where T1 : struct where T2 : struct where T : struct => Range(0, size.ToUint().ToInt()).Select(i => GenerateRandomByte(min, max));

public static IEnumerable<byte> GenerateRandomBytes<T1, T2, T>(T1? min, T2 max, T? size) where T1 : struct where T2 : struct where T : struct
{
for (var i = 0ul; i < size.ToUlong(); i++)
{
yield return GenerateRandomByte(min, max);
}
}
public static IEnumerable<byte> GenerateRandomBytes<T1, T2, T>(T1? min, T2 max, T? size) where T1 : struct where T2 : struct where T : struct => Range(0, size.ToUint().ToInt()).Select(i => GenerateRandomByte(min, max));

public static byte GenerateRandomByte<T1, T2>(T1 min, T2? max) where T1 : struct where T2 : struct => max.HasValue ? GenerateRandomByte(min, max.Value) : default;

public static IEnumerable<byte> GenerateRandomBytes<T1, T2, T>(T1 min, T2? max, T size) where T1 : struct where T2 : struct where T : struct
{
for (var i = 0ul; i < size.ToUlong(); i++)
{
yield return GenerateRandomByte(min, max);
}
}
public static IEnumerable<byte> GenerateRandomBytes<T1, T2, T>(T1 min, T2? max, T size) where T1 : struct where T2 : struct where T : struct => Range(0, size.ToUint().ToInt()).Select(i => GenerateRandomByte(min, max));

public static IEnumerable<byte> GenerateRandomBytes<T1, T2, T>(T1 min, T2? max, T? size) where T1 : struct where T2 : struct where T : struct
{
for (var i = 0ul; i < size.ToUlong(); i++)
{
yield return GenerateRandomByte(min, max);
}
}
public static IEnumerable<byte> GenerateRandomBytes<T1, T2, T>(T1 min, T2? max, T? size) where T1 : struct where T2 : struct where T : struct => Range(0, size.ToUint().ToInt()).Select(i => GenerateRandomByte(min, max));

public static byte GenerateRandomByte<T1, T2>(T1? min, T2? max) where T1 : struct where T2 : struct => min.HasValue ? GenerateRandomByte(min.Value, max) : default;

public static IEnumerable<byte> GenerateRandomBytes<T1, T2, T>(T1? min, T2? max, T size) where T1 : struct where T2 : struct where T : struct
{
for (var i = 0ul; i < size.ToUlong(); i++)
{
yield return GenerateRandomByte(min, max);
}
}
public static IEnumerable<byte> GenerateRandomBytes<T1, T2, T>(T1? min, T2? max, T size) where T1 : struct where T2 : struct where T : struct => Range(0, size.ToUint().ToInt()).Select(i => GenerateRandomByte(min, max));

public static IEnumerable<byte> GenerateRandomBytes<T1, T2, T>(T1? min, T2? max, T? size) where T1 : struct where T2 : struct where T : struct
{
for (var i = 0ul; i < size.ToUlong(); i++)
{
yield return GenerateRandomByte(min, max);
}
}
public static IEnumerable<byte> GenerateRandomBytes<T1, T2, T>(T1? min, T2? max, T? size) where T1 : struct where T2 : struct where T : struct => Range(0, size.ToUint().ToInt()).Select(i => GenerateRandomByte(min, max));

public static byte GenerateRandomByte<T>(T? max) where T : struct => max.HasValue ? GenerateRandomByte(byte.MinValue, max.Value) : default;
}
Loading

0 comments on commit c55a3b7

Please sign in to comment.