inflector/cases/titlecase/
mod.rs

1#![deny(warnings)]
2use cases::case::*;
3/// Converts a `&str` to `Title Case` `String`
4///
5/// ```
6///     use inflector::cases::titlecase::to_title_case;
7///     let mock_string: &str = "Foo bar";
8///     let expected_string: String = "Foo Bar".to_string();
9///     let asserted_string: String = to_title_case(mock_string);
10///     assert!(asserted_string == expected_string);
11///
12/// ```
13/// ```
14///     use inflector::cases::titlecase::to_title_case;
15///     let mock_string: &str = "FooBar";
16///     let expected_string: String = "Foo Bar".to_string();
17///     let asserted_string: String = to_title_case(mock_string);
18///     assert!(asserted_string == expected_string);
19///
20/// ```
21/// ```
22///     use inflector::cases::titlecase::to_title_case;
23///     let mock_string: &str = "fooBar";
24///     let expected_string: String = "Foo Bar".to_string();
25///     let asserted_string: String = to_title_case(mock_string);
26///     assert!(asserted_string == expected_string);
27///
28/// ```
29/// ```
30///     use inflector::cases::titlecase::to_title_case;
31///     let mock_string: &str = "FOO_BAR";
32///     let expected_string: String = "Foo Bar".to_string();
33///     let asserted_string: String = to_title_case(mock_string);
34///     assert!(asserted_string == expected_string);
35///
36/// ```
37/// ```
38///     use inflector::cases::titlecase::to_title_case;
39///     let mock_string: &str = "foo_bar";
40///     let expected_string: String = "Foo Bar".to_string();
41///     let asserted_string: String = to_title_case(mock_string);
42///     assert!(asserted_string == expected_string);
43///
44/// ```
45/// ```
46///     use inflector::cases::titlecase::to_title_case;
47///     let mock_string: &str = "foo-bar";
48///     let expected_string: String = "Foo Bar".to_string();
49///     let asserted_string: String = to_title_case(mock_string);
50///     assert!(asserted_string == expected_string);
51///
52/// ```
53pub fn to_title_case(non_title_case_string: &str) -> String {
54    let options = CamelOptions {
55        new_word: true,
56        last_char: ' ',
57        first_word: true,
58        injectable_char: ' ',
59        has_seperator: true,
60        inverted: false,
61    };
62    to_case_camel_like(non_title_case_string, options)
63}
64
65/// Determines if a `&str` is `Title Case`
66///
67/// ```
68///     use inflector::cases::titlecase::is_title_case;
69///     let mock_string: &str = "foo-bar-string-that-is-really-really-long";
70///     let asserted_bool: bool = is_title_case(mock_string);
71///     assert!(asserted_bool == false);
72///
73/// ```
74/// ```
75///     use inflector::cases::titlecase::is_title_case;
76///     let mock_string: &str = "FooBarIsAReallyReallyLongString";
77///     let asserted_bool: bool = is_title_case(mock_string);
78///     assert!(asserted_bool == false);
79///
80/// ```
81/// ```
82///     use inflector::cases::titlecase::is_title_case;
83///     let mock_string: &str = "fooBarIsAReallyReallyLongString";
84///     let asserted_bool: bool = is_title_case(mock_string);
85///     assert!(asserted_bool == false);
86///
87/// ```
88/// ```
89///     use inflector::cases::titlecase::is_title_case;
90///     let mock_string: &str = "FOO_BAR_STRING_THAT_IS_REALLY_REALLY_LONG";
91///     let asserted_bool: bool = is_title_case(mock_string);
92///     assert!(asserted_bool == false);
93///
94/// ```
95/// ```
96///     use inflector::cases::titlecase::is_title_case;
97///     let mock_string: &str = "foo_bar_string_that_is_really_really_long";
98///     let asserted_bool: bool = is_title_case(mock_string);
99///     assert!(asserted_bool == false);
100///
101/// ```
102/// ```
103///     use inflector::cases::titlecase::is_title_case;
104///     let mock_string: &str = "Foo bar string that is really really long";
105///     let asserted_bool: bool = is_title_case(mock_string);
106///     assert!(asserted_bool == false);
107///
108/// ```
109/// ```
110///     use inflector::cases::titlecase::is_title_case;
111///     let mock_string: &str = "foo";
112///     let asserted_bool: bool = is_title_case(mock_string);
113///     assert!(asserted_bool == false);
114///
115/// ```
116/// ```
117///     use inflector::cases::titlecase::is_title_case;
118///     let mock_string: &str = "Foo Bar String That Is Really Really Long";
119///     let asserted_bool: bool = is_title_case(mock_string);
120///     assert!(asserted_bool == true);
121///
122/// ```
123pub fn is_title_case(test_string: &str) -> bool {
124    test_string == to_title_case(test_string.clone())
125}
126
127#[cfg(all(feature = "unstable", test))]
128mod benchmarks {
129    extern crate test;
130    use self::test::Bencher;
131
132    #[bench]
133    fn bench_title(b: &mut Bencher) {
134        b.iter(|| super::to_title_case("Foo BAR"));
135    }
136
137    #[bench]
138    fn bench_is_title(b: &mut Bencher) {
139        b.iter(|| super::is_title_case("Foo bar"));
140    }
141
142    #[bench]
143    fn bench_title_from_snake(b: &mut Bencher) {
144        b.iter(|| super::to_title_case("foo_bar"));
145    }
146
147}
148
149
150#[cfg(test)]
151mod tests {
152    use ::to_title_case;
153    use ::is_title_case;
154
155    #[test]
156    fn from_camel_case() {
157        let convertable_string: String = "fooBar".to_owned();
158        let expected: String = "Foo Bar".to_owned();
159        assert_eq!(to_title_case(&convertable_string), expected)
160    }
161
162    #[test]
163    fn from_pascal_case() {
164        let convertable_string: String = "FooBar".to_owned();
165        let expected: String = "Foo Bar".to_owned();
166        assert_eq!(to_title_case(&convertable_string), expected)
167    }
168
169    #[test]
170    fn from_kebab_case() {
171        let convertable_string: String = "foo-bar".to_owned();
172        let expected: String = "Foo Bar".to_owned();
173        assert_eq!(to_title_case(&convertable_string), expected)
174    }
175
176    #[test]
177    fn from_sentence_case() {
178        let convertable_string: String = "Foo bar".to_owned();
179        let expected: String = "Foo Bar".to_owned();
180        assert_eq!(to_title_case(&convertable_string), expected)
181    }
182
183    #[test]
184    fn from_title_case() {
185        let convertable_string: String = "Foo Bar".to_owned();
186        let expected: String = "Foo Bar".to_owned();
187        assert_eq!(to_title_case(&convertable_string), expected)
188    }
189
190    #[test]
191    fn from_train_case() {
192        let convertable_string: String = "Foo-Bar".to_owned();
193        let expected: String = "Foo Bar".to_owned();
194        assert_eq!(to_title_case(&convertable_string), expected)
195    }
196
197    #[test]
198    fn from_screaming_snake_case() {
199        let convertable_string: String = "FOO_BAR".to_owned();
200        let expected: String = "Foo Bar".to_owned();
201        assert_eq!(to_title_case(&convertable_string), expected)
202    }
203
204    #[test]
205    fn from_snake_case() {
206        let convertable_string: String = "foo_bar".to_owned();
207        let expected: String = "Foo Bar".to_owned();
208        assert_eq!(to_title_case(&convertable_string), expected)
209    }
210
211    #[test]
212    fn from_case_with_loads_of_space() {
213        let convertable_string: String = "foo           bar".to_owned();
214        let expected: String = "Foo Bar".to_owned();
215        assert_eq!(to_title_case(&convertable_string), expected)
216    }
217
218    #[test]
219    fn a_name_with_a_dot() {
220        let convertable_string: String = "Robert C. Martin".to_owned();
221        let expected: String = "Robert C Martin".to_owned();
222        assert_eq!(to_title_case(&convertable_string), expected)
223    }
224
225    #[test]
226    fn random_text_with_bad_chars() {
227        let convertable_string: String = "Random text with *(bad) chars".to_owned();
228        let expected: String = "Random Text With Bad Chars".to_owned();
229        assert_eq!(to_title_case(&convertable_string), expected)
230    }
231
232    #[test]
233    fn trailing_bad_chars() {
234        let convertable_string: String = "trailing bad_chars*(()())".to_owned();
235        let expected: String = "Trailing Bad Chars".to_owned();
236        assert_eq!(to_title_case(&convertable_string), expected)
237    }
238
239    #[test]
240    fn leading_bad_chars() {
241        let convertable_string: String = "-!#$%leading bad chars".to_owned();
242        let expected: String = "Leading Bad Chars".to_owned();
243        assert_eq!(to_title_case(&convertable_string), expected)
244    }
245
246    #[test]
247    fn wrapped_in_bad_chars() {
248        let convertable_string: String = "-!#$%wrapped in bad chars&*^*&(&*^&(<><?>><?><>))".to_owned();
249        let expected: String = "Wrapped In Bad Chars".to_owned();
250        assert_eq!(to_title_case(&convertable_string), expected)
251    }
252
253    #[test]
254    fn has_a_sign() {
255        let convertable_string: String = "has a + sign".to_owned();
256        let expected: String = "Has A Sign".to_owned();
257        assert_eq!(to_title_case(&convertable_string), expected)
258    }
259
260    #[test]
261    fn is_correct_from_camel_case() {
262        let convertable_string: String = "fooBar".to_owned();
263        assert_eq!(is_title_case(&convertable_string), false)
264    }
265
266    #[test]
267    fn is_correct_from_pascal_case() {
268        let convertable_string: String = "FooBar".to_owned();
269        assert_eq!(is_title_case(&convertable_string), false)
270    }
271
272    #[test]
273    fn is_correct_from_kebab_case() {
274        let convertable_string: String = "foo-bar".to_owned();
275        assert_eq!(is_title_case(&convertable_string), false)
276    }
277
278    #[test]
279    fn is_correct_from_sentence_case() {
280        let convertable_string: String = "Foo bar".to_owned();
281        assert_eq!(is_title_case(&convertable_string), false)
282    }
283
284    #[test]
285    fn is_correct_from_title_case() {
286        let convertable_string: String = "Foo Bar".to_owned();
287        assert_eq!(is_title_case(&convertable_string), true)
288    }
289
290    #[test]
291    fn is_correct_from_train_case() {
292        let convertable_string: String = "Foo-Bar".to_owned();
293        assert_eq!(is_title_case(&convertable_string), false)
294    }
295
296    #[test]
297    fn is_correct_from_screaming_snake_case() {
298        let convertable_string: String = "FOO_BAR".to_owned();
299        assert_eq!(is_title_case(&convertable_string), false)
300    }
301
302    #[test]
303    fn is_correct_from_snake_case() {
304        let convertable_string: String = "foo_bar".to_owned();
305        assert_eq!(is_title_case(&convertable_string), false)
306    }
307}
308