Skip to content

Commit 066e3d3

Browse files
authored
Merge pull request #10 from guibranco/develop
Develop
2 parents 619278d + e757e3f commit 066e3d3

File tree

9 files changed

+288
-105
lines changed

9 files changed

+288
-105
lines changed

README.md

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,32 @@ The CrispyWaffle project - a toolkit for dotNet (framework & core) projects
1818

1919
---
2020

21+
## Installation
22+
23+
Download the latest zip file from the [Release](https://github.com/GuiBranco/CrispyWaffle/Releases) pages or simple install from [NuGet](https://www.nuget.org/packages/CrispyWaffle) package manager
24+
25+
---
26+
2127
NuGet package: https://www.nuget.org/packages/CrispyWaffle
2228

2329
```ps
2430
Install-Package CrispyWaffle
2531
```
32+
---
33+
34+
## Changelog
35+
36+
Version 1.1 (2019-09-27) [@guibranco](https://github.com/guibranco)
37+
- Add Math Extensions (CrispyWaffle.Extensions.MathExtensions namespace)
38+
- Add Personal Data Validations (CrispyWaffle.Validations.PersonalDataValidations)
39+
- Rename method *FormatDocument* to *FormatBrazilianDocument* (CrispyWaffle.Extensions.ConversionExtensions)
40+
- Rename method *ParsePhoneNumber* to *arseBrazilianPhoneNumber* (CrispyWaffle.Extensions.ConversionExtensions)
41+
- Removed *CleanListItems* (CrispyWaffle.Extensions.ConversionExtensions)
42+
- Rename method *TryParsePhoneNumber* to *TryParseBrazilianPhoneNumber* (CrispyWaffle.Extensions.ConversionExtensions)
43+
- Removed *CleanListItems* and *ToListString* (CrispyWaffle.Extensions.ConversionExtensions) **(Specific to application patterns)**
44+
45+
---
46+
47+
## TODO
2648

27-
---
49+
- Add tests

Src/CrispyWaffle/Extensions/ConversionExtensions.cs

Lines changed: 10 additions & 74 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111
using System.Text.RegularExpressions;
1212
using System.Xml;
1313
using Utilities;
14-
using Validations;
1514

1615
/// <summary>
1716
/// Helper class for generic conversions
@@ -225,25 +224,26 @@ public static DateTime FromUnixTimeStamp(this int epochTime)
225224
}
226225

227226
/// <summary>
228-
/// Parses the phone number.
227+
/// Parses the brazilian phone number.
229228
/// </summary>
230229
/// <param name="number">The number.</param>
231230
/// <returns></returns>
232-
public static PhoneNumber ParsePhoneNumber(this string number)
231+
/// <exception cref="InvalidTelephoneNumberException"></exception>
232+
public static PhoneNumber ParseBrazilianPhoneNumber(this string number)
233233
{
234234
var result = new PhoneNumber(0, 0, 0);
235-
if (number.TryParsePhoneNumber(ref result))
235+
if (number.TryParseBrazilianPhoneNumber(ref result))
236236
return result;
237237
throw new InvalidTelephoneNumberException(number.RemoveNonNumeric());
238238
}
239239

240240
/// <summary>
241-
/// Tries the parse phone number.
241+
/// Tries the parse brazilian phone number.
242242
/// </summary>
243-
/// <param name="number">The phone number as string.</param>
244-
/// <param name="result">The phone number as PhoneNumber struct.</param>
245-
/// <returns><b>True</b> if succeed, <b>false</b> otherwise</returns>
246-
public static bool TryParsePhoneNumber(this string number, ref PhoneNumber result)
243+
/// <param name="number">The number.</param>
244+
/// <param name="result">The result.</param>
245+
/// <returns></returns>
246+
public static bool TryParseBrazilianPhoneNumber(this string number, ref PhoneNumber result)
247247
{
248248
var dirty = number.RemoveNonNumeric();
249249
var dirtyLength = dirty.Length;
@@ -269,70 +269,6 @@ public static bool TryParsePhoneNumber(this string number, ref PhoneNumber resul
269269
return true;
270270
}
271271

272-
/// <summary>
273-
/// To HTML list string.
274-
/// </summary>
275-
/// <param name="exception">The exception.</param>
276-
/// <param name="includeListTag">Includes the UL tags in the return string.</param>
277-
/// <returns>System.String.</returns>
278-
public static string ToListString(this Exception exception, bool includeListTag = true)
279-
{
280-
var builder = new StringBuilder();
281-
if (includeListTag)
282-
builder.Append(@"<ul>");
283-
284-
var inner = exception;
285-
while (inner != null)
286-
{
287-
var stackTrace = inner.StackTrace ?? string.Empty;
288-
if (ExceptionValidations.StackTracePattern.IsMatch(stackTrace))
289-
stackTrace =
290-
ExceptionValidations
291-
.StackTracePattern
292-
.Matches(stackTrace)
293-
.Cast<Match>()
294-
.Aggregate(string.Empty,
295-
(current, match) =>
296-
current + $@"<li> {match.Groups["method"].Value}</li>");
297-
builder.AppendFormat(@"<li><b>{0}</b> (<u>{1}</u>)</li>{2}",
298-
inner.Message,
299-
inner.GetType().FullName,
300-
stackTrace);
301-
inner = inner.InnerException;
302-
}
303-
304-
if (includeListTag)
305-
builder.AppendLine(@"</ul>");
306-
return builder.ToString();
307-
}
308-
309-
/// <summary>
310-
/// Cleans the list items.
311-
/// </summary>
312-
/// <param name="input">The input.</param>
313-
/// <param name="maxItems">The maximum items.</param>
314-
/// <returns></returns>
315-
public static string CleanListItems(this string input, int maxItems = 15)
316-
{
317-
input = input?.Replace(@"<ul>", string.Empty).Replace(@"</ul>", string.Empty) ??
318-
string.Empty;
319-
320-
var pattern = new Regex(@"<li class='operation'>(?<operation>.+?)</li>(?:\s+<li>(?:.+?)</li>)*",
321-
RegexOptions.IgnoreCase |
322-
RegexOptions.CultureInvariant |
323-
RegexOptions.Multiline);
324-
if (!pattern.IsMatch(input) || maxItems <= 0)
325-
return input;
326-
var matches = pattern.Matches(input);
327-
var max = matches.Count > maxItems
328-
? maxItems
329-
: matches.Count;
330-
input = string.Empty;
331-
for (var i = 1; i <= max; i++)
332-
input += matches[matches.Count - i];
333-
return input;
334-
}
335-
336272
/// <summary>
337273
/// To the pretty string.
338274
/// </summary>
@@ -436,7 +372,7 @@ public static string ToOrdinal(this int number)
436372
/// </summary>
437373
/// <param name="document">The document.</param>
438374
/// <returns></returns>
439-
public static string FormatDocument(this string document)
375+
public static string FormatBrazilianDocument(this string document)
440376
{
441377
if (string.IsNullOrWhiteSpace(document))
442378
return "Invalid document";
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
namespace CrispyWaffle.Extensions
2+
{
3+
/// <summary>
4+
/// The math extensions class.
5+
/// </summary>
6+
public static class MathExtensions
7+
{
8+
/// <summary>
9+
/// Rounds down.
10+
/// </summary>
11+
/// <param name="currentValue">The current value.</param>
12+
/// <param name="multipleOf">The multiple of.</param>
13+
/// <returns>Int32.</returns>
14+
public static int RoundDown(this int currentValue, int multipleOf = 10)
15+
{
16+
return currentValue - currentValue % multipleOf;
17+
}
18+
19+
/// <summary>
20+
/// Rounds up.
21+
/// </summary>
22+
/// <param name="currentValue">The current value.</param>
23+
/// <param name="multipleOf">The multiple of.</param>
24+
/// <param name="forceDifferentValue">The force different value.</param>
25+
/// <returns>Int32.</returns>
26+
public static int RoundUp(this int currentValue, int multipleOf = 10, bool forceDifferentValue = false)
27+
{
28+
return !forceDifferentValue &&
29+
currentValue % multipleOf == 0
30+
? currentValue
31+
: multipleOf - currentValue % multipleOf + currentValue;
32+
}
33+
34+
/// <summary>
35+
/// Rounds the best.
36+
/// </summary>
37+
/// <param name="currentValue">The current value.</param>
38+
/// <param name="multipleOf">The multiple of.</param>
39+
/// <param name="forceDifferentValue">The force different value.</param>
40+
/// <returns>Int32.</returns>
41+
public static int RoundBest(
42+
this int currentValue,
43+
int multipleOf = 10,
44+
bool forceDifferentValue = false)
45+
{
46+
if (!forceDifferentValue &&
47+
currentValue % multipleOf == 0)
48+
return currentValue;
49+
var left = currentValue % multipleOf;
50+
var half = multipleOf / 2;
51+
52+
return left > half
53+
? currentValue - left + multipleOf
54+
: currentValue + left;
55+
}
56+
}
57+
}

Src/CrispyWaffle/Log/Handlers/DefaultExceptionHandler.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -66,14 +66,14 @@ private string GetCategory()
6666
{
6767
var method = stack.GetFrame(counter++).GetMethod();
6868
if (method == null)
69-
return @"IntegracaoService";
69+
return @"CrispyWaffle";
7070
var ns = method.DeclaringType?.FullName;
7171
if (string.IsNullOrWhiteSpace(ns))
7272
return method.Name;
73-
if (ns.StartsWith(@"IntegracaoService.Commons.Log"))
73+
if (ns.StartsWith(@"CrispyWaffle.Log"))
7474
continue;
75-
if (ns.StartsWith(@"IntegracaoService.", StringExtensions.Comparison))
76-
ns = ns.Substring(18);
75+
if (ns.StartsWith(@"CrispyWaffle.", StringExtensions.Comparison))
76+
ns = ns.Substring(13);
7777
return ns;
7878
}
7979
}

Src/CrispyWaffle/Validations/ExceptionValidations.cs

Lines changed: 0 additions & 18 deletions
This file was deleted.
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
namespace CrispyWaffle.Validations
2+
{
3+
using System;
4+
using System.Runtime.Serialization;
5+
6+
/// <summary>
7+
/// Class InvalidDocumentException. This class cannot be inherited.
8+
/// </summary>
9+
/// <seealso cref="Exception" />
10+
[Serializable]
11+
public class InvalidDocumentException : Exception
12+
{
13+
/// <summary>
14+
/// Initializes a new instance of the <see cref="InvalidDocumentException"/> class.
15+
/// </summary>
16+
/// <param name="documentType">Type of the document.</param>
17+
/// <param name="value">The value.</param>
18+
public InvalidDocumentException(string documentType, string value)
19+
: base($"The value '{value}' isn't a valid value for a document of type {documentType} ")
20+
{ }
21+
22+
/// <summary>
23+
/// Initializes a new instance of the <see cref="InvalidDocumentException"/> class.
24+
/// </summary>
25+
/// <param name="info">The <see cref="T:System.Runtime.Serialization.SerializationInfo" /> that holds the serialized object data about the exception being thrown.</param>
26+
/// <param name="context">The <see cref="T:System.Runtime.Serialization.StreamingContext" /> that contains contextual information about the source or destination.</param>
27+
protected InvalidDocumentException(SerializationInfo info, StreamingContext context) : base(info, context)
28+
{ }
29+
}
30+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
namespace CrispyWaffle.Validations
2+
{
3+
using System;
4+
using System.Runtime.Serialization;
5+
6+
/// <summary>
7+
/// Class InvalidEmailAddressException. This class cannot be inherited.
8+
/// </summary>
9+
/// <seealso cref="Exception" />
10+
[Serializable]
11+
public class InvalidEmailAddressException : Exception
12+
{
13+
/// <summary>
14+
/// Initializes a new instance of the <see cref="InvalidEmailAddressException"/> class.
15+
/// </summary>
16+
/// <param name="emailAddress">The email address.</param>
17+
public InvalidEmailAddressException(string emailAddress)
18+
: base($"The e-mail address '{emailAddress}' isn't in a valid e-mail address format")
19+
{
20+
}
21+
22+
/// <summary>
23+
/// Initializes a new instance of the <see cref="InvalidEmailAddressException"/> class.
24+
/// </summary>
25+
/// <param name="info">The <see cref="T:System.Runtime.Serialization.SerializationInfo" /> that holds the serialized object data about the exception being thrown.</param>
26+
/// <param name="context">The <see cref="T:System.Runtime.Serialization.StreamingContext" /> that contains contextual information about the source or destination.</param>
27+
protected InvalidEmailAddressException(SerializationInfo info, StreamingContext context) : base(info, context)
28+
{
29+
}
30+
}
31+
}

0 commit comments

Comments
 (0)