diff --git a/lib/YANLib/Nullable/YANDateTime.Nullable.cs b/lib/YANLib/Nullable/YANDateTime.Nullable.cs index ad309cb..21fb36d 100644 --- a/lib/YANLib/Nullable/YANDateTime.Nullable.cs +++ b/lib/YANLib/Nullable/YANDateTime.Nullable.cs @@ -24,7 +24,7 @@ public static partial class YANDateTime /// An enumerable collection of objects for each successfully converted input string in the specified format, and the specified default value for any strings that fail to convert. public static IEnumerable ToDateTime(this IEnumerable strs, string fmt, DateTime? dfltVal) { - if (strs.AllNullOrWhiteSpace()) + if (strs.AllWhiteSpaceOrNull()) { yield break; } diff --git a/lib/YANLib/Nullable/YANDateTime.cs b/lib/YANLib/Nullable/YANDateTime.cs index 6b78c6e..e1f49ca 100644 --- a/lib/YANLib/Nullable/YANDateTime.cs +++ b/lib/YANLib/Nullable/YANDateTime.cs @@ -25,7 +25,7 @@ public static partial class YANDateTime /// An enumerable collection of objects for each successfully converted input string. public static IEnumerable ToDateTime(this IEnumerable strs) { - if (strs.AllNullOrWhiteSpace()) + if (strs.AllWhiteSpaceOrNull()) { yield break; } @@ -53,7 +53,7 @@ public static partial class YANDateTime /// An enumerable collection of objects for each successfully converted input string in the specified format. public static IEnumerable ToDateTime(this IEnumerable strs, string fmt) { - if (strs.AllNullOrWhiteSpace()) + if (strs.AllWhiteSpaceOrNull()) { yield break; } @@ -83,7 +83,7 @@ public static partial class YANDateTime /// An enumerable collection of objects for each successfully converted input string in the specified format, and the specified default value for any strings that fail to convert. public static IEnumerable ToDateTime(this IEnumerable strs, string fmt, DateTime dfltVal) { - if (strs.AllNullOrWhiteSpace()) + if (strs.AllWhiteSpaceOrNull()) { yield break; } diff --git a/lib/YANLib/Ultimate/YANDateTime.Nullable.cs b/lib/YANLib/Ultimate/YANDateTime.Nullable.cs new file mode 100644 index 0000000..cd802d4 --- /dev/null +++ b/lib/YANLib/Ultimate/YANDateTime.Nullable.cs @@ -0,0 +1,210 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace YANLib.Ultimate; + +public static partial class YANDateTime +{ + /// + /// Converts a collection of string representations of dates and times to their equivalents using the specified format. + /// Returns an enumerable collection of objects for each successfully converted input string in the specified format, and returns the specified default value for any strings that fail to convert. + /// + /// The collection of strings to be converted to . + /// The format of the input strings. + /// The default value to return for any strings that fail to convert. + /// An enumerable collection of objects for each successfully converted input string in the specified format, and the specified default value for any strings that fail to convert. + public static IEnumerable ToDateTime(this IEnumerable strs, string fmt, DateTime? dfltVal) + { + if (strs.AllWhiteSpaceOrNull()) + { + yield break; + } + foreach (var str in strs) + { + yield return str.ToDateTime(fmt, dfltVal); + } + } + + public static IEnumerable GenerateRandomDateTimes(DateTime? min, DateTime? max, T size) where T : struct + { + for (var i = 0ul; i < size.ToUlong(); i++) + { + yield return YANLib.YANDateTime.GenerateRandomDateTime(min, max); + } + } + + public static IEnumerable GenerateRandomDateTimes(DateTime? min, DateTime? max, T? size) where T : struct + { + for (var i = 0ul; i < size.ToUlong(); i++) + { + yield return YANLib.YANDateTime.GenerateRandomDateTime(min, max); + } + } + + public static IEnumerable GenerateRandomDateTimes(DateTime min, DateTime? max, T size) where T : struct + { + for (var i = 0ul; i < size.ToUlong(); i++) + { + yield return YANLib.YANDateTime.GenerateRandomDateTime(min, max); + } + } + + public static IEnumerable GenerateRandomDateTimes(DateTime min, DateTime? max, T? size) where T : struct + { + for (var i = 0ul; i < size.ToUlong(); i++) + { + yield return YANLib.YANDateTime.GenerateRandomDateTime(min, max); + } + } + + public static IEnumerable GenerateRandomDateTimes(DateTime? min, DateTime max, T size) where T : struct + { + for (var i = 0ul; i < size.ToUlong(); i++) + { + yield return YANLib.YANDateTime.GenerateRandomDateTime(min, max); + } + } + + public static IEnumerable GenerateRandomDateTimes(DateTime? min, DateTime max, T? size) where T : struct + { + for (var i = 0ul; i < size.ToUlong(); i++) + { + yield return YANLib.YANDateTime.GenerateRandomDateTime(min, max); + } + } + + public static IEnumerable GetWeekOfYear(this IEnumerable dts) + { + if (dts is null || !dts.Any()) + { + yield break; + } + foreach (var dt in dts) + { + yield return dt.GetWeekOfYear(); + } + } + + public static IEnumerable ChangeTimeZone(this IEnumerable dts, T1 tzSrc, T2 tzDst) where T1 : struct where T2 : struct + { + if (dts is null || !dts.Any()) + { + yield break; + } + foreach (var dt in dts) + { + yield return dt.ChangeTimeZone(tzSrc, tzDst); + } + } + + public static IEnumerable ChangeTimeZone(this IEnumerable dts, T1? tzSrc, T2 tzDst) where T1 : struct where T2 : struct + { + if (dts is null || !dts.Any()) + { + yield break; + } + foreach (var dt in dts) + { + yield return dt.ChangeTimeZone(tzSrc, tzDst); + } + } + + public static IEnumerable ChangeTimeZone(this IEnumerable dts, T1 tzSrc, T2? tzDst) where T1 : struct where T2 : struct + { + if (dts is null || !dts.Any()) + { + yield break; + } + foreach (var dt in dts) + { + yield return dt.ChangeTimeZone(tzSrc, tzDst); + } + } + + public static IEnumerable ChangeTimeZone(this IEnumerable dts, T1? tzSrc, T2 tzDst) where T1 : struct where T2 : struct + { + if (dts is null || !dts.Any()) + { + yield break; + } + foreach (var dt in dts) + { + yield return dt.ChangeTimeZone(tzSrc, tzDst); + } + } + + public static IEnumerable ChangeTimeZone(this IEnumerable dts, T1 tzSrc, T2? tzDst) where T1 : struct where T2 : struct + { + if (dts is null || !dts.Any()) + { + yield break; + } + foreach (var dt in dts) + { + yield return dt.ChangeTimeZone(tzSrc, tzDst); + } + } + + public static IEnumerable ChangeTimeZone(this IEnumerable dts, T1? tzSrc, T2? tzDst) where T1 : struct where T2 : struct + { + if (dts is null || !dts.Any()) + { + yield break; + } + foreach (var dt in dts) + { + yield return dt.ChangeTimeZone(tzSrc, tzDst); + } + } + + public static IEnumerable ChangeTimeZone(this IEnumerable dts, T1? tzSrc, T2? tzDst) where T1 : struct where T2 : struct + { + if (dts is null || !dts.Any()) + { + yield break; + } + foreach (var dt in dts) + { + yield return dt.ChangeTimeZone(tzSrc, tzDst); + } + } + + public static IEnumerable ChangeTimeZone(this IEnumerable dts, T tzDst) where T : struct + { + if (dts is null || !dts.Any()) + { + yield break; + } + foreach (var dt in dts) + { + yield return dt.ChangeTimeZone(tzDst); + } + } + + public static IEnumerable ChangeTimeZone(this IEnumerable dts, T? tzDst) where T : struct + { + if (dts is null || !dts.Any()) + { + yield break; + } + foreach (var dt in dts) + { + yield return dt.ChangeTimeZone(tzDst); + } + } + + public static IEnumerable ChangeTimeZone(this IEnumerable dts, T? tzDst) where T : struct + { + if (dts is null || !dts.Any()) + { + yield break; + } + foreach (var dt in dts) + { + yield return dt.ChangeTimeZone(tzDst); + } + } +} diff --git a/lib/YANLib/Ultimate/YANDateTime.cs b/lib/YANLib/Ultimate/YANDateTime.cs new file mode 100644 index 0000000..e215dd0 --- /dev/null +++ b/lib/YANLib/Ultimate/YANDateTime.cs @@ -0,0 +1,105 @@ +namespace YANLib.Ultimate; + +public static partial class YANDateTime +{ + /// + /// Converts a collection of string representations of dates and times to their equivalents. + /// Returns an enumerable collection of objects for each successfully converted input string, and skips any strings that fail to convert. + /// + /// The collection of strings to be converted to . + /// An enumerable collection of objects for each successfully converted input string. + public static IEnumerable ToDateTime(this IEnumerable strs) + { + if (strs.AllWhiteSpaceOrNull()) + { + yield break; + } + foreach (var str in strs) + { + yield return str.ToDateTime(); + } + } + + /// + /// Converts a collection of string representations of dates and times to their equivalents using the specified format. + /// Returns an enumerable collection of objects for each successfully converted input string in the specified format, and skips any strings that fail to convert. + /// + /// The collection of strings to be converted to . + /// The format of the input strings. + /// An enumerable collection of objects for each successfully converted input string in the specified format. + public static IEnumerable ToDateTime(this IEnumerable strs, string fmt) + { + if (strs.AllWhiteSpaceOrNull()) + { + yield break; + } + foreach (var str in strs) + { + yield return str.ToDateTime(fmt); + } + } + + /// + /// Converts a collection of string representations of dates and times to their equivalents using the specified format. + /// Returns an enumerable collection of objects for each successfully converted input string in the specified format, and returns the specified default value for any strings that fail to convert. + /// + /// The collection of strings to be converted to . + /// The format of the input strings. + /// The default value to return for any strings that fail to convert. + /// An enumerable collection of objects for each successfully converted input string in the specified format, and the specified default value for any strings that fail to convert. + public static IEnumerable ToDateTime(this IEnumerable strs, string fmt, DateTime dfltVal) + { + if (strs.AllWhiteSpaceOrNull()) + { + yield break; + } + foreach (var str in strs) + { + yield return str.ToDateTime(fmt, dfltVal); + } + } + + public static IEnumerable GenerateRandomDateTimes(DateTime min, DateTime max, T size) where T : struct + { + for (var i = 0ul; i < size.ToUlong(); i++) + { + yield return YANLib.YANDateTime.GenerateRandomDateTime(min, max); + } + } + + public static IEnumerable GetWeekOfYear(this IEnumerable dts) + { + if (dts is null || !dts.Any()) + { + yield break; + } + foreach (var dt in dts) + { + yield return dt.GetWeekOfYear(); + } + } + + public static IEnumerable ChangeTimeZone(this IEnumerable dts, T1 tzSrc, T2 tzDst) where T1 : struct where T2 : struct + { + if (dts is null || !dts.Any()) + { + yield break; + } + foreach (var dt in dts) + { + yield return dt.ChangeTimeZone(tzSrc, tzDst); + } + } + + public static IEnumerable ChangeTimeZone(this IEnumerable dts, T tzDst) where T : struct + { + if (dts is null || !dts.Any()) + { + yield break; + } + foreach (var dt in dts) + { + yield return dt.ChangeTimeZone(tzDst); + } + } +} diff --git a/lib/YANLib/YANDateTime.Nullable.cs b/lib/YANLib/YANDateTime.Nullable.cs index ee22aca..6869ab9 100644 --- a/lib/YANLib/YANDateTime.Nullable.cs +++ b/lib/YANLib/YANDateTime.Nullable.cs @@ -1,4 +1,6 @@ -using static System.DateTime; +using System.Linq; +using static System.DateTime; +using static System.Linq.Enumerable; namespace YANLib; @@ -22,17 +24,7 @@ public static partial class YANDateTime /// The format of the input strings. /// The default value to return for any strings that fail to convert. /// An enumerable collection of objects for each successfully converted input string in the specified format, and the specified default value for any strings that fail to convert. - public static IEnumerable ToDateTime(this IEnumerable strs, string fmt, DateTime? dfltVal) - { - if (strs.AllNullOrWhiteSpace()) - { - yield break; - } - foreach (var str in strs) - { - yield return str.ToDateTime(fmt, dfltVal); - } - } + public static IEnumerable? ToDateTime(this IEnumerable strs, string fmt, DateTime? dfltVal) => strs.AllWhiteSpaceOrNull() ? default : strs.Select(s => s.ToDateTime(fmt, dfltVal)); /// /// Generates a random object between the specified minimum and maximum values. @@ -65,67 +57,21 @@ public static IEnumerable ToDateTime(this IEnumerable strs, st /// A randomly generated object between the minimum value and the specified maximum value. public static DateTime GenerateRandomDateTime(DateTime? max) => max.HasValue ? GenerateRandomDateTime(max.Value) : default; - public static IEnumerable GenerateRandomDateTimes(DateTime? min, DateTime? max, T size) where T : struct - { - for (var i = 0ul; i < size.ToUlong(); i++) - { - yield return GenerateRandomDateTime(min, max); - } - } + public static IEnumerable GenerateRandomDateTimes(DateTime? min, DateTime? max, T size) where T : struct => Range(0, size.ToUint().ToInt()).Select(i => GenerateRandomDateTime(min, max)); - public static IEnumerable GenerateRandomDateTimes(DateTime? min, DateTime? max, T? size) where T : struct - { - for (var i = 0ul; i < size.ToUlong(); i++) - { - yield return GenerateRandomDateTime(min, max); - } - } + public static IEnumerable GenerateRandomDateTimes(DateTime? min, DateTime? max, T? size) where T : struct => Range(0, size.ToUint().ToInt()).Select(i => GenerateRandomDateTime(min, max)); - public static IEnumerable GenerateRandomDateTimes(DateTime min, DateTime? max, T size) where T : struct - { - for (var i = 0ul; i < size.ToUlong(); i++) - { - yield return GenerateRandomDateTime(min, max); - } - } + public static IEnumerable GenerateRandomDateTimes(DateTime min, DateTime? max, T size) where T : struct => Range(0, size.ToUint().ToInt()).Select(i => GenerateRandomDateTime(min, max)); - public static IEnumerable GenerateRandomDateTimes(DateTime min, DateTime? max, T? size) where T : struct - { - for (var i = 0ul; i < size.ToUlong(); i++) - { - yield return GenerateRandomDateTime(min, max); - } - } + public static IEnumerable GenerateRandomDateTimes(DateTime min, DateTime? max, T? size) where T : struct => Range(0, size.ToUint().ToInt()).Select(i => GenerateRandomDateTime(min, max)); - public static IEnumerable GenerateRandomDateTimes(DateTime? min, DateTime max, T size) where T : struct - { - for (var i = 0ul; i < size.ToUlong(); i++) - { - yield return GenerateRandomDateTime(min, max); - } - } + public static IEnumerable GenerateRandomDateTimes(DateTime? min, DateTime max, T size) where T : struct => Range(0, size.ToUint().ToInt()).Select(i => GenerateRandomDateTime(min, max)); - public static IEnumerable GenerateRandomDateTimes(DateTime? min, DateTime max, T? size) where T : struct - { - for (var i = 0ul; i < size.ToUlong(); i++) - { - yield return GenerateRandomDateTime(min, max); - } - } + public static IEnumerable GenerateRandomDateTimes(DateTime? min, DateTime max, T? size) where T : struct => Range(0, size.ToUint().ToInt()).Select(i => GenerateRandomDateTime(min, max)); public static int GetWeekOfYear(this DateTime? dt) => dt.HasValue ? dt.Value.GetWeekOfYear() : default; - public static IEnumerable GetWeekOfYear(this IEnumerable dts) - { - if (dts is null || !dts.Any()) - { - yield break; - } - foreach (var dt in dts) - { - yield return dt.GetWeekOfYear(); - } - } + public static IEnumerable? GetWeekOfYear(this IEnumerable dts) => dts is null || !dts.Any() ? default : dts.Select(d => d.GetWeekOfYear()); public static int TotalMonths(DateTime? dt1, DateTime dt2) => dt1.HasValue ? TotalMonths(dt1.Value, dt2) : default; @@ -135,17 +81,7 @@ public static IEnumerable GetWeekOfYear(this IEnumerable dts) public static DateTime ChangeTimeZone(this DateTime? dt, T1 tzSrc, T2 tzDst) where T1 : struct where T2 : struct => dt.HasValue ? dt.Value.ChangeTimeZone(tzSrc, tzDst) : default; - public static IEnumerable ChangeTimeZone(this IEnumerable dts, T1 tzSrc, T2 tzDst) where T1 : struct where T2 : struct - { - if (dts is null || !dts.Any()) - { - yield break; - } - foreach (var dt in dts) - { - yield return dt.ChangeTimeZone(tzSrc, tzDst); - } - } + public static IEnumerable? ChangeTimeZone(this IEnumerable dts, T1 tzSrc, T2 tzDst) where T1 : struct where T2 : struct => dts is null || !dts.Any() ? default : dts.Select(d => d.ChangeTimeZone(tzSrc, tzDst)); public static void ChangeTimeZone(this IList dts, T1 tzSrc, T2 tzDst) where T1 : struct where T2 : struct { @@ -160,17 +96,7 @@ public static void ChangeTimeZone(this IList dts, T1 tzSrc, T public static DateTime ChangeTimeZone(this DateTime dt, T1? tzSrc, T2 tzDst) where T1 : struct where T2 : struct => tzSrc.HasValue ? dt.ChangeTimeZone(tzSrc.Value, tzDst) : dt.ChangeTimeZone(tzDst); - public static IEnumerable ChangeTimeZone(this IEnumerable dts, T1? tzSrc, T2 tzDst) where T1 : struct where T2 : struct - { - if (dts is null || !dts.Any()) - { - yield break; - } - foreach (var dt in dts) - { - yield return dt.ChangeTimeZone(tzSrc, tzDst); - } - } + public static IEnumerable? ChangeTimeZone(this IEnumerable dts, T1? tzSrc, T2 tzDst) where T1 : struct where T2 : struct => dts is null || !dts.Any() ? default : dts.Select(d => d.ChangeTimeZone(tzSrc, tzDst)); public static void ChangeTimeZone(this IList dts, T1? tzSrc, T2 tzDst) where T1 : struct where T2 : struct { @@ -185,17 +111,7 @@ public static void ChangeTimeZone(this IList dts, T1? tzSrc, T public static DateTime ChangeTimeZone(this DateTime dt, T1 tzSrc, T2? tzDst) where T1 : struct where T2 : struct => tzDst.HasValue ? dt.ChangeTimeZone(tzSrc, tzDst.Value) : dt; - public static IEnumerable ChangeTimeZone(this IEnumerable dts, T1 tzSrc, T2? tzDst) where T1 : struct where T2 : struct - { - if (dts is null || !dts.Any()) - { - yield break; - } - foreach (var dt in dts) - { - yield return dt.ChangeTimeZone(tzSrc, tzDst); - } - } + public static IEnumerable? ChangeTimeZone(this IEnumerable dts, T1 tzSrc, T2? tzDst) where T1 : struct where T2 : struct => dts is null || !dts.Any() ? default : dts.Select(d => d.ChangeTimeZone(tzSrc, tzDst)); public static void ChangeTimeZone(this IList dts, T1 tzSrc, T2? tzDst) where T1 : struct where T2 : struct { @@ -210,17 +126,7 @@ public static void ChangeTimeZone(this IList dts, T1 tzSrc, T2 public static DateTime ChangeTimeZone(this DateTime? dt, T1? tzSrc, T2 tzDst) where T1 : struct where T2 : struct => dt.HasValue ? dt.Value.ChangeTimeZone(tzSrc, tzDst) : default; - public static IEnumerable ChangeTimeZone(this IEnumerable dts, T1? tzSrc, T2 tzDst) where T1 : struct where T2 : struct - { - if (dts is null || !dts.Any()) - { - yield break; - } - foreach (var dt in dts) - { - yield return dt.ChangeTimeZone(tzSrc, tzDst); - } - } + public static IEnumerable? ChangeTimeZone(this IEnumerable dts, T1? tzSrc, T2 tzDst) where T1 : struct where T2 : struct => dts is null || !dts.Any() ? default : dts.Select(d => d.ChangeTimeZone(tzSrc, tzDst)); public static void ChangeTimeZone(this IList dts, T1? tzSrc, T2 tzDst) where T1 : struct where T2 : struct { @@ -235,17 +141,7 @@ public static void ChangeTimeZone(this IList dts, T1? tzSrc, public static DateTime ChangeTimeZone(this DateTime? dt, T1 tzSrc, T2? tzDst) where T1 : struct where T2 : struct => dt.HasValue ? dt.Value.ChangeTimeZone(tzSrc, tzDst) : default; - public static IEnumerable ChangeTimeZone(this IEnumerable dts, T1 tzSrc, T2? tzDst) where T1 : struct where T2 : struct - { - if (dts is null || !dts.Any()) - { - yield break; - } - foreach (var dt in dts) - { - yield return dt.ChangeTimeZone(tzSrc, tzDst); - } - } + public static IEnumerable? ChangeTimeZone(this IEnumerable dts, T1 tzSrc, T2? tzDst) where T1 : struct where T2 : struct => dts is null || !dts.Any() ? default : dts.Select(d => d.ChangeTimeZone(tzSrc, tzDst)); public static void ChangeTimeZone(this IList dts, T1 tzSrc, T2? tzDst) where T1 : struct where T2 : struct { @@ -260,17 +156,7 @@ public static void ChangeTimeZone(this IList dts, T1 tzSrc, T public static DateTime ChangeTimeZone(this DateTime dt, T1? tzSrc, T2? tzDst) where T1 : struct where T2 : struct => tzSrc.HasValue ? dt.ChangeTimeZone(tzSrc.Value, tzDst) : dt.ChangeTimeZone(tzDst); - public static IEnumerable ChangeTimeZone(this IEnumerable dts, T1? tzSrc, T2? tzDst) where T1 : struct where T2 : struct - { - if (dts is null || !dts.Any()) - { - yield break; - } - foreach (var dt in dts) - { - yield return dt.ChangeTimeZone(tzSrc, tzDst); - } - } + public static IEnumerable? ChangeTimeZone(this IEnumerable dts, T1? tzSrc, T2? tzDst) where T1 : struct where T2 : struct => dts is null || !dts.Any() ? default : dts.Select(d => d.ChangeTimeZone(tzSrc, tzDst)); public static void ChangeTimeZone(this IList dts, T1? tzSrc, T2? tzDst) where T1 : struct where T2 : struct { @@ -285,17 +171,7 @@ public static void ChangeTimeZone(this IList dts, T1? tzSrc, T public static DateTime ChangeTimeZone(this DateTime? dt, T1? tzSrc, T2? tzDst) where T1 : struct where T2 : struct => dt.HasValue ? dt.Value.ChangeTimeZone(tzSrc, tzDst) : default; - public static IEnumerable ChangeTimeZone(this IEnumerable dts, T1? tzSrc, T2? tzDst) where T1 : struct where T2 : struct - { - if (dts is null || !dts.Any()) - { - yield break; - } - foreach (var dt in dts) - { - yield return dt.ChangeTimeZone(tzSrc, tzDst); - } - } + public static IEnumerable? ChangeTimeZone(this IEnumerable dts, T1? tzSrc, T2? tzDst) where T1 : struct where T2 : struct => dts is null || !dts.Any() ? default : dts.Select(d => d.ChangeTimeZone(tzSrc, tzDst)); public static void ChangeTimeZone(this IList dts, T1? tzSrc, T2? tzDst) where T1 : struct where T2 : struct { @@ -310,17 +186,7 @@ public static void ChangeTimeZone(this IList dts, T1? tzSrc, public static DateTime ChangeTimeZone(this DateTime? dt, T tzDst) where T : struct => dt.ChangeTimeZone(0, tzDst); - public static IEnumerable ChangeTimeZone(this IEnumerable dts, T tzDst) where T : struct - { - if (dts is null || !dts.Any()) - { - yield break; - } - foreach (var dt in dts) - { - yield return dt.ChangeTimeZone(tzDst); - } - } + public static IEnumerable? ChangeTimeZone(this IEnumerable dts, T tzDst) where T : struct => dts is null || !dts.Any() ? default : dts.Select(d => d.ChangeTimeZone(tzDst)); public static void ChangeTimeZone(this IList dts, T tzDst) where T : struct { @@ -335,17 +201,7 @@ public static void ChangeTimeZone(this IList dts, T tzDst) where T public static DateTime ChangeTimeZone(this DateTime dt, T? tzDst) where T : struct => dt.ChangeTimeZone(0, tzDst); - public static IEnumerable ChangeTimeZone(this IEnumerable dts, T? tzDst) where T : struct - { - if (dts is null || !dts.Any()) - { - yield break; - } - foreach (var dt in dts) - { - yield return dt.ChangeTimeZone(tzDst); - } - } + public static IEnumerable? ChangeTimeZone(this IEnumerable dts, T? tzDst) where T : struct => dts is null || !dts.Any() ? default : dts.Select(d => d.ChangeTimeZone(tzDst)); public static void ChangeTimeZone(this IList dts, T? tzDst) where T : struct { @@ -360,17 +216,7 @@ public static void ChangeTimeZone(this IList dts, T? tzDst) where T public static DateTime ChangeTimeZone(this DateTime? dt, T? tzDst) where T : struct => dt.ChangeTimeZone(0, tzDst); - public static IEnumerable ChangeTimeZone(this IEnumerable dts, T? tzDst) where T : struct - { - if (dts is null || !dts.Any()) - { - yield break; - } - foreach (var dt in dts) - { - yield return dt.ChangeTimeZone(tzDst); - } - } + public static IEnumerable? ChangeTimeZone(this IEnumerable dts, T? tzDst) where T : struct => dts is null || !dts.Any() ? default : dts.Select(d => d.ChangeTimeZone(tzDst)); public static void ChangeTimeZone(this IList dts, T? tzDst) where T : struct { diff --git a/lib/YANLib/YANDateTime.cs b/lib/YANLib/YANDateTime.cs index 9c17504..f8bb3eb 100644 --- a/lib/YANLib/YANDateTime.cs +++ b/lib/YANLib/YANDateTime.cs @@ -2,6 +2,7 @@ using static System.Globalization.CultureInfo; using static System.Globalization.DateTimeFormatInfo; using static System.Globalization.DateTimeStyles; +using static System.Linq.Enumerable; using static System.Math; using static YANLib.YANNum; @@ -23,17 +24,7 @@ public static partial class YANDateTime /// /// The collection of strings to be converted to . /// An enumerable collection of objects for each successfully converted input string. - public static IEnumerable ToDateTime(this IEnumerable strs) - { - if (strs.AllNullOrWhiteSpace()) - { - yield break; - } - foreach (var str in strs) - { - yield return str.ToDateTime(); - } - } + public static IEnumerable? ToDateTime(this IEnumerable strs) => strs.AllWhiteSpaceOrNull() ? default : strs.Select(s => s.ToDateTime()); /// /// Converts the specified string representation of a date and time to its equivalent using the specified format. @@ -51,17 +42,7 @@ public static IEnumerable ToDateTime(this IEnumerable strs) /// The collection of strings to be converted to . /// The format of the input strings. /// An enumerable collection of objects for each successfully converted input string in the specified format. - public static IEnumerable ToDateTime(this IEnumerable strs, string fmt) - { - if (strs.AllNullOrWhiteSpace()) - { - yield break; - } - foreach (var str in strs) - { - yield return str.ToDateTime(fmt); - } - } + public static IEnumerable? ToDateTime(this IEnumerable strs, string fmt) => strs.AllWhiteSpaceOrNull() ? default : strs.Select(s => s.ToDateTime(fmt)); /// /// Converts the specified string representation of a date and time to its equivalent using the specified format. @@ -81,17 +62,7 @@ public static IEnumerable ToDateTime(this IEnumerable strs, st /// The format of the input strings. /// The default value to return for any strings that fail to convert. /// An enumerable collection of objects for each successfully converted input string in the specified format, and the specified default value for any strings that fail to convert. - public static IEnumerable ToDateTime(this IEnumerable strs, string fmt, DateTime dfltVal) - { - if (strs.AllNullOrWhiteSpace()) - { - yield break; - } - foreach (var str in strs) - { - yield return str.ToDateTime(fmt, dfltVal); - } - } + public static IEnumerable? ToDateTime(this IEnumerable strs, string fmt, DateTime dfltVal) => strs.AllWhiteSpaceOrNull() ? default : strs.Select(s => s.ToDateTime(fmt, dfltVal)); /// /// Generates a random object between the specified minimum and maximum values. @@ -114,27 +85,11 @@ public static IEnumerable ToDateTime(this IEnumerable strs, st /// A randomly generated object between the minimum value and the specified maximum value. public static DateTime GenerateRandomDateTime(DateTime max) => GenerateRandomDateTime(max > Today ? Today : MinValue, max); - public static IEnumerable GenerateRandomDateTimes(DateTime min, DateTime max, T size) where T : struct - { - for (var i = 0ul; i < size.ToUlong(); i++) - { - yield return GenerateRandomDateTime(min, max); - } - } + public static IEnumerable GenerateRandomDateTimes(DateTime min, DateTime max, T size) where T : struct => Range(0, size.ToUint().ToInt()).Select(i => GenerateRandomDateTime(min, max)); public static int GetWeekOfYear(this DateTime dt) => CurrentInfo.Calendar.GetWeekOfYear(dt, CurrentInfo.CalendarWeekRule, CurrentInfo.FirstDayOfWeek); - public static IEnumerable GetWeekOfYear(this IEnumerable dts) - { - if (dts is null || !dts.Any()) - { - yield break; - } - foreach (var dt in dts) - { - yield return dt.GetWeekOfYear(); - } - } + public static IEnumerable? GetWeekOfYear(this IEnumerable dts) => dts is null || !dts.Any() ? default : dts.Select(d => d.GetWeekOfYear()); public static int TotalMonths(DateTime dt1, DateTime dt2) => Abs((dt1.Year - dt2.Year) * 12 + dt1.Month - dt2.Month); @@ -149,17 +104,7 @@ public static DateTime ChangeTimeZone(this DateTime dt, T1 tzSrc, T2 tzD }; } - public static IEnumerable ChangeTimeZone(this IEnumerable dts, T1 tzSrc, T2 tzDst) where T1 : struct where T2 : struct - { - if (dts is null || !dts.Any()) - { - yield break; - } - foreach (var dt in dts) - { - yield return dt.ChangeTimeZone(tzSrc, tzDst); - } - } + public static IEnumerable? ChangeTimeZone(this IEnumerable dts, T1 tzSrc, T2 tzDst) where T1 : struct where T2 : struct => dts is null || !dts.Any() ? default : dts.Select(d => d.ChangeTimeZone(tzSrc, tzDst)); public static void ChangeTimeZone(this IList dts, T1 tzSrc, T2 tzDst) where T1 : struct where T2 : struct { @@ -174,17 +119,7 @@ public static void ChangeTimeZone(this IList dts, T1 tzSrc, T2 public static DateTime ChangeTimeZone(this DateTime dt, T tzDst) where T : struct => dt.ChangeTimeZone(0, tzDst); - public static IEnumerable ChangeTimeZone(this IEnumerable dts, T tzDst) where T : struct - { - if (dts is null || !dts.Any()) - { - yield break; - } - foreach (var dt in dts) - { - yield return dt.ChangeTimeZone(tzDst); - } - } + public static IEnumerable? ChangeTimeZone(this IEnumerable dts, T tzDst) where T : struct => dts is null || !dts.Any() ? default : dts.Select(d => d.ChangeTimeZone(tzDst)); public static void ChangeTimeZone(this IList dts, T tzDst) where T : struct { diff --git a/lib/YANLib/YANEnumerable.cs b/lib/YANLib/YANEnumerable.cs index e5a0650..6448549 100644 --- a/lib/YANLib/YANEnumerable.cs +++ b/lib/YANLib/YANEnumerable.cs @@ -10,7 +10,7 @@ public static partial class YANEnumerable public static IEnumerable> ChunkBySize(this List srcs, T1 chunkSize) where T1 : struct { var size = chunkSize.ToInt(); - if (srcs.IsNullOrEmpty() && size < 1) + if (srcs.IsEmptyOrNull() && size < 1) { yield break; } @@ -23,7 +23,7 @@ public static IEnumerable> ChunkBySize(this List srcs, T1 chun public static IEnumerable Clean(this IEnumerable srcs) { - if (srcs.IsNullOrEmpty()) + if (srcs.IsEmptyOrNull()) { yield break; } @@ -49,7 +49,7 @@ public static IEnumerable Clean(this IEnumerable srcs) public static void Clean(this ICollection srcs) { - if (srcs.IsNotNullAndEmpty()) + if (srcs.IsNotEmptyAndNull()) { var t = typeof(T); if (t.IsClass || GetUnderlyingType(t) is not null) @@ -73,7 +73,7 @@ public static IEnumerable Clean(this IEnumerable srcs) } foreach (var src in srcs) { - if (src.IsNotNullAndWhiteSpace()) + if (src.IsNotWhiteSpaceAndNull()) { yield return src; } @@ -86,7 +86,7 @@ public static void Clean(this ICollection srcs) { foreach (var src in srcs) { - if (src.IsNullOrWhiteSpace()) + if (src.IsWhiteSpaceOrNull()) { _ = srcs.Remove(src); } @@ -94,19 +94,19 @@ public static void Clean(this ICollection srcs) } } - public static bool IsNullOrEmpty(this IEnumerable srcs) => srcs is null || !srcs.Any(); + public static bool IsEmptyOrNull(this IEnumerable srcs) => srcs is null || !srcs.Any(); - public static bool IsNullOrEmpty(this IReadOnlyCollection srcs) => srcs is null || srcs.Count < 1; + public static bool IsEmptyOrNull(this ICollection srcs) => srcs is null || srcs.Count < 1; - public static bool IsNotNullAndEmpty(this IEnumerable srcs) => srcs is not null && srcs.Any(); + public static bool IsNotEmptyAndNull(this IEnumerable srcs) => srcs is not null && srcs.Any(); - public static bool IsNotNullAndEmpty(this IReadOnlyCollection srcs) => srcs is not null && srcs.Count > 0; + public static bool IsNotEmptyAndNull(this ICollection srcs) => srcs is not null && srcs.Count > 0; - public static bool AllNullOrEmpty(IEnumerable srcs) where T : class => !srcs.Any(x => x is not null || x.AnyPropertiesNotDefault()); + public static bool AllEmptyOrNull(this IEnumerable srcs) where T : class => !srcs.Any(x => x is not null || x.AnyPropertiesNotDefault()); - public static bool AnyNullOrEmpty(IEnumerable srcs) where T : class => srcs.Any(x => x is null || x.AllPropertiesDefault()); + public static bool AnyEmptyOrNull(this IEnumerable srcs) where T : class => srcs.Any(x => x is null || x.AllPropertiesDefault()); - public static bool AllNotNullAndEmpty(IEnumerable srcs) where T : class => !srcs.Any(x => x is null || x.AllPropertiesDefault()); + public static bool AllNotEmptyAndNull(this IEnumerable srcs) where T : class => !srcs.Any(x => x is null || x.AllPropertiesDefault()); - public static bool AnyNotNullAndEmpty(IEnumerable srcs) where T : class => srcs.Any(x => x is not null || x.AnyPropertiesNotDefault()); + public static bool AnyNotEmptyAndNull(this IEnumerable srcs) where T : class => srcs.Any(x => x is not null || x.AnyPropertiesNotDefault()); } diff --git a/lib/YANLib/YANModel.Property.cs b/lib/YANLib/YANModel.Property.cs index 4666fa3..27bfdd7 100644 --- a/lib/YANLib/YANModel.Property.cs +++ b/lib/YANLib/YANModel.Property.cs @@ -23,7 +23,7 @@ public static bool AllPropertiesNotDefault(this T? mdl) where T : class return true; } - public static bool AllPropertiesNotDefault(this IEnumerable mdls) where T : class => mdls.IsNotNullAndEmpty() && !mdls.Any(x => x.AnyPropertiesDefault()); + public static bool AllPropertiesNotDefault(this IEnumerable mdls) where T : class => mdls.IsNotEmptyAndNull() && !mdls.Any(x => x.AnyPropertiesDefault()); public static bool AllPropertiesDefault(this T? mdl) where T : class { @@ -42,7 +42,7 @@ public static bool AllPropertiesDefault(this T? mdl) where T : class return true; } - public static bool AllPropertiesDefault(this IEnumerable mdls) where T : class => mdls.IsNotNullAndEmpty() && !mdls.Any(x => x.AnyPropertiesNotDefault()); + public static bool AllPropertiesDefault(this IEnumerable mdls) where T : class => mdls.IsNotEmptyAndNull() && !mdls.Any(x => x.AnyPropertiesNotDefault()); public static bool AnyPropertiesNotDefault(this T? mdl) where T : class { @@ -61,7 +61,7 @@ public static bool AnyPropertiesNotDefault(this T? mdl) where T : class return false; } - public static bool AnyPropertiesNotDefault(this IEnumerable mdls) where T : class => mdls.IsNotNullAndEmpty() && mdls.Any(x => x.AnyPropertiesNotDefault()); + public static bool AnyPropertiesNotDefault(this IEnumerable mdls) where T : class => mdls.IsNotEmptyAndNull() && mdls.Any(x => x.AnyPropertiesNotDefault()); public static bool AnyPropertiesDefault(this T? mdl) where T : class { @@ -80,11 +80,11 @@ public static bool AnyPropertiesDefault(this T? mdl) where T : class return false; } - public static bool AnyPropertiesDefault(this IEnumerable mdls) where T : class => mdls.IsNotNullAndEmpty() && mdls.Any(x => x.AnyPropertiesDefault()); + public static bool AnyPropertiesDefault(this IEnumerable mdls) where T : class => mdls.IsNotEmptyAndNull() && mdls.Any(x => x.AnyPropertiesDefault()); public static bool AllPropertiesNotDefault(this T? mdl, params string[] names) where T : class { - if (mdl is null || names.AllNullOrWhiteSpace()) + if (mdl is null || names.AllWhiteSpaceOrNull()) { return false; } @@ -99,11 +99,11 @@ public static bool AllPropertiesNotDefault(this T? mdl, params string[] names return true; } - public static bool AllPropertiesNotDefault(this IEnumerable mdls, params string[] names) where T : class => mdls.IsNotNullAndEmpty() && !mdls.Any(x => x.AnyPropertiesDefault(names)); + public static bool AllPropertiesNotDefault(this IEnumerable mdls, params string[] names) where T : class => mdls.IsNotEmptyAndNull() && !mdls.Any(x => x.AnyPropertiesDefault(names)); public static bool AllPropertiesDefault(this T? mdl, params string[] names) where T : class { - if (mdl is null || names.AllNullOrWhiteSpace()) + if (mdl is null || names.AllWhiteSpaceOrNull()) { return false; } @@ -118,11 +118,11 @@ public static bool AllPropertiesDefault(this T? mdl, params string[] names) w return true; } - public static bool AllPropertiesDefault(this IEnumerable mdls, params string[] names) where T : class => mdls.IsNotNullAndEmpty() && !mdls.Any(x => x.AnyPropertiesNotDefault(names)); + public static bool AllPropertiesDefault(this IEnumerable mdls, params string[] names) where T : class => mdls.IsNotEmptyAndNull() && !mdls.Any(x => x.AnyPropertiesNotDefault(names)); public static bool AnyPropertiesNotDefault(this T? mdl, params string[] names) where T : class { - if (mdl is null || names.AllNullOrWhiteSpace()) + if (mdl is null || names.AllWhiteSpaceOrNull()) { return false; } @@ -137,11 +137,11 @@ public static bool AnyPropertiesNotDefault(this T? mdl, params string[] names return false; } - public static bool AnyPropertiesNotDefault(this IEnumerable mdls, params string[] names) where T : class => mdls.IsNotNullAndEmpty() && mdls.Any(x => x.AnyPropertiesNotDefault(names)); + public static bool AnyPropertiesNotDefault(this IEnumerable mdls, params string[] names) where T : class => mdls.IsNotEmptyAndNull() && mdls.Any(x => x.AnyPropertiesNotDefault(names)); public static bool AnyPropertiesDefault(this T? mdl, params string[] names) where T : class { - if (mdl is null || names.AllNullOrWhiteSpace()) + if (mdl is null || names.AllWhiteSpaceOrNull()) { return false; } @@ -156,11 +156,11 @@ public static bool AnyPropertiesDefault(this T? mdl, params string[] names) w return false; } - public static bool AnyPropertiesDefault(this IEnumerable mdls, params string[] names) where T : class => mdls.IsNotNullAndEmpty() && mdls.Any(x => x.AnyPropertiesDefault(names)); + public static bool AnyPropertiesDefault(this IEnumerable mdls, params string[] names) where T : class => mdls.IsNotEmptyAndNull() && mdls.Any(x => x.AnyPropertiesDefault(names)); public static bool AllPropertiesNotDefault(this T? mdl, IEnumerable names) where T : class { - if (mdl is null || names.AllNullOrWhiteSpace()) + if (mdl is null || names.AllWhiteSpaceOrNull()) { return false; } @@ -175,11 +175,11 @@ public static bool AllPropertiesNotDefault(this T? mdl, IEnumerable n return true; } - public static bool AllPropertiesNotDefault(this IEnumerable mdls, IEnumerable names) where T : class => mdls.IsNotNullAndEmpty() && !mdls.Any(x => x.AnyPropertiesDefault(names)); + public static bool AllPropertiesNotDefault(this IEnumerable mdls, IEnumerable names) where T : class => mdls.IsNotEmptyAndNull() && !mdls.Any(x => x.AnyPropertiesDefault(names)); public static bool AllPropertiesDefault(this T? mdl, IEnumerable names) where T : class { - if (mdl is null || names.AllNullOrWhiteSpace()) + if (mdl is null || names.AllWhiteSpaceOrNull()) { return false; } @@ -194,11 +194,11 @@ public static bool AllPropertiesDefault(this T? mdl, IEnumerable name return true; } - public static bool AllPropertiesDefault(this IEnumerable mdls, IEnumerable names) where T : class => mdls.IsNotNullAndEmpty() && !mdls.Any(x => x.AnyPropertiesNotDefault(names)); + public static bool AllPropertiesDefault(this IEnumerable mdls, IEnumerable names) where T : class => mdls.IsNotEmptyAndNull() && !mdls.Any(x => x.AnyPropertiesNotDefault(names)); public static bool AnyPropertiesNotDefault(this T? mdl, IEnumerable names) where T : class { - if (mdl is null || names.AllNullOrWhiteSpace()) + if (mdl is null || names.AllWhiteSpaceOrNull()) { return false; } @@ -213,11 +213,11 @@ public static bool AnyPropertiesNotDefault(this T? mdl, IEnumerable n return false; } - public static bool AnyPropertiesNotDefault(this IEnumerable mdls, IEnumerable names) where T : class => mdls.IsNotNullAndEmpty() && mdls.Any(x => x.AnyPropertiesNotDefault(names)); + public static bool AnyPropertiesNotDefault(this IEnumerable mdls, IEnumerable names) where T : class => mdls.IsNotEmptyAndNull() && mdls.Any(x => x.AnyPropertiesNotDefault(names)); public static bool AnyPropertiesDefault(this T? mdl, IEnumerable names) where T : class { - if (mdl is null || names.AllNullOrWhiteSpace()) + if (mdl is null || names.AllWhiteSpaceOrNull()) { return false; } @@ -232,5 +232,5 @@ public static bool AnyPropertiesDefault(this T? mdl, IEnumerable name return false; } - public static bool AnyPropertiesDefault(this IEnumerable mdls, IEnumerable names) where T : class => mdls.IsNotNullAndEmpty() && mdls.Any(x => x.AnyPropertiesDefault(names)); + public static bool AnyPropertiesDefault(this IEnumerable mdls, IEnumerable names) where T : class => mdls.IsNotEmptyAndNull() && mdls.Any(x => x.AnyPropertiesDefault(names)); } diff --git a/lib/YANLib/YANPass.cs b/lib/YANLib/YANPass.cs index 1428d0c..ee8be6b 100644 --- a/lib/YANLib/YANPass.cs +++ b/lib/YANLib/YANPass.cs @@ -27,7 +27,7 @@ public class YANPass public string? Hash(string password) { - if (password.IsNotNullAndWhiteSpace() && SaltSize > 0 && Iterations > 0 && KeySize > 0) + if (password.IsNotWhiteSpaceAndNull() && SaltSize > 0 && Iterations > 0 && KeySize > 0) { var salt = GetBytes(SaltSize); return new StringBuilder().Append(ToHexString(Pbkdf2(password, salt, Iterations, Algorithm, KeySize))).Append(SegmentDelimiter).Append(ToHexString(salt)).Append(SegmentDelimiter).Append(Iterations).Append(SegmentDelimiter).Append(Algorithm).ToString(); @@ -37,7 +37,7 @@ public class YANPass public bool Verify(string password, string strHash) { - if (strHash.IsNotNullAndWhiteSpace() && SegmentDelimiter.IsNotEmptyAndWhiteSpace()) + if (strHash.IsNotWhiteSpaceAndNull() && SegmentDelimiter.IsNotEmptyAndWhiteSpace()) { var segs = strHash.Split(SegmentDelimiter); if (segs.Length > 3) @@ -49,14 +49,14 @@ public bool Verify(string password, string strHash) return false; } - public static bool IsValidPasswordStandard(string password) => password.IsNotNullAndWhiteSpace() && password.Length >= 8 && new Regex(@"^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)(?=.*[^\da-zA-Z]).+$").IsMatch(password); + public static bool IsValidPasswordStandard(string password) => password.IsNotWhiteSpaceAndNull() && password.Length >= 8 && new Regex(@"^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)(?=.*[^\da-zA-Z]).+$").IsMatch(password); - public static bool IsValidPassword(string password, T len) where T : struct => password.IsNotNullAndWhiteSpace() && password.Length >= len.ToByte() && new Regex(@"^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)(?=.*[^\da-zA-Z]).+$").IsMatch(password); + public static bool IsValidPassword(string password, T len) where T : struct => password.IsNotWhiteSpaceAndNull() && password.Length >= len.ToByte() && new Regex(@"^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)(?=.*[^\da-zA-Z]).+$").IsMatch(password); public bool IsValidPassword(string password, params char[] splChars) { // has character - if (!password.IsNotNullAndWhiteSpace()) + if (!password.IsNotWhiteSpaceAndNull()) { return false; } @@ -74,7 +74,7 @@ public bool IsValidPassword(string password, params char[] splChars) public bool IsValidPassword(string password, IEnumerable splChars) { // has character - if (!password.IsNotNullAndWhiteSpace()) + if (!password.IsNotWhiteSpaceAndNull()) { return false; } @@ -92,7 +92,7 @@ public bool IsValidPassword(string password, IEnumerable splChars) public bool IsValidPassword(string password, T len, params char[] splChars) where T : struct { // has character - if (!password.IsNotNullAndWhiteSpace()) + if (!password.IsNotWhiteSpaceAndNull()) { return false; } @@ -110,7 +110,7 @@ public bool IsValidPassword(string password, T len, params char[] splChars) w public bool IsValidPassword(string password, T len, IEnumerable splChars) where T : struct { // has character - if (!password.IsNotNullAndWhiteSpace()) + if (!password.IsNotWhiteSpaceAndNull()) { return false; } diff --git a/lib/YANLib/YANProcess.cs b/lib/YANLib/YANProcess.cs index dac329f..05a5bb2 100644 --- a/lib/YANLib/YANProcess.cs +++ b/lib/YANLib/YANProcess.cs @@ -8,7 +8,7 @@ public static partial class YANProcess public static async Task KillAllProcessesByName(this string name) { - if (name.IsNotNullAndWhiteSpace()) + if (name.IsNotWhiteSpaceAndNull()) { await WhenAll(GetProcessesByName(name).Select(p => { @@ -23,7 +23,7 @@ await WhenAll(GetProcessesByName(name).Select(p => public static async Task KillAllProcessesByName(params string[] names) { - if (names.AllNotNullAndWhiteSpace()) + if (names.AllNotWhiteSpaceAndNull()) { await WhenAll(names.SelectMany(name => GetProcessesByName(name)).Select(p => { @@ -38,7 +38,7 @@ await WhenAll(names.SelectMany(name => GetProcessesByName(name)).Select(p => public static async Task KillAllProcessesByName(this IEnumerable names) { - if (names.AllNotNullAndWhiteSpace()) + if (names.AllNotWhiteSpaceAndNull()) { await WhenAll(names.SelectMany(name => GetProcessesByName(name)).Select(p => { diff --git a/lib/YANLib/YANText.Char.cs b/lib/YANLib/YANText.Char.cs index c5fd99d..e47aaa4 100644 --- a/lib/YANLib/YANText.Char.cs +++ b/lib/YANLib/YANText.Char.cs @@ -25,15 +25,15 @@ public static partial class YANText public static bool AnyWhiteSpace(this IEnumerable cs) => cs is not null && cs.Any(c => c.IsWhiteSpace()); - public static bool IsNullOrWhiteSpace(this char c) => c.IsEmpty() || c.IsWhiteSpace(); + public static bool IsWhiteSpaceOrNull(this char c) => c.IsEmpty() || c.IsWhiteSpace(); - public static bool AllNullOrWhiteSpace(params char[] cs) => cs is not null && !cs.Any(c => c.IsNotEmptyAndWhiteSpace()); + public static bool AllWhiteSpaceOrNull(params char[] cs) => cs is not null && !cs.Any(c => c.IsNotEmptyAndWhiteSpace()); - public static bool AnyNullOrWhiteSpace(params char[] cs) => cs is not null && cs.Any(c => c.IsNullOrWhiteSpace()); + public static bool AnyWhiteSpaceOrNull(params char[] cs) => cs is not null && cs.Any(c => c.IsWhiteSpaceOrNull()); - public static bool AllNullOrWhiteSpace(this IEnumerable cs) => cs is not null && !cs.Any(c => c.IsNotEmptyAndWhiteSpace()); + public static bool AllWhiteSpaceOrNull(this IEnumerable cs) => cs is not null && !cs.Any(c => c.IsNotEmptyAndWhiteSpace()); - public static bool AnyNullOrWhiteSpace(this IEnumerable cs) => cs is not null && cs.Any(c => c.IsNullOrWhiteSpace()); + public static bool AnyWhiteSpaceOrNull(this IEnumerable cs) => cs is not null && cs.Any(c => c.IsWhiteSpaceOrNull()); public static bool IsAlphabetic(this char c) => char.IsLetter(c); @@ -97,11 +97,11 @@ public static partial class YANText public static bool IsNotEmptyAndWhiteSpace(this char c) => c.IsNotEmpty() && c.IsNotWhiteSpace(); - public static bool AllNotEmptyAndWhiteSpace(params char[] cs) => cs is not null && !cs.Any(c => c.IsNullOrWhiteSpace()); + public static bool AllNotEmptyAndWhiteSpace(params char[] cs) => cs is not null && !cs.Any(c => c.IsWhiteSpaceOrNull()); public static bool AnyNotEmptyAndWhiteSpace(params char[] cs) => cs is not null && cs.Any(c => c.IsNotEmptyAndWhiteSpace()); - public static bool AllNotEmptyAndWhiteSpace(this IEnumerable cs) => cs is not null && !cs.Any(c => c.IsNullOrWhiteSpace()); + public static bool AllNotEmptyAndWhiteSpace(this IEnumerable cs) => cs is not null && !cs.Any(c => c.IsWhiteSpaceOrNull()); public static bool AnyNotEmptyAndWhiteSpace(this IEnumerable cs) => cs is not null && cs.Any(c => c.IsNotEmptyAndWhiteSpace()); @@ -197,7 +197,7 @@ public static IEnumerable GenerateRandomCharacters(T size) where T : st public static IEnumerable ToLower(this IEnumerable cs) { - if (cs.IsNullOrEmpty()) + if (cs.IsEmptyOrNull()) { yield break; } @@ -209,7 +209,7 @@ public static IEnumerable ToLower(this IEnumerable cs) public static void ToLower(this IList cs) { - if (cs.IsNotNullAndEmpty()) + if (cs.IsNotEmptyAndNull()) { for (var i = 0; i < cs.Count; i++) { @@ -222,7 +222,7 @@ public static void ToLower(this IList cs) public static IEnumerable ToLowerInvariant(this IEnumerable cs) { - if (cs.IsNullOrEmpty()) + if (cs.IsEmptyOrNull()) { yield break; } @@ -234,7 +234,7 @@ public static IEnumerable ToLowerInvariant(this IEnumerable cs) public static void ToLowerInvariant(this IList cs) { - if (cs.IsNotNullAndEmpty()) + if (cs.IsNotEmptyAndNull()) { for (var i = 0; i < cs.Count; i++) { @@ -247,7 +247,7 @@ public static void ToLowerInvariant(this IList cs) public static IEnumerable ToUpper(this IEnumerable cs) { - if (cs.IsNullOrEmpty()) + if (cs.IsEmptyOrNull()) { yield break; } @@ -259,7 +259,7 @@ public static IEnumerable ToUpper(this IEnumerable cs) public static void ToUpper(this IList cs) { - if (cs.IsNotNullAndEmpty()) + if (cs.IsNotEmptyAndNull()) { for (var i = 0; i < cs.Count; i++) { @@ -272,7 +272,7 @@ public static void ToUpper(this IList cs) public static IEnumerable ToUpperInvariant(this IEnumerable cs) { - if (cs.IsNullOrEmpty()) + if (cs.IsEmptyOrNull()) { yield break; } @@ -284,7 +284,7 @@ public static IEnumerable ToUpperInvariant(this IEnumerable cs) public static void ToUpperInvariant(this IList cs) { - if (cs.IsNotNullAndEmpty()) + if (cs.IsNotEmptyAndNull()) { for (var i = 0; i < cs.Count; i++) { diff --git a/lib/YANLib/YANText.String.cs b/lib/YANLib/YANText.String.cs index 8db3206..9ffdef9 100644 --- a/lib/YANLib/YANText.String.cs +++ b/lib/YANLib/YANText.String.cs @@ -17,25 +17,25 @@ public static partial class YANText public static bool AnyNull(this IEnumerable strs) => strs is not null && strs.Any(s => s.IsNull()); - public static bool IsNullOrEmpty(this string str) => string.IsNullOrEmpty(str); + public static bool IsEmptyOrNull(this string str) => string.IsNullOrEmpty(str); - public static bool AllNullOrEmpty(params string[] strs) => strs is not null && !strs.Any(s => s.IsNotNullAndEmpty()); + public static bool AllEmptyOrNull(params string[] strs) => strs is not null && !strs.Any(s => s.IsNotEmptyAndNull()); - public static bool AnyNullOrEmpty(params string[] strs) => strs is not null && strs.Any(s => s.IsNullOrEmpty()); + public static bool AnyEmptyOrNull(params string[] strs) => strs is not null && strs.Any(s => s.IsEmptyOrNull()); - public static bool AllNullOrEmpty(this IEnumerable strs) => strs is not null && !strs.Any(s => s.IsNotNullAndEmpty()); + public static bool AllEmptyOrNull(this IEnumerable strs) => strs is not null && !strs.Any(s => s.IsNotEmptyAndNull()); - public static bool AnyNullOrEmpty(this IEnumerable strs) => strs is not null && strs.Any(s => s.IsNullOrEmpty()); + public static bool AnyEmptyOrNull(this IEnumerable strs) => strs is not null && strs.Any(s => s.IsEmptyOrNull()); - public static bool IsNullOrWhiteSpace(this string str) => string.IsNullOrWhiteSpace(str); + public static bool IsWhiteSpaceOrNull(this string str) => string.IsNullOrWhiteSpace(str); - public static bool AllNullOrWhiteSpace(params string[] strs) => strs is not null && !strs.Any(s => s.IsNotNullAndWhiteSpace()); + public static bool AllWhiteSpaceOrNull(params string[] strs) => strs is not null && !strs.Any(s => s.IsNotWhiteSpaceAndNull()); - public static bool AnyNullOrWhiteSpace(params string[] strs) => strs is not null && strs.Any(s => s.IsNullOrWhiteSpace()); + public static bool AnyWhiteSpaceOrNull(params string[] strs) => strs is not null && strs.Any(s => s.IsWhiteSpaceOrNull()); - public static bool AllNullOrWhiteSpace(this IEnumerable strs) => strs is not null && !strs.Any(s => s.IsNotNullAndWhiteSpace()); + public static bool AllWhiteSpaceOrNull(this IEnumerable strs) => strs is not null && !strs.Any(s => s.IsNotWhiteSpaceAndNull()); - public static bool AnyNullOrWhiteSpace(this IEnumerable strs) => strs is not null && strs.Any(s => s.IsNullOrWhiteSpace()); + public static bool AnyWhiteSpaceOrNull(this IEnumerable strs) => strs is not null && strs.Any(s => s.IsWhiteSpaceOrNull()); public static bool EqualsIgnoreCase(this string str1, string str2) => AllNull(str1, str2) || str1.IsNotNull() && str1.IsNotNull() && string.Equals(str1, str2, OrdinalIgnoreCase); @@ -57,25 +57,25 @@ public static partial class YANText public static bool AnyNotNull(this IEnumerable strs) => strs is not null && strs.Any(s => s.IsNotNull()); - public static bool IsNotNullAndEmpty(this string str) => !string.IsNullOrEmpty(str); + public static bool IsNotEmptyAndNull(this string str) => !string.IsNullOrEmpty(str); - public static bool AllNotNullAndEmpty(params string[] strs) => strs is not null && !strs.Any(s => s.IsNullOrEmpty()); + public static bool AllNotEmptyAndNull(params string[] strs) => strs is not null && !strs.Any(s => s.IsEmptyOrNull()); - public static bool AnyNotNullAndEmpty(params string[] strs) => strs is not null && strs.Any(s => s.IsNotNullAndEmpty()); + public static bool AnyNotEmptyAndNull(params string[] strs) => strs is not null && strs.Any(s => s.IsNotEmptyAndNull()); - public static bool AllNotNullAndEmpty(this IEnumerable strs) => strs is not null && !strs.Any(s => s.IsNullOrEmpty()); + public static bool AllNotEmptyAndNull(this IEnumerable strs) => strs is not null && !strs.Any(s => s.IsEmptyOrNull()); - public static bool AnyNotNullAndEmpty(this IEnumerable strs) => strs is not null && strs.Any(s => s.IsNotNullAndEmpty()); + public static bool AnyNotEmptyAndNull(this IEnumerable strs) => strs is not null && strs.Any(s => s.IsNotEmptyAndNull()); - public static bool IsNotNullAndWhiteSpace(this string str) => !string.IsNullOrWhiteSpace(str); + public static bool IsNotWhiteSpaceAndNull(this string str) => !string.IsNullOrWhiteSpace(str); - public static bool AllNotNullAndWhiteSpace(params string[] strs) => strs is not null && !strs.Any(s => s.IsNullOrWhiteSpace()); + public static bool AllNotWhiteSpaceAndNull(params string[] strs) => strs is not null && !strs.Any(s => s.IsWhiteSpaceOrNull()); - public static bool AnyNotNullAndWhiteSpace(params string[] strs) => strs is not null && strs.Any(s => s.IsNotNullAndWhiteSpace()); + public static bool AnyNotWhiteSpaceAndNull(params string[] strs) => strs is not null && strs.Any(s => s.IsNotWhiteSpaceAndNull()); - public static bool AllNotNullAndWhiteSpace(this IEnumerable strs) => strs is not null && !strs.Any(s => s.IsNullOrWhiteSpace()); + public static bool AllNotWhiteSpaceAndNull(this IEnumerable strs) => strs is not null && !strs.Any(s => s.IsWhiteSpaceOrNull()); - public static bool AnyNotNullAndWhiteSpace(this IEnumerable strs) => strs is not null && strs.Any(s => s.IsNotNullAndWhiteSpace()); + public static bool AnyNotWhiteSpaceAndNull(this IEnumerable strs) => strs is not null && strs.Any(s => s.IsNotWhiteSpaceAndNull()); public static bool NotEqualsIgnoreCase(this string str1, string str2) => AllNotNull(str1, str2) && !string.Equals(str1, str2, OrdinalIgnoreCase); @@ -123,11 +123,11 @@ public static bool AllNotEqualsIgnoreCase(this IEnumerable strs) public static string GetValue(this string str, char dfltVal) => str ?? (dfltVal.IsNotEmpty() ? dfltVal.ToString() : string.Empty); - public static string ToLower(this string str) => str.IsNotNullAndWhiteSpace() ? str.ToLower() : str; + public static string ToLower(this string str) => str.IsNotWhiteSpaceAndNull() ? str.ToLower() : str; public static IEnumerable ToLower(this IEnumerable strs) { - if (strs.IsNullOrEmpty()) + if (strs.IsEmptyOrNull()) { yield break; } @@ -139,7 +139,7 @@ public static IEnumerable ToLower(this IEnumerable strs) public static void ToLower(this ICollection strs) { - if (strs.IsNotNullAndEmpty()) + if (strs.IsNotEmptyAndNull()) { foreach (var str in strs) { @@ -148,11 +148,11 @@ public static void ToLower(this ICollection strs) } } - public static string ToLowerInvariant(this string str) => str.IsNotNullAndWhiteSpace() ? str.ToLower(CultureInfo.InvariantCulture) : str; + public static string ToLowerInvariant(this string str) => str.IsNotWhiteSpaceAndNull() ? str.ToLower(CultureInfo.InvariantCulture) : str; public static IEnumerable ToLowerInvariant(this IEnumerable strs) { - if (strs.IsNullOrEmpty()) + if (strs.IsEmptyOrNull()) { yield break; } @@ -164,7 +164,7 @@ public static IEnumerable ToLowerInvariant(this IEnumerable strs public static void ToLowerInvariant(this IList strs) { - if (strs.IsNotNullAndEmpty()) + if (strs.IsNotEmptyAndNull()) { for (var i = 0; i < strs.Count; i++) { @@ -173,11 +173,11 @@ public static void ToLowerInvariant(this IList strs) } } - public static string ToUpper(this string str) => str.IsNotNullAndWhiteSpace() ? str.ToUpper() : str; + public static string ToUpper(this string str) => str.IsNotWhiteSpaceAndNull() ? str.ToUpper() : str; public static IEnumerable ToUpper(this IEnumerable strs) { - if (strs.IsNullOrEmpty()) + if (strs.IsEmptyOrNull()) { yield break; } @@ -189,7 +189,7 @@ public static IEnumerable ToUpper(this IEnumerable strs) public static void ToUpper(this IList strs) { - if (strs.IsNotNullAndEmpty()) + if (strs.IsNotEmptyAndNull()) { for (var i = 0; i < strs.Count; i++) { @@ -198,11 +198,11 @@ public static void ToUpper(this IList strs) } } - public static string ToUpperInvariant(this string str) => str.IsNotNullAndWhiteSpace() ? str.ToUpper(CultureInfo.InvariantCulture) : str; + public static string ToUpperInvariant(this string str) => str.IsNotWhiteSpaceAndNull() ? str.ToUpper(CultureInfo.InvariantCulture) : str; public static IEnumerable ToUpperInvariant(this IEnumerable strs) { - if (strs.IsNullOrEmpty()) + if (strs.IsEmptyOrNull()) { yield break; } @@ -214,7 +214,7 @@ public static IEnumerable ToUpperInvariant(this IEnumerable strs public static void ToUpperInvariant(this IList strs) { - if (strs.IsNotNullAndEmpty()) + if (strs.IsNotEmptyAndNull()) { for (var i = 0; i < strs.Count; i++) { diff --git a/lib/YANLib/YANText.cs b/lib/YANLib/YANText.cs index cc36189..d5b5bc3 100644 --- a/lib/YANLib/YANText.cs +++ b/lib/YANLib/YANText.cs @@ -6,7 +6,7 @@ namespace YANLib; public static partial class YANText { - public static string ToTitle(this string str) => str.IsNullOrWhiteSpace() ? str : CurrentCulture.TextInfo.ToTitleCase(str); + public static string ToTitle(this string str) => str.IsWhiteSpaceOrNull() ? str : CurrentCulture.TextInfo.ToTitleCase(str); public static IEnumerable ToTitle(this IEnumerable strs) { @@ -33,7 +33,7 @@ public static void ToTitle(this IList strs) public static string ToCapitalize(this string str) { - if (str.IsNullOrWhiteSpace()) + if (str.IsWhiteSpaceOrNull()) { return str; } @@ -79,7 +79,7 @@ public static void ToCapitalize(this IList strs) public static string CleanSpace(this string str) { - if (str.IsNullOrEmpty()) + if (str.IsEmptyOrNull()) { return str; } @@ -130,7 +130,7 @@ public static void CleanSpace(this IList strs) public static string FormatName(this string str) { - if (str.IsNullOrEmpty()) + if (str.IsEmptyOrNull()) { return str; } @@ -174,7 +174,7 @@ public static void FormatName(this IList strs) public static string FilterAlphabetic(this string str) { - if (str.IsNullOrEmpty()) + if (str.IsEmptyOrNull()) { return str; } @@ -215,7 +215,7 @@ public static void FilterAlphabetic(this IList strs) public static string FilterNumber(this string str) { - if (str.IsNullOrEmpty()) + if (str.IsEmptyOrNull()) { return str; } @@ -256,7 +256,7 @@ public static void FilterNumber(this IList strs) public static string FilterAlphanumeric(this string str) { - if (str.IsNullOrEmpty()) + if (str.IsEmptyOrNull()) { return str; }