Skip to content

armgit / fider

Public Imported from GitHub
0

Open platform to collect and prioritize feedback

1.5 KB
Raw Blame
@use "sass:list";
@use "sass:map";
@use "sass:meta";
@use "sass:string";

// deep-map-get
@function deep-map-get($map, $keys...) {
  @each $key in $keys {
    $map: map.get($map, $key);
  }
  @return $map;
}

// str-explode
@function str-explode($string, $delimiter: "") {
  @if meta.type-of($string) != "string" {
    @error 'The argument $string: `#{$string}` is of incorrect type: `#{meta.type-of($string)}`. Type of `String` is required!';
  } @else if meta.type-of($delimiter) != "string" {
    @error 'The argument $string: `#{$string}` is of incorrect type: `#{meta.type-of($string)}`. Type of `String` is required!';
  }

  $result: ();
  $running: true;

  @if string.length($delimiter) == 0 {
    @for $i from 1 through string.length($string) {
      $result: list.append($result, string.slice($string, $i, $i));
    }

    @return $result;
  }

  @while $running {
    $index: string.index($string, $delimiter);

    @if $index != null {
      $item: string.slice($string, 1, ($index - 1));
      $result: list.append($result, $item);
      $string: string.slice($string, ($index + string.length($delimiter)));
    } @else {
      $running: false;
    }
  }

  @return list.append($result, $string);
}

// deep-map-check
@function deep-map-check($map, $keys...) {
  @if meta.type-of($map) != "map" {
    @error 'The argument $map: `#{$map}` is of incorrect type: `#{meta.type-of($map)}`. Type of `Map` is required!';
  }

  @each $key in $keys {
    @if not map.has-key($map, $key) {
      @return false;
    }

    $map: map.get($map, $key);
  }

  @return true;
}