pub fn isolate_ids(id_string: &str) -> String {
const PREFIX: &str = "u-";
let mut isolated_ids = String::new();
for class in id_string.split_whitespace() {
if !isolated_ids.is_empty() {
isolated_ids.push(' ');
}
if !class.starts_with(PREFIX) {
isolated_ids.push_str(PREFIX);
}
isolated_ids.push_str(class);
}
isolated_ids
}
#[test]
fn test_isolate_ids() {
macro_rules! test {
($input:expr, $expected:expr) => {
assert_eq!(
isolate_ids($input),
$expected,
"Actual isolated ID string doesn't match expected",
);
};
}
test!("", "");
test!(" ", "");
test!("apple", "u-apple");
test!("apple banana", "u-apple u-banana");
test!("apple banana", "u-apple u-banana");
test!(" apple banana", "u-apple u-banana");
test!(" apple banana ", "u-apple u-banana");
test!("apple banana cherry", "u-apple u-banana u-cherry");
test!("apple banana cherry", "u-apple u-banana u-cherry");
test!(" apple banana\tcherry", "u-apple u-banana u-cherry");
test!("u-apple banana cherry", "u-apple u-banana u-cherry");
test!("u-apple u-banana cherry", "u-apple u-banana u-cherry");
test!("u-apple u-banana u-cherry", "u-apple u-banana u-cherry");
test!("apple u-banana cherry", "u-apple u-banana u-cherry");
test!("u-u-apple", "u-u-apple");
}