// Split a string into a list
// http://stackoverflow.com/questions/32376461/how-to-split-a-string-into-two-lists-of-numbers-in-sass#answer-42295154
@function str-split($string, $separator) {
  // Empty list
  $list: ();
  // First index of separator in string
  $index: str-index($string, $separator);
  // Loop through string
  @while $index != null {
    // Get the substring from the first character to the separator
    $item: str-slice($string, 1, $index - 1);
    // Push item to list
    $list: append($list, $item);
    // Remove item and separator from string
    $string: str-slice($string, $index + 1);
    // Find new index of separator
    $index: str-index($string, $separator);
  }
  // Add the remaining string to list (the last item)
  $list: append($list, $string);

  @return $list;
}

// Casting a string to a number
// http://hugogiraudel.com/2014/01/15/sass-string-to-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);
}
