better handling of single length lists in join

This commit is contained in:
ConnorSkees 2020-04-02 18:43:48 -04:00
parent e7008cd7e8
commit f86d613374
2 changed files with 7 additions and 6 deletions

View File

@ -147,14 +147,10 @@ pub(crate) fn register(f: &mut HashMap<String, Builtin>) {
"join".to_owned(), "join".to_owned(),
Box::new(|mut args, _| { Box::new(|mut args, _| {
max_args!(args, 4); max_args!(args, 4);
let mut is_first_list = true;
let (mut list1, sep1, brackets) = match arg!(args, 0, "list1") { let (mut list1, sep1, brackets) = match arg!(args, 0, "list1") {
Value::List(v, sep, brackets) => (v, sep, brackets), Value::List(v, sep, brackets) => (v, sep, brackets),
Value::Map(m) => (m.entries(), ListSeparator::Comma, Brackets::None), Value::Map(m) => (m.entries(), ListSeparator::Comma, Brackets::None),
v => { v => (vec![v], ListSeparator::Space, Brackets::None),
is_first_list = false;
(vec![v], ListSeparator::Space, Brackets::None)
}
}; };
let (list2, sep2) = match arg!(args, 1, "list2") { let (list2, sep2) = match arg!(args, 1, "list2") {
Value::List(v, sep, ..) => (v, sep), Value::List(v, sep, ..) => (v, sep),
@ -168,7 +164,7 @@ pub(crate) fn register(f: &mut HashMap<String, Builtin>) {
) { ) {
Value::Ident(s, ..) => match s.as_str() { Value::Ident(s, ..) => match s.as_str() {
"auto" => { "auto" => {
if list1.is_empty() || !is_first_list { if list1.is_empty() || (list1.len() == 1 && sep1 == ListSeparator::Space) {
sep2 sep2
} else { } else {
sep1 sep1

View File

@ -227,6 +227,11 @@ test!(
"a {\n color: join(c, (d, e));\n}\n", "a {\n color: join(c, (d, e));\n}\n",
"a {\n color: c, d, e;\n}\n" "a {\n color: c, d, e;\n}\n"
); );
test!(
join_single_bracketed_first_takes_separator_of_second,
"a {\n color: join([a], (b, ));\n}\n",
"a {\n color: [a, b];\n}\n"
);
test!(bracketed_ident, "a {\n color: [a];\n}\n"); test!(bracketed_ident, "a {\n color: [a];\n}\n");
test!(bracketed_space_list, "a {\n color: [a b];\n}\n"); test!(bracketed_space_list, "a {\n color: [a b];\n}\n");
test!(bracketed_comma_list, "a {\n color: [a, b];\n}\n"); test!(bracketed_comma_list, "a {\n color: [a, b];\n}\n");