#[must_use]
pub fn slugify(s: &str) -> String {
let mut out = String::new();
let mut prev_dash = false;
for c in s.chars() {
if c.is_ascii_alphanumeric() {
out.push(c.to_ascii_lowercase());
prev_dash = false;
} else if !prev_dash {
out.push('-');
prev_dash = true;
}
}
out.trim_matches('-').to_owned()
}
#[cfg(test)]
mod tests {
use super::slugify;
#[test]
fn collapses_punctuation_and_trims() {
assert_eq!(slugify("Install & build"), "install-build");
assert_eq!(
slugify("The five ways to run it"),
"the-five-ways-to-run-it"
);
assert_eq!(slugify(" ยง2 โ Context! "), "2-context");
assert_eq!(
slugify("Cross-repo: a hub and its spokes"),
"cross-repo-a-hub-and-its-spokes"
);
assert_eq!(slugify("!!!"), "");
}
}