Skip to content

Utils (draft)

Marin Popov edited this page Aug 2, 2022 · 5 revisions

Functions

Map

clean

The clean function is used to cleen all null key-value pairs from a map it accespts as argument a map to be cleaned and returns a map with null key-value pairs removed.

@function clean($map) {
    $result: ();

    @each $key, $value in $map {
        @if $value {
            $result: map.merge(
                $result,
                (
                    #{$key}: $value
                )
            );
        }
    }

    @return $result;
}

extend

The extend function is used to extend a map with the values of another map/s. It accepts one or more maps as arguments and return merged map disregarding any null key-values pairs in the process.

@function extend($maps...) {
    $result: ();

    @each $map in $maps {
        $result: map.merge($result, clean($map));
    }

    @return $result;
}

Math

to-fixed

The to-fixed() functions accept two arguments $number and $precision, where the $precision is used to tell how many digits should appear after the decimal point of the $number.

@function to-fixed($number, $precision: 2) {
    $pow: math.pow(10, $precision);

    @return math.div(math.round($number * $pow), $pow);
}

Meta

is-root

The function returns true if the scope where it's called is the root of the document.

@function is-root() {
    @each $selector in & {
        @return if($selector == null, true, false);
    }
}

Here is an example.

@mixin smart-mixin() {
     $scope: if(is-root(), ':root', '&');

     #{$scope} {
       /* style rules here */
     }
}
Clone this wiki locally