_str-to-num.scss (1245B)
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 10 // Result 11 $result: 0; 12 $divider: 0; 13 $minus: false; 14 15 // Looping through all characters 16 @for $i from 1 through str-length($string) { 17 $character: str-slice($string, $i, $i); 18 $index: index($strings, $character); 19 20 @if $character == "-" { 21 $minus: true; 22 } 23 24 @else if $character == "." { 25 $divider: 1; 26 } 27 28 @else { 29 @if not $index { 30 $result: if($minus, $result * -1, $result); 31 @return _convert-units($result, str-slice($string, $i)); 32 } 33 34 $number: nth($numbers, $index); 35 36 @if $divider == 0 { 37 $result: $result * 10; 38 } 39 40 @else { 41 // Move the decimal dot to the left 42 $divider: $divider * 10; 43 $number: $number / $divider; 44 } 45 46 $result: $result + $number; 47 } 48 } 49 @return if($minus, $result * -1, $result); 50 }