Implement builtin function round()

This commit is contained in:
ConnorSkees 2020-02-09 16:10:32 -05:00
parent 405a1c2d42
commit 116bb43bd5
2 changed files with 17 additions and 2 deletions

View File

@ -6,11 +6,16 @@ use crate::value::{Number, Value};
pub(crate) fn register(f: &mut BTreeMap<String, Builtin>) {
decl!(f "percentage", |args, _| {
let arg = dbg!(arg!(args, 0, "number").eval());
let num = match arg {
let num = match arg!(args, 0, "number").eval() {
Value::Dimension(n, Unit::None) => n * Number::from(100),
_ => todo!("expected unitless number in builtin function `percentage()`")
};
Some(Value::Dimension(num, Unit::Percent))
});
decl!(f "round", |args, _| {
match arg!(args, 0, "number").eval() {
Value::Dimension(n, u) => Some(Value::Dimension(n.round(), u)),
_ => todo!("expected number in builtin function `round()`")
}
});
}

View File

@ -18,3 +18,13 @@ test!(
"a {\n color: percentage(2);\n}\n",
"a {\n color: 200%;\n}\n"
);
test!(
rounds_down,
"a {\n color: round(10.4px);\n}\n",
"a {\n color: 10px;\n}\n"
);
test!(
rounds_up,
"a {\n color: round(10.6px);\n}\n",
"a {\n color: 11px;\n}\n"
);