From 4c9a427806b47b916849dbc98330fdbb469b659b Mon Sep 17 00:00:00 2001 From: Volpeon Date: Mon, 7 Feb 2022 12:28:59 +0100 Subject: Replace deep map functions with built-ins --- src/_functions.scss | 73 ++++++++--------------------------------------------- src/_props.scss | 3 ++- 2 files changed, 12 insertions(+), 64 deletions(-) (limited to 'src') diff --git a/src/_functions.scss b/src/_functions.scss index d091afa..236b548 100644 --- a/src/_functions.scss +++ b/src/_functions.scss @@ -9,6 +9,7 @@ /// @access public //// +@use 'sass:map'; @use 'sass:math'; @use './vars'; @@ -65,9 +66,11 @@ @function list-slice($list, $start: 1, $end: length($list)) { $result: (); - @for $i from $start through $end { - @if $i != 0 { - $result: append($result, nth($list, $i), list-separator($list)); + @if $end >= $start { + @for $i from $start through $end { + @if $i != 0 { + $result: append($result, nth($list, $i), list-separator($list)); + } } } @@ -162,67 +165,11 @@ /// /// @return {any} Either the value assigned to $key or $default /// -@function map-get-default($map, $key, $default) { - @return if(map-has-key($map, $key), map-get($map, $key), $default); -} - -/// -/// Get the value for a map within a map (or deeper). -/// -/// @param {map} $map -/// @param {string | list} $key -/// @param {any} $default [null] -/// -/// @return {any} Either the value assigned to $key or $default -/// -@function map-get-deep($map, $key, $default: null) { - $value: null; - - @if type-of($key) == list { - $value: $map; - - @each $k in $key { - $value: map-get($value, $k); - - @if $value == null { - @return $default; - } - } - } @else { - $value: map-get($map, $key); +@function map-get-default($map, $key, $keys...) { + $default: nth($keys, length($keys)); + $keys: list-slice($keys, 1, length($keys) - 1); - @if $value == null { - @return $default; - } - } - - @return $value; -} - -/// -/// Merge two maps recursively. -/// -/// @param {map} $map1 -/// @param {map} $map2 -/// -/// @return {map} The result of a recursive merge of $map1 and $map2 -/// -@function map-merge-recursive($map1, $map2) { - @if type-of($map1) != map or type-of($map2) != map { - @error 'Two maps expected.'; - } - - $result: $map1; - - @each $key, $value in $map2 { - @if type-of(map-get($result, $key)) == map and type-of($value) == map { - $result: map-merge($result, ($key: map-merge-recursive(map-get($result, $key), $value))); - } @else { - $result: map-merge($result, ($key: $value)); - } - } - - @return $result; + @return if(map-has-key($map, $key, $keys...), map-get($map, $key, $keys...), $default); } /// diff --git a/src/_props.scss b/src/_props.scss index db24dff..196e2f9 100644 --- a/src/_props.scss +++ b/src/_props.scss @@ -10,6 +10,7 @@ /// @access public //// +@use 'sass:map'; @use './functions'; @use './contexts'; @@ -127,7 +128,7 @@ $namespace-context-id: 'namespace' !default; @if map-has-key($trees, $tree) { @if $merge { - $map: functions.map-merge-recursive(map-get($trees, $tree), $map); + $map: map.deep-merge(map-get($trees, $tree), $map); } @else { @error 'Property tree #{inspect($tree)} does already exist.'; } -- cgit v1.2.3-70-g09d2