From b0e5073b39d4ec98f579a5b83bd9901aa4315c53 Mon Sep 17 00:00:00 2001 From: Oliver Booth Date: Tue, 17 Dec 2019 11:50:27 +0000 Subject: [PATCH] =?UTF-8?q?=E2=9C=A8=20Add=20DictionaryExtensions?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- X10D/DictionaryExtensions.cs | 108 +++++++++++++++++++++++++++++++++++ X10D/README.md | 1 + 2 files changed, 109 insertions(+) create mode 100644 X10D/DictionaryExtensions.cs 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