Skip to content

Commit c53fc98

Browse files
authored
Merge pull request #31 from I-RzR-I/feature/AddExtensions
Add new string and Guid extensions
2 parents e192564 + 2ec14f3 commit c53fc98

4 files changed

Lines changed: 120 additions & 68 deletions

File tree

docs/CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,3 +133,8 @@
133133
-> Add new array extensions: `AppendItem`, `AppendIfNotExists`, `RemoveItem`, `RemoveAtIdx`;<br />
134134
-> Add new enumerable extensions: `GetDuplicates`, `ForEach`, `ForEachAndReturn`;<br />
135135
-> Add passcode/password generation util;<br />
136+
137+
### **v2.1.1.6403**
138+
-> Add new string extension: `FormatWith`;<br />
139+
-> Add new Guid/Guid? extension: `IsEmpty`;<br />
140+
-> Relocate several string extensions: `IsGuid`, `ToGuid`, `FromDoubleQuotesWithBackSlashesToGuid`;<br />

src/DomainCommonExtensions/DataTypeExtensions/GuidExtensions.cs

Lines changed: 47 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -17,75 +17,28 @@
1717
#region U S A G E S
1818

1919
using System;
20-
using System.Text.RegularExpressions;
21-
using DomainCommonExtensions.Resources;
20+
using DomainCommonExtensions.CommonExtensions;
2221

2322
#endregion
2423

2524
namespace DomainCommonExtensions.DataTypeExtensions
2625
{
26+
/// -------------------------------------------------------------------------------------------------
2727
/// <summary>
28-
/// Guid extensions
28+
/// Guid extensions.
2929
/// </summary>
30-
/// <remarks></remarks>
30+
/// =================================================================================================
3131
public static class GuidExtensions
3232
{
33+
/// -------------------------------------------------------------------------------------------------
3334
/// <summary>
34-
/// Check if is string is value
35+
/// Parse string to Guid.
3536
/// </summary>
36-
/// <param name="source"></param>
37-
/// <returns></returns>
38-
public static bool IsGuid(this string source)
39-
{
40-
if (source.IsNullOrEmpty()) return false;
41-
42-
var options = RegexOptions.IgnoreCase | RegexOptions.Multiline;
43-
var guidRegEx = new Regex(RegularExpressions.GUID, options);
44-
45-
return guidRegEx.IsMatch(source);
46-
}
47-
48-
/// <summary>
49-
/// Parse string to Guid
50-
/// </summary>
51-
/// <param name="source"></param>
52-
/// <returns></returns>
53-
public static Guid ToGuid(this string source)
54-
{
55-
if (source.IsNullOrEmpty()) return Guid.Empty;
56-
try
57-
{
58-
return Guid.Parse(source);
59-
}
60-
catch
61-
{
62-
return Guid.Empty;
63-
}
64-
}
65-
66-
/// <summary>
67-
/// Parse string to Guid from format: "\"9ffd2c3-6er456ds\""
68-
/// </summary>
69-
/// <param name="source"></param>
70-
/// <returns></returns>
71-
public static Guid FromDoubleQuotesWithBackSlashesToGuid(this string source)
72-
{
73-
if (source.IsNullOrEmpty()) return Guid.Empty;
74-
try
75-
{
76-
return Guid.ParseExact(source.Replace("-", "").Replace("\"", ""), "N");
77-
}
78-
catch
79-
{
80-
return Guid.Empty;
81-
}
82-
}
83-
84-
/// <summary>
85-
/// Parse string to Guid
86-
/// </summary>
87-
/// <param name="source"></param>
88-
/// <returns></returns>
37+
/// <param name="source">.</param>
38+
/// <returns>
39+
/// A Guid?
40+
/// </returns>
41+
/// =================================================================================================
8942
public static Guid? TryToGuid(this string source)
9043
{
9144
if (source.IsNullOrEmpty()) return null;
@@ -99,28 +52,58 @@ public static Guid FromDoubleQuotesWithBackSlashesToGuid(this string source)
9952
}
10053
}
10154

55+
/// -------------------------------------------------------------------------------------------------
10256
/// <summary>
103-
/// Guid to int 32
57+
/// Guid to int 32.
10458
/// </summary>
105-
/// <param name="uuid"></param>
106-
/// <returns></returns>
59+
/// <param name="uuid">.</param>
60+
/// <returns>
61+
/// Uuid as an int.
62+
/// </returns>
63+
/// =================================================================================================
10764
public static int ToInt32(this Guid uuid)
10865
{
10966
var gb = uuid.ToByteArray();
11067

11168
return BitConverter.ToInt32(gb, 0);
11269
}
11370

71+
/// -------------------------------------------------------------------------------------------------
11472
/// <summary>
115-
/// Guid to long
73+
/// Guid to long.
11674
/// </summary>
117-
/// <param name="uuid"></param>
118-
/// <returns></returns>
75+
/// <param name="uuid">.</param>
76+
/// <returns>
77+
/// Uuid as a long.
78+
/// </returns>
79+
/// =================================================================================================
11980
public static long ToLong(this Guid uuid)
12081
{
12182
var gb = uuid.ToByteArray();
12283

12384
return BitConverter.ToInt64(gb, 0);
12485
}
86+
87+
/// -------------------------------------------------------------------------------------------------
88+
/// <summary>
89+
/// A Guid? extension method that query if 'source' is empty.
90+
/// </summary>
91+
/// <param name="source">Source Guid value to be checked.</param>
92+
/// <returns>
93+
/// True if empty, false if not.
94+
/// </returns>
95+
/// =================================================================================================
96+
public static bool IsEmpty(this Guid? source) => source.IsNull() || source == Guid.Empty;
97+
98+
/// -------------------------------------------------------------------------------------------------
99+
/// <summary>
100+
/// A Guid? extension method that query if 'source' is empty.
101+
/// </summary>
102+
/// <param name="source">Source Guid value to be checked.</param>
103+
/// <returns>
104+
/// True if empty, false if not.
105+
/// </returns>
106+
/// =================================================================================================
107+
public static bool IsEmpty(this Guid source) => source == Guid.Empty;
125108
}
126109
}

src/DomainCommonExtensions/DataTypeExtensions/StringExtensions.cs

Lines changed: 65 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1604,7 +1604,7 @@ public static string[] ToStringArray(this string source, bool returnEmptyIfMissi
16041604

16051605
return source.ToCharArray().Select(c => c.ToString()).ToArray();
16061606
}
1607-
1607+
16081608
/// <summary>
16091609
/// Cast source array of letters/string value to one string.
16101610
/// </summary>
@@ -1618,5 +1618,69 @@ public static string ArrayToString(this string[] source, bool returnEmptyIfMissi
16181618

16191619
return string.Concat(source);
16201620
}
1621+
1622+
/// <summary>
1623+
/// Check if is string is value
1624+
/// </summary>
1625+
/// <param name="source">Source string value to check.</param>
1626+
/// <returns></returns>
1627+
public static bool IsGuid(this string source)
1628+
{
1629+
if (source.IsNullOrEmpty()) return false;
1630+
1631+
var options = RegexOptions.IgnoreCase | RegexOptions.Multiline;
1632+
var guidRegEx = new Regex(RegularExpressions.GUID, options);
1633+
1634+
return guidRegEx.IsMatch(source);
1635+
}
1636+
1637+
/// <summary>
1638+
/// Parse string to Guid
1639+
/// </summary>
1640+
/// <param name="source">Source string value to cast as Guid.</param>
1641+
/// <returns>Returns Guid.Empty if source is empty or parse error.</returns>
1642+
public static Guid ToGuid(this string source)
1643+
{
1644+
if (source.IsNullOrEmpty()) return Guid.Empty;
1645+
try
1646+
{
1647+
return Guid.Parse(source);
1648+
}
1649+
catch
1650+
{
1651+
return Guid.Empty;
1652+
}
1653+
}
1654+
1655+
/// <summary>
1656+
/// Parse string to Guid from format: "\"9ffd2c3-6er456ds\""
1657+
/// </summary>
1658+
/// <param name="source"></param>
1659+
/// <returns></returns>
1660+
public static Guid FromDoubleQuotesWithBackSlashesToGuid(this string source)
1661+
{
1662+
if (source.IsNullOrEmpty()) return Guid.Empty;
1663+
try
1664+
{
1665+
return Guid.ParseExact(source.Replace("-", "").Replace("\"", ""), "N");
1666+
}
1667+
catch
1668+
{
1669+
return Guid.Empty;
1670+
}
1671+
}
1672+
1673+
/// <summary>
1674+
/// Format source string values with parameters
1675+
/// </summary>
1676+
/// <param name="source">Parameterized source string value.</param>
1677+
/// <param name="args">Parameters to supply source string.</param>
1678+
/// <returns></returns>
1679+
public static string FormatWith(this string source, params object[] args)
1680+
{
1681+
if (source.IsMissing()) return string.Empty;
1682+
1683+
return string.Format(CultureInfo.CurrentCulture, source, args);
1684+
}
16211685
}
16221686
}

src/shared/GeneralAssemblyInfo.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,6 @@
4747
[assembly: NeutralResourcesLanguage("en-US", UltimateResourceFallbackLocation.MainAssembly)]
4848
#endif
4949

50-
[assembly: AssemblyVersion("2.1.0.0")]
51-
[assembly: AssemblyFileVersion("2.1.0.0")]
52-
[assembly: AssemblyInformationalVersion("2.1.0.0")]
50+
[assembly: AssemblyVersion("2.1.1.6403")]
51+
[assembly: AssemblyFileVersion("2.1.1.6403")]
52+
[assembly: AssemblyInformationalVersion("2.1.1.6403")]

0 commit comments

Comments
 (0)