Skip to main content

sofie_core/
plural.rs

1/// How both sofie-http and `sofie gen` name a resource's path and list handler.
2#[must_use]
3pub fn plural(name: &str) -> String {
4    if let Some(stem) = name.strip_suffix('y')
5        && !stem.ends_with(['a', 'e', 'i', 'o', 'u'])
6    {
7        return format!("{stem}ies");
8    }
9    if name.ends_with('s')
10        && !["ss", "us", "is", "as", "os"]
11            .iter()
12            .any(|end| name.ends_with(end))
13    {
14        return name.to_owned();
15    }
16    if name.ends_with(['s', 'x', 'z']) || name.ends_with("ch") || name.ends_with("sh") {
17        return format!("{name}es");
18    }
19    format!("{name}s")
20}
21
22#[cfg(test)]
23mod tests {
24    use super::*;
25
26    #[test]
27    fn a_name_takes_its_english_plural_ending() {
28        for (name, expected) in [
29            ("order", "orders"),
30            ("order_line", "order_lines"),
31            ("category", "categories"),
32            ("day", "days"),
33            ("address", "addresses"),
34            ("box", "boxes"),
35            ("batch", "batches"),
36            ("opening_hours", "opening_hours"),
37            ("settings", "settings"),
38            ("status", "statuses"),
39            ("bus", "buses"),
40            ("alias", "aliases"),
41            ("glass", "glasses"),
42        ] {
43            assert_eq!(plural(name), expected, "{name}");
44        }
45    }
46}