Skip to content

Instantly share code, notes, and snippets.

@jdlehman
Created July 30, 2014 04:40
Show Gist options
  • Save jdlehman/da9e601ecde435bef4d6 to your computer and use it in GitHub Desktop.
Save jdlehman/da9e601ecde435bef4d6 to your computer and use it in GitHub Desktop.
Generated by SassMeister.com.
// ----
// Sass (v3.3.12)
// Compass (v1.0.0.alpha.21)
// ----
$t: (a: (x: (2 2), y: 2), b: (3 4), c: 10);
//susy
@function _susy-deep-get(
$map,
$keys...
) {
$_return: $map;
@each $key in $keys {
@if type-of($_return) == map {
$_return: map-get($_return, $key);
}
}
@return $_return;
}
//sassy maps
@function map-get-deep($map, $keys...) {
@if length($keys) == 1 {
$keys: nth($keys, 1);
}
$warn: "#{nth($keys, 1)}";
$length: length($keys);
$get: map-get($map, nth($keys, 1));
@if $length > 1 {
@for $i from 2 through $length {
@if $get != null and type-of($get) == 'map' {
$warn: $warn + "->#{nth($keys, $i)}";
$get: map-get($get, nth($keys, $i));
@if $get == null {
@return map-get-deep-warning($warn, $get);
}
}
@else {
@return map-get-deep-warning($warn, $get);
}
}
}
@return $get;
}
@function map-get-deep-str($map, $str) {
$list: ();
$index: str-index($str, '.');
$end: null;
@while $index != null {
$beg: str-slice($str, 0, $index - 1);
$end: str-slice($str, $index + 1);
$str: $end;
$index: str-index($str, '.');
$list: append($list, unquote($beg), comma);
}
$list: append($list, unquote($end), comma);
@return map-get-deep($map, $list);
}
html {
susy: _susy-deep-get($t, a, x);
sassy-maps: map-get-deep($t,a,x);
test: map-get-deep-str($t, 'a.x');
}
html {
susy: 2 2;
sassy-maps: 2 2;
test: 2 2;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment