list.select()

The list.select() function returns the list with elements selected by comparison consisting of $operand, $operator, $value, and $values....

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

// Functions.
@use '../comparison/comparison.compare.function';
@use 'list.append.function' as *;
@use 'list.empty.function' as *;
@use 'list.first.function' as *;
@use 'list.index.function' as *;
@use 'list.nth.function' as *;
@use 'remove/remove.duplicate.function' as *;

// The `list.select()` function.
@function select(
  $select: index,
  $from: null,
  $operand: value,
  $operator: '==',
  $value,
  $values...
) {
  $result: ();
  $select: if(list.length($select) == 1, any $select, $select);
  $occurrence: list.nth(
    $select,
    index($select, all, any, end, first, last, only, start)
  );
  $select: list.nth($select, index($select, has, index, type, value));
  @each $value in append((), $value, comma, $values...) {
    $do: true;
    $compared-result: ();
    $index: 1;
    @while $do == true {
      $comparison-result: comparison.compare(
        map.get(
          (
            index: $index,
            type: meta.type-of(list.nth($from, $index)),
            value: list.nth($from, $index),
          ),
          $operand
        ),
        $operator,
        $value
      );

      @if $comparison-result or ($select == has and $occurrence == only) {
        $compared-result: list.append(
          $compared-result,
          map.get(
            (
              has: $comparison-result,
              index: $index,
              type: meta.type-of(list.nth($from, $index)),
              value: list.nth($from, $index),
            ),
            $select
          ),
          comma
        );
      }

      // Break the while.
      $do: if(
        $index ==
          list.length($from) or
          (
            not empty($compared-result) and list.index(first start, $occurrence)
          ),
        false,
        $do
      );
      $index: $index + 1;
    }

    $compared-result: if(empty($compared-result), null, $compared-result);

    @if $compared-result {
      @if $select == has and list.index(all only, $occurrence) {
        $compared-result: duplicate($compared-result);
        $compared-result: if(
          list.index($compared-result, null) or
            list.index($compared-result, false),
          false,
          true
        );
      }

      $compared-result: if(
        list.index(any first start end last, $occurrence),
        list.nth(
          $compared-result,
          if(
            list.index(any first start, $occurrence),
            1,
            list.length($compared-result)
          )
        ),
        if(
          list.length($compared-result) == 1,
          first($compared-result),
          $compared-result
        )
      );
    }

    $result: list.append($result, $compared-result, comma);
  }

  // has
  @if $select == has {
    $result: if(
      list.index(all, $occurrence),
      if(list.index($result, null) or list.index($result, false), false, true),
      if(
        list.index(any, $occurrence),
        if(list.index($result, true), true, false),
        if($occurrence == only, $result, false)
      )
    );
  }

  @return if(
    list.length($result) > 0,
    if(
      list.length($result) == 1 and not (type-of(nth($result, 1)) == list),
      first($result),
      $result
    ),
    null
  );
}

Parameters

$select

The value of list or string type indicates what to retrieve from the given $from list.

The value of list type consists of occurrence and selection, in order (occurrence selection). It's changed to (selection occurrence) when selection has is used, and there are has only, has any, has all. Occurrence (all, any, end, first, last, only) indicates how often compared elements occur in the given $from list. Selection can take the values has, index, type, value and indicates what to retrieve from the list.

$from

The list to retrieve elements from, by the given comparison.

$operand

The value of string type can take (index, type, value) and indicates an operand from the list used in the comparison.

index

element's index

type

type of the element

value

element's value

$operator

The operator used to compare $value and/or $values with $operand of the $from list.

!

not

:

type of

(,)

list length

(:)

map length

===

compatbile or comparable

==

equal to

~=

text contains the given string (string.index())

>

greater than

>=

greater or equal than

<

less than

<=

less or equal than

$value

The required value to select from the given $from list.

$values...

Additional values to select from the given $from list.

Return

The return value is the list with the elements selected by the given comparison.

Examples

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

// Examples.

Last updated