Created
January 24, 2013 23:39
-
-
Save pdwetz/4629697 to your computer and use it in GitHub Desktop.
Custom Simple Data pluralizer; makes an attempt to properly handle words ending in "y".
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using Simple.Data; | |
/// <summary> | |
/// Custom Simple Data pluralizer; makes an attempt to properly handle words ending in "y". | |
/// </summary> | |
public class Pluralizer : 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) | |
{ | |
if (!word.EndsWith("y", StringComparison.InvariantCultureIgnoreCase) | |
|| word.EndsWith("ey", StringComparison.InvariantCultureIgnoreCase) | |
|| word.EndsWith("ay", StringComparison.InvariantCultureIgnoreCase) | |
|| word.EndsWith("oy", StringComparison.InvariantCultureIgnoreCase)) | |
{ | |
return string.Concat(word, "s"); | |
} | |
return string.Concat(word.Substring(0, word.Length - 1), "ies"); | |
} | |
public string Singularize(string word) | |
{ | |
if (word.EndsWith("ies", StringComparison.InvariantCultureIgnoreCase)) | |
{ | |
return string.Concat(word.Substring(0, word.Length - 3), "y"); | |
} | |
return word.EndsWith("s", StringComparison.InvariantCultureIgnoreCase) | |
? word.Substring(0, word.Length - 1) | |
: word; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment