diff --git a/X10D/src/DictionaryExtensions/DictionaryExtensions.cs b/X10D/src/DictionaryExtensions/DictionaryExtensions.cs index ad2be55..abb09eb 100644 --- a/X10D/src/DictionaryExtensions/DictionaryExtensions.cs +++ b/X10D/src/DictionaryExtensions/DictionaryExtensions.cs @@ -1,4 +1,4 @@ -using System.Web; +using System.Web; namespace X10D; @@ -11,15 +11,15 @@ public static class DictionaryExtensions /// Converts an of to an data connection /// string. /// - /// The key type. - /// The value type. - /// The source dictionary. - /// A representing the dictionary as a key=value; set. - public static string ToConnectionString(this IEnumerable> value) + /// The type of the key element of the key/value pair. + /// The type of the value element of the key/value pair. + /// The source dictionary. + /// A representing the dictionary as a key=value set, concatenated with ;. + public static string ToConnectionString(this IEnumerable> source) { - if (value is null) + if (source is null) { - throw new ArgumentNullException(nameof(value)); + throw new ArgumentNullException(nameof(source)); } static string SanitizeValue(string? value) @@ -29,9 +29,9 @@ public static class DictionaryExtensions return string.Empty; } - for (var index = 0; index < value.Length; index++) + foreach (char character in value) { - if (char.IsWhiteSpace(value[index])) + if (char.IsWhiteSpace(character)) { return $"\"{value}\""; } @@ -40,48 +40,36 @@ public static class DictionaryExtensions return value; } - var list = new List(); - - // ReSharper disable once UseDeconstruction - // .NET Standard 2.0 does not support tuple deconstruct for KeyValuePair - // ReSharper disable once LoopCanBeConvertedToQuery - foreach (var pair in value) + static string GetQueryParameter(KeyValuePair pair) { - list.Add($"{pair.Key}={SanitizeValue(pair.Value?.ToString())}"); + return $"{pair.Key}={SanitizeValue(pair.Value?.ToString())}"); } - return string.Join(";", list); + return string.Join(';', source.Select(GetQueryParameter)); } /// - /// Converts an of to a HTTP GET parameter - /// string. + /// Converts an of to a HTTP GET query string. /// - /// The key type. - /// The value type. - /// The source dictionary. - /// Returns a representing the dictionary as a key=value& set. - public static string ToGetParameters(this IEnumerable> value) + /// The type of the key element of the key/value pair. + /// The type of the value element of the key/value pair. + /// The source dictionary. + /// A representing the dictionary as a key=value set, concatenated with &. + public static string ToGetParameters(this IEnumerable> source) + where TKey : notnull { - if (value is null) + if (source is null) { - throw new ArgumentNullException(nameof(value)); + throw new ArgumentNullException(nameof(source)); } - static string Sanitize(KeyValuePair pair) + static string GetQueryParameter(KeyValuePair pair) { - string? key = HttpUtility.UrlEncode(pair.Key?.ToString()); + string key = HttpUtility.UrlEncode(pair.Key.ToString())!; string? value = HttpUtility.UrlEncode(pair.Value?.ToString()); return $"{key}={value}"; } - var list = new List(); - - foreach (var pair in value) - { - list.Add(Sanitize(pair)); - } - - return string.Join('&', list); + return string.Join('&', source.Select(GetQueryParameter)); } }