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 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204
use regex::Regex;
/// Return a version of the string in `camelCase` format.
///
/// # Arguments
///
/// * `string` - The string to get a camelCase version of.
///
/// # Examples
///
/// ```
/// let result = case_switcher::to_camel("sample_string");
/// assert_eq!(result, "sampleString");
/// ```
pub fn to_camel(string: &str) -> String {
let words = get_words(string);
let mut first_word = String::new();
if let Some(word) = words.first() {
first_word = if word.chars().next().unwrap().is_uppercase() {
word.to_owned()
} else {
word.to_lowercase()
};
}
let remaining_words = words.iter().skip(1);
let mut result = String::new();
result.push_str(&first_word);
result.push_str(&remaining_words.map(|s| capitalize(s)).collect::<String>());
result
}
/// Return a version of the string in `dot.case` format.
///
/// # Arguments
///
/// * `string` - The string to get a dot.case version of.
///
/// # Examples
///
/// ```
/// let result = case_switcher::to_dot("sample_string");
/// assert_eq!(result, "sample.string");
/// ```
pub fn to_dot(string: &str) -> String {
lower_join(string, ".")
}
/// Return a version of the string in `kebab-case` format.
///
/// # Arguments
///
/// * `string` - The string to get a kebab-case version of.
///
/// # Examples
///
/// ```
/// let result = case_switcher::to_kebab("sample_string");
/// assert_eq!(result, "sample-string");
/// ```
pub fn to_kebab(string: &str) -> String {
lower_join(string, "-")
}
/// Return a version of the string in `PascalCase` format.
///
/// # Arguments
///
/// * `string` - The string to get a PascalCase version of.
///
/// # Examples
///
/// ```
/// let result = case_switcher::to_pascal("sample_string");
/// assert_eq!(result, "SampleString");
/// ```
pub fn to_pascal(string: &str) -> String {
get_words(string)
.iter()
.map(|s| capitalize(s))
.collect::<String>()
}
/// Return a version of the string in `path/case` format.
///
/// # Arguments
///
/// * `string` - The string to get a path/case version of.
///
/// # Examples
///
/// ```
/// let result = case_switcher::to_path("sample_string");
/// assert_eq!(result, "sample/string");
/// ```
pub fn to_path(string: &str) -> String {
lower_join(string, "/")
}
/// Return a version of the string in `snake_case` format.
///
/// # Arguments
///
/// * `string` - The string to get a snake_case version of.
///
/// # Examples
///
/// ```
/// let result = case_switcher::to_snake("sampleString");
/// assert_eq!(result, "sample_string");
/// ```
pub fn to_snake(string: &str) -> String {
lower_join(string, "_")
}
/// Return a version of the string in `Title Case` format.
///
/// # Arguments
///
/// * `string` - The string to get a Title Case version of.
///
/// # Examples
///
/// ```
/// let result = case_switcher::to_title("sample_string");
/// assert_eq!(result, "Sample String");
/// ```
pub fn to_title(string: &str) -> String {
get_words(string)
.iter()
.map(|s| capitalize(s))
.collect::<Vec<String>>()
.join(" ")
}
/// Get all of the words in a string.
///
/// # Arguments
///
/// * `string` - The string to get words from.
///
/// # Examples
///
/// ```
/// let result = case_switcher::get_words("sample_string");
/// assert_eq!(result, vec!["sample", "string"]);
/// ```
pub fn get_words(string: &str) -> Vec<String> {
// Split on word boundaries and underscores
// let re = Regex::new(r"(.*?)[!@#$%^&*()\-_=+{}\[\]\\;:',.<>/?\n\t ]").unwrap();
let re = Regex::new(r"(.*?)[\W_]").unwrap();
let words = re.replace_all(string, "$1 $3");
// Split on lower then upper: "oneTwo" -> ["one", "Two"]
let re = Regex::new(r"([a-z])([A-Z])").unwrap();
let words = re.replace_all(&words, "$1 $2");
// Split on upper then upper + lower: "JSONWord" -> ["JSON", "Word"]
let re = Regex::new(r"([A-Z])([A-Z])([a-z])").unwrap();
let words = re.replace_all(&words, "$1 $2$3");
// Split on number + letter: "TO1Cat23dog" -> ["TO1", "Cat23", "dog"]
let re = Regex::new(r"(\d)([A-Za-z])").unwrap();
let words = re.replace_all(&words, "$1 $2");
words.split_whitespace().map(|s| s.to_string()).collect::<Vec<String>>()
}
/// Return a version of the string with the first letter capitalized.
///
/// # Arguments
///
/// * `string` - The string to get a capitalized version of.
///
/// # Examples
///
/// ```
/// let result = case_switcher::capitalize("sample_string");
/// assert_eq!(result, "Sample_string");
/// ```
pub fn capitalize(string: &str) -> String {
let mut chars = string.chars();
if let Some(first_char) = chars.next() {
let capitalized = first_char.to_uppercase().collect::<String>();
capitalized + chars.as_str()
} else {
String::new()
}
}
fn lower_join(string: &str, join_string: &str) -> String {
get_words(string)
.into_iter()
.map(|w| w.to_lowercase())
.collect::<Vec<String>>()
.join(join_string)
}