implement builtin function index

This commit is contained in:
ConnorSkees 2020-04-02 14:22:38 -04:00
parent dbbadaf62e
commit 72bc9a46e3
2 changed files with 31 additions and 0 deletions

View File

@ -193,4 +193,20 @@ pub(crate) fn register(f: &mut HashMap<String, Builtin>) {
}))
}),
);
f.insert(
"index".to_owned(),
Box::new(|mut args, _| {
max_args!(args, 2);
let list = match arg!(args, 0, "list") {
Value::List(v, ..) => v,
v => vec![v],
};
let value = arg!(args, 1, "value");
let index = match list.into_iter().position(|v| v == value) {
Some(v) => Number::from(v + 1),
None => return Ok(Value::Null),
};
Ok(Value::Dimension(index, Unit::None))
}),
);
}

View File

@ -189,3 +189,18 @@ test!(
deeply_nested_square_braces,
"a {\n color: [[[[[[a]]]]]];\n}\n"
);
test!(
index_found_space_separated,
"a {\n color: index(1px solid red, solid);\n}\n",
"a {\n color: 2;\n}\n"
);
test!(
index_not_found_space_separated,
"a {\n color: index(1px solid red, dashed);\n}\n",
""
);
// test!(
// index_found_map,
// "a {\n color: index((width: 10px, height: 20px), (height 20px));\n}\n",
// "a {\n color: 2;\n}\n"
// );