Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
  • Loading branch information
nameofSEOKWONHONG committed Sep 26, 2024
1 parent 3c9bb99 commit fe2cb8d
Show file tree
Hide file tree
Showing 4 changed files with 94 additions and 41 deletions.
44 changes: 12 additions & 32 deletions src/XIsIfExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,8 @@ public static bool xIsFalse(this bool state)
}

/// <summary>
/// XisEmpty dose not support number type.
/// xIsEmpty dose not support number type.<br/>
/// xIsEmpty dose not support DateTime type.
/// </summary>
/// <param name="obj"></param>
/// <typeparam name="T"></typeparam>
Expand All @@ -48,36 +49,27 @@ public static bool xIsEmpty<T>(this T obj)
return string.IsNullOrWhiteSpace(v);
}

if (obj.xIsDateTime())
{
var t = obj.xAs<DateTime>();
if (t <= DateTime.MinValue) return true;
}

// collection type
switch (obj)
{
case ICollection { Count: 0 }:
case Array { Length: 0 }:
// ReSharper disable once HeapView.PossibleBoxingAllocation
case IEnumerable e when !e.GetEnumerator().MoveNext():
return true;

default: return false;
}
}

public static bool xIsDateTime<T>(this T obj)
{
var type = typeof(T);
return type == typeof(DateTime);
}


public static bool xIsNotEmpty<T>(this T obj)
{
return !obj.xIsEmpty();
}
}

public static bool xIsDateTime<T>(this T obj)
{
return obj is DateTime;
}

public static bool xIsEmptyNumber<T>(this T number)
where T : INumber<T>
{
Expand All @@ -103,22 +95,10 @@ public static bool xIsNotSame<T>(this T src, T compare)
return !src.xIsSame(compare);
}

public static bool IsNullableType<T>(this T o)
{
var type = typeof(T);
return Nullable.GetUnderlyingType(type).xIsNotEmpty();
}

public static bool xIf(this string item, string match)
{
if (item.xIsSame(match)) return true;
return false;
}

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

#endregion [xIs Series]
Expand Down
42 changes: 33 additions & 9 deletions src/XNumberExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,41 @@

namespace eXtensionSharp
{
public class PhoneInfo
{
public string Nation { get; set; }
public string NationCode { get; set; }
public string Number { get; set; }
}

public static class XNumberExtensions
{
public static string xToPhoneNumber(this string str)
{
if (str.xIsEmpty()) return string.Empty;
if (str.Length < 11) throw new Exception("str length is less than 11");

var nation = string.Empty;
var head = string.Empty;
var body = string.Empty;
var tail = string.Empty;

if (str.Length > 11)
{
nation = str.xSubstringFirst(2);
head = str.xSubstringMiddle(2, 3);
body = str.xSubstringMiddle(5, 4);
tail = str.Substring(9);
return $"+{nation}-{head}-{body}-{tail}";
}

head = str.xSubstringFirst(3);
body = str.xSubstringMiddle(3, 4);
tail = str.xSubstringLast(4);

return $"{head}-{body}-{tail}";
}

public static string xDisplayNumber<T>(this T val, ENUM_NUMBER_FORMAT_TYPE type, ENUM_VIEW_ALLOW_TYPE allow = ENUM_VIEW_ALLOW_TYPE.NotAllow) where T : struct
{
if (val.GetType() == typeof(DateTime)) throw new NotSupportedException("DateTime is not support.");
Expand All @@ -15,13 +48,6 @@ public static string xDisplayNumber<T>(this T val, ENUM_NUMBER_FORMAT_TYPE type,
{
ENUM_NUMBER_FORMAT_TYPE.Comma => $"{val:#,#}",
ENUM_NUMBER_FORMAT_TYPE.Rate => $"{val:##.##}",
ENUM_NUMBER_FORMAT_TYPE.Mobile => allow switch
{
ENUM_VIEW_ALLOW_TYPE.Allow => $"{val.ToString().xSubstringFirst(3)}-{val.ToString().xSubstringMiddle(3, 4)}-{val.ToString().xSubstringLast(4)}",
ENUM_VIEW_ALLOW_TYPE.NotAllow => $"{val.ToString().xSubstringFirst(3)}-{val.ToString().xSubstringMiddle(3, 4)}-****",
_ => throw new NotSupportedException()
},
ENUM_NUMBER_FORMAT_TYPE.Phone => MakePhoneString(val, allow),
ENUM_NUMBER_FORMAT_TYPE.RRN => MakeRRNString(val, allow),
_ => throw new NotSupportedException("do not convert value")
};
Expand Down Expand Up @@ -125,9 +151,7 @@ public enum ENUM_NUMBER_FORMAT_TYPE
{
Comma,
Rate,
Mobile,
RRN,
CofficePrice,
Phone
}
}
26 changes: 26 additions & 0 deletions test/XIsIfExtensionTest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
using System;
using System.Collections.Generic;
using NUnit.Framework;

namespace eXtensionSharp.test;

public class XIsIfExtensionTest
{
[Test]
public void xis_if_test()
{
var expected = false;
var a = "a";
a.xIf("b", () => Assert.AreEqual(expected, true), () => Assert.AreEqual(expected, false));

Check warning on line 14 in test/XIsIfExtensionTest.cs

View workflow job for this annotation

GitHub Actions / build

Consider using the constraint model, Assert.That(actual, Is.EqualTo(expected)), instead of the classic model, Assert.AreEqual(expected, actual) (https://github.com/nunit/nunit.analyzers/tree/master/documentation/NUnit2005.md)

Check warning on line 14 in test/XIsIfExtensionTest.cs

View workflow job for this annotation

GitHub Actions / build

Consider using the constraint model, Assert.That(actual, Is.EqualTo(expected)), instead of the classic model, Assert.AreEqual(expected, actual) (https://github.com/nunit/nunit.analyzers/tree/master/documentation/NUnit2005.md)

Check warning on line 14 in test/XIsIfExtensionTest.cs

View workflow job for this annotation

GitHub Actions / build

The actual value should not be a constant - perhaps the actual value and the expected value have switched places (https://github.com/nunit/nunit.analyzers/tree/master/documentation/NUnit2007.md)

Check warning on line 14 in test/XIsIfExtensionTest.cs

View workflow job for this annotation

GitHub Actions / build

The actual value should not be a constant - perhaps the actual value and the expected value have switched places (https://github.com/nunit/nunit.analyzers/tree/master/documentation/NUnit2007.md)
}

[Test]
public void xis_if_test2()
{
var expected = false;

Check warning on line 20 in test/XIsIfExtensionTest.cs

View workflow job for this annotation

GitHub Actions / build

The variable 'expected' is assigned but its value is never used
var a = 1;
var result = 0;
a.xIf(2, () => result = 1, () => result = 2);
Assert.AreNotEqual(a, result);

Check warning on line 24 in test/XIsIfExtensionTest.cs

View workflow job for this annotation

GitHub Actions / build

Consider using the constraint model, Assert.That(actual, Is.Not.EqualTo(expected)), instead of the classic model, Assert.AreNotEqual(expected, actual) (https://github.com/nunit/nunit.analyzers/tree/master/documentation/NUnit2006.md)
}
}
23 changes: 23 additions & 0 deletions test/XNumberExtentionsTest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
using NUnit.Framework;

namespace eXtensionSharp.test;


public class XNumberExtentionsTest
{
[Test]
public void xto_13_length_phonenumber_test()
{
var text = "8201011112222";
var number = text.xToPhoneNumber();
Assert.That(number, Is.EqualTo("+82-010-1111-2222"));
}

[Test]
public void xto_13_under_length_phonenumber_test2()
{
var text = "01011112222";
var number = text.xToPhoneNumber();
Assert.That(number, Is.EqualTo("010-1111-2222"));
}
}

0 comments on commit fe2cb8d

Please sign in to comment.