diff --git a/libs/design/scss/core/map/map-get/map-get-error.scss b/libs/design/scss/core/map/map-get/map-get-error.scss new file mode 100644 index 0000000000..d9925d5fa4 --- /dev/null +++ b/libs/design/scss/core/map/map-get/map-get-error.scss @@ -0,0 +1,13 @@ +$debug: false !default; + +@function map-get-error($message, $override: $debug) { + @if $override { + @return $message; + } @else { + @error $message; + } +} + +@mixin set-debug($state: true) { + $debug: $state !global; +} \ No newline at end of file diff --git a/libs/design/scss/core/map/map-get/map-get.scss b/libs/design/scss/core/map/map-get/map-get.scss index 136cd971fd..2eab9c807a 100644 --- a/libs/design/scss/core/map/map-get/map-get.scss +++ b/libs/design/scss/core/map/map-get/map-get.scss @@ -1,4 +1,5 @@ @use 'sass:map'; +@use './map-get-error'; // // Return a the value of a nested key in a map if it exists. @@ -13,7 +14,7 @@ @function daff-map-get($map, $keys...) { @each $key in $keys { @if not map.has-key($map, $key) { - @error 'The map doesn\'t contain the $key: `#{$key}`\''; + @return map-get-error.map-get-error("The map doesn't contain the $key: `#{$key}`'"); } $map: map.get($map, $key); } diff --git a/libs/design/scss/core/map/map-get/map-get.spec.scss b/libs/design/scss/core/map/map-get/map-get.spec.scss index 3cc640b6f5..484a1f5b50 100644 --- a/libs/design/scss/core/map/map-get/map-get.spec.scss +++ b/libs/design/scss/core/map/map-get/map-get.spec.scss @@ -1,5 +1,6 @@ @use 'true' as *; @use './map-get' as *; +@use './map-get-error'; @include describe('map-get') { $nested-map: ( @@ -15,6 +16,20 @@ ) ); + $deep-map: ( + 'level1': ( + 'level2': ( + 'level3': ( + 'level': 4 + ) + ) + ) + ); + + $empty-map: (); + + @include map-get-error.set-debug(true); + @include it('returns the value of the nested key if it exists in the map') { @include assert-equal(daff-map-get($map, 'nested', 'string'), '1'); @include assert-equal(daff-map-get($map, 'nested', 'number'), 2); @@ -22,4 +37,24 @@ @include assert-equal(daff-map-get($map, 'nested', 'map', 'string'), '3'); @include assert-equal(daff-map-get($map, 'nested', 'map', 'number'), 4); } + + @include it('returns the correct value for deeply nested maps') { + @include assert-equal(daff-map-get($deep-map, 'level1', 'level2', 'level3', 'level'), 4); + } + + @include it('errors if the value of the nested key does not exist in the map') { + @include assert-equal( + daff-map-get($map, 'missing'), + "The map doesn't contain the $key: `missing`'" + ); + } + + @include it('errors if the map is empty') { + @include assert-equal( + daff-map-get($empty-map, 'nested'), + "The map doesn't contain the $key: `nested`'" + ); + } + + @include map-get-error.set-debug(false); }