Skip to content

Commit

Permalink
feat(map.get-any()): add function to get the value from any key where…
Browse files Browse the repository at this point in the history
… value is not null.
  • Loading branch information
sciborrudnicki committed Sep 30, 2023
1 parent 47fe073 commit cef2405
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 0 deletions.
1 change: 1 addition & 0 deletions map/_index.scss
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
@forward 'map.deep-merge-key.function';
@forward 'map.deep-merge.function';
@forward 'map.get-any.function';
@forward 'map.get.function';
@forward 'map.has-keys.function';
@forward 'map.key-replace.function';
Expand Down
35 changes: 35 additions & 0 deletions map/_map.get-any.function.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
// Sass.
@use 'sass:list';
@use 'sass:map';

// Modules.
@use '../list/list.append.function' as *;

// Status: DONE
// The `map.get-any()` function returns first value not `null` associated with `$key` or any of `$keys` in `$map`. If all values are `null`
// returns fallback value, which is the last value in `$keys`.
// @param `$map` A map to get the value by using `$key` or any of `$keys`.
// @param `$key` The key used to get the value from `$map`.
// @arbitrary `$keys...` Any keys to get the value from `$map`. The last argument is the fallback value.
// @return The return value is not `null` value associated with `$key` or any of `$keys` from the `$map`, if `null` returns fallback value.
@function get-any($map, $key, $keys...) {
$i: 1;
@each $key in append((), $key, auto, $keys...) {
@if not ($i == list.length(append((), $key, comma, $keys...))) {
$result: if(
list.length($key) > 0,
if(list.separator($key) == comma, map.get($map, $key...), map.get($map, $key)),
null
);
@if $result {
@return $result;
}
}

$i: $i + 1;
}
@return list.nth($keys, list.length($keys));
}

// Examples.
// @debug get-any((a-test: 1, b: 2, c: null, b-name: 5), c, b-name, null);

0 comments on commit cef2405

Please sign in to comment.