Add support for nested maps to has-key (#1075)

This commit is contained in:
Jennifer Thakar 2020-09-16 14:41:23 -07:00 committed by GitHub
parent 6b66241e95
commit f5e3a5a669
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 17 additions and 3 deletions

View File

@ -6,6 +6,12 @@
[map-get]: https://sass-lang.com/documentation/modules/map#get
* Add support for nested maps in `map.has-key`.
For example, `map.has-key((a: (b: (c: d))), a, b, c)` would return true.
See [the documentation][map-has-key] for more details.
[map-has-key]: https://sass-lang.com/documentation/modules/map#has-key
* Add a `map.deep-merge()` function. This works like `map.merge()`, except that
nested map values are *also* recursively merged. For example:

View File

@ -81,10 +81,18 @@ final _values = _function(
(arguments) => SassList(
arguments[0].assertMap("map").contents.values, ListSeparator.comma));
final _hasKey = _function("has-key", r"$map, $key", (arguments) {
final _hasKey = _function("has-key", r"$map, $key, $keys...", (arguments) {
var map = arguments[0].assertMap("map");
var key = arguments[1];
return SassBoolean(map.contents.containsKey(key));
var keys = [arguments[1], ...arguments[2].asList];
for (var key in keys.take(keys.length - 1)) {
var value = map.contents[key];
if (value is SassMap) {
map = value;
} else {
return sassFalse;
}
}
return SassBoolean(map.contents.containsKey(keys.last));
});
/// Merges [map1] and [map2], with values in [map2] taking precedence.