Copy // Sass.
@use 'sass:list';
@use 'sass:meta';
// The `math.calculate()` function.
@function calculate($operations...) {
@each $value, $operator,
$operand in if(list.length($operations) == 3, ($operations), $operations)
{
@if $value and $operand {
@if $operator == '+' {
@return calc($value + $operand);
} @else if $operator == '-' {
@return calc($value - $operand);
} @else if $operator == '*' {
@return calc($value * $operand);
} @else if $operator == '/' {
@return calc($value / $operand);
}
}
}
@return null;
}
Arbitrary values in order (value, operator, operand) to perform calculation.
Copy // Use.
@use '@angular-package/sass/math';
// Examples.
// number
@debug math.calculate(15 '+' 13); // 28
@debug math.calculate(15 '-' 12); // 3
@debug math.calculate(55 '*' 17); // 935
@debug math.calculate(42 '/' 14); // 3
@debug math.calculate(15, '+', 13); // 28
// var()
@debug math.calculate(15, '+', var(--age)); // calc(15 + var(--age))
@debug math.calculate(var(--age), '+', var(--age)); // calc(var(--age) + var(--age))