Skip to content

Commit

Permalink
Merge pull request #45 from guibranco/develop
Browse files Browse the repository at this point in the history
Develop
  • Loading branch information
guibranco authored Jul 24, 2020
2 parents 18211ef + 3ce3503 commit 60988b3
Show file tree
Hide file tree
Showing 9 changed files with 749 additions and 56 deletions.
2 changes: 2 additions & 0 deletions CrispyWaffle.sln.DotSettings
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
<wpf:ResourceDictionary xml:space="preserve" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:s="clr-namespace:System;assembly=mscorlib" xmlns:ss="urn:shemas-jetbrains-com:settings-storage-xaml" xmlns:wpf="http://schemas.microsoft.com/winfx/2006/xaml/presentation">
<s:Int64 x:Key="/Default/CodeInspection/CleanCode/MinimumMeaningfulMethodNameLength/@EntryValue">3</s:Int64></wpf:ResourceDictionary>
111 changes: 81 additions & 30 deletions Src/CrispyWaffle/Extensions/ConversionExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -94,12 +94,15 @@ public static DateTime ToDateTime(this string input)
}
}



/// <summary>
/// Tries to convert string to date time.
/// </summary>
/// <param name="input">The input string a valid DateTime format.</param>
/// <param name="value">The DateTime value.</param>
/// <returns><b>True</b> if success, <b>false</b> otherwise</returns>
// ReSharper disable once MethodTooLong
public static bool TryToDateTime(this string input, out DateTime value)
{
value = DateTime.MinValue;
Expand All @@ -110,30 +113,39 @@ public static bool TryToDateTime(this string input, out DateTime value)
{
case "agora":
case "now":

value = DateTime.Now;

return true;

case "hoje":
case "today":

value = DateTime.Today;

return true;

case "ontem":
case "yesterday":

value = DateTime.Today.AddDays(-1);

return true;

case "amanhã":
case "amanha":
case "tomorrow":

value = DateTime.Today.AddDays(1);

return true;

default:

if (DateTime.TryParse(input, out value))
return true;
return input.Length == 10 &&
DateTime.TryParseExact(input,
@"dd/MM/yyyy",
CultureInfo.InvariantCulture,
DateTimeStyles.None,
out value);

return input.Length == 10 && DateTime.TryParseExact(input, @"dd/MM/yyyy", CultureInfo.InvariantCulture, DateTimeStyles.None, out value);
}
}

Expand All @@ -146,7 +158,9 @@ public static int ToInt32(this string input)
{
if (string.IsNullOrWhiteSpace(input))
return 0;

var success = int.TryParse(input, NumberStyles.Number, StringExtensions.Culture, out var result);

return success ? result : 0;
}

Expand All @@ -159,7 +173,9 @@ public static long ToInt64(this string input)
{
if (string.IsNullOrWhiteSpace(input))
return 0;

var success = long.TryParse(input, NumberStyles.Number, StringExtensions.Culture, out var result);

return success ? result : 0;
}

Expand All @@ -172,6 +188,7 @@ public static decimal ToDecimal(this string input)
{
if (string.IsNullOrWhiteSpace(input))
return 0M;

return decimal.TryParse(input, NumberStyles.Number, StringExtensions.Culture, out var result)
? result
: 0M;
Expand Down Expand Up @@ -232,8 +249,10 @@ public static DateTime FromUnixTimeStamp(this int epochTime)
public static PhoneNumber ParseBrazilianPhoneNumber(this string number)
{
var result = new PhoneNumber(0, 0, 0);

if (number.TryParseBrazilianPhoneNumber(ref result))
return result;

throw new InvalidTelephoneNumberException(number.RemoveNonNumeric());
}

Expand Down Expand Up @@ -262,11 +281,16 @@ public static bool TryParseBrazilianPhoneNumber(this string number, ref PhoneNum
(dirtyLength == 11 || dirtyLength == 12)
? dirty.Substring(1, 2)
: dirty.Substring(0, 2);

var hasNineDigits = dirty.Substring(dirtyLength - 9, 1)
.Equals(@"9", StringExtensions.Comparison);

var allowedDigits = hasNineDigits ? 9 : 8;

var telephoneNumber = dirty.Substring(dirtyLength - allowedDigits, allowedDigits);

result = new PhoneNumber(55, prefix.ToInt32(), telephoneNumber.ToInt64());

return true;
}

Expand Down Expand Up @@ -297,6 +321,7 @@ public static string ToIdentString(this XmlDocument document)
{
if (document == null)
return null;

var builder = new StringBuilder();

var settings = new XmlWriterSettings
Expand Down Expand Up @@ -350,6 +375,16 @@ public static int ToModule10(this string input)
return digit;
}

/// <summary>
/// The ordinal suffix
/// </summary>
private static readonly Dictionary<int, string> OrdinalSuffix = new Dictionary<int, string>
{
{1,"st"},
{2,"nd"},
{3,"rd"}
};

/// <summary>
/// To the ordinal.
/// </summary>
Expand All @@ -359,20 +394,18 @@ public static string ToOrdinal(this long number)
{
if (number < 0)
return number.ToString();

var rem = number % 100;

if (rem >= 11 && rem <= 13)
return $"{number}th";
switch (number % 10)
{
case 1:
return $"{number}st";
case 2:
return $"{number}nd";
case 3:
return $"{number}rd";
default:
return $"{number}th";
}


var key = (int)number % 10;
if (OrdinalSuffix.ContainsKey(key))
return $"{number}{OrdinalSuffix[key]}";

return $"{number}th";
}

/// <summary>
Expand Down Expand Up @@ -427,27 +460,45 @@ public static T DeepClone<T>(this T instance, bool useNonPublic = true)

var arguments = new List<object>();
var parameters = ctor.GetParameters();

foreach (var parameter in parameters)
{
var property = type.GetProperty(parameter.Name, BindingFlags.Public | BindingFlags.IgnoreCase | BindingFlags.Instance);
ParseParameters(instance, useNonPublic, type, parameter, arguments);
}

if (property == null && !useNonPublic)
continue;
return (T)ctor.Invoke(arguments.ToArray());
}

if (property == null)
{
property = type.GetProperty(parameter.Name, BindingFlags.NonPublic | BindingFlags.IgnoreCase | BindingFlags.Instance);
/// <summary>
/// Parses the parameters.
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="instance">The instance.</param>
/// <param name="useNonPublic">if set to <c>true</c> [use non public].</param>
/// <param name="type">The type.</param>
/// <param name="parameter">The parameter.</param>
/// <param name="arguments">The arguments.</param>
private static void ParseParameters<T>(T instance, bool useNonPublic, Type type, ParameterInfo parameter, List<object> arguments)
{
var property = type.GetProperty(parameter.Name,
BindingFlags.Public | BindingFlags.IgnoreCase | BindingFlags.Instance);

if (property == null)
continue;
}
if (property == null && !useNonPublic)
return;

if (property.PropertyType != parameter.ParameterType)
continue;
if (property == null)
{
property = type.GetProperty(parameter.Name,
BindingFlags.NonPublic | BindingFlags.IgnoreCase | BindingFlags.Instance);

arguments.Add(property.GetValue(instance));
if (property == null)
return;
}
return (T)ctor.Invoke(arguments.ToArray());

if (property.PropertyType != parameter.ParameterType)
return;

arguments.Add(property.GetValue(instance));
}
}
}
1 change: 1 addition & 0 deletions Src/CrispyWaffle/Extensions/StringExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -282,6 +282,7 @@ public static string ToValidFileName(this string fileName)
/// <param name="inputToCompare">The string to compare.</param>
/// <returns>An Int32.</returns>
[Pure]
// ReSharper disable once MethodTooLong
public static int Levenshtein(this string input, string inputToCompare)
{
var n = input.Length;
Expand Down
3 changes: 2 additions & 1 deletion Src/CrispyWaffle/Extensions/TypeExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ public static bool IsSimpleType(this Type type)
/// <summary>
/// The primitive numeric types
/// </summary>
private static HashSet<TypeCode> PrimitiveNumericTypes = new HashSet<TypeCode>
private static readonly HashSet<TypeCode> PrimitiveNumericTypes = new HashSet<TypeCode>
{
TypeCode.Byte, TypeCode.SByte, TypeCode.UInt16, TypeCode.UInt32, TypeCode.UInt64, TypeCode.Int16,
TypeCode.Int32, TypeCode.Int64, TypeCode.Decimal, TypeCode.Double, TypeCode.Single
Expand All @@ -132,6 +132,7 @@ public static bool IsSimpleType(this Type type)
/// </summary>
/// <param name="type">The type.</param>
/// <returns>Boolean.</returns>
// ReSharper disable once CognitiveComplexity
public static bool IsNumericType(this Type type)
{
while (true)
Expand Down
Loading

0 comments on commit 60988b3

Please sign in to comment.