// Converts number[unit] string into value // ------------------------------------------------------------------------------- // @documentation http://hugogiraudel.com/2014/01/15/sass-string-to-number/ // ------------------------------------------------------------------------------- // @param $number [string] : number // @param $unit [string] : unit // ------------------------------------------------------------------------------- // @return [number] @function flint-num-length($number, $unit) { $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; $index: index($strings, $unit); @if not $index { @warn "Unknown unit `#{$unit}`."; @return false; } @return $number * nth($units, $index); } // Converts string to number // ------------------------------------------------------------------------------- // @documentation http://hugogiraudel.com/2014/01/15/sass-string-to-number/ // ------------------------------------------------------------------------------- // @param $string [string] : string // ------------------------------------------------------------------------------- // @return [number] @function flint-to-number($string) { // Use Ruby function if available @if $flint__use-ruby-functions { @return string_to_number($string); } @else { // Matrices $strings: '0' '1' '2' '3' '4' '5' '6' '7' '8' '9'; $numbers: 0 1 2 3 4 5 6 7 8 9; // Result $result: 0; $divider: 0; $minus: false; // Looping through all characters @for $i from 1 through str-length($string) { $character: str-slice($string, $i, $i); $index: index($strings, $character); @if $character == '-' { $minus: true; } @else if $character == '.' { $divider: 1; } @else { @if not $index { $result: if($minus, $result * -1, $result); @return flint-num-length($result, str-slice($string, $i)); } $number: nth($numbers, $index); @if $divider == 0 { $result: $result * 10; } @else { // Move the decimal dot to the left $divider: $divider * 10; $number: $number / $divider; } $result: $result + $number; } } @return if($minus, $result * -1, $result); } }