Skip to content

Instantly share code, notes, and snippets.

@jakob-e
Last active April 9, 2018 16:48
Show Gist options
  • Save jakob-e/f3accce1f6277fea1d89 to your computer and use it in GitHub Desktop.
Save jakob-e/f3accce1f6277fea1d89 to your computer and use it in GitHub Desktop.
Generated by SassMeister.com.
===========================
str-replace
str-to-map
TODO - Add mqs to lists
===========================
// ----
// Sass (v3.4.7)
// Compass (v1.0.1)
// ----
@function str-replace($string, $find, $replace:''){
@if $find == $replace { @return $string; }
$index: str-index($string, $find);
@if $index {
$len1: str-length($replace);
$len2: str-length($find);
@return str-replace(
// Insert replace before find - then concat with the remaining
// string after find and make a recursive call to str-replace
str-slice(str-insert($string, $replace, $index), 0, $index + $len1 - 1) +
str-slice($string, $index + $len2), $find, $replace);
}
@return $string;
}
@function str-to-map($string, $map:()){
@if $string {
$comma : str-index($string,',');
$colon : str-index($string,':');
$item : if($comma, str-slice($string, 0, $comma - 1), $string);
$string : if($comma, str-slice($string, $comma +1), null);
$key : str-slice($item, 0, $colon - 1);
$val : str-slice($item, $colon +1);
$map : map-merge($map,($key: $val));
@return str-to-map($string, $map);
}
@return $map;
}
sass {
$string : '(min-width: 480px) and (max-width: 40em) and (min-width: 90em)';
// $string : '(min-width: 700px), (min-width: 900px) and (orientation: landscape), (orientation: portrait)';
$replaced : str-replace(str-replace(str-replace($string,' '),'and'),')(',',');
$list-str : str-replace(str-replace(str-replace(str-replace(str-replace(str-replace($string,' '),'and'),')(',','),':',','),'('),')');
$map-str : str-replace(str-replace(str-replace(str-replace(str-replace($string,' '),'and'),')(',','),'('),')');
string : unquote($string);
string-list: unquote($list-str);
string-map : unquote($map-str);
map : map-get(str-to-map($map-str),min-width);
map : map-get(str-to-map($map-str),max-width);
map : map-get(str-to-map($map-str),foo-width);
//
//test:sl-explode($list,',',space);
}
sass {
string: (min-width: 480px) and (max-width: 40em) and (min-width: 90em);
string-list: min-width,480px,max-width,40em,min-width,90em;
string-map: min-width:480px,max-width:40em,min-width:90em;
map: "90em";
map: "40em";
}
===========================
str-replace
str-to-map
TODO - Add mqs to lists
===========================
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment