using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Quobject.EngineIoClientDotNet.Modules
{
///
/// Provides methods for parsing a query string into an object, and vice versa.
/// Ported from the JavaScript module.
/// https://www.npmjs.org/package/parseqs
///
public class ParseQS
{
///
/// Compiles a querystring
/// Returns string representation of the object
///
///
///
public static string Encode(ConcurrentDictionary obj)
{
var sb = new StringBuilder();
foreach (var key in obj.Keys.OrderBy(x=>x))
{
if (sb.Length > 0)
{
sb.Append("&");
}
sb.Append(Global.EncodeURIComponent(key));
sb.Append("=");
sb.Append(Global.EncodeURIComponent(obj[key]));
}
return sb.ToString();
}
///
/// Compiles a querystring
/// Returns string representation of the object
///
///
///
internal static string Encode(System.Collections.Generic.Dictionary obj)
{
var sb = new StringBuilder();
foreach (var key in obj.Keys)
{
if (sb.Length > 0)
{
sb.Append("&");
}
sb.Append(Global.EncodeURIComponent(key));
sb.Append("=");
sb.Append(Global.EncodeURIComponent(obj[key]));
}
return sb.ToString();
}
///
/// Parses a simple querystring into an object
///
///
///
public static Dictionary Decode(string qs)
{
var qry = new Dictionary();
var pairs = qs.Split('&');
for (int i = 0; i < pairs.Length; i++)
{
var pair = pairs[i].Split('=');
qry.Add(Global.DecodeURIComponent(pair[0]), Global.DecodeURIComponent(pair[1]));
}
return qry;
}
}
}