Last active
October 13, 2016 09:30
-
-
Save esr360/0b640a7c44ce8834160d799084152a03 to your computer and use it in GitHub Desktop.
This file contains 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
/// Map Search | |
/// Get the first occurance of a key from a nested map | |
/// | |
/// @author [@esr360](http://twitter.com/esr360) | |
/// | |
/// @access public | |
/// @group Tools | |
/// | |
/// @param {map} $map - the map you would like to search | |
/// @param {string} $target-key - the key you are searching for | |
@function map-search($map, $target-key, $target-value: '') { | |
@each $key, $value in $map { | |
// Is this our key? | |
@if $key == $target-key { | |
$target-value: $value; | |
} @else { | |
@if type-of($value) == 'map' { | |
// Does the new map contain our key? | |
@if map-has-key($value, $target-key) { | |
$target-value: map-get($value, $target-key); | |
} @else { | |
// if not, recurse the function | |
$target-value: map-search($value, $target-key, $target-value); | |
} | |
} | |
} | |
} | |
@return $target-value; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment