list.nth()

The list.nth() function returns the element of $list at index $n and/or with elements of given indexes $nts....

Function modify original by adding $nts... arbitrary parameter at the end.

// Sass.
@use 'sass:list';

// The `list.nth()` function.
@function nth($list, $n, $nts...) {
  $result: ();
  @each $n in list.join($n, $nts, comma) {
    @if $n and $n != 0 {
      $result: list.append(
        $result,
        if(
          list.length($list) >= if($n < 0, calc($n * -1), $n),
          list.nth($list, $n),
          null
        ),
        list.separator($list)
      );
    }
  }
  @return if(list.length($nts) > 0, $result, list.nth($result, 1));
}

Parameters

$list

A list from which the element of index $n and/or multiple indexes $nts are retrieved.

$n

The required index of $list.

$nts...

Optional multiple indexes of $list.

Return

The return value is a retrieved element or list of retrieved elements.

Examples

// Use.
@use 'angular-package/sass/list';

// Examples.
// single `$n`
@debug list.nth(('a', 'b', 'c', 'd', 'e', 'f'), 4); // d

// multiple `$nts...`
@debug list.nth(('a', 'b', 'c', 'd', 'e', 'f'), 4, 3, 4); // "d", "c", "d"

// null or false
@debug list.nth(('a', 'b', 'c', 'd', 'e', 'f'), 10); // null
@debug list.nth(('a', 'b', 'c', 'd', 'e', 'f'), 10) or false; // false
@debug list.nth(('a', 'b', 'c', 'd', 'e', 'f'), -10); // null
@debug list.nth(('a', 'b', 'c', 'd', 'e', 'f'), -10) or false; // false

Last updated