Skip to content

Commit

Permalink
Parse from QueryString added.
Browse files Browse the repository at this point in the history
  • Loading branch information
Bruno de Souza Melo committed Sep 10, 2023
1 parent 16eeb5e commit dd588e8
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 3 deletions.
2 changes: 1 addition & 1 deletion src/NuvTools.Common/NuvTools.Common.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
<SignAssembly>True</SignAssembly>
<AssemblyOriginatorKeyFile>NuvTools.Common.snk</AssemblyOriginatorKeyFile>
<Description>Common library for Web, Desktop and Mobile (MAUI) applications.</Description>
<Version>7.1.1</Version>
<Version>7.1.2</Version>
<GenerateDocumentationFile>True</GenerateDocumentationFile>
<EnforceCodeStyleInBuild>True</EnforceCodeStyleInBuild>
<PackageIcon>icon.png</PackageIcon>
Expand Down
35 changes: 35 additions & 0 deletions src/NuvTools.Common/Web/ObjectExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using System.Globalization;
using System.Text.Encodings.Web;
using System.Text.Json;
using System.Web;

namespace NuvTools.Common.Web;

Expand Down Expand Up @@ -56,4 +57,38 @@ where p.GetValue(obj, null) != null

return result;
}


/// <summary>
/// Gets the dictionary from QueryString.
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="queryString">QueryString to parse</param>
/// <returns></returns>
public static Dictionary<string, object> ParseQueryString(this string queryString)
{
if (string.IsNullOrEmpty(queryString)) throw new ArgumentNullException(nameof(queryString));

var result = new Dictionary<string, object>();

var parts = queryString.Split('?');

if (parts.Length == 2)
result.Add("UriBase", parts[0]);

var parameters = parts[parts.Length == 2 ? 1 : 0].Split('&');

var listAux = new List<KeyValuePair<string, string>>();

foreach (var item in parameters)
{
var keyValue = item.Split('=');
listAux.Add(new KeyValuePair<string, string>(HttpUtility.UrlDecode(keyValue[0]), keyValue.Length == 2 ? HttpUtility.UrlDecode(keyValue[1]) : null));
}

foreach (var item in listAux.GroupBy(e => e.Key))
result.Add(item.Key, item.Count() > 1 ? item.Select(e => e.Value).ToArray() : item.First().Value);

return result;
}
}
2 changes: 1 addition & 1 deletion tests/NuvTools.Common.Test/NuvTools.Common.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<PropertyGroup>
<TargetFramework>net7.0</TargetFramework>
<IsPackable>false</IsPackable>
<Version>7.1.1</Version>
<Version>7.1.2</Version>
<Authors>Nuv Tools</Authors>
<Copyright>Copyright © 2023 Nuv Tools</Copyright>
<PackageProjectUrl>https://nuv.tools</PackageProjectUrl>
Expand Down
9 changes: 8 additions & 1 deletion tests/NuvTools.Common.Test/Web/QueryStringTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public void Setup()
}

[Test]
public void Test1()
public void GetQueryString()
{
var obj = new FakeTest
{
Expand All @@ -34,4 +34,11 @@ public void Test1()
var queryString = obj.GetQueryString("https://nuv.tools");
Assert.That(queryString, Is.EqualTo("https://nuv.tools?Id=1&Date=2023-01-01T12%3A00%3A00&Name=Hello%20World!&Codes=1&Codes=2&Codes=3"));
}

[Test]
public void ParseQueryString()
{
var parsedQueryString = "https://nuv.tools?Id=1&Date=2023-01-01T12%3A00%3A00&Name=Hello%20World!&Codes=1&Codes=2&Codes=3&Exists".ParseQueryString();
Assert.That(parsedQueryString.Count, Is.EqualTo(5));
}
}

0 comments on commit dd588e8

Please sign in to comment.