Skip to content

Commit

Permalink
feat(map.get-index()): add function to get value based on key checked…
Browse files Browse the repository at this point in the history
… by `list.index()`.
  • Loading branch information
sciborrudnicki committed Dec 7, 2023
1 parent 8ac459b commit e1459b9
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 0 deletions.
1 change: 1 addition & 0 deletions map/_index.scss
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
@forward 'map.deep-merge.function';
@forward 'map.depth.function';
@forward 'map.get-any.function';
@forward 'map.get-index.function';
@forward 'map.get.function';
@forward 'map.has-keys.function';
@forward 'map.key-pattern-index.function';
Expand Down
36 changes: 36 additions & 0 deletions map/_map.get-index.function.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
// Modules.
@use '../list';

// Status: DONE
// The `map.get-index()` function returns value from `$key` and `$keys`.
// @param `$map` A map to get value.
// @param `$key` Required key to `list.index()` map.
// @arbitrary `$keys...` Optional nested keys.
// @returns The returned value is value of `$key` and/or `$keys`.
@function get-index($map, $key, $keys...) {
@if $key {
@each $key-list, $value in $map {
@if list.index($key-list, $key) {
@if list.length($keys) > 0 {
@return get-index($value, list.nth($keys, 1), list.remove-nth($keys, 1)...);
} @else {
@return $value;
}
}
}
}
@return $map;
}

// Examples.
// $example: (
// test basic: (
// basic two: (
// hue saturation: (
// lightness alpha: hsla(15deg, 30%, 35%, 1)
// )
// )
// )
// );

// @debug get-index($example, test, two, hue, alpha); // hsl(15deg, 30%, 35%)

0 comments on commit e1459b9

Please sign in to comment.