Skip to content

Commit f8c36fc

Browse files
feat(list-remove): add.
1 parent 3e75adf commit f8c36fc

10 files changed

+330
-0
lines changed

src/lib/remove/_index.scss

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
@forward 'remove.duplicate.function';
2+
// @forward 'remove.list.function';
3+
@forward 'remove.map.function';
4+
@forward 'remove.nth.function';
5+
@forward 'remove.number.function';
6+
// @forward 'remove.range.function';
7+
@forward 'remove.string.function';
8+
@forward 'remove.type.function';
9+
@forward 'remove.value.function';
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
// Sass.
2+
@use "sass:list";
3+
4+
// Completed
5+
// The `list.remove-duplicate()` or `remove.duplicate()` function returns the list without duplicate values.
6+
// @param `$list` The list from which duplicate values are being removed.
7+
// @arbitrary `$values...` Arbitrary list of elements to remove from `$list`.
8+
// @returns The returned value is a copy of `$list` with unique elements.
9+
@function duplicate($list, $values...) {
10+
$result: ();
11+
@for $i from 1 through list.length($list) {
12+
@if list.length($values) >
13+
0 and not
14+
list.index($values, list.nth($list, $i)) or not
15+
list.index($result, list.nth($list, $i))
16+
{
17+
$result: list.append($result, list.nth($list, $i), list.separator($list));
18+
}
19+
}
20+
@return $result;
21+
}
22+
23+
// Examples.
24+
// SECTION: $remove not defined
25+
// @debug duplicate(('a', 'b', 'c', ('d', 'e', 'f'), 'g', 'h', 'i', 'j', 'k')); // "a", "b", "c", ("d", "e", "f"), "g", "h", "i", "j", "k"
26+
27+
// string
28+
// @debug duplicate(('a', 'b', 'a', 'c', ('d', 'e', 'f'), 'h', 'g', 'h', 'i', 'j', 'k')); // "a", "b", "c", ("d", "e", "f"), "h", "g", "i", "j", "k"
29+
30+
// map
31+
// @debug duplicate(((1: 1), (2: 2), (3, 3), (1: 1), (3, 3))); // (1: 1), (2: 2), (3, 3)
32+
33+
// number
34+
// @debug duplicate((0.9, 1, 1.1, 1, 2, 'a', 2, 'b', 3, 3, 4, 5, 6, 7, 8)); // 0.9, 1, 1.1, 2, "a", "b", 3, 4, 5, 6, 7, 8
35+
// @debug duplicate((a, b, 1, true, false, a, c, 1, c, (d, e, f), h, g, true, h, i, j, k), 1); // a, b, 1, true, false, a, c, c, (d, e, f), h, g, true, h, i, j, k
36+
// @debug duplicate((a, b, 1, true, 3.14, a, c, 1, c, (d, e, f), h, g, true, 3.14, h, i, j, k), 1, 3.14); // a, b, 1, true, 3.14, a, c, c, (d, e, f), h, g, true, h, i, j, k
37+
38+
// list
39+
// @debug duplicate(( (1, 1), (2, 2), (3, 3), (1, 1), (3, 3) )); // (1, 1), (2, 2), (3, 3)
40+
// @debug duplicate(('a', primary dark, 'a', 'c', primary dark, ('d', 'e', 'f'), 'h', 'g', 'h', ('d', 'e', 'f'), 'i', 'j', 'k'), primary dark, ('d', 'e', 'f')); // "a", primary dark, "a", "c", ("d", "e", "f"), "h", "g", "h", "i", "j", "k"
41+
42+
// SECTION: $remove defined
43+
// remove specific duplicates
44+
// @debug duplicate(('a', 'b', 'a', 'c', 'c', ('d', 'e', 'f'), 'h', 'g', 'h', 'i', 'j', 'k'), 'c'); // "a", "b", "a", ("d", "e", "f"), "h", "g", "h", "i", "j", "k"
45+
// @debug duplicate(('a', 'b', 'a', 'c', 'c', ('d', 'e', 'f'), 'h', 'g', 'h', 'i', 'j', 'k'), 'c', 'a'); // "b", ("d", "e", "f"), "h", "g", "h", "i", "j", "k"
46+
47+
// string
48+
// @debug duplicate(('a', 'b', 'a', 'c', 'c', ('d', 'e', 'f'), 'h', 'g', 'h', 'i', 'j', 'k'), 'c'); // "a", "b", "a", "c", ("d", "e", "f"), "h", "g", "h", "i", "j", "k"
49+
// @debug duplicate(('a', 'k', 'b', 'a', 'c', 'c', ('d', 'e', 'f'), 'h', 'g', 'h', 'i', 'j', 'k'), 'k'); // "a", "k", "b", "a", "c", "c", ("d", "e", "f"), "h", "g", "h", "i", "j"
50+
51+
// bool
52+
// @debug duplicate(('a', 'b', true, false, 'a', 'c', 'c', ('d', 'e', 'f'), 'h', 'g', true, 'h', 'i', 'j', 'k'), true); // "a", "b", true, false, "a", "c", "c", ("d", "e", "f"), "h", "g", "h", "i", "j", "k"
53+
54+
// null
55+
// @debug duplicate(((1, 1), null, (2, 2), (3, 3), (1, 1), null, (3, 3) )); // (1, 1), null, (2, 2), (3, 3)
56+
57+
// list
58+
// @debug duplicate(((1, 1), null, (2, 2), (3, 3), (1, 1), null, (3, 3) ), (3, 3)); // (1, 1), null, (2, 2), (3, 3), (1, 1), null
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
// Sass.
2+
@use "sass:list";
3+
@use "sass:meta";
4+
5+
// Functions.
6+
@use "../has/has.type.function" as has-type;
7+
8+
// Todo
9+
// The `list.remove-list()` or `remove.list()` function removes element of the list type from the given `$list`.
10+
// @param `$length` The length of the list to be removed.
11+
// (false) An optional length of the list to remove.
12+
// @param `$bracketed` The value of the bool type indicates the bracketed list to remove.
13+
// true (remove bracketed), false (do not remove bracketed), only (remove only bracketed)
14+
// @returns The returned value is a copy of `$list` without list and/or bracketed optionally of the given length.
15+
@function list($list, $length: false, $bracketed: true) {
16+
@if has-type.type($list, any, "==", list) {
17+
$result: ();
18+
@each $element in $list {
19+
$append: true;
20+
@if meta.type-of($element) == list {
21+
$append: false;
22+
@if not $bracketed and list.is-bracketed($element) {
23+
$append: true;
24+
} @else {
25+
@if meta.type-of($length) == number {
26+
$append: not (list.length($element) == $length);
27+
}
28+
@if $bracketed == only and not list.is-bracketed($element) {
29+
$append: true;
30+
}
31+
}
32+
}
33+
@if $append {
34+
$result: list.append($result, $element, list.separator($list));
35+
}
36+
}
37+
@return $result;
38+
}
39+
@return $list;
40+
}
41+
42+
// Examples.
43+
// @debug list(('a', 'b', 'c')); // "a", "b", "c"
44+
// @debug list(('a', 'b', 'c', ('d', 'e', 'f'))); // "a", "b", "c"
45+
// @debug list(('a', 'b', 'c', ('d', 'e', 'f'), 'g', 'h', (1, 2, 3))); // "a", "b", "c", "g", "h"
46+
// @debug list(('a', 'b', 'c', ('d', (1, 2, 3, (4, 5, 6)), 'e', 'f'))); // "a", "b", "c"
47+
48+
// length
49+
// @debug list((a, (4, 5), b, [d, e, f], c, [1, 2], g, (6, 7, 8), h, i), 2); // a, b, [d, e, f], c, g, (6, 7, 8), h, i
50+
// @debug list((a, (), (4, 5), b, (), [d, e, f], (), c, [1, 2], (), g, (6, 7, 8), h, i), 0); // a, (4, 5), b, [d, e, f], c, [1, 2], g, (6, 7, 8), h, i
51+
52+
// bracketed
53+
// @debug list((a, (4, 5), b, c, [d, e, f], [1, 2], g, (6, 7, 8), h, i), $bracketed: true); // a, b, c, g, h, i
54+
// @debug list((a, (4, 5), b, [d, e, f], c, [1, 2], g, (6, 7, 8), h, i), $bracketed: false); // a, b, [d, e, f], c, [1, 2], g, h, i
55+
// @debug list((a, (4, 5), b, [d, e, f], c, [1, 2], g, (6, 7, 8), h, i), $bracketed: only); // a, (4, 5), b, c, g, (6, 7, 8), h, i
56+
57+
// length + bracketed
58+
// @debug list((a, (4, 5), b, [d, e, f], c, [1, 2], g, (6, 7, 8), h, i), 2, false); // a, b, [d, e, f], c, [1, 2], g, (6, 7, 8), h, i
59+
// @debug list((a, (4, 5), b, [d, e, f], c, [1, 2], g, (6, 7, 8), h, i), 2, only); // a, (4, 5), b, [d, e, f], c, g, (6, 7, 8), h, i
60+
// @debug list((a, (4, 5), b, [d, e, f], c, [1, 2], g, (6, 7, 8), h, i), 2, false); // a, b, [d, e, f], c, [1, 2], g, h, i
61+
// @debug list((a, (4, 5), b, c, [d, e, f], [1, 2], g, (6, 7, 8), h, i), $bracketed: true); // a, b, c, g, h, i
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
// Functions.
2+
@use "remove.type.function" as remove-type;
3+
4+
// Todo Add occurrence any, last, all.
5+
// The `list.remove-map()` or `remove.map()` function returns the list without the map type elements.
6+
// @param `$list` The list from which `map` type elements are being removed.
7+
// @returns The returned value is a copy of `$list` without elements of `map` type.
8+
@function map($list) {
9+
@return remove-type.type($list, map);
10+
}
11+
12+
// Examples.
13+
// @debug map(('a', 'b', 'c', (a: 1), true, false, null)); // "a", "b", "c", true, false, null
14+
// @debug map(('a', 'b', 'c', (a: 1), true, (b: 2), false, null)); // "a", "b", "c", true, false, null
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
// Sass.
2+
@use 'sass:list';
3+
4+
// Completed
5+
// The `list.remove-nth()` or `remove.nth()` function returns the list with removed indexes.
6+
// @param `$list` A list from which required index `$n` and additional indexes `$nts` are being removed.
7+
// @param `$n` A required index of `$list` to be removed.
8+
// @arbitrary `$nts...` An additional indexes of `$list` to be removed.
9+
// @returns The returned value is a copy of `$list` without required index `$n` and indexes `$nts`.
10+
@function nth($list, $n, $nts...) {
11+
$nts: list.join($n, $nts, comma);
12+
$result: ();
13+
@for $i from 1 through list.length($nts) {
14+
@if list.nth($nts, $i) and list.nth($nts, $i) < 0 {
15+
$nts: list.set-nth(
16+
$nts,
17+
$i,
18+
list.length($list) - (list.nth($nts, $i) * -1) + 1
19+
);
20+
}
21+
}
22+
@for $i from 1 through list.length($list) {
23+
@if not list.index($nts, $i) {
24+
$result: list.append($result, list.nth($list, $i), list.separator($list));
25+
}
26+
}
27+
@return $result;
28+
}
29+
30+
// Different name.
31+
@function index($list, $n, $indexes...) {
32+
@return nth($list, $n, $nts...);
33+
}
34+
35+
// Examples.
36+
// index
37+
// @debug nth(('a', 'b', 'c', ('d', 'e', 'f'), 'g', 'h', 'i', 'j', 'k'), 4); // "a", "b", "c", "g", "h", "i", "j", "k"
38+
39+
// indexes
40+
// @debug nth(('a', 'b', 'c', ('d', 'e', 'f'), 'g', 'h', 'i', 'j', 'k'), 2, 4, 7); // "a", "c", "g", "h", "j", "k"
41+
// @debug nth(('a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i'), 2, 3, 4, 5); // "a", "f", "g", "h", "i"
42+
43+
// negative indexes
44+
// @debug nth(('a', 'b', 'c', ('d', 'e', 'f'), 'g', 'h', 'i', 'j', 'k'), -1, -6); // "a", "b", "c", "g", "h", "i", "j"
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
// Functions.
2+
@use "remove.type.function" as remove-type;
3+
4+
// Completed
5+
// The `list.remove-number()` or `remove.number()` function returns the list without the number type elements.
6+
// @param `$list` The list from which the `number` type elements are being removed.
7+
// @returns The returned value is a copy of `$list` without elements of the `number` type.
8+
@function number($list) {
9+
@return remove-type.type($list, number);
10+
}
11+
12+
// Examples.
13+
// @debug number(( 1, 2, 3, 4)); // ()
14+
// @debug number(('a', 1, 2, 3, 'b', 4, 'c')); // "a", "b", "c"
15+
// @debug number(('a', 14, 'b', 13, 'c', 26, ('d', 'e', 'f'))); // "a", "b", "c", ("d", "e", "f")
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
// Sass.
2+
@use "sass:list";
3+
4+
// Functions.
5+
@use "../../math/math.range.function" as math-range;
6+
@use "remove.nth.function" as remove-nth;
7+
8+
// Todo
9+
// The `list.remove-range()` or`remove.range()` function removes the indexes of the given range.
10+
// @param `$list` A list from which indexes of range `$from` to `$to` are being removed.
11+
// @param `$from` The index from which the range of elements to remove begins.
12+
// @param `$to` The end index of the range of elements to remove.
13+
// @returns The returned value is a copy of `$list` without the elements of the given index range `$from` to `$to`.
14+
@function range($list, $from: 1, $to: list.length($list)) {
15+
@return remove-nth.nth($list, math-range.range($from, $to, 1, 0)...);
16+
}
17+
18+
// Examples.
19+
// @debug range(('a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i'), 2, 5); // "a", "f", "g", "h", "i"
20+
// @debug range(('a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i'), 3, 4); // "a", "b", "e", "f", "g", "h", "i"
21+
22+
// From 3 to length.
23+
// @debug range(('a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i'), 3); // "a", "b"
24+
25+
// From 1 to 3.
26+
// @debug range(('a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i'), $to: 3); // "d", "e", "f", "g", "h", "i"
27+
28+
// Negative index
29+
// @debug range(('a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i'), 3, -2); // "d", "e", "f", "g"
30+
// @debug range(('a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i'), -1, -6); // "a", "b", "c"
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
// Functions.
2+
@use "remove.type.function" as remove-type;
3+
4+
// Completed
5+
// The `list.remove-string()` or `remove.string()` function returns the list without the `string` type elements.
6+
// @param `$list` The list from which the `string` type elements are being removed.
7+
// @returns The returned value is a copy of `$list` without the strings.
8+
@function string($list) {
9+
@return remove-type.type($list, string);
10+
}
11+
12+
// Examples.
13+
// @debug string(('a', 'b', 'c')); // ()
14+
// @debug string(('a', 'b', 'c', ('d', 'e', 'f'))); // (("d", "e", "f"),)
15+
// @debug string(('a', 'b', 'c', ('d', 'e', 'f'), 'g', 'h', (1, 2, 3))); // ("d", "e", "f"), (1, 2, 3)
16+
// @debug string(('a', 'b', 'c', ('d', (1, 2, 3, (4, 5, 6)), 'e', 'f'))); // (("d", (1, 2, 3, (4, 5, 6)), "e", "f"),)
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
// Sass.
2+
@use "sass:list";
3+
@use "sass:meta";
4+
5+
// Completed
6+
// The `list.remove-type()` or `remove.type()` function returns the list without the elements of the given types.
7+
// @param `$list` The list from which required `$type` and additional `$types` are being removed.
8+
// @param `$type` A required type to remove from `$list`.
9+
// @arbitrary `$types...` An additional types to remove from the given `$list`.
10+
// @returns The returned value is a copy of `$list` without required `$type` and additional `$types`.
11+
@function type($list, $type, $types...) {
12+
$types: list.join($type, $types, comma);
13+
$exists: false;
14+
$i: 0;
15+
@if list.length($list) > 0 {
16+
@while meta.type-of($i) == number {
17+
$i: $i + 1;
18+
@if list.index($types, meta.type-of(list.nth($list, $i))) {
19+
$exists: true;
20+
}
21+
@if $exists or $i == list.length($list) {
22+
$i: null;
23+
}
24+
}
25+
@if $exists {
26+
$result: ();
27+
@each $element in $list {
28+
$result: if(
29+
not list.index($types, meta.type-of($element)),
30+
list.append($result, $element, list.separator($list)),
31+
$result
32+
);
33+
}
34+
@return $result;
35+
}
36+
}
37+
@return $list;
38+
}
39+
40+
// Examples.
41+
// all strings
42+
// @debug type(('a', 'b', 'c'), string); // ()
43+
// @debug type(('a', 1, 'b', 2, 'c', 3), string); // 1, 2, 3
44+
// @debug type(('a', 'b', 'c', ('d', 'e', 'f')), string); // (("d", "e", "f"),)
45+
// @debug type(('a', 'b', 'c', ('d', 'e', 'f'), 'g', 'h', (1, 2, 3)), string); // ("d", "e", "f"), (1, 2, 3)
46+
// @debug type(('a', 'b', 'c', ('d', (1, 2, 3, (4, 5, 6)), 'e', 'f')), string); // (("d", (1, 2, 3, (4, 5, 6)), "e", "f"),)
47+
// @debug type(('a', 'b', 'c', ('d', 'e', 'f'), 'g', 'h', (1, 2, 3)), string); // ("d", "e", "f"), (1, 2, 3)
48+
49+
// all numbers
50+
// @debug type(('a', 1, 'b', 2, 'c', 3), number); // "a", "b", "c"
51+
52+
// all strings + numbers
53+
// @debug type(('a', 1, 'b', 2, 'c', 3, (a: 1)), number, string); // ((a: 1),)
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
// Sass.
2+
@use "sass:list";
3+
4+
// Completed
5+
// The `list.remove-value()` or `remove.value()` function returns the list without given values.
6+
// @param `$list` The list from which required `$value` and additional `$values` are being removed.
7+
// @param `$value` A required value to remove from `$list`.
8+
// @arbitrary `$values...` An additional values to remove from `$list`.
9+
// @returns The returned value is a copy of `$list` without required `$value` and additional `$values`.
10+
@function value($list, $value, $values...) {
11+
$result: ();
12+
$values: list.join(($value,), $values, comma);
13+
@for $i from 1 through list.length($list) {
14+
@if not list.index($values, list.nth($list, $i)) {
15+
$result: list.append($result, list.nth($list, $i), list.separator($list));
16+
}
17+
}
18+
@return $result;
19+
}
20+
21+
// Examples.
22+
// string
23+
// @debug value(('a', 'b', 'c', ('d', 'e', 'f'), 'g', 'h', 'i', 'j', 'k'), h, g, a); // "b", "c", ("d", "e", "f"), "i", "j", "k"
24+
25+
// list
26+
// @debug value(('a', 'b', 'c', ('d', 'e', 'f'), 'g', 'h', 'i', 'j', 'k'), ('d', 'e', 'f')); // "a" "b" "c" "g" "h" "i" "j" "k"
27+
// @debug value(('a', 'b', 'c', ('d', 'e', 'f'), 'g', 'h', 'i', 'j', 'k'), 'b', ('d', 'e', 'f')); // "a" "c" "g" "h" "i" "j" "k"
28+
29+
// null
30+
// @debug value(('a', null, 'c', null, 'g', null, 'i', 'j', null), null); // "a", "c", "g", "i", "j"

0 commit comments

Comments
 (0)