implement builtin function append()
This commit is contained in:
parent
4fdac4c5f1
commit
b0aff089aa
@ -107,6 +107,36 @@ pub(crate) fn register(f: &mut HashMap<String, Builtin>) {
|
||||
list[len - n.abs().to_integer().to_usize().unwrap()] = val;
|
||||
}
|
||||
|
||||
Ok(Value::List(list, sep))
|
||||
}),
|
||||
);
|
||||
f.insert(
|
||||
"append".to_owned(),
|
||||
Box::new(|args, _| {
|
||||
max_args!(args, 3);
|
||||
let (mut list, sep) = match arg!(args, 0, "list") {
|
||||
Value::List(v, sep) => (v, sep),
|
||||
v => (vec![v], ListSeparator::Space),
|
||||
};
|
||||
let val = arg!(args, 1, "val");
|
||||
let sep = match arg!(
|
||||
args,
|
||||
2,
|
||||
"separator" = Value::Ident("auto".to_owned(), QuoteKind::None)
|
||||
) {
|
||||
Value::Ident(s, ..) => match s.as_str() {
|
||||
"auto" => sep,
|
||||
"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()),
|
||||
};
|
||||
|
||||
list.push(val);
|
||||
|
||||
Ok(Value::List(list, sep))
|
||||
}),
|
||||
);
|
||||
|
@ -80,3 +80,28 @@ test!(
|
||||
"a {\n color: set-nth((a, b, c), 1, e);\n}\n",
|
||||
"a {\n color: e, b, c;\n}\n"
|
||||
);
|
||||
test!(
|
||||
append_space_separated,
|
||||
"a {\n color: append(a b, c);\n}\n",
|
||||
"a {\n color: a b c;\n}\n"
|
||||
);
|
||||
test!(
|
||||
append_comma_separated,
|
||||
"a {\n color: append((a, b), c);\n}\n",
|
||||
"a {\n color: a, b, c;\n}\n"
|
||||
);
|
||||
test!(
|
||||
append_list,
|
||||
"a {\n color: append(a b, c d);\n}\n",
|
||||
"a {\n color: a b c d;\n}\n"
|
||||
);
|
||||
test!(
|
||||
append_list_separator_comma,
|
||||
"a {\n color: append(a, b, comma);\n}\n",
|
||||
"a {\n color: a, b;\n}\n"
|
||||
);
|
||||
test!(
|
||||
append_list_separator_space,
|
||||
"a {\n color: append((a, b), c, space);\n}\n",
|
||||
"a {\n color: a b c;\n}\n"
|
||||
);
|
||||
|
Loading…
x
Reference in New Issue
Block a user