initial implementation of zip
This commit is contained in:
parent
0f5ebdebf9
commit
24176bb1f0
@ -240,4 +240,37 @@ pub(crate) fn register(f: &mut HashMap<String, Builtin>) {
|
||||
Ok(Value::Dimension(index, Unit::None))
|
||||
}),
|
||||
);
|
||||
f.insert(
|
||||
"zip".to_owned(),
|
||||
Box::new(|mut args, _| {
|
||||
let lists = args
|
||||
.get_variadic()?
|
||||
.into_iter()
|
||||
.map(|x| match x {
|
||||
Value::List(v, ..) => v,
|
||||
Value::Map(m) => m.entries(),
|
||||
v => vec![v],
|
||||
})
|
||||
.collect::<Vec<Vec<Value>>>();
|
||||
|
||||
let len = lists.iter().map(|l| l.len()).min().unwrap_or(0);
|
||||
|
||||
if len == 0 {
|
||||
return Ok(Value::List(
|
||||
Vec::new(),
|
||||
ListSeparator::Comma,
|
||||
Brackets::None,
|
||||
));
|
||||
}
|
||||
|
||||
let result = (0..len)
|
||||
.map(|i| {
|
||||
let items = lists.iter().map(|v| v[i].clone()).collect();
|
||||
Value::List(items, ListSeparator::Space, Brackets::None)
|
||||
})
|
||||
.collect();
|
||||
|
||||
Ok(Value::List(result, ListSeparator::Comma, Brackets::None))
|
||||
}),
|
||||
);
|
||||
}
|
||||
|
@ -298,3 +298,8 @@ test!(
|
||||
"a {\n color: index(1px 1in 1cm, 96px);\n}\n",
|
||||
"a {\n color: 2;\n}\n"
|
||||
);
|
||||
test!(
|
||||
zip_three,
|
||||
"a {\n color: zip(1px 1px 3px, solid dashed solid, red green blue);\n}\n",
|
||||
"a {\n color: 1px solid red, 1px dashed green, 3px solid blue;\n}\n"
|
||||
);
|
||||
|
Loading…
x
Reference in New Issue
Block a user