list.type()

The list.type() or list.nth-type() function returns the type of all or selected $n indexes of $list.

// Functions.
@use 'get/get.map.function';
@use 'remove/remove.map.function';

// The `list.type()` function.
@function type($list, $n...) {
  $result: if(list.length($n) > 0, $n, $list);
  @for $i from 1 through list.length($result) {
    $result: list.set-nth(
      $result,
      $i,
      meta.type-of(
        list.nth($list, if(list.length($n) > 0, list.nth($result, $i), $i))
      )
    );
  }
  @return if(list.length($result) > 0, $result, null);
}
https://github.com/angular-package/sass/blob/main/list/_list.type.function.scss
// Sass.
@use 'sass:list';
@use 'sass:meta';

// Status: DONE
// The `list.type()` function returns the type of all or selected `$n` indexes of `$list`.
// @param `$list` The list to check type of all or given `$n`.
// @arbitrary `$n...` Indexes of number type to check their types in `$list`.
// @return The list with the types of the given `$n` indexes.
@function type($list, $n...) {
  $result: if(list.length($n) > 0, $n, $list);
  @for $i from 1 through list.length($result) {
    $result: list.set-nth($result, $i, meta.type-of(list.nth($list, if(list.length($n) > 0, list.nth($result, $i), $i))));
  }
  @return if(list.length($result) > 0, if(list.length($n) == 1, list.nth($result, 1), $result), null);
}

// Alias function name.
@function nth-type($list, $n...) {
  @return type($list, $n...);
}

// Examples.
// $-list: ('a', 2, (a: 231), 'b', 7, null, 'c', 5, 'd', 231);

// @debug type($-list); // string, number, map, bool, string, number, null, string, number, string, number
// @debug type($-list, 4, 1, 3, 6); // string, string, map, null
// @debug type($-list, 4, 1, 3, 6, 9); // string, string, map, null, string

// not in range.
// @debug type($-list, 4, 1, 3, 6, 11); // ! Error not in range

Parameters

$list

The list to check type of all or given $n.

$n...

Indexes of number type to check their types in $list.

Return

The list with the types of the given $n indexes.

Examples

All types

// Use.
@use 'angular-package/sass/list';
$-list: ('a', 2, (a: 231), 'b', 7, null, 'c', 5, 'd', 231);

// Examples.
@debug list.type($-list); // string, number, map, bool, string, number, null, string, number, string, number

Selected

// Examples.
@debug list.type($-list, 4, 1, 3, 6); // string, string, map, null
@debug list.type($-list, 4, 1, 3, 6, 9); // string, string, map, null, string

Not in range

// Examples.
@debug list.type($-list, 4, 1, 3, 6, 11); // ! Error not in range

Last updated

Was this helpful?