-
Notifications
You must be signed in to change notification settings - Fork 22
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Request: Deep Merge #9
Comments
I'd recommend using Toolkit's Settings functionality to do what you're requesting. Otherwise, |
@Snugug I think it would be a really useful bit of functionality in Sassy Maps and would nicely complement
Sassmeister Gist here |
I guess I'm not sure what you're looking for exactly. What's the difference in output between set-deep and merge-deep? Also, if you'd like code to be considered, as a PR please. |
I'm looking for a way to take two maps and combine them into a new map that contains all the values from the first map and all all the values from the second, overwriting the values from the first if they are already present. The problem with map-merge is that it won't recurse into values that are themselves maps. The problem with map-set-deep is that I need to know the path into the map to change the value; to change all values I need to recurse through the map and call map-set-deep for each value to avoid blowing away existing values. The function above recurses through the map as deep as is needed and either creates or replaces values, ultimately returning the merged map. If you look at the Sassmeister you will see the output is a merging of map-1 and map-2. I would be more than happy to submit a pull request if this functionality is something you would find useful. Here are two maps and the resulting merged map
|
+1!! |
Thanks for an awesomely quick response! And about my question I've just deleted - everything works, I've just, well, forgot to include |
@cibulka No problem. Glad it's helpful. |
Gentle bump. |
The following works for me @function spire-map-extend($map, $maps.../*, $deep */) {
$last: nth($maps, -1);
$deep: $last == true;
$max: if($deep, length($maps) - 1, length($maps));
// Loop through all maps in $maps...
@for $i from 1 through $max {
// Store current map
$current: nth($maps, $i);
// If not in deep mode, simply merge current map with map
@if not $deep {
$map: map-merge($map, $current);
} @else {
// If in deep mode, loop through all tuples in current map
@each $key, $value in $current {
// If value is a nested map and same key from map is a nested map as well
@if type-of($value) == "map" and type-of(map-get($map, $key)) == "map" {
// Recursive extend
$value: spire-map-extend(map-get($map, $key), $value, true);
}
// Merge current tuple with map
$map: map-merge($map, ($key: $value));
}
}
}
@return $map;
} |
There is not going to be a recursive/deep-merge added to Sass core anytime soon, so I would like to suggest you add it to Sassy-maps.
Example use: I keep all my colour-refs inside a nested map structure. I would like to merge project-specific colour-values into defaults, overriding anything that is already present.
The text was updated successfully, but these errors were encountered: