diff --git a/X10D/DictionaryExtensions.cs b/X10D/DictionaryExtensions.cs new file mode 100644 index 0000000..d92977f --- /dev/null +++ b/X10D/DictionaryExtensions.cs @@ -0,0 +1,108 @@ +namespace X10D +{ + #region Using Directives + + using System; + using System.Collections.Generic; + using System.Linq; + using System.Text.RegularExpressions; + using System.Web; + + #endregion + + /// + /// 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) + { + static string SanitizeValue(T value) + { + return + value is string str && + Regex.IsMatch(str, "\\s") + ? $"\"{str}\"" + : value.ToString(); + } + + IEnumerable strings = + dictionary.Select(o => $"{o.Key}={SanitizeValue(o.Value)}"); + return String.Join(";", strings); + } + + /// + /// 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) + { + string key = HttpUtility.UrlEncode(kvp.Key.ToString()); + string 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/README.md b/X10D/README.md index f185661..a160289 100644 --- a/X10D/README.md +++ b/X10D/README.md @@ -24,3 +24,4 @@ Below is a list of the number of extension methods written for a given type. Ove | `IList` | `X10D` | 2 | | `Random` | `X10D` | 2 | | `string` / `SecureString` | `X10D` | 8 | +| `Dictionary` | 2 | \ No newline at end of file