Reorganize
This commit is contained in:
36
crates/lib/toolbox/src/misc.rs
Normal file
36
crates/lib/toolbox/src/misc.rs
Normal file
@@ -0,0 +1,36 @@
|
||||
/// 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()
|
||||
}
|
||||
*/
|
||||
Reference in New Issue
Block a user