mago_casing/lib.rs
1pub use cruet::case::camel::is_camel_case;
2pub use cruet::case::camel::to_camel_case;
3pub use cruet::case::kebab::is_kebab_case;
4pub use cruet::case::kebab::to_kebab_case;
5pub use cruet::case::pascal::is_pascal_case;
6pub use cruet::case::pascal::to_pascal_case;
7pub use cruet::case::screaming_snake::is_screaming_snake_case as is_constant_case;
8pub use cruet::case::screaming_snake::to_screaming_snake_case as to_constant_case;
9pub use cruet::case::sentence::is_sentence_case;
10pub use cruet::case::sentence::to_sentence_case;
11pub use cruet::case::table::is_table_case;
12pub use cruet::case::table::to_table_case;
13pub use cruet::case::title::is_title_case;
14pub use cruet::case::title::to_title_case;
15pub use cruet::case::train::is_train_case;
16pub use cruet::case::train::to_train_case;
17
18/// Determines if a `&str` is `ClassCase` `bool`
19///
20/// Unlike `cruet::case::is_class_case`, this function does not
21/// require the string to be in singular form.
22///
23/// ```
24/// use mago_casing::is_class_case;
25///
26/// assert!(is_class_case("Foo"));
27/// assert!(is_class_case("FooBarIsAReallyReallyLongString"));
28/// assert!(is_class_case("FooBarIsAReallyReallyLongStrings"));
29///
30/// assert!(!is_class_case("foo"));
31/// assert!(!is_class_case("foo-bar-string-that-is-really-really-long"));
32/// assert!(!is_class_case("foo_bar_is_a_really_really_long_strings"));
33/// assert!(!is_class_case("fooBarIsAReallyReallyLongString"));
34/// assert!(!is_class_case("FOO_BAR_STRING_THAT_IS_REALLY_REALLY_LONG"));
35/// assert!(!is_class_case("foo_bar_string_that_is_really_really_long"));
36/// assert!(!is_class_case("Foo bar string that is really really long"));
37/// assert!(!is_class_case("Foo Bar Is A Really Really Long String"));
38/// ```
39pub fn is_class_case(test_string: &str) -> bool {
40 to_class_case(test_string) == test_string
41}
42
43/// Converts a `&str` to `ClassCase` `String`
44///
45/// Unlike `cruet::case::to_class_case`, this function does not
46/// convert the string to singular form.
47///
48/// ```
49/// use mago_casing::to_class_case;
50///
51/// assert_eq!(to_class_case("UInt"), "UInt");
52/// assert_eq!(to_class_case("Uint"), "Uint");
53/// assert_eq!(to_class_case("Http2Client"), "Http2Client");
54/// assert_eq!(to_class_case("HTTP2Client"), "HTTP2Client");
55/// assert_eq!(to_class_case("FooBar"), "FooBar");
56/// assert_eq!(to_class_case("FooBars"), "FooBars");
57/// assert_eq!(to_class_case("foo_bars"), "FooBars");
58/// assert_eq!(to_class_case("Foo Bar"), "FooBar");
59/// assert_eq!(to_class_case("foo-bar"), "FooBar");
60/// assert_eq!(to_class_case("fooBar"), "FooBar");
61/// assert_eq!(to_class_case("Foo_Bar"), "FooBar");
62/// assert_eq!(to_class_case("Foo bar"), "FooBar");
63/// ```
64pub fn to_class_case(non_class_case_string: &str) -> String {
65 // grab the prefix, which is the first N - 1 uppercase characters, leaving only one uppercase
66 // character at the beginning of the string
67 let characters = non_class_case_string.chars();
68 let mut prefix_length = 0;
69 for character in characters {
70 if character.is_uppercase() || character.is_numeric() {
71 prefix_length += 1;
72 } else {
73 break;
74 }
75 }
76
77 let options = cruet::case::CamelOptions {
78 new_word: true,
79 last_char: ' ',
80 first_word: false,
81 injectable_char: ' ',
82 has_seperator: false,
83 inverted: false,
84 concat_num: true,
85 };
86
87 if prefix_length == 0 {
88 return cruet::case::to_case_camel_like(non_class_case_string, options);
89 }
90
91 let prefix = &non_class_case_string[..prefix_length - 1];
92 let remaining = &non_class_case_string[prefix_length - 1..];
93
94 let mut class_name = cruet::case::to_case_camel_like(remaining, options);
95 class_name.insert_str(0, prefix);
96
97 class_name
98}
99
100/// Determines if a `&str` is `snake_case` `bool`
101///
102/// Unlike `cruet::case::is_snake_case`, this function allows for
103/// numbers to be included in the string without separating them.
104///
105/// ```
106/// use mago_casing::is_snake_case;
107///
108/// assert!(is_snake_case("foo_2_bar"));
109/// assert!(is_snake_case("foo2bar"));
110/// assert!(is_snake_case("foo_bar"));
111/// assert!(is_snake_case("http_foo_bar"));
112/// assert!(is_snake_case("http_foo_bar"));
113/// assert!(is_snake_case("foo_bar"));
114/// assert!(is_snake_case("foo"));
115/// assert!(!is_snake_case("FooBar"));
116/// assert!(!is_snake_case("FooBarIsAReallyReallyLongString"));
117/// assert!(!is_snake_case("FooBarIsAReallyReallyLongStrings"));
118/// assert!(!is_snake_case("foo-bar-string-that-is-really-really-long"));
119/// ```
120pub fn is_snake_case(test_string: &str) -> bool {
121 test_string == to_snake_case(test_string)
122}
123
124/// Converts a `&str` to `snake_case` `String`
125///
126/// Unlike `cruet::case::to_snake_case`, this function allows for
127/// numbers to be included in the string without separating them.
128///
129/// ```
130/// use mago_casing::to_snake_case;
131///
132/// assert_eq!(to_snake_case("foo_2_bar"), "foo_2_bar");
133/// assert_eq!(to_snake_case("foo_bar"), "foo_bar");
134/// assert_eq!(to_snake_case("HTTP Foo bar"), "http_foo_bar");
135/// assert_eq!(to_snake_case("HTTPFooBar"), "http_foo_bar");
136/// assert_eq!(to_snake_case("Foo bar"), "foo_bar");
137/// assert_eq!(to_snake_case("Foo Bar"), "foo_bar");
138/// assert_eq!(to_snake_case("FooBar"), "foo_bar");
139/// assert_eq!(to_snake_case("FOO_BAR"), "foo_bar");
140/// assert_eq!(to_snake_case("fooBar"), "foo_bar");
141/// assert_eq!(to_snake_case("fooBar3"), "foo_bar3");
142/// assert_eq!(to_snake_case("lower2upper"), "lower2upper");
143/// ```
144pub fn to_snake_case(non_snake_case_string: &str) -> String {
145 let mut first_character: bool = true;
146 let mut last_separator: bool = true;
147 let mut result: String = String::with_capacity(non_snake_case_string.len() * 2);
148
149 for char_with_index in non_snake_case_string.trim_end_matches(|c: char| !c.is_alphanumeric()).char_indices() {
150 if !char_with_index.1.is_alphanumeric() {
151 if !first_character && !last_separator {
152 first_character = true;
153 last_separator = true;
154 result.push('_');
155 }
156 } else {
157 first_character = false;
158 if !last_separator
159 && !first_character
160 && char_with_index.1.is_uppercase()
161 && (non_snake_case_string.chars().nth(char_with_index.0 + 1).unwrap_or('A').is_lowercase()
162 || non_snake_case_string.chars().nth(char_with_index.0 - 1).unwrap_or('A').is_lowercase())
163 {
164 last_separator = true;
165 result.push('_');
166 } else {
167 last_separator = false;
168 }
169
170 result.push(char_with_index.1.to_ascii_lowercase());
171 }
172 }
173 result
174}