< Summary - Syki

Information
Class: Syki.Back.Shared.StringExtensions
Assembly: Back
File(s): /home/runner/work/syki/syki/Back/Shared/Extensions/StringExtensions.cs
Tag: 56_26538939494
Line coverage
33%
Covered lines: 28
Uncovered lines: 55
Coverable lines: 83
Total lines: 223
Line coverage: 33.7%
Branch coverage
41%
Covered branches: 23
Total branches: 56
Branch coverage: 41%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
IsEmpty(...)100%22100%
IsIn(...)100%66100%
HasValue(...)100%11100%
HasValue(...)100%210%
OnlyNumbers(...)100%22100%
ToSnakeCase(...)75%44100%
ToBase64(...)100%210%
Format(...)100%11100%
Format(...)100%210%
IsValidEmail(...)100%22100%
.cctor()100%11100%
Serialize(...)100%11100%
GenerateQrCodeBase64(...)100%210%
MinutesToString(...)100%88100%
ToThousandSeparated(...)100%210%
ToTwo(...)0%620%
ToMinuteString(...)0%620%
ToMinuteString(...)0%620%
AddQueryString(...)100%210%
ConvertObjectToDictionary(...)0%110100%
ParseJsonString(...)0%620%
GetSqlSpanName(...)0%156120%
ToInt(...)0%620%

File(s)

/home/runner/work/syki/syki/Back/Shared/Extensions/StringExtensions.cs

#LineLine coverage
 1using QRCoder;
 2using System.Text;
 3using Newtonsoft.Json;
 4using System.Reflection;
 5using System.Globalization;
 6using Newtonsoft.Json.Linq;
 7using Newtonsoft.Json.Converters;
 8using System.Text.RegularExpressions;
 9using Microsoft.Extensions.Primitives;
 10using Microsoft.AspNetCore.WebUtilities;
 11
 12namespace Syki.Back.Shared;
 13
 14public static class StringExtensions
 15{
 16    public static bool IsEmpty(this string? text)
 17    {
 104018        return string.IsNullOrEmpty(text) || string.IsNullOrWhiteSpace(text);
 19    }
 20
 21    public static bool IsIn(this string? text, params string[] others)
 22    {
 7023        if (text.IsEmpty())
 624            return true;
 25
 30026        foreach (var other in others)
 27        {
 10228            if (other.Contains(text!, StringComparison.OrdinalIgnoreCase))
 3229                return true;
 30        }
 31
 3232        return false;
 33    }
 34
 35    public static bool HasValue(this string? text)
 36    {
 163837        return !string.IsNullOrEmpty(text);
 38    }
 39
 40    public static bool HasValue(this StringValues text)
 41    {
 042        return !string.IsNullOrEmpty(text);
 43    }
 44
 45    public static string OnlyNumbers(this string text)
 46    {
 47047        if (text.HasValue())
 48        {
 45649            return new string(text.Where(char.IsDigit).ToArray());
 50        }
 51
 1452        return "";
 53    }
 54
 55    public static string ToSnakeCase(this string input)
 56    {
 31057        if (input.IsEmpty()) { return ""; }
 58
 29859        var startUnderscores = Regex.Match(input, @"^_+");
 29860        return startUnderscores + Regex.Replace(input, @"([a-z0-9])([A-Z])", "$1_$2").ToLower();
 61    }
 62
 63    public static string ToBase64(this string value)
 64    {
 065        var bytes = System.Text.Encoding.UTF8.GetBytes(value);
 066        return Convert.ToBase64String(bytes);
 67    }
 68
 69    public static string Format(this decimal value)
 70    {
 1071        return value.ToString("0.00", CultureInfo.InvariantCulture);
 72    }
 73
 74    public static string Format(this int value)
 75    {
 076        return value.ToString("N0", CultureInfo.CreateSpecificCulture("pt-BR"));
 77    }
 78
 79    public static bool IsValidEmail(this string email)
 80    {
 52481        if (email.IsEmpty()) return false;
 51282        return Regex.IsMatch(email, @"\A(?:[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*@(?:[a-z0-9](?
 83    }
 84
 285    private static JsonSerializerSettings _settings = new()
 286    {
 287        Converters = [new StringEnumConverter()],
 288    };
 89
 90    public static string Serialize(this object obj)
 91    {
 194292        return JsonConvert.SerializeObject(obj, _settings);
 93    }
 94
 95    public static string GenerateQrCodeBase64(this string key, string email)
 96    {
 97        const string provider = "Syki";
 98
 099        using var qrGenerator = new QRCodeGenerator();
 0100        using var qrCodeData = qrGenerator.CreateQrCode(
 0101            $"otpauth://totp/{provider}:{email}?secret={key}&issuer={provider}",
 0102            QRCodeGenerator.ECCLevel.Q
 0103        );
 104
 0105        var qrCode = new PngByteQRCode(qrCodeData);
 106
 0107        var bytes = qrCode.GetGraphic(20);
 108
 0109        return string.Format("data:image/png;base64,{0}", Convert.ToBase64String(bytes));
 0110    }
 111
 112    public static string MinutesToString(this int value)
 113    {
 16114        var hours = value / 60;
 16115        var minutes = value % 60;
 116
 18117        if (hours == 0 && minutes == 0) return "0";
 20118        if (hours == 0) return $"{minutes}min";
 10119        if (minutes == 0) return $"{hours}h";
 120
 6121        return $"{hours}h e {minutes}min";
 122    }
 123
 124    public static string ToThousandSeparated(this int value)
 125    {
 0126        return value.ToString("N0", CultureInfo.CreateSpecificCulture("pt-BR"));
 127    }
 128
 129    public static string ToTwo(this int value)
 130    {
 0131        return value < 10 ? $"0{value}" : value.ToString();
 132    }
 133
 134    public static string ToMinuteString(this DateTime date)
 135    {
 0136        if (date == DateTime.MinValue)
 0137            return "-";
 138
 0139        return date.ToLocalTime().ToString("dd/MM/yyyy HH:mm:ss");
 140    }
 141
 142    public static string ToMinuteString(this DateTime? date)
 143    {
 0144        if (date == null)
 0145            return "-";
 146
 0147        return date.Value.ToMinuteString();
 148    }
 149
 150    public static string AddQueryString(this string path, object obj)
 151    {
 0152        return QueryHelpers.AddQueryString(path, ConvertObjectToDictionary(obj));
 153    }
 154
 155    private static Dictionary<string, string?> ConvertObjectToDictionary(object obj)
 156    {
 0157        if (obj == null) return [];
 158
 0159        Dictionary<string, string?> dictionary = [];
 0160        PropertyInfo[] properties = obj.GetType().GetProperties();
 161
 0162        foreach (PropertyInfo property in properties)
 163        {
 0164            string propertyName = property.Name;
 0165            object propertyValue = property.GetValue(obj)!;
 166
 0167            if (propertyValue != null)
 168            {
 0169                var valueAsString = propertyValue.ToString();
 170
 0171                if (property.PropertyType == typeof(DateTime))
 0172                    valueAsString = ((DateTime)propertyValue).ToString("yyyy-MM-ddTHH:mm:sszzz");
 173
 0174                if (property.PropertyType == typeof(DateOnly))
 0175                    valueAsString = ((DateOnly)propertyValue).ToString("yyyy-MM-dd");
 176
 0177                dictionary.Add(propertyName, valueAsString);
 178            }
 179        }
 180
 0181        return dictionary;
 182    }
 183
 184    public static string ParseJsonString(this string input)
 185    {
 0186        if (input.IsEmpty()) return "";
 187
 188        try
 189        {
 0190            return JToken
 0191                .Parse(input)
 0192                .ToString(Formatting.Indented);
 193        }
 0194        catch
 195        {
 0196            return input;
 197        }
 0198    }
 199
 200    public static string GetSqlSpanName(this string sql)
 201    {
 0202        var comparer = StringComparison.InvariantCultureIgnoreCase;
 0203        var insert = sql.Contains("INSERT", comparer);
 0204        var update = sql.Contains("UPDATE", comparer);
 0205        var delete = sql.Contains("DELETE", comparer);
 0206        var select = sql.Contains("SELECT", comparer);
 207
 0208        var builder = new StringBuilder();
 209
 0210        if (insert) builder.Append("INSERT ");
 0211        if (update) builder.Append("UPDATE ");
 0212        if (delete) builder.Append("DELETE");
 213
 0214        if (!insert && !update && !delete && select) builder.Append("SELECT");
 215
 0216        return builder.ToString().Trim();
 217    }
 218
 219    public static int ToInt(this string value)
 220    {
 0221        return int.TryParse(value, out int integer) ? integer : 0;
 222    }
 223}