Skip to content

Instantly share code, notes, and snippets.

@AlexandrBukhtatyy
Last active June 15, 2017 10:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save AlexandrBukhtatyy/b72165df6d532a031a5df2c41dffb6be to your computer and use it in GitHub Desktop.
Save AlexandrBukhtatyy/b72165df6d532a031a5df2c41dffb6be to your computer and use it in GitHub Desktop.
Полезные миксины
/*********************************************************STRING REPLACE************************************************************/
@function str-replace($string, $search, $replace: '') {
$index: str-index($string, $search);
@if $index {
@return str-slice($string, 1, $index - 1) + $replace + str-replace(str-slice($string, $index + str-length($search)), $search, $replace);
}
@return $string;
}
$str-replace: str-replace('(100%-5x)','x','10'); // "(100%-510)"
/*********************************************************INSERT STRING************************************************************/
@function insert-string($string, $replace: '', $search...) {
@each $ext in $search {
$index: str-index($string, $ext);
@if $index {
@return str-slice($string, 1, $index - 1) + $replace + str-slice($string, $index, str-length($string));
}
}
@return $string;
}
$filename: '../images/best-image-ever.png';
$src: insert-string($filename, '__', '.png', '.jpg', '.other-extension'); // ../images/best-image-ever__.png
/*********************************************************CONVERT STRING TO NUMBER************************************************************/
@function _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);
}
@function number($string) {
$strings: '0' '1' '2' '3' '4' '5' '6' '7' '8' '9';
$numbers: 0 1 2 3 4 5 6 7 8 9;
$result: 0;
$minus: false;
$divider: 0;
@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 _length($result, str-slice($string, $i));
}
$number: nth($numbers, $index);
// Decimal dot hasn't been found yet
@if $divider == 0 {
$result: $result * 10;
}
// Decimal dot has been found
@else {
// Move the decimal dot to the left
$divider: $divider * 10;
$number: $number / $divider;
}
$result: $result + $number;
}
}
@return if($minus, $result * -1, $result);
@return $result;
}
.sass {
cast: number("-15")+10; //5
cast: number("-1"); // -1
cast: number("-0.5"); // -.5
cast: number("-0"); // 0
cast: number("0"); // 0
case: number(".10"); // 0.1
cast: number("1"); // 1
cast: number("1.5"); // 1.5
cast: number("10."); // 10
cast: number("12.380"); // 12.38
cast: number("42"); // 42
cast: number("1337"); // 1337
cast: number("-10px"); // -10px
cast: number("20em"); // 20em
cast: number("30ch"); // 30ch
cast: number("1fail"); // Error
cast: number("string"); // Error
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment