diff --git a/src/builtin/math.rs b/src/builtin/math.rs index 6d7d122..aa75042 100644 --- a/src/builtin/math.rs +++ b/src/builtin/math.rs @@ -30,4 +30,10 @@ pub(crate) fn register(f: &mut BTreeMap) { _ => 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()`") + } + }); } diff --git a/src/value/number.rs b/src/value/number.rs index 0f73979..7e068b7 100644 --- a/src/value/number.rs +++ b/src/value/number.rs @@ -4,6 +4,7 @@ use std::ops::{Add, AddAssign, Div, DivAssign, Mul, MulAssign, Rem, RemAssign, S use num_bigint::BigInt; use num_rational::BigRational; +use num_traits::sign::Signed; const PRECISION: usize = 10; @@ -42,6 +43,12 @@ impl Number { val: self.val.floor(), } } + + pub fn abs(self) -> Self { + Number { + val: self.val.abs(), + } + } } impl fmt::LowerHex for Number { diff --git a/tests/math.rs b/tests/math.rs index 4218c2d..a98d766 100644 --- a/tests/math.rs +++ b/tests/math.rs @@ -48,3 +48,18 @@ test!( "a {\n color: ceil(10.6px);\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" +);