diff --git a/X10D/src/DictionaryExtensions.cs b/X10D/src/DictionaryExtensions.cs deleted file mode 100644 index 3e87521..0000000 --- a/X10D/src/DictionaryExtensions.cs +++ /dev/null @@ -1,123 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text.RegularExpressions; -using System.Web; - -namespace X10D -{ - /// - /// A set of extension methods for . - /// - public static class DictionaryExtensions - { - /// - /// Converts a to an object-relational-safe connection string. - /// - /// The key type. - /// The value type. - /// The dictionary. - /// Returns a representing the dictionary as a key=value; set. - public static string ToConnectionString(this IReadOnlyDictionary dictionary) - { - if (dictionary is null) - { - throw new ArgumentNullException(nameof(dictionary)); - } - - static string SanitizeValue(string? value) - { - if (value is null) - { - return string.Empty; - } - - for (var index = 0; index < value.Length; index++) - { - if (char.IsWhiteSpace(value[index])) - { - return $"\"{value}\""; - } - } - - return value; - } - - var list = new List(); - - foreach (var pair in dictionary) - { - list.Add($"{pair.Key}={SanitizeValue(pair.Value?.ToString())}"); - } - - return string.Join(";", list); - } - - /// - /// Converts a to an object-relational-safe connection string. - /// - /// The key type. - /// The value type. - /// The dictionary. - /// Returns a representing the dictionary as a key=value; set. - public static string ToConnectionString(this IDictionary dictionary) - { - return ((IReadOnlyDictionary)dictionary).ToConnectionString(); - } - - /// - /// Converts a to an object-relational-safe connection string. - /// - /// The key type. - /// The value type. - /// The dictionary. - /// Returns a representing the dictionary as a key=value; set. - public static string ToConnectionString(this Dictionary dictionary) - { - return ((IReadOnlyDictionary)dictionary).ToConnectionString(); - } - - /// - /// Converts an to a HTTP GET parameter string. - /// - /// The key type. - /// The value type. - /// The dictionary. - /// Returns a representing the dictionary as a key=value& set. - public static string ToGetParameters(this IReadOnlyDictionary dictionary) - { - static string Sanitize(KeyValuePair kvp) - { - var key = HttpUtility.UrlEncode(kvp.Key.ToString()); - var value = HttpUtility.UrlEncode(kvp.Value.ToString()); - return $"{key}={value}"; - } - - return string.Join("&", dictionary.Select(Sanitize)); - } - - /// - /// Converts an to a HTTP GET parameter string. - /// - /// The key type. - /// The value type. - /// The dictionary. - /// Returns a representing the dictionary as a key=value& set. - public static string ToGetParameters(this IDictionary dictionary) - { - return ((IReadOnlyDictionary)dictionary).ToGetParameters(); - } - - /// - /// Converts a to a HTTP GET parameter string. - /// - /// The key type. - /// The value type. - /// The dictionary. - /// Returns a representing the dictionary as a key=value& set. - public static string ToGetParameters(this Dictionary dictionary) - { - return ((IReadOnlyDictionary)dictionary).ToGetParameters(); - } - } -} diff --git a/X10D/src/DictionaryExtensions/DictionaryExtensions.cs b/X10D/src/DictionaryExtensions/DictionaryExtensions.cs new file mode 100644 index 0000000..223451b --- /dev/null +++ b/X10D/src/DictionaryExtensions/DictionaryExtensions.cs @@ -0,0 +1,95 @@ +using System; +using System.Collections.Generic; +using System.Diagnostics.CodeAnalysis; +using System.Web; + +namespace X10D.DictionaryExtensions +{ + /// + /// Extension methods for and similar types. + /// + 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) + { + if (value is null) + { + throw new ArgumentNullException(nameof(value)); + } + + static string SanitizeValue(string? value) + { + if (value is null) + { + return string.Empty; + } + + for (var index = 0; index < value.Length; index++) + { + if (char.IsWhiteSpace(value[index])) + { + return $"\"{value}\""; + } + } + + return value; + } + + var list = new List(); + + // ReSharper disable once UseDeconstruction + // .NET Standard 2.0 does not support tuple deconstruct for KeyValuePair + foreach (var pair in value) + { + list.Add($"{pair.Key}={SanitizeValue(pair.Value?.ToString())}"); + } + + return string.Join(";", list); + } + + /// A representing the dictionary as a key=value; set. + /// + /// + /// + /// Converts an of to a HTTP GET parameter + /// string. + /// + /// The key type. + /// The value type. + /// The source dictionary. + /// Returns a representing the dictionary as a key=value& set. + [SuppressMessage("ReSharper", "UseDeconstructionOnParameter")] + [SuppressMessage("ReSharper", "UseDeconstruction")] + public static string ToGetParameters(this IEnumerable> value) + { + if (value is null) + { + throw new ArgumentNullException(nameof(value)); + } + + static string Sanitize(KeyValuePair pair) + { + var key = HttpUtility.UrlEncode(pair.Key?.ToString()); + var 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); + } + } +}