list.nth()
The list.nth()
function returns the element of $list
at index $n
and/or with elements of given indexes $nts...
.
// 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));
}
https://github.com/angular-package/sass/blob/main/list/_list.nth.function.scss
// Sass.
@use 'sass:list';
// Status: DONE
// 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.
// @param `$list` A list from which the element of index `$n` and/or multiple indexes `$nts` are retrieved.
// @param `$n` The required index of `$list`.
// @arbitrary `$nts...` Optional multiple indexes of `$list`.
// @return The return value is a retrieved element or list of retrieved elements.
@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));
}
// Examples.
// single `$n`
// @debug nth(('a', 'b', 'c', 'd', 'e', 'f'), 4); // d
// multiple `$nts...`
// @debug nth(('a', 'b', 'c', 'd', 'e', 'f'), 4, 3, 4); // "d", "c", "d"
// null or false
// @debug nth(('a', 'b', 'c', 'd', 'e', 'f'), 10); // null
// @debug nth(('a', 'b', 'c', 'd', 'e', 'f'), 10) or false; // false
// @debug nth(('a', 'b', 'c', 'd', 'e', 'f'), -10); // null
// @debug nth(('a', 'b', 'c', 'd', 'e', 'f'), -10) or false; // false
Parameters
$list
$list
A list from which the element of index $n
and/or multiple indexes $nts
are retrieved.
$n
$n
The required index of $list
.
$nts...
$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
Was this helpful?