-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(map.get-any()): add function to get the value from any key where…
… value is not null.
- Loading branch information
1 parent
47fe073
commit cef2405
Showing
2 changed files
with
36 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); |