37 lines
1.2 KiB
Rust
37 lines
1.2 KiB
Rust
/// Normalize a domain. This does the following:
|
|
/// - removes protocol prefixes
|
|
/// - removes leading `www`
|
|
/// - removes query params and path segments.
|
|
///
|
|
/// This function is for roach, and should exactly match the ts implementation.
|
|
///
|
|
/// ## Examples:
|
|
/// ```
|
|
/// # use toolbox::misc::normalize_domain;
|
|
/// assert_eq!("domain.com", normalize_domain("domain.com"));
|
|
/// assert_eq!("domain.com", normalize_domain("domain.com/"));
|
|
/// assert_eq!("domain.com", normalize_domain("domain.com/en/us"));
|
|
/// assert_eq!("domain.com", normalize_domain("domain.com/?key=val"));
|
|
/// assert_eq!("domain.com", normalize_domain("www.domain.com"));
|
|
/// assert_eq!("domain.com", normalize_domain("https://www.domain.com"));
|
|
/// assert_eq!("us.domain.com", normalize_domain("us.domain.com"));
|
|
/// ```
|
|
pub fn normalize_domain(domain: &str) -> &str {
|
|
let mut domain = domain.strip_prefix("http://").unwrap_or(domain);
|
|
domain = domain.strip_prefix("https://").unwrap_or(domain);
|
|
domain = domain.strip_prefix("www.").unwrap_or(domain);
|
|
domain = domain.find("/").map_or(domain, |x| &domain[0..x]);
|
|
|
|
return domain;
|
|
}
|
|
|
|
/*
|
|
pub fn random_string(length: usize) -> String {
|
|
rand::rng()
|
|
.sample_iter(&Alphanumeric)
|
|
.take(length)
|
|
.map(char::from)
|
|
.collect()
|
|
}
|
|
*/
|