implement math.div

This commit is contained in:
Connor Skees 2021-07-04 16:19:38 -04:00
parent 5d268be2ed
commit 81d5dbbb7e
3 changed files with 29 additions and 1 deletions

View File

@ -7,6 +7,7 @@ use rand::Rng;
use crate::{
args::CallArgs,
common::Op,
error::SassResult,
parse::{HigherIntermediateValue, Parser, ValueVisitor},
unit::Unit,
@ -270,6 +271,22 @@ pub(crate) fn max(args: CallArgs, parser: &mut Parser<'_>) -> SassResult<Value>
Ok(Value::Dimension(Some(max.0), max.1, true))
}
pub(crate) fn divide(mut args: CallArgs, parser: &mut Parser<'_>) -> SassResult<Value> {
args.max_args(2)?;
let number1 = args.get_err(0, "number1")?;
let number2 = args.get_err(1, "number2")?;
ValueVisitor::new(parser, args.span()).eval(
HigherIntermediateValue::BinaryOp(
Box::new(HigherIntermediateValue::Literal(number1)),
Op::Div,
Box::new(HigherIntermediateValue::Literal(number2)),
),
true,
)
}
pub(crate) fn declare(f: &mut GlobalFunctionMap) {
f.insert("percentage", Builtin::new(percentage));
f.insert("round", Builtin::new(round));

View File

@ -5,7 +5,7 @@ use num_traits::{One, Signed, Zero};
use crate::{
args::CallArgs,
builtin::{
math::{abs, ceil, comparable, floor, max, min, percentage, round},
math::{abs, ceil, comparable, divide, floor, max, min, percentage, round},
meta::{unit, unitless},
modules::Module,
},
@ -607,6 +607,7 @@ pub(crate) fn declare(f: &mut Module) {
f.insert_builtin("log", log);
f.insert_builtin("pow", pow);
f.insert_builtin("hypot", hypot);
f.insert_builtin("div", divide);
f.insert_builtin("atan2", atan2);
#[cfg(feature = "random")]
f.insert_builtin("random", random);

View File

@ -588,3 +588,13 @@ test!(
"@use 'sass:math';\na {\n color: math.atan2(math.acos(2), 3deg);\n}\n",
"a {\n color: NaNdeg;\n}\n"
);
test!(
div_two_integers,
"@use 'sass:math';\na {\n color: math.div(1, 2);\n}\n",
"a {\n color: 0.5;\n}\n"
);
test!(
div_two_strings,
"@use 'sass:math';\na {\n color: math.div(\"1\",\"2\");\n}\n",
"a {\n color: \"1\"/\"2\";\n}\n"
);