Skip to main content

teaql_tool_std/
text.rs

1use convert_case::{Case, Casing};
2
3pub struct TextTool;
4
5impl TextTool {
6    pub fn new() -> Self {
7        Self
8    }
9
10    pub fn trim(&self, s: &str) -> String {
11        s.trim().to_string()
12    }
13
14    pub fn lowercase(&self, s: &str) -> String {
15        s.to_lowercase()
16    }
17
18    pub fn uppercase(&self, s: &str) -> String {
19        s.to_uppercase()
20    }
21
22    pub fn capitalize(&self, s: &str) -> String {
23        let mut c = s.chars();
24        match c.next() {
25            None => String::new(),
26            Some(f) => f.to_uppercase().collect::<String>() + c.as_str(),
27        }
28    }
29
30    pub fn to_snake_case(&self, s: &str) -> String {
31        s.to_case(Case::Snake)
32    }
33
34    pub fn to_camel_case(&self, s: &str) -> String {
35        s.to_case(Case::Camel)
36    }
37
38    pub fn to_pascal_case(&self, s: &str) -> String {
39        s.to_case(Case::Pascal)
40    }
41
42    pub fn to_kebab_case(&self, s: &str) -> String {
43        s.to_case(Case::Kebab)
44    }
45
46    pub fn normalize_whitespace(&self, s: &str) -> String {
47        s.split_whitespace().collect::<Vec<&str>>().join(" ")
48    }
49
50    pub fn contains(&self, s: &str, substring: &str) -> bool {
51        s.contains(substring)
52    }
53
54    pub fn starts_with(&self, s: &str, prefix: &str) -> bool {
55        s.starts_with(prefix)
56    }
57
58    pub fn ends_with(&self, s: &str, suffix: &str) -> bool {
59        s.ends_with(suffix)
60    }
61}
62
63impl Default for TextTool {
64    fn default() -> Self {
65        Self::new()
66    }
67}