| | | 1 | | using System.Text.Json; |
| | | 2 | | using ArturRios.Util.RegularExpressions; |
| | | 3 | | |
| | | 4 | | namespace ArturRios.Extensions; |
| | | 5 | | |
| | | 6 | | /// <summary> |
| | | 7 | | /// Provides extension methods for working with strings, including validation helpers, |
| | | 8 | | /// parsing utilities, and convenient join operations. |
| | | 9 | | /// </summary> |
| | | 10 | | public static class StringExtensions |
| | | 11 | | { |
| | | 12 | | /// <summary> |
| | | 13 | | /// Checks if the string corresponds to a valid value of the specified enum type. |
| | | 14 | | /// </summary> |
| | | 15 | | /// <typeparam name="TEnum">The enum type to validate against.</typeparam> |
| | | 16 | | /// <param name="string">Input string value.</param> |
| | | 17 | | /// <param name="ignoreCase">Whether to ignore case during parsing (default true).</param> |
| | | 18 | | /// <returns>True if the string can be parsed to the enum; otherwise false.</returns> |
| | | 19 | | public static bool IsValidEnumValue<TEnum>(this string @string, bool ignoreCase = true) where TEnum : Enum => |
| | 5 | 20 | | Enum.TryParse(typeof(TEnum), @string, ignoreCase, out _); |
| | | 21 | | |
| | | 22 | | /// <summary> |
| | | 23 | | /// Returns the string if it has value; otherwise returns the provided default. |
| | | 24 | | /// </summary> |
| | | 25 | | /// <param name="string">The input string.</param> |
| | | 26 | | /// <param name="defaultValue">Default value when input is null or empty.</param> |
| | | 27 | | /// <returns>The input string or the default value.</returns> |
| | | 28 | | public static string? ValueOrDefault(this string? @string, string? defaultValue = null) => |
| | 3 | 29 | | string.IsNullOrEmpty(@string) ? defaultValue : @string; |
| | | 30 | | |
| | | 31 | | /// <summary> |
| | | 32 | | /// Joins the sequence of strings using the provided separator. |
| | | 33 | | /// </summary> |
| | | 34 | | /// <param name="source">The sequence of strings to join.</param> |
| | | 35 | | /// <param name="separator">Separator to use (default ", ").</param> |
| | | 36 | | /// <returns>A single concatenated string.</returns> |
| | | 37 | | public static string JoinWith(this IEnumerable<string> source, string separator = ", ") => |
| | 2 | 38 | | string.Join(separator, source); |
| | | 39 | | |
| | | 40 | | /// <summary> |
| | | 41 | | /// Joins the sequence of items using the provided separator, converting each to string. |
| | | 42 | | /// </summary> |
| | | 43 | | /// <typeparam name="T">Item type.</typeparam> |
| | | 44 | | /// <param name="source">The sequence to join.</param> |
| | | 45 | | /// <param name="separator">Separator to use (default ", ").</param> |
| | | 46 | | /// <returns>A single concatenated string.</returns> |
| | | 47 | | public static string JoinWith<T>(this IEnumerable<T> source, string separator = ", ") => |
| | 7 | 48 | | string.Join(separator, source.Select(x => x?.ToString())); |
| | | 49 | | |
| | | 50 | | /// <summary> |
| | | 51 | | /// Provides validation helpers for the given non-null string. |
| | | 52 | | /// </summary> |
| | | 53 | | extension(string @string) |
| | | 54 | | { |
| | | 55 | | /// <summary> |
| | | 56 | | /// Checks whether the string contains at least one lowercase character. |
| | | 57 | | /// </summary> |
| | | 58 | | /// <returns>True if the string has a lowercase character; otherwise false.</returns> |
| | 5 | 59 | | public bool HasLowerChar() => RegexCollection.HasLowerChar().IsMatch(@string); |
| | | 60 | | |
| | | 61 | | /// <summary> |
| | | 62 | | /// Checks whether the string length is less than or equal to the given maximum. |
| | | 63 | | /// </summary> |
| | | 64 | | /// <param name="maxLength">Maximum allowed length.</param> |
| | | 65 | | /// <returns>True if within the max length; otherwise false.</returns> |
| | 4 | 66 | | public bool HasMaxLength(int maxLength) => !(@string.Length > maxLength); |
| | | 67 | | |
| | | 68 | | /// <summary> |
| | | 69 | | /// Checks whether the string length is greater than or equal to the given minimum. |
| | | 70 | | /// </summary> |
| | | 71 | | /// <param name="minLength">Minimum required length.</param> |
| | | 72 | | /// <returns>True if meets the min length; otherwise false.</returns> |
| | 4 | 73 | | public bool HasMinLength(int minLength) => !(@string.Length < minLength); |
| | | 74 | | |
| | | 75 | | /// <summary> |
| | | 76 | | /// Checks whether the string contains at least one numeric character. |
| | | 77 | | /// </summary> |
| | | 78 | | /// <returns>True if a number is present; otherwise false.</returns> |
| | 4 | 79 | | public bool HasNumber() => RegexCollection.HasNumber().IsMatch(@string); |
| | | 80 | | |
| | | 81 | | /// <summary> |
| | | 82 | | /// Checks whether the string contains at least one uppercase character. |
| | | 83 | | /// </summary> |
| | | 84 | | /// <returns>True if the string has an uppercase character; otherwise false.</returns> |
| | 5 | 85 | | public bool HasUpperChar() => RegexCollection.HasUpperChar().IsMatch(@string); |
| | | 86 | | |
| | | 87 | | /// <summary> |
| | | 88 | | /// Validates whether the string matches a basic email format. |
| | | 89 | | /// </summary> |
| | | 90 | | /// <returns>True if the string is a valid email; otherwise false.</returns> |
| | 5 | 91 | | public bool IsValidEmail() => RegexCollection.Email().IsMatch(@string); |
| | | 92 | | |
| | | 93 | | /// <summary> |
| | | 94 | | /// Trims leading/trailing whitespace and the specified character from the ends of the string. |
| | | 95 | | /// </summary> |
| | | 96 | | /// <param name="charToTrim">Character to trim from both ends.</param> |
| | | 97 | | /// <returns>The trimmed string.</returns> |
| | | 98 | | public string TrimChar(char charToTrim) => |
| | 4 | 99 | | string.IsNullOrEmpty(@string) ? @string : @string.Trim().Trim(charToTrim); |
| | | 100 | | } |
| | | 101 | | |
| | | 102 | | /// <summary> |
| | | 103 | | /// Provides parsing helpers for a nullable string. |
| | | 104 | | /// </summary> |
| | | 105 | | extension(string? @string) |
| | | 106 | | { |
| | | 107 | | /// <summary> |
| | | 108 | | /// Parses the string to a boolean, or returns the provided default if parsing fails. |
| | | 109 | | /// </summary> |
| | | 110 | | /// <param name="defaultValue">Value to return when parsing fails.</param> |
| | | 111 | | /// <returns>The parsed boolean or the default value.</returns> |
| | | 112 | | public bool? ParseToBoolOrDefault(bool? defaultValue = null) => |
| | 6 | 113 | | bool.TryParse(@string, out var result) ? result : defaultValue; |
| | | 114 | | |
| | | 115 | | /// <summary> |
| | | 116 | | /// Parses the string to an integer, or returns the provided default if parsing fails. |
| | | 117 | | /// </summary> |
| | | 118 | | /// <param name="defaultValue">Value to return when parsing fails.</param> |
| | | 119 | | /// <returns>The parsed integer or the default value.</returns> |
| | | 120 | | public int? ParseToIntOrDefault(int? defaultValue = null) => |
| | 4 | 121 | | int.TryParse(@string, out var result) ? result : defaultValue; |
| | | 122 | | |
| | | 123 | | /// <summary> |
| | | 124 | | /// Attempts to deserialize the JSON string to an object of type <typeparamref name="T" />. |
| | | 125 | | /// </summary> |
| | | 126 | | /// <typeparam name="T">Target reference type.</typeparam> |
| | | 127 | | /// <returns>An instance of T if deserialization succeeds; otherwise null.</returns> |
| | | 128 | | public T? ParseToObjectOrDefault<T>() where T : class |
| | 4 | 129 | | { |
| | 4 | 130 | | if (string.IsNullOrEmpty(@string)) |
| | 2 | 131 | | { |
| | 2 | 132 | | return null; |
| | | 133 | | } |
| | | 134 | | |
| | | 135 | | try |
| | 2 | 136 | | { |
| | 2 | 137 | | return JsonSerializer.Deserialize<T>(@string); |
| | | 138 | | } |
| | 1 | 139 | | catch |
| | 1 | 140 | | { |
| | 1 | 141 | | return null; |
| | | 142 | | } |
| | | 143 | | } |
| | | 144 | | } |
| | | 145 | | } |