Path: redb-route/redb.Route.DataFormats.XmlJson/Internal/Plural.cs Snapshot: redb-route, development snapshot 2026-12-06. This is the development state of the public mirrors; it may differ from the published packages. License: Apache-2.0. Repository: https://github.com/redbase-app/redb-route Page: https://redbase.app/src/dev-2026-12-06/redb-route/redb.Route.DataFormats.XmlJson/Internal/Plural.cs ---- namespace redb.Route.DataFormats.XmlJson.Internal; /// /// The plural-wrapper rule: a parent named as the English plural of its children's name (Incidents / /// Incident) holds a list. The singular candidates of a plural are the regular forms (-ies to -y, /// -es and -s left out) and a short list of irregular ones; a name with none of them is never a plural. /// Names compare as written, case included. /// internal static class Plural { private static readonly Dictionary Irregular = new(StringComparer.OrdinalIgnoreCase) { ["children"] = "child", ["people"] = "person", ["men"] = "man", ["women"] = "woman", ["indices"] = "index", ["matrices"] = "matrix", ["vertices"] = "vertex", ["analyses"] = "analysis", ["criteria"] = "criterion", ["feet"] = "foot", ["teeth"] = "tooth", ["mice"] = "mouse", }; /// Whether is a plural of . public static bool IsPair(string parent, string child) { if (child.Length == 0) return false; foreach (var singular in Singulars(parent)) if (string.Equals(singular, child, StringComparison.Ordinal)) return true; return false; } /// The singular forms may stand for; none for a name that is not a plural. public static IEnumerable Singulars(string plural) { if (Irregular.TryGetValue(plural, out var irregular)) yield return MatchCase(plural, irregular); if (plural.Length > 3 && plural.EndsWith("ies", StringComparison.Ordinal)) yield return plural[..^3] + "y"; if (plural.Length > 2 && plural.EndsWith("es", StringComparison.Ordinal)) yield return plural[..^2]; if (plural.Length > 1 && plural.EndsWith('s') && !plural.EndsWith("ss", StringComparison.Ordinal)) yield return plural[..^1]; } /// /// The one singular JSON to XML writes for the plural , or null when the name is not /// a plural. Irregular first, then -ies to -y (Categories), -es after ss, us, /// x, z, ch, sh (Addresses, Statuses, Boxes), else -s /// (Cases, Incidents). A choice has to be made where English is ambiguous: Movies gives /// Movy; such a name is written with arrays= and an explicit wrapper instead. /// public static string? SingularFor(string plural) { if (Irregular.TryGetValue(plural, out var irregular)) return MatchCase(plural, irregular); if (plural.Length > 3 && plural.EndsWith("ies", StringComparison.Ordinal)) return plural[..^3] + "y"; foreach (var ending in (string[])["sses", "uses", "xes", "zes", "ches", "shes"]) if (plural.Length > ending.Length && plural.EndsWith(ending, StringComparison.Ordinal)) return plural[..^2]; if (plural.Length > 1 && plural.EndsWith('s') && !plural.EndsWith("ss", StringComparison.Ordinal)) return plural[..^1]; return null; } /// The irregular singular with the first letter cased as the plural's (Children gives Child). private static string MatchCase(string plural, string singular) => char.IsUpper(plural[0]) ? char.ToUpperInvariant(singular[0]) + singular[1..] : singular; }