From f5e3a5a6699ebdd82ee6b03714ac9b0b72d585e9 Mon Sep 17 00:00:00 2001 From: Jennifer Thakar Date: Wed, 16 Sep 2020 14:41:23 -0700 Subject: [PATCH] Add support for nested maps to has-key (#1075) --- CHANGELOG.md | 6 ++++++ lib/src/functions/map.dart | 14 +++++++++++--- 2 files changed, 17 insertions(+), 3 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index c5c8578d..5448bf0a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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: diff --git a/lib/src/functions/map.dart b/lib/src/functions/map.dart index 1a6bd00d..a93882bc 100644 --- a/lib/src/functions/map.dart +++ b/lib/src/functions/map.dart @@ -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.