use crate::{escape_into, js_trim, record};
pub fn class_into(out: &mut String, after: bool, items: &[&str]) {
let mut sep = after;
for item in items {
let item = js_trim(item);
if item.is_empty() {
continue;
}
if sep {
out.push(' ');
}
escape_into(out, item);
sep = true;
}
}
pub fn class_object(entries: &[(bool, &str)]) -> String {
let mut names: Vec<(&str, bool)> = Vec::new();
for (on, name) in entries {
match names.iter_mut().find(|(n, _)| n == name) {
Some(entry) => entry.1 = *on,
None => names.push((name, *on)),
}
}
let mut ordered: Vec<(Option<u32>, &str, bool)> = names
.into_iter()
.map(|(n, on)| (record::array_index(n), n, on))
.collect();
ordered.sort_by_key(|(i, _, _)| i.map_or((1, 0), |i| (0, i)));
let mut s = String::new();
for (_, name, on) in ordered {
if on {
s.push_str(name);
s.push(' ');
}
}
js_trim(&s).to_owned()
}
#[cfg(test)]
mod tests;