forked from ThatRendle/Simple.Data
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStringExtensions.cs
More file actions
70 lines (57 loc) · 1.84 KB
/
StringExtensions.cs
File metadata and controls
70 lines (57 loc) · 1.84 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
using System;
using System.Collections.Generic;
using System.Linq;
namespace Simple.Data.Extensions
{
public static class StringExtensions
{
public static bool IsPlural(this string str)
{
return _pluralizer.IsPlural(str);
}
public static string Pluralize(this string str)
{
return _pluralizer.Pluralize(str);
}
public static string Singularize(this string str)
{
return _pluralizer.Singularize(str);
}
public static bool IsAllUpperCase(this string str)
{
return !str.Any(char.IsLower);
}
public static string NullIfWhitespace(this string str)
{
return string.IsNullOrWhiteSpace(str) ? null : str;
}
public static string OrDefault(this string str, string defaultValue)
{
return str ?? defaultValue;
}
private static IPluralizer _pluralizer = new SimplePluralizer();
internal static void SetPluralizer(IPluralizer pluralizer)
{
_pluralizer = pluralizer ?? new SimplePluralizer();
}
}
class SimplePluralizer : IPluralizer
{
public bool IsSingular(string word)
{
return !IsPlural(word);
}
public bool IsPlural(string word)
{
return word.EndsWith("s", StringComparison.InvariantCultureIgnoreCase);
}
public string Pluralize(string word)
{
return string.Concat(word, word.IsAllUpperCase() ? "S" : "s");
}
public string Singularize(string word)
{
return word.EndsWith("s", StringComparison.InvariantCultureIgnoreCase) ? word.Substring(0, word.Length - 1) : word;
}
}
}