-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathBasicExtensions.cs
173 lines (147 loc) · 5.42 KB
/
BasicExtensions.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
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Security.Cryptography;
using System.Text;
namespace Negri.Wot
{
public static class BasicExtensions
{
private static readonly DateTime UnixEpoch = new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc);
/// <summary>
/// Muda o tipo de Data (não é o mesmo que ToLocal ou ToUniversal, só mexe no DateTimeKind)
/// </summary>
public static DateTime ChangeKind(this DateTime date, DateTimeKind kind)
{
return DateTime.SpecifyKind(date, kind);
}
public static DateTime RemoveKind(this DateTime date)
{
return DateTime.SpecifyKind(date, DateTimeKind.Unspecified);
}
public static DateTime ToDateTime(this long unixTime)
{
return UnixEpoch.AddSeconds(unixTime);
}
/// <summary>
/// Retorna o dia da semana imediatamente anterior a data passada
/// </summary>
/// <param name="date"></param>
/// <param name="dayOfWeek"></param>
/// <returns></returns>
public static DateTime PreviousDayOfWeek(this DateTime date, DayOfWeek dayOfWeek)
{
date = date.AddDays(-1);
while (date.DayOfWeek != dayOfWeek)
{
date = date.AddDays(-1);
}
return date.Date;
}
/// <summary>
/// Faz o Equals de strings, desconsiderando Case e Acentos (Diacriticos)
/// </summary>
public static bool EqualsCiAi(this string a, string b)
{
if ((string.IsNullOrWhiteSpace(a)) && (string.IsNullOrWhiteSpace(b)))
{
return true;
}
if ((!string.IsNullOrWhiteSpace(a)) && (string.IsNullOrWhiteSpace(b)))
{
return false;
}
if ((string.IsNullOrWhiteSpace(a)) && (!string.IsNullOrWhiteSpace(b)))
{
return false;
}
return
string.Compare(a, b, CultureInfo.InvariantCulture,
CompareOptions.IgnoreCase | CompareOptions.IgnoreNonSpace) == 0;
}
private static readonly List<string> RomanNumerals = new List<string> { "M", "CM", "D", "CD", "C", "XC", "L", "XL", "X", "IX", "V", "IV", "I" };
private static readonly List<int> Numerals = new List<int> { 1000, 900, 500, 400, 100, 90, 50, 40, 10, 9, 5, 4, 1 };
/// <summary>
/// Converte para romanos
/// </summary>
/// <param name="number"></param>
/// <returns></returns>
/// <returns>
/// from https://stackoverflow.com/questions/22392810/integer-to-roman-format
/// </returns>
public static string ToRomanNumeral(this int number)
{
var romanNumeral = string.Empty;
while (number > 0)
{
// find biggest numeral that is less than equal to number
var index = Numerals.FindIndex(x => x <= number);
// subtract it's value from your number
number -= Numerals[index];
// tack it onto the end of your roman numeral
romanNumeral += RomanNumerals[index];
}
return romanNumeral;
}
/// <summary>
/// Remove acentos e cedilhas
/// </summary>
public static string RemoveDiacritics(this string input)
{
if (string.IsNullOrEmpty(input))
{
return string.Empty;
}
string stFormD = input.Normalize(NormalizationForm.FormD);
int len = stFormD.Length;
var sb = new StringBuilder();
for (int i = 0; i < len; i++)
{
UnicodeCategory uc = CharUnicodeInfo.GetUnicodeCategory(stFormD[i]);
if (uc != UnicodeCategory.NonSpacingMark)
{
sb.Append(stFormD[i]);
}
}
return (sb.ToString().Normalize(NormalizationForm.FormC));
}
public static string SanitizeForFileName(this string s)
{
if (s == null)
{
return null;
}
if (string.IsNullOrWhiteSpace(s))
{
return string.Empty;
}
return s.Replace('/', '_').Replace('\\', '_').Replace('?', '_').Replace('*', '_').Replace('!', '_');
}
public static string GetHash(this string phrase)
{
SHA512Managed hashTool = new SHA512Managed();
Byte[] phraseAsByte = Encoding.UTF8.GetBytes(string.Concat(phrase));
Byte[] encryptedBytes = hashTool.ComputeHash(phraseAsByte);
hashTool.Clear();
return Convert.ToBase64String(encryptedBytes).SanitizeForFileName();
}
public static string SanitizeToCsv(this string original)
{
if (string.IsNullOrEmpty(original))
{
return string.Empty;
}
if (original.Contains('"'))
{
// replace double quotes with two double quotes
original = original.Replace("\"", "\"\"");
}
if (original.Contains(','))
{
original = "\"" + original + "\"";
}
return original;
}
}
}