Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
  • Loading branch information
seokwon hong committed Oct 17, 2023
1 parent 4f77eac commit 85d1948
Show file tree
Hide file tree
Showing 8 changed files with 93 additions and 5 deletions.
20 changes: 20 additions & 0 deletions src/Statics/Utils.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
namespace eXtensionSharp.Statics;

public partial class Utils
{
public static Dictionary<string, object> xToEnumDictionary<T>() where T : Enum
{
var keys = Enum.GetNames(typeof(T));
var values = Enum.GetValues(typeof(T));

Dictionary<string, object> map = new();
for (var i = 0; i < keys.Length; i++)
{
var key = keys[i];
var value = values.GetValue(i);
map.Add(key, value);
}

return map;
}
}
4 changes: 2 additions & 2 deletions src/XFileExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@ public static void xFileCreate(this string fileName)
/// <param name="fileName"></param>
public static void xFileCreateAll(this string fileName)
{
List<string> paths = fileName.xToSplit(Path.DirectorySeparatorChar.ToString()).xToList();
List<string> paths = fileName.xSplit(Path.DirectorySeparatorChar.ToString()).xToList();

var dir = string.Empty;
paths.xForEach((path, i) =>
Expand Down Expand Up @@ -210,7 +210,7 @@ public static void xDirCreate(this string path)

public static void xDirCreateAll(this string path)
{
List<string> paths = path.xToSplit(Path.DirectorySeparatorChar.ToString()).xToList();
List<string> paths = path.xSplit(Path.DirectorySeparatorChar.ToString()).xToList();

var dir = string.Empty;
paths.xForEach((path, i) =>
Expand Down
5 changes: 5 additions & 0 deletions src/XIsIfExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,11 @@ public static bool xIf(this string item, string match)
return false;
}

public static string xIf(this string item, string @case, Func<string> match, Func<string> notMatch)
{
if (item.xIsSame(@case)) return match();
return notMatch();
}

#endregion [xIs Series]

Expand Down
2 changes: 1 addition & 1 deletion src/XStringExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -234,7 +234,7 @@ public static string xToJoin(this string[] values, string separator = ",")
/// <param name="value"></param>
/// <param name="separator">'§'</param>
/// <returns></returns>
public static string[] xToSplit(this string value, string separator = ",")
public static string[] xSplit(this string value, string separator = ",")
{
if (value.xIsEmpty()) return Array.Empty<string>();
return value.Split(separator, StringSplitOptions.RemoveEmptyEntries | StringSplitOptions.TrimEntries);
Expand Down
17 changes: 16 additions & 1 deletion src/XValueExtensions.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
using System.Text;
using System.Xml.Serialization;

namespace eXtensionSharp
{
public static class XValueExtensions
Expand Down Expand Up @@ -77,6 +80,18 @@ public static T xAs<T>(this object src)
// return default;
// }


public static string xToXmlString(this object obj, Type type)
{
string result = string.Empty;
XmlSerializer xmlSerialzer = new XmlSerializer(type);

using (MemoryStream ms = new MemoryStream())
{
xmlSerialzer.Serialize(ms, obj);
result = Encoding.UTF8.GetString(ms.ToArray());
}

return result;
}
}
}
10 changes: 10 additions & 0 deletions test/XDateTimeTest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
using System;
using System.Security.Cryptography.X509Certificates;
using NUnit.Framework;

namespace eXtensionSharp.test;

public class XDateTimeTest
{

}
16 changes: 15 additions & 1 deletion test/XStringTest.cs
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
using System;
using System.Text.Json;
using System.Text.Json.Nodes;
using NUnit.Framework;

namespace eXtensionSharp.test {
public class XStringTest {
[Test]
public void split_test() {
var chars ="a,b,c,d,e".xToSplit(",");
var chars ="a,b,c,d,e".xSplit(",");
Assert.AreEqual(chars.xFirst(), "a");
Assert.AreEqual(chars.xLast(), "e");
}
Expand Down Expand Up @@ -49,5 +51,17 @@ public void hidden_case_test()
Assert.AreEqual(expected2, result2);

}

[Test]
public void jsonnode_to_value_test()
{
using var doc = JsonDocument.Parse(
"[\n {\n \"Name\" : \"Test 2\",\n \"NumberOfComponents\" : 1,\n \"IsActive\" : true,\n \"CreatedBy\" : \"bsharma\"\n },\n {\n \"Name\" : \"Test 2\",\n \"NumberOfComponents\" : 1,\n \"IsActive\" : true,\n \"CreatedBy\" : \"bsharma\"\n }\n]");

foreach (var element in doc.RootElement.EnumerateArray())
{
TestContext.Out.WriteLine(element.GetProperty("Name").GetString());
}
}
}
}
24 changes: 24 additions & 0 deletions test/XValueTest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
using System;
using System.Collections.Generic;
using NUnit.Framework;

namespace eXtensionSharp.test;

public class XValueTest
{
[Test]
public void datetime_test()
{
object o = DateTime.Now;
o.xValue<DateTime>();
TestContext.WriteLine(o);
}

[Test]
public void value_collection_test()
{
object o = new List<string>() { "1", "2", "3" };
var list = o.xValue<List<string>>();
TestContext.WriteLine(list[0]);
}
}

0 comments on commit 85d1948

Please sign in to comment.