-
Notifications
You must be signed in to change notification settings - Fork 0
/
ClassBuilder.cs
64 lines (53 loc) · 1.77 KB
/
ClassBuilder.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
namespace TablerForNet
{
public class ClassBuilder
{
public List<string> ClassNames = new List<string>();
public ClassBuilder(string classNames = null)
{
Add(classNames);
}
public ClassBuilder Add(string className)
{
if (!string.IsNullOrWhiteSpace(className))
{
var classNames = className
.Split(new[] {' '}, StringSplitOptions.RemoveEmptyEntries)
.Distinct()
.ToList();
foreach (var name in classNames.Where(name =>
!string.IsNullOrWhiteSpace(name) && !ClassNames.Contains(name)))
ClassNames.Add(name);
}
return this;
}
public ClassBuilder AddIf(string className, bool isOk)
{
return isOk ? Add(className) : this;
}
public ClassBuilder AddCompare<T>(T compare, Dictionary<T, string> with)
{
foreach (var kvp in with) AddCompare(kvp.Value, compare, kvp.Key);
return this;
}
public ClassBuilder AddCompare<T>(string className, T compare, T with)
{
return AddIf(className, compare.Equals(with));
}
public ClassBuilder Remove(string className)
{
ClassNames.RemoveAll(c => c.Equals(className, StringComparison.InvariantCultureIgnoreCase));
return this;
}
public override string ToString()
{
if (ClassNames == null || !ClassNames.Any())
{
return null;
}
return string.Join(" ", ClassNames
.Distinct()
.Where(c => !string.IsNullOrWhiteSpace(c)));
}
}
}