'How to check if variable defined in Sass Maps and set fallback value if it doesn't

I'm trying to make the function which checks in Sass Maps that if value is defined of that variable or not, If it doesn't then I set the fallback value. here's the code below:

:root {
  --headings-clr: #000000;
  --system-headingsForeground: #333333;
}

$headingClr: (
  primary: var(--system-headingsForeground),
  fallback: var(--headings-clr)
);

@function isExist($var) {
  $result: undefined;
  @each $prop, $val in $var {
    @if variable-exists(#{$val}) && map-get($var, primary) {
      $result: map-get($var, primary);
    } @else {
      $result: map-get($var, fallback);
   } 
 }
 @return $result;
}

.a {
  color: isExist($headingClr);
}

above function should give me primary value if it exist else it should give me fallback value if system variable can't be found or not defined. above example is for demo purpose. but you get the idea what I'm trying to achieve.



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source