Created
August 29, 2014 14:44
-
-
Save jakob-e/3a422ff6432a9c86fa15 to your computer and use it in GitHub Desktop.
Generated by SassMeister.com.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// ---- | |
// Sass (v3.4.1) | |
// Compass (v1.0.1) | |
// ---- | |
@function isNaN($val) { @return type-of($val)!=number; } | |
@function convert-to-unit($string){ | |
@if(not isNaN($string) ){ @return $string; } // It is a valid unit/number | |
$string:to-lower-case($string); // Convert to lowercase | |
$numbers:'0','1','2','3','4','5','6','7','8','9'; // Numbers | |
$units:(px:1px, pt:1pt, pc:1pc, in:1in, mm:1mm, cm:1cm, // Absolute | |
em:1em, rem:1rem, ch:1ch, ex:1ex, // Relative - font based | |
vv:1vw, vh:1vh, vmin:1vmin, vmax:1vmax, // Relative - viewport based | |
deg:1deg, turn:1turn, grad:1grad, // Angles | |
s:1s, ms:1ms, // Time | |
hz:1hz, kHz:1khz, // Frequencies | |
dpi:1dpi, dpcm:1dpcm, dppx:1dppx, // Resolutions | |
); | |
// Variables | |
$number:0; | |
$devider:0; | |
$digits:false; // change to counter to catch multi dots | |
$unit:''; | |
@for $i from 1 through str-length($string) { | |
$char:str-slice($string, $i, $i); | |
$index:index($numbers,$char); | |
$digits:if($char=='.', true, $digits); | |
@if($index){ | |
$devider:if($digits ,$devider + 1, $devider); | |
$number: $number * 10 + $index - 1; | |
} | |
@if(not $index and not index('-' '.',$char)){ | |
$unit:$unit + $char; | |
} | |
} | |
// Negative | |
$number:if(str-slice($string, 1, 1)=='-',$number * -1,$number); | |
// Decimals | |
$number:$number / pow(10, $devider); | |
// No unit found return number | |
@if($unit == ''){ @return $number; } | |
// Unit found check if valid | |
$unit:map-get($units,$unit); | |
@if(not $unit){ @warn "Unknown unit `#{$unit}`."; @return null; } // <-- change "error" to null | |
@return $unit * $number; | |
} | |
.foo { hey:convert-to-unit('-123.7243--R.E.M'); isNaN:isNaN(-123.7243turn);} | |
@function number($val){ @return convert-to-unit($val)} | |
sass { | |
cast: convert-to-unit("-15"); // -15 | |
cast: convert-to-unit("-1"); // -1 | |
cast: convert-to-unit("-.5"); // -.5 | |
cast: convert-to-unit("-0"); // 0 | |
cast: convert-to-unit("0"); // 0 | |
case: convert-to-unit(".10"); // 0.1 | |
cast: convert-to-unit("1"); // 1 | |
cast: convert-to-unit("1.5"); // 1.5 | |
cast: convert-to-unit("10."); // 10 | |
cast: convert-to-unit("12.380"); // 12.38 | |
cast: convert-to-unit("42"); // 42 | |
cast: convert-to-unit("1337"); // 1337 | |
cast: convert-to-unit("-10px"); // -10px | |
cast: convert-to-unit("20em"); // 20em | |
cast: convert-to-unit("30ch"); // 30ch | |
cast: convert-to-unit("1fail"); // Error | |
cast: convert-to-unit("string"); // Error | |
} | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
.foo { | |
hey: -123.7243rem; | |
isNaN: false; | |
} | |
sass { | |
cast: -15; | |
cast: -1; | |
cast: -0.5; | |
cast: 0; | |
cast: 0; | |
case: 0.1; | |
cast: 1; | |
cast: 1.5; | |
cast: 10; | |
cast: 12.38; | |
cast: 42; | |
cast: 1337; | |
cast: -10px; | |
cast: 20em; | |
cast: 30ch; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment