math.calculate()

The math.calculate() function performs value and operand calculation by using a given operator + addition, - subtraction, * multiplication, / division of string type.

// 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;
}

Parameters

$operations...

Arbitrary values in order (value, operator, operand) to perform calculation.

Return

The return value is calculated number or CSS function calc() with a specified calculation.

Examples

// 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))

Last updated