diff options
| author | ivarlovlie <git@ivarlovlie.no> | 2022-02-26 18:36:39 +0100 |
|---|---|---|
| committer | ivarlovlie <git@ivarlovlie.no> | 2022-02-26 18:36:39 +0100 |
| commit | 0112bcfd629c534997017ad6bc6d9b6caf2b95ad (patch) | |
| tree | 2945c10d4f8ea86aebfb643bf468ebaed2ce256a /src/IOL.Helpers/StringHelpers.cs | |
| parent | feb989c750fe3bad630b8950af7a54b51a0c46a1 (diff) | |
| download | dotnet-helpers-0112bcfd629c534997017ad6bc6d9b6caf2b95ad.tar.xz dotnet-helpers-0112bcfd629c534997017ad6bc6d9b6caf2b95ad.zip | |
feat: Add tests for StringHelpers; Various breaking changes
Diffstat (limited to 'src/IOL.Helpers/StringHelpers.cs')
| -rw-r--r-- | src/IOL.Helpers/StringHelpers.cs | 148 |
1 files changed, 96 insertions, 52 deletions
diff --git a/src/IOL.Helpers/StringHelpers.cs b/src/IOL.Helpers/StringHelpers.cs index 366ca00..20def44 100644 --- a/src/IOL.Helpers/StringHelpers.cs +++ b/src/IOL.Helpers/StringHelpers.cs @@ -1,92 +1,136 @@ using System; using System.Collections.Generic; +using System.IO; using System.Linq; using System.Text; using System.Text.RegularExpressions; -using Microsoft.Extensions.Configuration; namespace IOL.Helpers; public static class StringHelpers { - public static bool IsNullOrWhiteSpace(this string value) { - return string.IsNullOrWhiteSpace(value); + /// <summary> + /// Check wheter or not the given string has a value + /// </summary> + /// <param name="input"></param> + /// <returns>False if the input string is null or only whitespace, otherwise it returns true</returns> + public static bool IsNullOrWhiteSpace(this string input) { + return string.IsNullOrWhiteSpace(input); } - public static string Slugified(this string input) { - return Slug.Generate(true, input); + /// <summary> + /// Convert a string to snake_casing + /// </summary> + /// <param name="input">The input string to convert</param> + /// <returns>A snake cased string representation of the input string</returns> + public static string AsSnakeCasedString(this string input) { + if (input.IsNullOrWhiteSpace()) return default; + input = input.ToLower().Trim(); + return input.Replace(" ", "_"); } - public static string ToSnakeCase(this string str) { - return string.Concat(str.Select((x, i) => i > 0 && char.IsUpper(x) ? "_" + x : x.ToString())).ToLower(); + /// <summary> + /// Creatas a url safe string (slug) of the input + /// </summary> + /// <param name="input">The string to convert into a slug</param> + /// <returns>A string (slug) representation of the input</returns> + public static string AsSlug(this string input) { + return Slug.Generate(true, input); } - public static bool HasValue(this string value) { - return !value.IsNullOrWhiteSpace(); + /// <summary> + /// Check wheter or not the given string has a value + /// </summary> + /// <param name="input"></param> + /// <returns>False if the input string is null or only whitespace, otherwise it returns true</returns> + public static bool HasValue(this string input) { + return !input.IsNullOrWhiteSpace(); } + /// <summary> + /// Performs a unicorn format on the input. + /// </summary> + /// <example> + /// const string input = "Hello, {name}"; + /// var result = input.UnicornFormat(new Dictionary<string, string> { + /// { + /// "name", "World" + /// } + /// }); + /// // result -> "Hello, World" + /// </example> + /// <param name="input"></param> + /// <param name="values"></param> + /// <returns></returns> public static string UnicornFormat(this string input, IDictionary<string, string> values) { if (string.IsNullOrWhiteSpace(input)) return default; - return values.Count == 0 ? default : values.Aggregate(input, (current, value1) => current.Replace("{" + value1.Key + "}", value1.Value)); + if (values.Count == 0) return input; + return values.Aggregate(input, (current, value1) => current.Replace("{" + value1.Key + "}", value1.Value)); } - public static string UnicornFormatWithEnvironment(this string input, IConfiguration configuration) { - if (string.IsNullOrWhiteSpace(input)) return default; - var matchList = Regex.Matches(input, "{[a-z|_]*}", RegexOptions.IgnoreCase); - foreach (var key in matchList.Select(match => match.Value)) { - var value = configuration.GetValue<string>(key.Substring(1, key.Length - 2)); - if (string.IsNullOrWhiteSpace(value)) continue; - input = input.Replace(key, value); - } - - return input; + /// <summary> + /// Converts a string input to it's Guid representation, if the string does not conform to the guid specification a FormatException will be thrown + /// </summary> + /// <param name="value">The string to interpret as a guid</param> + /// <returns>The input string as a guid</returns> + public static Guid AsGuid(this string input) { + return new Guid(input); } - public static Guid ToGuid(this string value) { - return new Guid(value); + /// <summary> + /// Converts a string input to it's Guid representation, if the string does not conform to the guid specification, a default input will be returned + /// </summary> + /// <param name="value">The string to interpret as a guid</param> + /// <returns>The string as a guid or the default input for guids</returns> + public static Guid AsGuidOrDefault(this string input) { + return !Guid.TryParse(input, out var res) ? default : res; } - public static Guid ToGuidOrDefault(this string value) { - return !Guid.TryParse(value, out var res) ? default : res; + /// <summary> + /// Encodes a string input to it's base 64 equalient + /// </summary> + /// <param name="input">The input to encode</param> + /// <returns>The base 64 encoded string</returns> + public static string AsBase64EncodedString(this string input) { + return input.IsNullOrWhiteSpace() ? default : Convert.ToBase64String(Encoding.UTF8.GetBytes(input)); } - public static string Base64Encode(this string text) { - return Convert.ToBase64String(Encoding.UTF8.GetBytes(text)); + /// <summary> + /// Decodes a string input to it's original input + /// </summary> + /// <param name="input">The input to decode</param> + /// <returns>The decoded string input of the base 64 string</returns> + public static string AsBase64DecodedString(this string input) { + return input.IsNullOrWhiteSpace() ? default : Encoding.UTF8.GetString(Convert.FromBase64String(input)); } - public static string ExtractFileName(this string value) { - if (value.IsNullOrWhiteSpace()) return default; - var lastIndex = value.LastIndexOf('.'); - return lastIndex <= 0 ? default : value[..lastIndex]; - } + /// <summary> + /// Capitalize the input + /// </summary> + /// <param name="input">The string input to capitalize</param> + /// <returns></returns> + public static string Capitalize(this string input, bool onlyFirstChar = false) { + if (input.IsNullOrWhiteSpace()) return default; + input = input.Trim(); - public static string ExtractExtension(this string value) { - if (value.IsNullOrWhiteSpace()) return default; - var lastIndex = value.LastIndexOf('.'); - return lastIndex <= 0 ? default : value.Substring(lastIndex); - } + if (!onlyFirstChar) { + return Regex.Replace(input, @"\b(\w)", m => m.Value.ToUpper(), RegexOptions.None); + } - public static string Capitalize(this string input) { - return input.IsNullOrWhiteSpace() - ? input - : Regex.Replace(input, @"\b(\w)", m => m.Value.ToUpper(), RegexOptions.None); + input = char.ToUpper(input[0]) + input.Substring(1, input.Length - 1); + return input; } - /// <summary> - /// Check if the given MIME is a JSON MIME. + /// Obfucates the input string /// </summary> - /// <param name="mime">MIME</param> - /// <returns>Returns true if MIME type is json.</returns> - public static bool IsJsonMime(this string mime) { - var jsonRegex = new Regex("(?i)^(application/json|[^;/ \t]+/[^;/ \t]+[+]json)[ \t]*(;.*)?$"); - return mime != null && (jsonRegex.IsMatch(mime) || mime.Equals("application/json-patch+json")); - } - - public static string Obfuscate(this string value) { + /// <param name="input"></param> + /// <returns></returns> + public static string Obfuscate(this string input) { + if (input.IsNullOrWhiteSpace()) return default; var last4Chars = "****"; - if (value.HasValue() && value.Length > 4) { - last4Chars = value.Substring(value.Length - 4); + if (input.HasValue() && input.Length > 4) { + last4Chars = input[^4..]; } return "****" + last4Chars; |
