| | | 1 | | using QRCoder; |
| | | 2 | | using System.Text; |
| | | 3 | | using Newtonsoft.Json; |
| | | 4 | | using System.Reflection; |
| | | 5 | | using System.Globalization; |
| | | 6 | | using Newtonsoft.Json.Linq; |
| | | 7 | | using Newtonsoft.Json.Converters; |
| | | 8 | | using System.Text.RegularExpressions; |
| | | 9 | | using Microsoft.Extensions.Primitives; |
| | | 10 | | using Microsoft.AspNetCore.WebUtilities; |
| | | 11 | | |
| | | 12 | | namespace Syki.Back.Shared; |
| | | 13 | | |
| | | 14 | | public static class StringExtensions |
| | | 15 | | { |
| | | 16 | | public static bool IsEmpty(this string? text) |
| | | 17 | | { |
| | 1040 | 18 | | return string.IsNullOrEmpty(text) || string.IsNullOrWhiteSpace(text); |
| | | 19 | | } |
| | | 20 | | |
| | | 21 | | public static bool IsIn(this string? text, params string[] others) |
| | | 22 | | { |
| | 70 | 23 | | if (text.IsEmpty()) |
| | 6 | 24 | | return true; |
| | | 25 | | |
| | 300 | 26 | | foreach (var other in others) |
| | | 27 | | { |
| | 102 | 28 | | if (other.Contains(text!, StringComparison.OrdinalIgnoreCase)) |
| | 32 | 29 | | return true; |
| | | 30 | | } |
| | | 31 | | |
| | 32 | 32 | | return false; |
| | | 33 | | } |
| | | 34 | | |
| | | 35 | | public static bool HasValue(this string? text) |
| | | 36 | | { |
| | 1638 | 37 | | return !string.IsNullOrEmpty(text); |
| | | 38 | | } |
| | | 39 | | |
| | | 40 | | public static bool HasValue(this StringValues text) |
| | | 41 | | { |
| | 0 | 42 | | return !string.IsNullOrEmpty(text); |
| | | 43 | | } |
| | | 44 | | |
| | | 45 | | public static string OnlyNumbers(this string text) |
| | | 46 | | { |
| | 470 | 47 | | if (text.HasValue()) |
| | | 48 | | { |
| | 456 | 49 | | return new string(text.Where(char.IsDigit).ToArray()); |
| | | 50 | | } |
| | | 51 | | |
| | 14 | 52 | | return ""; |
| | | 53 | | } |
| | | 54 | | |
| | | 55 | | public static string ToSnakeCase(this string input) |
| | | 56 | | { |
| | 310 | 57 | | if (input.IsEmpty()) { return ""; } |
| | | 58 | | |
| | 298 | 59 | | var startUnderscores = Regex.Match(input, @"^_+"); |
| | 298 | 60 | | return startUnderscores + Regex.Replace(input, @"([a-z0-9])([A-Z])", "$1_$2").ToLower(); |
| | | 61 | | } |
| | | 62 | | |
| | | 63 | | public static string ToBase64(this string value) |
| | | 64 | | { |
| | 0 | 65 | | var bytes = System.Text.Encoding.UTF8.GetBytes(value); |
| | 0 | 66 | | return Convert.ToBase64String(bytes); |
| | | 67 | | } |
| | | 68 | | |
| | | 69 | | public static string Format(this decimal value) |
| | | 70 | | { |
| | 10 | 71 | | return value.ToString("0.00", CultureInfo.InvariantCulture); |
| | | 72 | | } |
| | | 73 | | |
| | | 74 | | public static string Format(this int value) |
| | | 75 | | { |
| | 0 | 76 | | return value.ToString("N0", CultureInfo.CreateSpecificCulture("pt-BR")); |
| | | 77 | | } |
| | | 78 | | |
| | | 79 | | public static bool IsValidEmail(this string email) |
| | | 80 | | { |
| | 524 | 81 | | if (email.IsEmpty()) return false; |
| | 512 | 82 | | return Regex.IsMatch(email, @"\A(?:[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*@(?:[a-z0-9](? |
| | | 83 | | } |
| | | 84 | | |
| | 2 | 85 | | private static JsonSerializerSettings _settings = new() |
| | 2 | 86 | | { |
| | 2 | 87 | | Converters = [new StringEnumConverter()], |
| | 2 | 88 | | }; |
| | | 89 | | |
| | | 90 | | public static string Serialize(this object obj) |
| | | 91 | | { |
| | 1942 | 92 | | return JsonConvert.SerializeObject(obj, _settings); |
| | | 93 | | } |
| | | 94 | | |
| | | 95 | | public static string GenerateQrCodeBase64(this string key, string email) |
| | | 96 | | { |
| | | 97 | | const string provider = "Syki"; |
| | | 98 | | |
| | 0 | 99 | | using var qrGenerator = new QRCodeGenerator(); |
| | 0 | 100 | | using var qrCodeData = qrGenerator.CreateQrCode( |
| | 0 | 101 | | $"otpauth://totp/{provider}:{email}?secret={key}&issuer={provider}", |
| | 0 | 102 | | QRCodeGenerator.ECCLevel.Q |
| | 0 | 103 | | ); |
| | | 104 | | |
| | 0 | 105 | | var qrCode = new PngByteQRCode(qrCodeData); |
| | | 106 | | |
| | 0 | 107 | | var bytes = qrCode.GetGraphic(20); |
| | | 108 | | |
| | 0 | 109 | | return string.Format("data:image/png;base64,{0}", Convert.ToBase64String(bytes)); |
| | 0 | 110 | | } |
| | | 111 | | |
| | | 112 | | public static string MinutesToString(this int value) |
| | | 113 | | { |
| | 16 | 114 | | var hours = value / 60; |
| | 16 | 115 | | var minutes = value % 60; |
| | | 116 | | |
| | 18 | 117 | | if (hours == 0 && minutes == 0) return "0"; |
| | 20 | 118 | | if (hours == 0) return $"{minutes}min"; |
| | 10 | 119 | | if (minutes == 0) return $"{hours}h"; |
| | | 120 | | |
| | 6 | 121 | | return $"{hours}h e {minutes}min"; |
| | | 122 | | } |
| | | 123 | | |
| | | 124 | | public static string ToThousandSeparated(this int value) |
| | | 125 | | { |
| | 0 | 126 | | return value.ToString("N0", CultureInfo.CreateSpecificCulture("pt-BR")); |
| | | 127 | | } |
| | | 128 | | |
| | | 129 | | public static string ToTwo(this int value) |
| | | 130 | | { |
| | 0 | 131 | | return value < 10 ? $"0{value}" : value.ToString(); |
| | | 132 | | } |
| | | 133 | | |
| | | 134 | | public static string ToMinuteString(this DateTime date) |
| | | 135 | | { |
| | 0 | 136 | | if (date == DateTime.MinValue) |
| | 0 | 137 | | return "-"; |
| | | 138 | | |
| | 0 | 139 | | return date.ToLocalTime().ToString("dd/MM/yyyy HH:mm:ss"); |
| | | 140 | | } |
| | | 141 | | |
| | | 142 | | public static string ToMinuteString(this DateTime? date) |
| | | 143 | | { |
| | 0 | 144 | | if (date == null) |
| | 0 | 145 | | return "-"; |
| | | 146 | | |
| | 0 | 147 | | return date.Value.ToMinuteString(); |
| | | 148 | | } |
| | | 149 | | |
| | | 150 | | public static string AddQueryString(this string path, object obj) |
| | | 151 | | { |
| | 0 | 152 | | return QueryHelpers.AddQueryString(path, ConvertObjectToDictionary(obj)); |
| | | 153 | | } |
| | | 154 | | |
| | | 155 | | private static Dictionary<string, string?> ConvertObjectToDictionary(object obj) |
| | | 156 | | { |
| | 0 | 157 | | if (obj == null) return []; |
| | | 158 | | |
| | 0 | 159 | | Dictionary<string, string?> dictionary = []; |
| | 0 | 160 | | PropertyInfo[] properties = obj.GetType().GetProperties(); |
| | | 161 | | |
| | 0 | 162 | | foreach (PropertyInfo property in properties) |
| | | 163 | | { |
| | 0 | 164 | | string propertyName = property.Name; |
| | 0 | 165 | | object propertyValue = property.GetValue(obj)!; |
| | | 166 | | |
| | 0 | 167 | | if (propertyValue != null) |
| | | 168 | | { |
| | 0 | 169 | | var valueAsString = propertyValue.ToString(); |
| | | 170 | | |
| | 0 | 171 | | if (property.PropertyType == typeof(DateTime)) |
| | 0 | 172 | | valueAsString = ((DateTime)propertyValue).ToString("yyyy-MM-ddTHH:mm:sszzz"); |
| | | 173 | | |
| | 0 | 174 | | if (property.PropertyType == typeof(DateOnly)) |
| | 0 | 175 | | valueAsString = ((DateOnly)propertyValue).ToString("yyyy-MM-dd"); |
| | | 176 | | |
| | 0 | 177 | | dictionary.Add(propertyName, valueAsString); |
| | | 178 | | } |
| | | 179 | | } |
| | | 180 | | |
| | 0 | 181 | | return dictionary; |
| | | 182 | | } |
| | | 183 | | |
| | | 184 | | public static string ParseJsonString(this string input) |
| | | 185 | | { |
| | 0 | 186 | | if (input.IsEmpty()) return ""; |
| | | 187 | | |
| | | 188 | | try |
| | | 189 | | { |
| | 0 | 190 | | return JToken |
| | 0 | 191 | | .Parse(input) |
| | 0 | 192 | | .ToString(Formatting.Indented); |
| | | 193 | | } |
| | 0 | 194 | | catch |
| | | 195 | | { |
| | 0 | 196 | | return input; |
| | | 197 | | } |
| | 0 | 198 | | } |
| | | 199 | | |
| | | 200 | | public static string GetSqlSpanName(this string sql) |
| | | 201 | | { |
| | 0 | 202 | | var comparer = StringComparison.InvariantCultureIgnoreCase; |
| | 0 | 203 | | var insert = sql.Contains("INSERT", comparer); |
| | 0 | 204 | | var update = sql.Contains("UPDATE", comparer); |
| | 0 | 205 | | var delete = sql.Contains("DELETE", comparer); |
| | 0 | 206 | | var select = sql.Contains("SELECT", comparer); |
| | | 207 | | |
| | 0 | 208 | | var builder = new StringBuilder(); |
| | | 209 | | |
| | 0 | 210 | | if (insert) builder.Append("INSERT "); |
| | 0 | 211 | | if (update) builder.Append("UPDATE "); |
| | 0 | 212 | | if (delete) builder.Append("DELETE"); |
| | | 213 | | |
| | 0 | 214 | | if (!insert && !update && !delete && select) builder.Append("SELECT"); |
| | | 215 | | |
| | 0 | 216 | | return builder.ToString().Trim(); |
| | | 217 | | } |
| | | 218 | | |
| | | 219 | | public static int ToInt(this string value) |
| | | 220 | | { |
| | 0 | 221 | | return int.TryParse(value, out int integer) ? integer : 0; |
| | | 222 | | } |
| | | 223 | | } |