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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
use once_cell::sync::Lazy;
use regex::Regex;

use crate::string::constants::UNACCONTABLE_WORDS;

macro_rules! special_cases{
    ($s:ident, $($singular: expr => $plural:expr), *) => {
        match &$s[..] {
            $(
                $singular => {
                    return $plural.to_owned();
                },
            )*
            _ => ()
        }
    }
}

/// Converts a `&str` to singularized `String`
///
/// ```
///     use cruet::string::singularize::to_singular;
///     let mock_string: &str = "foo_bars";
///     let expected_string: String = "foo_bar".to_owned();
///     let asserted_string: String = to_singular(mock_string);
///     assert!(asserted_string == expected_string);
///
/// ```
/// ```
///     use cruet::string::singularize::to_singular;
///     let mock_string: &str = "oxen";
///     let expected_string: String = "ox".to_owned();
///     let asserted_string: String = to_singular(mock_string);
///     assert!(asserted_string == expected_string);
///
/// ```
/// ```
///     use cruet::string::singularize::to_singular;
///     let mock_string: &str = "crates";
///     let expected_string: String = "crate".to_owned();
///     let asserted_string: String = to_singular(mock_string);
///     assert!(asserted_string == expected_string);
///
/// ```
/// ```
///     use cruet::string::singularize::to_singular;
///     let mock_string: &str = "oxen";
///     let expected_string: String = "ox".to_owned();
///     let asserted_string: String = to_singular(mock_string);
///     assert!(asserted_string == expected_string);
///
/// ```
/// ```
///     use cruet::string::singularize::to_singular;
///     let mock_string: &str = "boxes";
///     let expected_string: String = "box".to_owned();
///     let asserted_string: String = to_singular(mock_string);
///     assert!(asserted_string == expected_string);
///
/// ```
/// ```
///     use cruet::string::singularize::to_singular;
///     let mock_string: &str = "vengeance";
///     let expected_string: String = "vengeance".to_owned();
///     let asserted_string: String = to_singular(mock_string);
///     assert!(asserted_string == expected_string);
///
/// ```
/// ```
///     use cruet::string::singularize::to_singular;
///     let mock_string: &str = "yoga";
///     let expected_string: String = "yoga".to_owned();
///     let asserted_string: String = to_singular(mock_string);
///     assert!(asserted_string == expected_string);
///
/// ```
///
pub fn to_singular(non_singular_string: &str) -> String {
    if UNACCONTABLE_WORDS.contains(&non_singular_string) {
        non_singular_string.to_owned()
    } else {
        special_cases![non_singular_string,
            "oxen" => "ox",
            "boxes" => "box",
            "men" => "man",
            "women" => "woman",
            "dice" => "die",
            "yeses" => "yes",
            "feet" => "foot",
            "eaves" => "eave",
            "geese" => "goose",
            "teeth" => "tooth",
            "quizzes" => "quiz"
        ];
        for &(ref rule, replace) in RULES.iter().rev() {
            if let Some(captures) = rule.captures(non_singular_string) {
                if let Some(c) = captures.get(1) {
                    let mut buf = String::new();
                    captures.expand(&format!("{}{}", c.as_str(), replace), &mut buf);
                    return buf;
                }
            }
        }

        non_singular_string.to_owned()
    }
}

static RULES: Lazy<Vec<(Regex, &'static str)>> = Lazy::new(|| {
    vec![
        (r"(\w*)s$", ""),
        (r"(\w*)(ss)$", "$2"),
        (r"(n)ews$", "ews"),
        (r"(\w*)(o)es$", ""),
        (r"(\w*)([ti])a$", "um"),
        (
            r"((a)naly|(b)a|(d)iagno|(p)arenthe|(p)rogno|(s)ynop|(t)he)(sis|ses)$",
            "sis",
        ),
        (r"(^analy)(sis|ses)$", "sis"),
        (r"(\w*)([^f])ves$", "fe"),
        (r"(\w*)(hive)s$", ""),
        (r"(\w*)(tive)s$", ""),
        (r"(\w*)([lr])ves$", "f"),
        (r"(\w*([^aeiouy]|qu))ies$", "y"),
        (r"(s)eries$", "eries"),
        (r"(m)ovies$", "ovie"),
        (r"(\w*)(x|ch|ss|sh)es$", "$2"),
        (r"(m|l)ice$", "ouse"),
        (r"(bus)(es)?$", ""),
        (r"(shoe)s$", ""),
        (r"(cris|test)(is|es)$", "is"),
        (r"^(a)x[ie]s$", "xis"),
        (r"(octop|vir)(us|i)$", "us"),
        (r"(alias|status)(es)?$", ""),
        (r"^(ox)en", ""),
        (r"(vert|ind)ices$", "ex"),
        (r"(matr)ices$", "ix"),
        (r"(quiz)zes$", ""),
        (r"(database)s$", ""),
    ]
    .into_iter()
    .map(|(rule, replace)| (Regex::new(rule).unwrap(), replace))
    .collect()
});

#[test]
fn singularize_ies_suffix() {
    assert_eq!("reply", to_singular("replies"));
    assert_eq!("lady", to_singular("ladies"));
    assert_eq!("soliloquy", to_singular("soliloquies"));
}

#[test]
fn singularize_ss_suffix() {
    assert_eq!("glass", to_singular("glass"));
    assert_eq!("access", to_singular("access"));
    assert_eq!("glass", to_singular("glasses"));
    assert_eq!("witch", to_singular("witches"));
    assert_eq!("dish", to_singular("dishes"));
}

#[test]
fn singularize_string_if_a_regex_will_match() {
    let expected_string: String = "ox".to_owned();
    let asserted_string: String = to_singular("oxen");
    assert!(expected_string == asserted_string);
}

#[test]
fn singularize_string_returns_none_option_if_no_match() {
    let expected_string: String = "bacon".to_owned();
    let asserted_string: String = to_singular("bacon");

    assert!(expected_string == asserted_string);
}