aboutsummaryrefslogtreecommitdiff
path: root/macros/src/util/string.rs
blob: a04a021953a8dcd54f03aa7105ada081b10248c8 (plain)
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
use once_cell::sync::Lazy;
use regex::Regex;

static CAMELCASE_RE: Lazy<Regex> = Lazy::new(|| Regex::new(r"([a-z])([A-Z])").unwrap());

pub fn camelcase_to_snakecase(camelcased: &str) -> String
{
    CAMELCASE_RE
        .replace(camelcased, "${1}_$2")
        .to_string()
        .to_lowercase()
}

#[cfg(test)]
mod tests
{
    use super::*;

    #[test]
    fn camelcase_to_snakecase_works()
    {
        assert_eq!(camelcase_to_snakecase("LoginHandler"), "login_handler");

        assert_eq!(camelcase_to_snakecase("Regex"), "regex");
    }
}