forked from sergiisyrovatchenko/SQLIndexManager
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUtils.cs
More file actions
115 lines (92 loc) · 3.34 KB
/
Copy pathUtils.cs
File metadata and controls
115 lines (92 loc) · 3.34 KB
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
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
namespace SQLIndexManager {
public static class Utils {
public static string Description(this Enum value) {
var da = (DescriptionAttribute[])(value.GetType().GetField(value.ToString())).GetCustomAttributes(typeof(DescriptionAttribute), false);
return da.Length > 0 ? da[0].Description : value.ToString();
}
public static T ToEnum<T>(this object value) {
return (T)Enum.Parse(typeof(T), value.ToString(), true);
}
public static T GetValueFromDescription<T>(string description) {
var type = typeof(T);
if (!type.IsEnum) throw new InvalidOperationException();
foreach (var field in type.GetFields()) {
if (Attribute.GetCustomAttribute(field, typeof(DescriptionAttribute)) is DescriptionAttribute attribute) {
if (attribute.Description == description)
return (T)field.GetValue(null);
}
else {
if (field.Name == description)
return (T)field.GetValue(null);
}
}
return default(T);
}
public static string OnOff(this bool value) {
return value ? "ON" : "OFF";
}
public static bool IsBetween(this int value, int minimum, int maximum) {
return value >= minimum && value <= maximum;
}
public static string Sort(this string value) {
if (string.IsNullOrEmpty(value))
return string.Empty;
return value.Replace("], ", "],").Split(',').ToList().OrderBy(_ => _).Aggregate((_, __) => _ + __);
}
public static string Left(this string value, int maxLength) {
if (string.IsNullOrEmpty(value))
return value;
maxLength = Math.Abs(maxLength);
return value.Length <= maxLength ? value : value.Substring(0, maxLength);
}
public static List<string> RemoveInvalidTokens(this List<string> value) {
List<string> items = new List<string>();
foreach (string item in value) {
string t = item.Replace("'", "").Trim();
if (!string.IsNullOrEmpty(t))
items.Add(t);
}
return items;
}
public static string ToQuota(this string value) {
return $"[{value?.Replace("[", "[[").Replace("]", "]]")}]";
}
public static string Truncate(this string value, int maxLength) {
if (string.IsNullOrEmpty(value)) return value;
return value.Length <= maxLength ? value : value.Substring(0, maxLength);
}
public static int PageSize(this int val) {
return val * 1024 / 8;
}
public static string FormatSize(this decimal val) {
decimal aval = Math.Abs(val);
string dimension = "KB";
if (aval > 1024 * 1024 * 1024) {
val = val / 1024 / 1024 / 1024;
dimension = "TB";
}
else if (aval > 1024 * 1024) {
val = val / 1024 / 1024;
dimension = "GB";
}
else if (aval > 1024) {
val = val / 1024;
dimension = "MB";
}
return $"{ (val.ToString(val - Math.Truncate(val) == 0 ? "N0" : "N2")) } {dimension}";
}
public static string FormatMbSize(this int val) {
decimal value = val;
string dimension = "MB";
if (value > 1000) {
value = value / 1024;
dimension = "GB";
}
return $"{ (value.ToString(value - Math.Truncate(value) == 0 ? "N0" : "N2")) } {dimension}";
}
}
}