
// Remove the unit and return the integer
@function strip-unit($value) {
    @return ($value / ($value * 0 + 1));
}

// Convert a unit value to a pixel while using $base-size as the reference
@function to-pixel($from) {
    $from-unit: unit($from);

    @if $from-unit == "px" {
        @return $from;

    } @else if $from-unit == "%" or $from-unit == "em" or $from-unit == "rem" {
        @return (strip-unit($from) * $base-size);

    } @else {
        @warn "Unknown pixel conversion unit type.";
    }

    @return $from;
}

// Convert a unit value to a percentage while using $base-size as the reference
@function to-percent($from) {
    $from-unit: unit($from);

    @if $from-unit == "%" {
        @return $from;

    } @else if $from-unit == "px" or $from-unit == "em" or $from-unit == "rem" {
        @return percentage((to-pixel($from) / $base-size) / 100);

    } @else {
        @warn "Unknown percentage conversion unit type.";
    }

    @return $from;
}

// Convert a unit value to a rem unit while using $base-size as the reference
@function to-rem($from) {
    $from-unit: unit($from);

    @if $from-unit == "rem" {
        @return $from;

    } @else if $from-unit == "px" or $from-unit == "%" or $from-unit == "em" {
        @return (to-pixel($from) / $base-size) * 1rem;

    } @else {
        @warn "Unknown rem conversion unit type.";
    }

    @return $from;
}

// Convert a unit value to an em unit while using $base-size as the reference
@function to-em($from) {
    $from-unit: unit($from);

    @if $from-unit == "em" {
        @return $from;

    } @else if $from-unit == "px" or $from-unit == "%" or $from-unit == "rem" {
        @return (to-pixel($from) / $base-size) * 1em;

    } @else {
        @warn "Unknown em conversion unit type.";
    }

    @return $from;
}
