forked from JSONAPIdotNET/JSONAPI.NET
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathIPluralizationService.cs
More file actions
95 lines (86 loc) · 3.37 KB
/
IPluralizationService.cs
File metadata and controls
95 lines (86 loc) · 3.37 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
using System;
using System.Collections.Generic;
namespace JSONAPI.Core
{
/// <summary>
/// A mirror of System.Data.Entity.Infrastructure.Pluralization.IPluralizationService,
/// but redefined here to allow usage without a dependency on Entity Framework.
/// </summary>
public interface IPluralizationService
{
/// <summary>
/// Return the plural form of a word. This function should be idempotent! (Allowing for the caveat that
/// explicit mappings may be required to make this be true.)
/// </summary>
/// <param name="word">The word to pluralize</param>
/// <returns>The plural form of the word</returns>
string Pluralize(string word);
/// <summary>
/// Return the plural form of a word. This function should be idempotent! (Allowing for the caveat that
/// explicit mappings may be required to make this be true.)
/// </summary>
/// <param name="word">The word to singularize</param>
/// <returns>The singular form of the word</returns>
string Singularize(string word);
}
/// <summary>
/// A horribly naive, default implementation of <see cref="IPluralizationService"/>.
/// If you use this, at least specify mappings extensively!
/// </summary>
public class PluralizationService : IPluralizationService
{
private Dictionary<string,string> s2p;
private Dictionary<string,string> p2s;
public PluralizationService()
{
s2p = new Dictionary<string,string>();
p2s = new Dictionary<string,string>();
}
public PluralizationService(Dictionary<string, string> explicitMappings)
{
s2p = new Dictionary<string,string>();
p2s = new Dictionary<string,string>();
foreach(KeyValuePair<string,string> pair in explicitMappings)
{
s2p.Add(pair.Key, pair.Value);
p2s.Add(pair.Value, pair.Key);
}
}
public void AddMapping(string singular, string plural)
{
if (!s2p.ContainsKey(singular) && !p2s.ContainsKey(plural))
{
s2p.Add(singular, plural);
p2s.Add(plural, singular);
}
else
{
throw new ArgumentException("At least one side of the mapping already exists.");
}
}
public void RemoveMapping(string singular, string plural)
{
if (s2p.ContainsKey(singular) && p2s.ContainsKey(plural) && s2p[singular] == plural && p2s[plural] == singular)
{
s2p.Remove(singular);
p2s.Remove(plural);
}
else
{
throw new ArgumentException("Specified mapping does not already exist.");
}
}
public string Pluralize(string word)
{
if (s2p.ContainsKey(word)) return s2p[word];
if (p2s.ContainsKey(word)) return word; // idempotence!
return word + "s";
}
public string Singularize(string word)
{
if (p2s.ContainsKey(word)) return p2s[word];
if (s2p.ContainsKey(word)) return word; // idempotentce!
return word.TrimEnd('s');
}
}
}