Skip to content

Instantly share code, notes, and snippets.

@bleuebuzz
Created April 16, 2014 13:21
Show Gist options
  • Save bleuebuzz/10873996 to your computer and use it in GitHub Desktop.
Save bleuebuzz/10873996 to your computer and use it in GitHub Desktop.
Generated by SassMeister.com.
// ----
// Sass (v3.3.5)
// Compass (v1.0.0.alpha.18)
// ----
@function str-explode($string, $delimiter, $separator: auto, $trim: false) {
$_result: ();
$_length: str-length($delimiter);
// search for a substring occurence
$_index: str-index($string, $delimiter);
@while $_index {
// append string before the index
@if $_index > 1 {
$_result: append($_result, str-slice($string, 1, $_index - 1), $separator);
}
// append empty string
@elseif not $trim {
$_result: append($_result, '', $separator);
}
// slice string after the found substring
$string: str-slice($string, $_index + $_length);
// search for a new substring occurence
$_index: str-index($string, $delimiter);
}
// append remaining string
@if str-length($string) != 0 or not $trim {
$_result: append($_result, $string, $separator);
}
@return $_result;
}
@function str-to-number($string) {
$_number-chars: '0' '1' '2' '3' '4' '5' '6' '7' '8' '9';
$string: str-explode($string, '.');
$_number: 0;
// More than one decimal separator;
@if length($string) > 2 {
@return null;
}
// Integer part string
$_part: nth($string, 1);
$_length: str-length($_part);
@if $_length > 0 {
$_coef: 1;
@for $_i from $_length through 1 {
$_n: index($_number-chars, str-slice($_part, $_i, $_i));
// char is not a number
@if not $_n {
@return null;
}
$_number: $_number + ($_n - 1) * $_coef;
$_coef: $_coef * 10;
}
}
// Decimal part string
@if length($string) == 2 {
$_part: nth($string, 2);
$_length: str-length($_part);
@if $_length > 0 {
$_coef: 10;
@for $_i from 1 through $_length {
$_n: index($_number-chars, str-slice($_part, $_i, $_i));
// char is not a number
@if not $_n {
@return null;
}
$_number: $_number + ($_n - 1) / $_coef;
$_coef: $_coef * 10;
}
}
}
@return $_number;
}
test1 {
type: type-of(str-to-number('0.11'));
value: str-to-number('0.11');
weird: str-to-number('0.11') == 0.11;
}
test2 {
type: type-of(str-to-number('0.12'));
value: str-to-number('0.12');
weird: str-to-number('0.12') == 0.12;
}
test3 {
type: type-of(str-to-number('0.13'));
value: str-to-number('0.13');
weird: str-to-number('0.13') == 0.13;
}
test1 {
type: number;
value: 0.11;
weird: true;
}
test2 {
type: number;
value: 0.12;
weird: false;
}
test3 {
type: number;
value: 0.13;
weird: true;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment