From 8cbf8cbc9f450db8f3422cae610289effc6d1c1f Mon Sep 17 00:00:00 2001 From: ivarlovlie Date: Fri, 23 Apr 2021 23:10:26 +0200 Subject: Add UnicornFormat --- src/IOL.Helpers/IOL.Helpers.csproj | 4 +++- src/IOL.Helpers/StringHelpers.cs | 28 ++++++++++++++++++++++++---- 2 files changed, 27 insertions(+), 5 deletions(-) (limited to 'src') diff --git a/src/IOL.Helpers/IOL.Helpers.csproj b/src/IOL.Helpers/IOL.Helpers.csproj index dcdf4cf..63772ad 100644 --- a/src/IOL.Helpers/IOL.Helpers.csproj +++ b/src/IOL.Helpers/IOL.Helpers.csproj @@ -17,8 +17,10 @@ Add optional parameter to skip X-Forwarded-Host and X-Forwarded-Proto - + + + diff --git a/src/IOL.Helpers/StringHelpers.cs b/src/IOL.Helpers/StringHelpers.cs index 1dde161..c090117 100644 --- a/src/IOL.Helpers/StringHelpers.cs +++ b/src/IOL.Helpers/StringHelpers.cs @@ -1,6 +1,9 @@ using System; +using System.Collections.Generic; +using System.Linq; using System.Text; using System.Text.RegularExpressions; +using Microsoft.Extensions.Configuration; namespace IOL.Helpers { @@ -18,6 +21,23 @@ namespace IOL.Helpers return !value.IsNullOrWhiteSpace(); } + public static string UnicornFormat(this string input, IDictionary 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, ""); + foreach (var key in matchList.Select(match => match.Value)) { + var value = configuration.GetValue(key); + input = input.Replace(key, value); + } + + return input; + } + public static Guid ToGuid(this string value) { return Guid.Parse(value); } @@ -29,7 +49,7 @@ namespace IOL.Helpers public static string ExtractFileName(this string value) { if (value.IsNullOrWhiteSpace()) return default; var lastIndex = value.LastIndexOf('.'); - return lastIndex <= 0 ? default : value.Substring(0, lastIndex); + return lastIndex <= 0 ? default : value[..lastIndex]; } public static string ExtractExtension(this string value) { @@ -40,8 +60,8 @@ namespace IOL.Helpers public static string Capitalize(this string input) { return input.IsNullOrWhiteSpace() - ? input - : Regex.Replace(input, @"\b(\w)", m => m.Value.ToUpper(), RegexOptions.None); + ? input + : Regex.Replace(input, @"\b(\w)", m => m.Value.ToUpper(), RegexOptions.None); } @@ -64,4 +84,4 @@ namespace IOL.Helpers return "****" + last4Chars; } } -} \ No newline at end of file +} -- cgit v1.3