const IRREGULAR: [(&str, &str); 6] = [
("ies", "y"),
("sses", "ss"),
("ches", "ch"),
("shes", "sh"),
("xes", "x"),
("zes", "z"),
];
const NOT_PLURAL: [&str; 3] = ["ss", "us", "is"];
pub fn singularize(name: &str) -> String {
for (suffix, replacement) in IRREGULAR {
if let Some(stem) = name.strip_suffix(suffix) {
if !stem.is_empty() {
return format!("{stem}{replacement}");
}
}
}
if NOT_PLURAL.iter().any(|ending| name.ends_with(ending)) || name == "s" {
return name.to_string();
}
match name.strip_suffix('s') {
Some(stem) if !stem.is_empty() => stem.to_string(),
_ => name.to_string(),
}
}