// Parses a JSON encoded string to see if it's a CSS length
// --------------------------------------------------------------------------------
// @param [string] $string: JSON string
// --------------------------------------------------------------------------------
// @return [number|string] string or number, depending on the match

@function _length($string) {
  @if type-of($string) == number {
    @return $string;
  }

  $strings: "px" "cm" "mm" "%" "ch" "pica" "in" "em" "rem" "pt" "pc" "ex" "vw"
    "vh" "vmin" "vmax";
  $units: 1px 1cm 1mm 1% 1ch 1pica 1in 1em 1rem 1pt 1pc 1ex 1vw 1vh 1vmin 1vmax;
  $number: "";
  $unit: "";

  @for $i from 1 through str-length($string) {
    $c: str-slice($string, $i, $i);
    @if $c == " " or $c == "	" {
      @if $number != "" {
        @return $string;
      }
    } @else if index("0" "1" "2" "3" "4" "5" "6" "7" "8" "9" "-" ".", $c) {
      $number: $number + $c;
    } @else {
      @if $number == "" {
        @return $string;
      }
      $unit: $unit + $c;
    }
  }

  $number: nth(_json-decode--number($number, 2), 2);
  $index: index($strings, to-lower-case($unit));

  @if $index and $index > 0 {
    @return $number * nth($units, $index);
  }

  @return $string;
}
