Skip to content

Commit

Permalink
feat(functions): Add list-remove function
Browse files Browse the repository at this point in the history
  • Loading branch information
MorevM committed Sep 10, 2022
1 parent e6a8c69 commit d7d89ee
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/functions/list/_index.scss
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
@forward './list-contains/list-contains.scss';
@forward './list-includes/list-includes.scss';
@forward './list-join/list-join.scss';
@forward './list-remove/list-remove.scss';
@forward './list-slice/list-slice.scss';
26 changes: 26 additions & 0 deletions src/functions/list/list-remove/list-remove.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
@use 'sass:list';
@use 'sass:meta';

/**
* Removes the list element by index.
*
* @param {list} $list The input list.
* @param {number} $index [''] The list element index to remove. Use negative index to remove from the end of list.
*
* @return {list} A list without element with specified `$index` if found, original list otherwise.
*/
@function list-remove($list, $index: null) {
$result: ();
$index: if(meta.type-of($index) == 'number', $index, 0);
$index: if($index < 0, list.length($list) + $index + 1, $index);
$bracketed: list.is-bracketed($list);
$separator: list.separator($list);

@for $i from 1 through list.length($list) {
@if $i != $index {
$result: list.append($result, list.nth($list, $i));
}
}

@return list.join((), $result, $separator, $bracketed);
}
41 changes: 41 additions & 0 deletions src/functions/list/list-remove/list-remove.test.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
@use 'true' as *; // sass-true
@use './list-remove.scss' as *;

@include describe('Functions') {
@include describe('list-remove') {
@include it('Removes element from list using positive index') {
@include assert-equal(list-remove((1, 2, 3, 4, 5), 1), (2, 3, 4, 5));
@include assert-equal(list-remove((1, 2, 3, 4, 5), 2), (1, 3, 4, 5));
@include assert-equal(list-remove((1, (2, 2), (3, 3), 4, 5), 2), (1, (3, 3), 4, 5));
}

@include it('Removes element from list using negative index') {
@include assert-equal(list-remove((1, 2, 3, 4, 5), -1), (1, 2, 3, 4));
@include assert-equal(list-remove((1, 2, 3, 4, 5), -2), (1, 2, 3, 5));
@include assert-equal(list-remove((1, (2, 2), (3, 3), 4, 5), -3), (1, (2, 2), 4, 5));
}

@include it('Returns original list clone if calculated $index is out of range or invalid') {
@include assert-equal(list-remove((1, 2, 3, 4, 5), 'string'), (1, 2, 3, 4, 5));
@include assert-equal(list-remove((1, 2, 3, 4, 5), null), (1, 2, 3, 4, 5));
@include assert-equal(list-remove((1, 2, 3, 4, 5), true), (1, 2, 3, 4, 5));
@include assert-equal(list-remove((1, 2, 3, 4, 5), 1.5), (1, 2, 3, 4, 5));
@include assert-equal(list-remove((1, 2, 3, 4, 5), 1s), (1, 2, 3, 4, 5));
@include assert-equal(list-remove((1, 2, 3, 4, 5), ()), (1, 2, 3, 4, 5));
@include assert-equal(list-remove((1, 2, 3, 4, 5), 0), (1, 2, 3, 4, 5));
@include assert-equal(list-remove((1, 2, 3, 4, 5), 100), (1, 2, 3, 4, 5));
@include assert-equal(list-remove((1, (2, 2), (3, 3), 4, 5), -100), (1, (2, 2), (3, 3), 4, 5));
}

@include it('Respect the list separator') {
@include assert-equal(list-remove((1 2 3 4 5), 2), (1 3 4 5));
}

@include it('Respect the list brackets') {
$list-input: 1, 2, 3, 4, 5;
$list-expected: 1, 3, 4, 5;

@include assert-equal(list-remove($list-input, 2), $list-expected);
}
}
}

0 comments on commit d7d89ee

Please sign in to comment.