Skip to content

Commit 6d22dd0

Browse files
feat(list): add functions.
1 parent f8c36fc commit 6d22dd0

20 files changed

+567
-0
lines changed

src/lib/_list.add.function.scss

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
// Sass.
2+
@use 'sass:list';
3+
4+
// Completed
5+
// The `list.add()` function returns a copy of `$list` with `$value` and additional `$values`.
6+
// @param `$list` The list to add the `$value` and additional `$values` into.
7+
// @param `$separator` The separator `comma`, `space` or `slash` of the list. Default `auto`.
8+
// @param `$value` The value to add to the end of `$list`.
9+
// @arbitrary `$values...` Additional values to add into `$list`.
10+
// @returns Returns a copy of `$list` with `$value` and additional `$values` added to the end.
11+
@function add($list, $separator: auto, $value, $values...) {
12+
@return list.join(
13+
$list,
14+
list.join(($value,), $values, list.separator($separator)),
15+
$separator
16+
);
17+
}
18+
19+
// Examples.
20+
// @debug add(10px 20px, auto, 30px); // 10px 20px 30px
21+
// @debug add((red, green), auto, blue, space); // red, green, blue, space
22+
23+
// additional values
24+
// @debug add((blue, red), auto, green, blue, red); // blue, red, green, blue, red
25+
// @debug add(10px 20px, auto, 30px 40px, 1px, 15px); // 10px 20px (30px 40px) 1px 15px
26+
// @debug add(10px, auto, 20px, 1em, 2rem); // 10px, 20px, 1em, 2rem
27+
// @debug add(10px 20px, auto, 30px 40px, 1px, 15px, (1px 2px 3px)); // 10px 20px (30px 40px) 1px 15px (1px 2px 3px)
28+
// @debug add((10px, 20px), auto, 30px 40px, 1px, 15px, (1px 2px 3px), (4px, 5px)); // 10px, 20px, 30px 40px, 1px, 15px, 1px 2px 3px, (4px, 5px)
29+
30+
// add map
31+
// @debug add(1 2 3, auto, (a: 1, b: 2, c: 3), (d: 4, e: 5)); // 1 2 3 (a: 1, b: 2, c: 3) (d: 4, e: 5)
32+
// @debug add((a: 1, b: 2), auto, (c: 3, d: 4), (e: 5, f: 6)); // a 1, b 2, (c: 3, d: 4), (e: 5, f: 6)
33+
// @debug add(1 2 3, auto, (a: 1, b: 2, c: 3), (d: 4, e: 5), (4, 5, 6), (7, 8, 9)); // 1 2 3 (a: 1, b: 2, c: 3) (d: 4, e: 5) (4, 5, 6) (7, 8, 9)
34+
// @debug add(1 2 3, auto, (a: 1, b: 2, c: 3), (d: 4, e: 5), 4, 5, 6, 7, 8, 9); // 1 2 3 (a: 1, b: 2, c: 3) (d: 4, e: 5) 4 5 6 7 8 9

src/lib/_list.append.function.scss

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
// Sass.
2+
@use 'sass:list';
3+
@use 'sass:meta';
4+
5+
// Completed
6+
// The modified `list.append()` function returns a copy of `$list` with `$val` and/or with added multiple `$values`.
7+
// The function is modified by adding arbitrary values to the end of the parameters to preserve the original functionality.
8+
// @param `$list` The list to append the `$val` and/or `$values` into.
9+
// @param `$val` The value to append to the end of `$list`.
10+
// @param `$separator` The separator `comma`, `space` or `slash` of the list. Default `auto`.
11+
// @arbitrary `$values...` Additional values to append into `$list`.
12+
// @returns Returns a copy of `$list` with `$val` and/or `$values` added to the end.
13+
@function append($list, $val, $separator: auto, $values...) {
14+
@if not list.index(arglist list, meta.type-of($list)) {
15+
@error "$list: #{$list} is not arglist or list";
16+
}
17+
@return list.join(
18+
$list,
19+
list.join(($val,), $values, list.separator($list)),
20+
$separator
21+
);
22+
}
23+
24+
// Examples.
25+
// $-list1: (30px, 10px);
26+
// $-list2: (50px, 40px);
27+
28+
// @debug append(10px 20px, 30px); // 10px 20px 30px
29+
// @debug append((red, green), blue, space); // red green blue
30+
31+
// @debug list.join(((10px 20px),), ((30px, 10px), (50px, 40px)), $separator: comma);
32+
// @debug append(((10px 20px),), (30px, 10px), comma, (50px, 40px));
33+
34+
// @debug list.join(((10px 20px),), ($-list1, $-list2), $separator: comma);
35+
// @debug append(((10px 20px),), $-list1, comma, $-list2);
36+
37+
// @debug list.join(((10px 20px),), ((30px, 10px),), comma);
38+
// @debug list.append(((10px 20px),), (30px, 10px), comma);
39+
40+
// additional values
41+
// @debug append((blue, red), green, auto, blue, red); // blue, red, green, blue, red
42+
// @debug append(10px 20px, 30px 40px, auto, 1px, 15px); // 10px 20px (30px 40px) 1px 15px
43+
// @debug append(10px, 20px, auto, 1em, 2rem); // 10px 20px 1em 2rem
44+
// @debug append(10px 20px, 30px 40px, auto, 1px, 15px, (1px 2px 3px)); // 10px 20px (30px 40px) 1px 15px (1px 2px 3px)
45+
// @debug append((10px, 20px), 30px 40px, auto, 1px, 15px, (1px 2px 3px), (4px, 5px)); // 10px, 20px, 30px 40px, 1px, 15px, 1px 2px 3px, (4px, 5px)
46+
47+
// append map
48+
// @debug append(1 2 3, (a: 1, b: 2, c: 3), auto, (d: 4, e: 5)); // 1 2 3 (a: 1, b: 2, c: 3) (d: 4, e: 5)
49+
// @debug append((a: 1, b: 2), (c: 3, d: 4), auto, (e: 5, f: 6)); // a 1, b 2, (c: 3, d: 4), (e: 5, f: 6)
50+
// @debug append(1 2 3, (a: 1, b: 2, c: 3), auto, (d: 4, e: 5), (4, 5, 6), (7, 8, 9)); // 1 2 3 (a: 1, b: 2, c: 3) (d: 4, e: 5) (4, 5, 6) (7, 8, 9)
51+
// @debug append(1 2 3, (a: 1, b: 2, c: 3), auto, (d: 4, e: 5), 4, 5, 6, 7, 8, 9); // 1 2 3 (a: 1, b: 2, c: 3) (d: 4, e: 5) 4 5 6 7 8 9

src/lib/_list.empty.function.scss

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
// Sass.
2+
@use 'sass:list';
3+
4+
// Completed
5+
// The `list.empty()` function returns the `bool` indicating whether the list is empty.
6+
// @param `$list` The list to check against its length.
7+
// @returns The returned value is a `bool` indicating whether the given `$list` is empty.
8+
@function empty($list) {
9+
@return list.length($list) == 0;
10+
}
11+
12+
// Alias name for `list.empty()`.
13+
@function is-empty($list) {
14+
@return empty($list);
15+
}
16+
17+
// Examples.
18+
// @debug empty(()); // true
19+
// @debug empty((1, )); // true
20+
// @debug empty((a: 1)); // false
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
// Functions.
2+
@use 'get/get.map.function' as get-map;
3+
@use 'remove/remove.map.function' as remove-map;
4+
5+
// Completed
6+
// The `list.extract-map()` gets and removes the map from `$list`.
7+
// @param `$list` A list from which the map is extracted.
8+
// @param `$occurrence` Extract `first`, `last` map or `all` maps.
9+
// @returns The returned value is a map with extracted from `$list` map(or multiple maps in list) in `map` key, and list without `map`.
10+
@function extract-map($list, $occurrence: first) {
11+
@return (map: get-map.map($list, $occurrence), list: remove-map.map($list));
12+
}
13+
14+
// Examples.
15+
// first map
16+
// @debug extract-map((a, b, (a: 1, b: 5), c, d)); // (map: (a: 1, b: 5), list: (a, b, c, d))
17+
18+
// last map
19+
// @debug extract-map((a, b, (a: 1, b: 5), c, (c: 1, d: 1), d), last); // (map: (c: 1, d: 1), list: (a, b, c, d))
20+
21+
// all maps
22+
// @debug extract-map((a, b, (a: 1, b: 5), c, (c: 1, d: 1), d), all); // (map: ((a: 1, b: 5), (c: 1, d: 1)), list: (a, b, c, d))

src/lib/_list.first.function.scss

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
// Sass.
2+
@use 'sass:list';
3+
4+
// Completed
5+
// The `list.first()` function returns the first element of `$list`.
6+
// @param `$list` The list to get the first element from.
7+
// @returns The returned value is the first element of `$list`.
8+
@function first($list) {
9+
@return list.nth($list, 1);
10+
}
11+
12+
// Examples.
13+
// @debug first(('a', 'b', c, d, 2, 4, 5, (a: 1))); // a
14+
// @debug first(((a: 1), 'b', c, d, 2, 4, 5, (a: 1))); // (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+
@use 'sass:meta';
4+
5+
// Completed
6+
// The `list.flatten()` function returns flattened list of `$list`, otherwise `null`.
7+
// @param `$list` The list to flatten.
8+
// @returns The returned value is flattened copy of `$list`, otherwise `null`.
9+
@function flatten($list) {
10+
@if meta.type-of($list) == list {
11+
$result: ();
12+
@for $i from 1 through list.length($list) {
13+
@if meta.type-of(list.nth($list, $i)) == list {
14+
@each $element in list.nth($list, $i) {
15+
$result: list.append($result, $element, list.separator($list));
16+
}
17+
} @else {
18+
$result: list.append($result, list.nth($list, $i), list.separator($list));
19+
}
20+
}
21+
@return $result;
22+
}
23+
@return null;
24+
}
25+
26+
// Examples.
27+
// @debug flatten(('a', 'b', (5, 6), c, d, 2, 4, 5, (a: 1), (1, 2, 3) )); // "a", "b", 5, 6, c, d, 2, 4, 5, (a: 1), 1, 2, 3
28+
// @debug flatten(('a', 'b', (5, 6), (c, d, 2, 4), 5, (a: 1), (1, 2, 3) )); // "a", "b", 5, 6, c, d, 2, 4, 5, (a: 1), 1, 2, 3
29+
// @debug flatten(('a', (b: 1), 'b', (5, 6), c, d, 2, 4, 5, (a: 1), (1, 2, 3) )); // "a", (b: 1), "b", 5, 6, c, d, 2, 4, 5, (a: 1), 1, 2, 3
30+
// @debug flatten((b: 1)); // null

src/lib/_list.index.function.scss

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
// Sass.
2+
@use "sass:list";
3+
@use "sass:meta";
4+
5+
// Completed
6+
// The `list.index()` function returns the index and/or indexes of the given `$values`.
7+
// @param `$list` The list from which indexes of the given values are retrieved.
8+
// @param `$value` The single value to find in the given list and return its index.
9+
// @arbitrary `$values...` Multiple values to find their indexes in the given list.
10+
// @returns The returned value is an index or list of indexes of the given values.
11+
@function index($list, $value, $values...) {
12+
@if not list.index(arglist list, meta.type-of($list)) {
13+
@error "$list: #{$list} is not arglist or list";
14+
}
15+
$result: ();
16+
@each $value in list.join(($value,), $values, comma) {
17+
$result: list.index($list, $value)
18+
and list.append($result, list.index($list, $value), comma)
19+
or $result;
20+
}
21+
@return list.length($result) > 0
22+
and (list.length($result) == 1 and list.nth($result, 1) or $result)
23+
or null;
24+
}
25+
26+
// Examples.
27+
// @debug index(1px solid red, 1px); // 1
28+
// @debug index(1px solid red, solid); // 2
29+
// @debug index(1px solid red, dashed); // null
30+
31+
// bool
32+
// @debug index(1px false solid red, false); // 2
33+
34+
// null
35+
// @debug index(1px false solid null red, null); // 4
36+
37+
// list
38+
// @debug index((1px solid) 'red' (12px, 14px) 'green', (1px solid), (12px, 14px)); // 1, 3
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
// Sass.
2+
@use 'sass:list';
3+
4+
// Completed
5+
// The `list.insert-nth()` function returns the `$list` with `$value` inserted into index `$n`.
6+
// @param `$list` The list to insert `$value` at index `$n`.
7+
// @param `$n` The index `$n` under which `$value` is inserted.
8+
// @param `$value` The value to insert at `$n` index.
9+
// @return The return value is the list with the inserted `$value` at the `$n` index.
10+
@function insert-nth($list, $n, $value) {
11+
$result: ();
12+
@for $i from 1 through list.length($list) {
13+
@if $n == $i {
14+
$result: list.append($result, $value, list.separator($list));
15+
}
16+
17+
$result: list.append($result, list.nth($list, $i), list.separator($list));
18+
}
19+
@return $result;
20+
}
21+
22+
// Examples.
23+
// @debug insert-nth(('a', 'b', 'd', 'e', 'f', 'g'), 3 , 'c'); // "a", "b", "c", "d", "e", "f", "g"
24+
// @debug insert-nth('a' 'b' 'd' 'e' 'f' 'g', 3, 'c'); // "a" "b" "c" "d" "e" "f" "g"
25+
// @debug insert-nth(('a' 1) ('b' 2) ('d' 4) ('e' 5) ('f' 6) ('g' 7), 3, ('c' 3)); // ("a" 1) ("b" 2) ("c" 3) ("d" 4) ("e" 5) ("f" 6) ("g" 7)

src/lib/_list.invert.function.scss

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
// Sass.
2+
@use 'sass:list';
3+
4+
// Completed
5+
// The `list.invert()` function invert `$list` elements.
6+
// @param `$list` The list to invert elements.
7+
// @returns The returned value is a copy of `$list` with inverted elements.
8+
@function invert($list) {
9+
$inverted: ();
10+
@for $i from list.length($list) through 1 {
11+
$inverted: list.append($inverted, list.nth($list, $i), list.separator($list));
12+
}
13+
@return $inverted;
14+
}
15+
16+
// Examples.
17+
// $-list: ('a', 'b', c, d, 2, 4, 5, (a: 1));
18+
19+
// @debug invert($-list);
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
// Sass.
2+
@use "sass:list";
3+
4+
// Completed
5+
// The `list.is-length()` function checks whether `$list` length is equal to `$length`.
6+
// @param `$list` The list to check it is of `$length`.
7+
// @returns The returned value is `bool` indicating whether `$list` length is equal to `$length`.
8+
@function is-length($list, $length) {
9+
@return $length and list.length($list) == $length;
10+
}
11+
12+
// Alias name.
13+
@function of-length($list, $length) {
14+
@return is-length($list, $length);
15+
}
16+
17+
// Examples.
18+
// @debug of-length((a, b, c), 4); // false

0 commit comments

Comments
 (0)