-
Notifications
You must be signed in to change notification settings - Fork 0
/
Extensions.cs
187 lines (162 loc) · 5.71 KB
/
Extensions.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
namespace PowerDotNet
{
/// <summary>
/// Summary description for Extensions
/// </summary>
public static class Extensions
{
public static string GetString(this object value, string defaultValue = "")
{
if (value == null)
return defaultValue;
if (value == DBNull.Value)
return defaultValue;
try
{
return value.ToString();
}
catch
{
return defaultValue;
}
}
public static T GetEnum<T>(this object value, T defaultValue) where T : struct
{
if (value == null)
return defaultValue;
if (value == DBNull.Value)
return defaultValue;
T result;
if (Enum.TryParse<T>(value.ToString(), out result))
{
return result;
}
else
{
return defaultValue; // you may want to throw exception here
}
}
public static int GetInt(this object value, int defaultValue = 0)
{
if (value == null)
return defaultValue;
if (value == DBNull.Value)
return defaultValue;
try
{
return Convert.ToInt32(value);
}
catch
{
return defaultValue;
}
}
public static bool GetBool(this object value, bool defaultValue = false)
{
if (value == null)
return defaultValue;
if (value == DBNull.Value)
return defaultValue;
try
{
if (value == "true" || value == "True")
return true;
if (value == "1")
return true;
return Convert.ToBoolean(value);
}
catch
{
return defaultValue;
}
}
public static DateTime GetDate(this object value, DateTime defaultValue = new DateTime())
{
if (value == null)
return defaultValue;
if (value == DBNull.Value)
return defaultValue;
try
{
return Convert.ToDateTime(value);
}
catch
{
return defaultValue;
}
}
//Goes together with PowerLib.js getEpoch, outputs the same value
public static long GetEpoch(this DateTime value)
{
return (value.ToUniversalTime().Ticks - 621355968000000000) / 10000000;
}
public static string NormalizeForUrl(this string value)
{
if (!string.IsNullOrEmpty(value))
{
value = value.NormalizeString();
// Replaces all non-alphanumeric character by a space
System.Text.StringBuilder builder = new System.Text.StringBuilder();
char[] removeables = new char[] { '’', '\'', '"' };
for (int i = 0; i < value.Length; i++)
{
if (!removeables.Contains(value[i]))
builder.Append(char.IsLetterOrDigit(value[i]) ? value[i] : ' ');
}
value = builder.ToString();
// Replace multiple spaces into a single dash
value = System.Text.RegularExpressions.Regex.Replace(value, @"[ ]{1,}", @"-", System.Text.RegularExpressions.RegexOptions.None);
value = value.TrimEnd('?', '.', ',', '-');
}
return value;
}
/// <summary>
/// Strips the value from any non english character by replacing thoses with their english equivalent.
/// </summary>
/// <param name="value">The string to normalize.</param>
/// <returns>A string where all characters are part of the basic english ANSI encoding.</returns>
/// <seealso cref="http://stackoverflow.com/questions/249087/how-do-i-remove-diacritics-accents-from-a-string-in-net"/>
public static string NormalizeString(this string value)
{
string normalizedFormD = value.Normalize(System.Text.NormalizationForm.FormD);
System.Text.StringBuilder builder = new System.Text.StringBuilder();
for (int i = 0; i < normalizedFormD.Length; i++)
{
System.Globalization.UnicodeCategory uc = System.Globalization.CharUnicodeInfo.GetUnicodeCategory(normalizedFormD[i]);
if (uc != System.Globalization.UnicodeCategory.NonSpacingMark)
{
builder.Append(normalizedFormD[i]);
}
}
return builder.ToString().Normalize(System.Text.NormalizationForm.FormC);
}
public static string StripFrom(this string value, int startPosition)
{
return startPosition > value.Length ? value : value.Remove(startPosition);
}
public static string StripTo(this string value, int endPosition)
{
return endPosition > value.Length ? "" : value.Remove(0, endPosition);
}
public static string Encrypt(this Object value)
{
try
{
return Crypt.EncryptString(value.GetString());
}
catch { return ""; }
}
public static string Decrypt(this Object value)
{
try
{
return Crypt.DecryptString(value.GetString());
}
catch { return ""; }
}
}
}