-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(functions): Add
list-remove
function
- Loading branch information
Showing
3 changed files
with
68 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} | ||
} |