Skip to content

Commit e5b2a03

Browse files
authored
Merge pull request #5 from ssswagatss/development
Merge dev to master
2 parents f3c723f + 9b32345 commit e5b2a03

File tree

9 files changed

+413
-60
lines changed

9 files changed

+413
-60
lines changed

Docs/DateTimeExtensionsDocs.md

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
# DateTime Extension Methods
2+
This contains Extension Methods that mostly deal with `DateTime`. You can check the examples below for more details.
3+
Followings are the Methods available for public use.
4+
5+
- [ToMMDDYY()](#tommddyy)
6+
7+
## Api Reference Details
8+
* ### ToMMDDYY()
9+
Converts a given datetime to MM/dd/yyyy. You can also specify the separator.
10+
```csharp
11+
/// dateTime : The DateTime object.
12+
/// separator : string separator.
13+
public static string ToMMDDYY(this DateTime dateTime, char separator = '/');
14+
```
15+
#### Example
16+
```csharp
17+
//Example goes here.
18+
```

Docs/EnumExtensionsDocs.md

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
# DateTime Extension Methods
2+
This contains Extension Methods that mostly deal with `enum`. You can check the examples below for more details.
3+
Followings are the Methods available for public use.
4+
5+
- [EnumToList()](#enumtolist)
6+
7+
## Api Reference Details
8+
* ### EnumToList()
9+
Converts an Enum to a list containinng all values
10+
```csharp
11+
public static IEnumerable<T> EnumToList<T>();
12+
```
13+
#### Example
14+
```csharp
15+
//Example goes here.
16+
```

Docs/GenericExtensionsDocs.md

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
# Generic Extension Methods
2+
This contains Extension Methods that mostly deal with all kinds of data type (`T`). You can check the examples below for more details.
3+
Followings are the Methods available for public use.
4+
5+
- [ForEach()](#foreach)
6+
7+
## Api Reference Details
8+
* ### ForEach()
9+
Itterates over any collection that implements IEnumerable
10+
```csharp
11+
/// source : The source.
12+
/// action : The action.
13+
public static void ForEach<T>(this IEnumerable<T> source, Action<T> action);
14+
```
15+
#### Example
16+
```csharp
17+
//Example goes here.
18+
```

Docs/JsonExtensionsDocs.md

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
# JSON Extension Methods
2+
This contains Extension Methods that mostly deal with JSON Data. You can check the examples below for more details.
3+
Followings are the Methods available for public use.
4+
5+
- [JsonToObject()](#jsontoobject)
6+
7+
## Api Reference Details
8+
* ### JsonToObject()
9+
Converts a Json string to object of type T method applicable for multi hierarchy objects i.e having zero or many parent child relationships, Ignore loop references and do not serialize if cycles are detected.
10+
```csharp
11+
/// json : JSON String
12+
public static T JsonToObject<T>(this string json);
13+
```
14+
#### Example
15+
```csharp
16+
//Example goes here.
17+
```

Docs/NumberExtensionsDocs.md

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
# Number Extension Methods
2+
This contains Extension Methods that mostly deal with numbers (`integer`,`double`,`float`). You can check the examples below for more details.
3+
Followings are the Methods available for public use.
4+
5+
- [ToString()](#tostring)
6+
7+
## Api Reference Details
8+
* ### ToString()
9+
Returns a formatted double or emtpy string`System.DateTime` else returns false.
10+
```csharp
11+
/// data : double value
12+
/// format : double formatstring
13+
public static string ToString(this double data, string format);
14+
```
15+
#### Example
16+
```csharp
17+
//Example goes here.
18+
```

Docs/StringExtensionDocs.md

Lines changed: 285 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,285 @@
1+
# String Extension Methods
2+
This contains Extension Methods that mostly deal with strings. You can check the examples below for more details.
3+
Followings are the Methods available for public use.
4+
5+
- [IsDateTime()](#isdateTime)
6+
- [IsInteger()](#isinteger)
7+
- [IsDecimal()](#isdecimal)
8+
- [IsNumeric()](#isnumeric)
9+
- [IsGuid()](#isguid)
10+
- [IsAlpha()](#isalpha)
11+
- [IsAlphaNumeric()](#isalphanumeric)
12+
- [IsEmailAddress()](#isemailaddress)
13+
- [IsIPv4()](#isipv4)
14+
- [IsMatch()](#ismatch)
15+
- [ToCSVString()](#tocsvstring)
16+
- [SplitTo()](#splitto)
17+
- [ToBoolean()](#toboolean)
18+
- [ToEnum()](#toenum)
19+
- [GetNullIfEmptyString()](#getnullifemptystring)
20+
- [FirstCharacter()](#firstcharacter)
21+
- [LastCharacter()](#lastcharacter)
22+
- [EndsWithIgnoreCase()](#endswithignorecase)
23+
- [StartsWithIgnoreCase()](#startswithignorecase)
24+
25+
## Api Reference Details
26+
* ### IsDateTime()
27+
Checks if date string with dateFormat is parsable to ```System.DateTime``` format. True if is valid ```System.DateTime``` else returns false.
28+
```csharp
29+
/// data : datetime string
30+
/// dateFormat : date format "dd/MM/yyyy" by default
31+
public static bool IsDateTime(this string data, string dateFormat = "dd/MM/yyyy");
32+
```
33+
#### Example
34+
```csharp
35+
var isDate = "09/10/2019".IsDateTime(); //Returns true.
36+
isDate = "11/28/2019".IsDateTime("MM/dd/yyyy"); // Returns true
37+
isDate = "11/28/2019".IsDateTime("dd/MM/yyyy"); // Returns false
38+
```
39+
* ### IsInteger()
40+
Checks if a string is a valid int32 value. Returns true if the string is a valid Integer, else returns false.
41+
```csharp
42+
/// val : string value
43+
public static bool IsInteger(this string val);
44+
```
45+
#### Example
46+
```csharp
47+
var isNumber = "112".IsInteger(); //Returns true.
48+
isNumber = "-23".IsInteger(); // Returns true
49+
isNumber = "".IsInteger(); // Returns false
50+
```
51+
* ### IsDecimal()
52+
Checks if a string is a valid decimal value. Returns true if the string is a valid decimal, else returns false.
53+
```csharp
54+
/// val : string value
55+
public static bool IsDecimal(this string val);
56+
```
57+
#### Example
58+
```csharp
59+
var isDecimal = "-112.0".IsDecimal(); //Returns true.
60+
isDecimal = "112".IsDecimal(); // Returns true
61+
isDecimal = "".IsDecimal(); // Returns false
62+
```
63+
* ### IsNumeric()
64+
Checks if a string is a valid floating value. Returns true if the string is a valid floating, else returns false.
65+
```csharp
66+
/// val : string value
67+
public static bool IsNumeric(this string val);
68+
```
69+
#### Example
70+
```csharp
71+
var isNumeric = "-112.0".IsNumeric(); //Returns true.
72+
isNumeric = "112".IsNumeric(); // Returns true
73+
isNumeric = "".IsNumeric(); // Returns false
74+
```
75+
* ### IsGuid()
76+
Checks if a string is a valid Guid value. Returns true if the string is a valid Guid, else returns false.
77+
```csharp
78+
/// val : string value
79+
public static bool IsGuid(this string val);
80+
```
81+
#### Example
82+
```csharp
83+
var isGuid = "ABCDER".IsGuid(); //Returns False.
84+
isGuid = "7FB05CF8-1D45-4935-81C0-A4DD22264C34".IsGuid(); // Returns true
85+
isGuid = "A005041CCCFF4D349AA0FF48F384A0D3".IsGuid(); // Returns true
86+
```
87+
* ### IsAlpha()
88+
Checks if the String contains only Unicode letters. ```null``` will return false. An empty String ("") will return false.
89+
```csharp
90+
/// val : string value
91+
public static bool IsAlpha(this string val);
92+
```
93+
#### Example
94+
```csharp
95+
var isAlpha = "Abc Das".IsAlpha(); //Returns true.
96+
isAlpha = "Abc90Das".IsAlpha(); // Returns false
97+
isAlpha = "Abc$#3Das".IsAlpha(); // Returns false
98+
```
99+
* ### IsAlphaNumeric()
100+
Checks if the String contains only Unicode letters, digits. ```null``` will return false. An empty String ("") will return false.
101+
```csharp
102+
/// val : string value
103+
public static bool IsAlphaNumeric(this string val);
104+
```
105+
#### Example
106+
```csharp
107+
var isAlphaNumeric = "Abc Das".IsAlphaNumeric(); //Returns true.
108+
isAlphaNumeric = "1234".IsAlphaNumeric(); //Returns true.
109+
isAlphaNumeric = "Abc90Das".IsAlphaNumeric(); // Returns true
110+
isAlphaNumeric = "Abc90DasA#$".IsAlphaNumeric(); // Returns false
111+
```
112+
* ### IsEmailAddress()
113+
Validates email address. Returns true, if a given string is a valid email address. Else returns false.
114+
```csharp
115+
/// val : string value
116+
public static bool IsEmailAddress(this string val);
117+
```
118+
#### Example
119+
```csharp
120+
var isEmail = "abc@gmail.com".IsEmailAddress(); //Returns true.
121+
isEmail = "1234".IsEmailAddress(); //Returns false.
122+
isEmail = "23@gmail.com".IsEmailAddress(); // Returns false
123+
isEmail = "23+12@gmail.co.in.com".IsEmailAddress(); // Returns false
124+
```
125+
* ### IsIPv4()
126+
Validates if a string is valid IPv4 Returns true, if a given string is a valid IP. Else returns false.
127+
```csharp
128+
/// val : string value
129+
public static bool IsIPv4(this string val);
130+
```
131+
#### Example
132+
```csharp
133+
var isIpV4 = "1.1.1.1".IsIPv4(); //Returns true.
134+
isIpV4 = ".1.1".IsIPv4(); //Returns false.
135+
isIpV4 = "23@gmail.com".IsIPv4(); // Returns false
136+
isIpV4 = "192.168.1.1".IsIPv4(); // Returns true
137+
```
138+
* ### IsMatch()
139+
Determines whether the specified string matches a regular expression or not. Returns true if the regex matches, else returns false.
140+
```csharp
141+
/// val : string value
142+
/// regEx : The reg ex.
143+
public static bool IsMatch(this string content, string regEx);
144+
```
145+
#### Example
146+
```csharp
147+
var isRegexMatch = "ssswagatss@gmail.com".IsMatch("^[a-zA-Z][\\w\\.-]*[a-zA-Z0-9]@[a-zA-Z0-9][\\w\\.-]*[a-zA-Z0-9]\\.[a-zA-Z][a-zA-Z\\.]*[a-zA-Z]$"); //Returns true.
148+
isRegexMatch = "ssswagatss+09@gmail.com".IsMatch("^[a-zA-Z][\\w\\.-]*[a-zA-Z0-9]@[a-zA-Z0-9][\\w\\.-]*[a-zA-Z0-9]\\.[a-zA-Z][a-zA-Z\\.]*[a-zA-Z]$"); //Returns false.
149+
```
150+
* ### IsUrl()
151+
Determines whether this string is URL. Returns true if URL, else returns false.
152+
```csharp
153+
/// val : string value
154+
public static bool IsUrl(this string val);
155+
```
156+
#### Example
157+
```csharp
158+
var isUrl = "http://google.com".IsUrl(); //Returns true.
159+
isUrl = "http://go ogle.com".IsUrl(); //Returns false.
160+
isUrl = "http://google.com/abc-ght?url=bar".IsUrl(); //Returns true.
161+
```
162+
* ### ToCSVString()
163+
Converts an object to CSV String separated by a Separator.
164+
```csharp
165+
/// val : string value
166+
/// separator : string separator, be default it separates with comma.
167+
public static string ToCSVString(this object obj, string separator = ",");
168+
```
169+
#### Example
170+
```csharp
171+
var user = new TestUser { Id = 1, Name = "Extension Methods", Age = 18 };
172+
var csv =user.ToCSVString(); //Returns "1,Extension Methods,18"
173+
csv =user.ToCSVString("|"); //Returns "1|Extension Methods|18"
174+
```
175+
* ### SplitTo()
176+
Returns an enumerable collection of the specified type containing the substrings in this instance that are delimited by elements of a specified Char array
177+
```csharp
178+
/// val : string value
179+
/// separator : An array of Unicode characters that delimit the substrings in this instance, an empty array containing no delimiters, or null
180+
public static IEnumerable<T> SplitTo<T>(this string val, params char[] separator) where T : IConvertible ;
181+
```
182+
#### Example
183+
```csharp
184+
var bar = "23,34,56,-2,33,100";
185+
var foo = bar.SplitTo<int>(',').ToArray(); //Returns new int[] { 23, 34, 56, -2, 33, 100 };
186+
```
187+
There is one more overloaded method of `Split()` that Returns an enumerable collection of the specified type containing the substrings in this instance that are delimited by elements of a specified Char array, but this time it does so taking the `StringSplitOptions` into consideration.
188+
```csharp
189+
/// val : string value
190+
/// options : Represents StringSplitOptions like 'RemoveEmptyEntries' or 'None'
191+
/// separator : An array of Unicode characters that delimit the substrings in this instance, an empty array containing no delimiters, or null
192+
public static IEnumerable<T> SplitTo<T>(this string val, StringSplitOptions options, params char[] separator) where T : IConvertible;
193+
```
194+
#### Example
195+
```csharp
196+
var bar = "23,34,,56,,-2,33,100";
197+
var foo = bar.SplitTo<int>(StringSplitOptions.RemoveEmptyEntries, ',').ToArray(); //Returns new int[] { 23, 34, 56, -2, 33, 100 };
198+
```
199+
* ### ToBoolean()
200+
Converts string to its boolean equivalent. This checks ignoring cases and trimming the string.
201+
```csharp
202+
/// val : string value
203+
public static bool ToBoolean(this string val);
204+
```
205+
#### Example
206+
```csharp
207+
"false".ToBoolean(); //Returns false
208+
"f".ToBoolean(); //Returns false
209+
"n".ToBoolean(); //Returns false
210+
"no".ToBoolean(); //Returns false
211+
"False".ToBoolean(); //Returns false
212+
"True".ToBoolean(); //Returns true
213+
"True".ToBoolean(); //Returns true
214+
"Yes".ToBoolean(); //Returns true
215+
"y".ToBoolean(); //Returns true
216+
```
217+
* ### ToEnum()
218+
Converts string to its Enum type. Checks of string is a member of type `T` enum before converting. If fails returns `default enum`
219+
```csharp
220+
/// val : string value
221+
public static T ToEnum<T>(this string val, T defaultValue = default(T), bool ignoreCase = false) where T : struct;
222+
```
223+
#### Example
224+
```csharp
225+
public enum TestEnums
226+
{
227+
Admin = 1,
228+
PublicUser = 2,
229+
Supervisor = 3
230+
}
231+
232+
"1".ToEnum<TestEnums>(); //Returns TestEnums.Admin
233+
"2".ToEnum<TestEnums>(); //Returns TestEnums.PublicUser
234+
```
235+
* ### GetNullIfEmptyString()
236+
Checks if a string is Null or EmptyString (`string.Empty`), retuns NULL if the string is NULL or Empty and returns the same string if Not.
237+
```csharp
238+
/// val : string value
239+
public static string GetNullIfEmptyString(this string val);
240+
```
241+
#### Example
242+
```csharp
243+
var bar = " Swagat ".GetNullIfEmptyString(); //Returns Swagat
244+
bar = "".GetNullIfEmptyString(); //Returns null
245+
```
246+
* ### FirstCharacter()
247+
Gets first character in string
248+
```csharp
249+
/// val : string value
250+
public static string FirstCharacter(this string val);
251+
```
252+
#### Example
253+
```csharp
254+
var bar = "Royal College".FirstCharacter(); //Returns R
255+
```
256+
* ### LastCharacter()
257+
Gets Last character in string
258+
```csharp
259+
/// val : string value
260+
public static string LastCharacter(this string val);
261+
```
262+
#### Example
263+
```csharp
264+
var bar = "Royal College".LastCharacter(); //Returns e
265+
```
266+
* ### EndsWithIgnoreCase()
267+
Checks a String ends with another sub-string ignoring the case. If it does, returns true, else returns false.
268+
```csharp
269+
/// val : string value
270+
public static bool EndsWithIgnoreCase(this string val, string suffix);
271+
```
272+
#### Example
273+
```csharp
274+
var bar = "Royal College".EndsWithIgnoreCase("Ege"); //Returns true
275+
```
276+
* ### StartsWithIgnoreCase()
277+
Checks a String Starts with another sub-string ignoring the case. If it does, returns true, else returns false.
278+
```csharp
279+
/// val : string value
280+
public static bool StartsWithIgnoreCase(this string val, string suffix);
281+
```
282+
#### Example
283+
```csharp
284+
var bar = "Royal College".StartsWithIgnoreCase("royal"); //Returns true
285+
```

Extensions.UnitTest/StringExtensionsTest.cs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -655,5 +655,14 @@ public void IsGuid_ShouldPass(string data, bool expected)
655655
Assert.True(data.IsGuid()==expected);
656656
//Assert.True(Guid.TryParse(data, out var guidvalue) == expected);
657657
}
658+
659+
[Theory]
660+
[InlineData("http://google.com",true)]
661+
[InlineData("http://goog le.com",false)]
662+
[InlineData("http://google.com/abc-ght?url=bar",true)]
663+
public void IsUrl_ShouldPass(string url, bool actual)
664+
{
665+
Assert.True(url.IsUrl() == actual);
666+
}
658667
}
659668
}

0 commit comments

Comments
 (0)