Created
July 24, 2014 20:24
-
-
Save jackie/ce22c5bdc12dd67c17c4 to your computer and use it in GitHub Desktop.
Slice and a recursive map-get
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$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