Implement builtin function join
This commit is contained in:
parent
f9730b9ec5
commit
29011f88bb
@ -140,4 +140,43 @@ pub(crate) fn register(f: &mut HashMap<String, Builtin>) {
|
||||
Ok(Value::List(list, sep))
|
||||
}),
|
||||
);
|
||||
f.insert(
|
||||
"join".to_owned(),
|
||||
Box::new(|args, _| {
|
||||
max_args!(args, 3);
|
||||
let (mut list1, sep1) = match arg!(args, 0, "list") {
|
||||
Value::List(v, sep) => (v, sep),
|
||||
v => (vec![v], ListSeparator::Space),
|
||||
};
|
||||
let list2 = match arg!(args, 1, "list") {
|
||||
Value::List(v, ..) => v,
|
||||
v => vec![v],
|
||||
};
|
||||
let sep = match arg!(
|
||||
args,
|
||||
2,
|
||||
"separator" = Value::Ident("auto".to_owned(), QuoteKind::None)
|
||||
) {
|
||||
Value::Ident(s, ..) => match s.as_str() {
|
||||
"auto" => {
|
||||
if list1.len() < 2 && list2.len() < 2 {
|
||||
ListSeparator::Space
|
||||
} else {
|
||||
sep1
|
||||
}
|
||||
},
|
||||
"comma" => ListSeparator::Comma,
|
||||
"space" => ListSeparator::Space,
|
||||
_ => {
|
||||
return Err("$separator: Must be \"space\", \"comma\", or \"auto\".".into())
|
||||
}
|
||||
},
|
||||
_ => return Err("$separator: Must be \"space\", \"comma\", or \"auto\".".into()),
|
||||
};
|
||||
|
||||
list1.extend(list2);
|
||||
|
||||
Ok(Value::List(list1, sep))
|
||||
}),
|
||||
);
|
||||
}
|
||||
|
@ -115,3 +115,33 @@ test!(
|
||||
"a {\n color: append((), a);\n}\n",
|
||||
"a {\n color: a;\n}\n"
|
||||
);
|
||||
test!(
|
||||
join_space_separated,
|
||||
"a {\n color: join(a b, c d);\n}\n",
|
||||
"a {\n color: a b c d;\n}\n"
|
||||
);
|
||||
test!(
|
||||
join_comma_separated,
|
||||
"a {\n color: join((a, b), (c, d));\n}\n",
|
||||
"a {\n color: a, b, c, d;\n}\n"
|
||||
);
|
||||
test!(
|
||||
join_non_list,
|
||||
"a {\n color: join(a, b);\n}\n",
|
||||
"a {\n color: a b;\n}\n"
|
||||
);
|
||||
test!(
|
||||
join_separator_comma,
|
||||
"a {\n color: join(a, b, comma);\n}\n",
|
||||
"a {\n color: a, b;\n}\n"
|
||||
);
|
||||
test!(
|
||||
join_separator_space,
|
||||
"a {\n color: join((a, b), (c, d), space);\n}\n",
|
||||
"a {\n color: a b c d;\n}\n"
|
||||
);
|
||||
// test!(
|
||||
// join_bracketed,
|
||||
// "a {\n color: join([a], b);\n}\n",
|
||||
// "a {\n color: [a b];\n}\n"
|
||||
// );
|
||||
|
Loading…
x
Reference in New Issue
Block a user