Implement builtin function abs()
This commit is contained in:
parent
af8bd516b2
commit
f7351e1458
@ -30,4 +30,10 @@ pub(crate) fn register(f: &mut BTreeMap<String, Builtin>) {
|
|||||||
_ => todo!("expected number in builtin function `floor()`")
|
_ => todo!("expected number in builtin function `floor()`")
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
decl!(f "abs", |args, _| {
|
||||||
|
match arg!(args, 0, "number").eval() {
|
||||||
|
Value::Dimension(n, u) => Some(Value::Dimension(n.abs(), u)),
|
||||||
|
_ => todo!("expected number in builtin function `abs()`")
|
||||||
|
}
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
@ -4,6 +4,7 @@ use std::ops::{Add, AddAssign, Div, DivAssign, Mul, MulAssign, Rem, RemAssign, S
|
|||||||
|
|
||||||
use num_bigint::BigInt;
|
use num_bigint::BigInt;
|
||||||
use num_rational::BigRational;
|
use num_rational::BigRational;
|
||||||
|
use num_traits::sign::Signed;
|
||||||
|
|
||||||
const PRECISION: usize = 10;
|
const PRECISION: usize = 10;
|
||||||
|
|
||||||
@ -42,6 +43,12 @@ impl Number {
|
|||||||
val: self.val.floor(),
|
val: self.val.floor(),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn abs(self) -> Self {
|
||||||
|
Number {
|
||||||
|
val: self.val.abs(),
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl fmt::LowerHex for Number {
|
impl fmt::LowerHex for Number {
|
||||||
|
@ -48,3 +48,18 @@ test!(
|
|||||||
"a {\n color: ceil(10.6px);\n}\n",
|
"a {\n color: ceil(10.6px);\n}\n",
|
||||||
"a {\n color: 11px;\n}\n"
|
"a {\n color: 11px;\n}\n"
|
||||||
);
|
);
|
||||||
|
test!(
|
||||||
|
abs_positive,
|
||||||
|
"a {\n color: abs(10);\n}\n",
|
||||||
|
"a {\n color: 10;\n}\n"
|
||||||
|
);
|
||||||
|
test!(
|
||||||
|
abs_negative,
|
||||||
|
"a {\n color: abs(-10);\n}\n",
|
||||||
|
"a {\n color: 10;\n}\n"
|
||||||
|
);
|
||||||
|
test!(
|
||||||
|
abs_unit,
|
||||||
|
"a {\n color: abs(-10px);\n}\n",
|
||||||
|
"a {\n color: 10px;\n}\n"
|
||||||
|
);
|
||||||
|
Loading…
x
Reference in New Issue
Block a user