Skip to content

Instantly share code, notes, and snippets.

@jackie
Created July 24, 2014 20:24
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 jackie/ce22c5bdc12dd67c17c4 to your computer and use it in GitHub Desktop.
Save jackie/ce22c5bdc12dd67c17c4 to your computer and use it in GitHub Desktop.
Slice and a recursive map-get
$test: (
one: (
two: (
val: 'socks'
)
),
socks: (
pants: (
shoes: 'hats'
)
)
);
@function map-me($map, $keys) {
$key: nth($keys, 1);
@if length($keys) > 1 {
@if map-has-key($map, $key) {
$slice: slice($keys, 2);
@return map-me(map-get($map, $key), $slice);
}
@else {
@warn 'No key #{$key} in map';
@return null;
}
}
@else {
$map: map-get($map, $key);
}
@return $map;
}
@function slice($list, $offset: 1, $length: length($list)) {
$new: ();
@if $offset > $length {
@warn 'Offset #{$offset} is greater than length #{$length}';
@return null;
}
@for $i from $offset through $length {
$new: append($new, nth($list, $i));
}
@return $new;
}
//@debug slice(one two three);
@debug 'final answer is #{map-me($test, one two val)}';
@debug 'final answer is #{map-me($test, socks pants shoes)}';
@debug 'final answer is #{map-me($test, a b c)}';
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment