// http://sassmeister.com/gist/1b4f2da5527830088e4d
@function str-replace($string, $search, $replace: "") {
    $index: str-index($string, $search);

    @if $index {
        @return str-slice($string, 1, $index - 1) + $replace + str-replace(str-slice($string, $index + str-length($search)), $search, $replace);
    }

    @return $string;
}

// https://stackoverflow.com/questions/32376461/how-to-split-a-string-into-two-lists-of-numbers-in-sass
///
/// Split `$string` into a list of two strings at the first occurrence of `$separator`.
///
/// @param {String} $string - The string to be split
/// @param {String} $separator - Character indicating where the split should occur
///
/// @return {List} - A list of the two created strings. The second string will be null if `$separator` is not found.
///
@function str-split($string, $separator) {

    $index: str-index($string, $separator);

    @if not $index {
        @return $string "";
    }

    $str-1: str-slice($string, 1, $index - 1);
    $str-2: str-slice($string, $index + 1);

    @return $str-1 $str-2;
}

// https://www.sassmeister.com/gist/9fa19d254864f33d4a80
///
/// Converts a String representation of a number into a Number
///
/// @param {String} $value - The String to be converted
///
/// @return {Number} - The converted Number
///
@function to-number($value) {
    @if type-of($value) == "number" {
        @return $value;
    } @else if type-of($value) != "string" {
        $_: log("Value for `to-number` should be a number or a string.");
    }

    $result: 0;
    $digits: 0;
    $minus: str-slice($value, 1, 1) == "-";
    $numbers: ("0": 0, "1": 1, "2": 2, "3": 3, "4": 4, "5": 5, "6": 6, "7": 7, "8": 8, "9": 9);

    @for $i from if($minus, 2, 1) through str-length($value) {
        $character: str-slice($value, $i, $i);

        @if not (index(map-keys($numbers), $character) or $character == ".") {
            @return to-length(if($minus, -$result, $result), str-slice($value, $i));
        }

        @if $character == "." {
            $digits: 1;
        } @else if $digits == 0 {
            $result: $result * 10 + map-get($numbers, $character);
        } @else {
            $digits: $digits * 10;
            $result: $result + map-get($numbers, $character) / $digits;
        }
    }

    @return if($minus, -$result, $result);
}


///
/// Add `$unit` to `$value`
///
/// @param {Number} $value - Value to add unit to
/// @param {String} $unit - String representation of the unit
///
/// @return {Number} - `$value` expressed in `$unit`
///
@function to-length($value, $unit) {
    $units: ("px": 1px, "cm": 1cm, "mm": 1mm, "%": 1%, "ch": 1ch, "pc": 1pc, "in": 1in, "em": 1em, "rem": 1rem, "pt": 1pt, "ex": 1ex, "vw": 1vw, "vh": 1vh, "vmin": 1vmin, "vmax": 1vmax);

    @if not index(map-keys($units), $unit) {
        $_: log("Invalid unit `#{$unit}`.");
    }

    @return $value * map-get($units, $unit);
}
