diff options
| author | ivarlovlie <git@ivarlovlie.no> | 2021-11-20 21:31:30 +0100 |
|---|---|---|
| committer | ivarlovlie <git@ivarlovlie.no> | 2021-11-20 21:31:30 +0100 |
| commit | 7ff7f3902e5bdcc33bfc07fa6ad71a5798d7f8d6 (patch) | |
| tree | 6e7eaaed636558c4feb88f2bb18bbb7d4c0c87be /src/IOL.Helpers/StringHelpers.cs | |
| parent | 4e41b1f31a7309b299398fd9dd53d499204fa6cf (diff) | |
| download | dotnet-helpers-7ff7f3902e5bdcc33bfc07fa6ad71a5798d7f8d6.tar.xz dotnet-helpers-7ff7f3902e5bdcc33bfc07fa6ad71a5798d7f8d6.zip | |
Change to file-scoped namespaces.
Add ConditionalWhere to QueryableHelpers.cs
Diffstat (limited to 'src/IOL.Helpers/StringHelpers.cs')
| -rw-r--r-- | src/IOL.Helpers/StringHelpers.cs | 123 |
1 files changed, 61 insertions, 62 deletions
diff --git a/src/IOL.Helpers/StringHelpers.cs b/src/IOL.Helpers/StringHelpers.cs index 6589df6..783d238 100644 --- a/src/IOL.Helpers/StringHelpers.cs +++ b/src/IOL.Helpers/StringHelpers.cs @@ -5,84 +5,83 @@ 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); - } +namespace IOL.Helpers; - public static string Slugified(this string input) { - return Slug.Generate(true, input); - } +public static class StringHelpers +{ + public static bool IsNullOrWhiteSpace(this string value) { + return string.IsNullOrWhiteSpace(value); + } - public static bool HasValue(this string value) { - return !value.IsNullOrWhiteSpace(); - } + public static string Slugified(this string input) { + return Slug.Generate(true, input); + } - 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)); - } + public static bool HasValue(this string value) { + return !value.IsNullOrWhiteSpace(); + } + 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)); + } - 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; + 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); } - public static Guid ToGuid(this string value) { - return !Guid.TryParse(value, out var res) ? default : res; - } + return input; + } - public static string Base64Encode(this string text) { - return Convert.ToBase64String(Encoding.UTF8.GetBytes(text)); - } + public static Guid ToGuid(this string value) { + return !Guid.TryParse(value, out var res) ? default : res; + } - public static string ExtractFileName(this string value) { - if (value.IsNullOrWhiteSpace()) return default; - var lastIndex = value.LastIndexOf('.'); - return lastIndex <= 0 ? default : value[..lastIndex]; - } + public static string Base64Encode(this string text) { + return Convert.ToBase64String(Encoding.UTF8.GetBytes(text)); + } - public static string ExtractExtension(this string value) { - if (value.IsNullOrWhiteSpace()) return default; - var lastIndex = value.LastIndexOf('.'); - return lastIndex <= 0 ? default : value.Substring(lastIndex); - } + public static string ExtractFileName(this string value) { + if (value.IsNullOrWhiteSpace()) return default; + var lastIndex = value.LastIndexOf('.'); + return lastIndex <= 0 ? default : value[..lastIndex]; + } - public static string Capitalize(this string input) { - return input.IsNullOrWhiteSpace() - ? input - : Regex.Replace(input, @"\b(\w)", m => m.Value.ToUpper(), RegexOptions.None); - } + public static string ExtractExtension(this string value) { + if (value.IsNullOrWhiteSpace()) return default; + var lastIndex = value.LastIndexOf('.'); + return lastIndex <= 0 ? default : value.Substring(lastIndex); + } + public static string Capitalize(this string input) { + return input.IsNullOrWhiteSpace() + ? input + : Regex.Replace(input, @"\b(\w)", m => m.Value.ToUpper(), RegexOptions.None); + } - /// <summary> - /// Check if the given MIME is a JSON MIME. - /// </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) { - var last4Chars = "****"; - if (value.HasValue() && value.Length > 4) { - last4Chars = value.Substring(value.Length - 4); - } + /// <summary> + /// Check if the given MIME is a JSON MIME. + /// </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")); + } - return "****" + last4Chars; + public static string Obfuscate(this string value) { + var last4Chars = "****"; + if (value.HasValue() && value.Length > 4) { + last4Chars = value.Substring(value.Length - 4); } + + return "****" + last4Chars; } } |
