_str-to-num.scss 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. //************************************************************************//
  2. // Helper function for linear/radial-gradient-parsers.
  3. // Source: http://sassmeister.com/gist/9647408
  4. //************************************************************************//
  5. @function _str-to-num($string) {
  6. // Matrices
  7. $strings: '0' '1' '2' '3' '4' '5' '6' '7' '8' '9';
  8. $numbers: 0 1 2 3 4 5 6 7 8 9;
  9. // Result
  10. $result: 0;
  11. $divider: 0;
  12. $minus: false;
  13. // Looping through all characters
  14. @for $i from 1 through str-length($string) {
  15. $character: str-slice($string, $i, $i);
  16. $index: index($strings, $character);
  17. @if $character == '-' {
  18. $minus: true;
  19. }
  20. @else if $character == '.' {
  21. $divider: 1;
  22. }
  23. @else {
  24. @if not $index {
  25. $result: if($minus, $result * -1, $result);
  26. @return _convert-units($result, str-slice($string, $i));
  27. }
  28. $number: nth($numbers, $index);
  29. @if $divider == 0 {
  30. $result: $result * 10;
  31. }
  32. @else {
  33. // Move the decimal dot to the left
  34. $divider: $divider * 10;
  35. $number: $number / $divider;
  36. }
  37. $result: $result + $number;
  38. }
  39. }
  40. @return if($minus, $result * -1, $result);
  41. }