Skip to content

Commit 3e75adf

Browse files
feat(list-get): add.
1 parent 2a3b0bf commit 3e75adf

File tree

9 files changed

+294
-0
lines changed

9 files changed

+294
-0
lines changed
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
// Functions.
2+
@use 'get.type.function' as get-type;
3+
4+
// Completed
5+
// The `get.bool()` function returns any, first, last bool, or all occurring bools.
6+
// @param `$list` The list from which the occurring bools are retrieved.
7+
// @param `$occurrence` Any, first, last occurring bool or all occurred bools in `$list`.
8+
// @returns The returned value is occurred any, first, last bool or all occurred bools retrieved from `$list` or `null`.
9+
@function bool($list, $occurrence: first) {
10+
@return get-type.type($list, $occurrence, bool);
11+
}
12+
13+
// Examples.
14+
// @debug bool(('a', 'b', false, 'c', 2)); // `false`
15+
// @debug bool(('a', 'b', true, 'c', 2)); // `true`
16+
// @debug bool(('a', 'b', 'c', (true, '5', (6,)))); // `null`
17+
// @debug bool(('a', 'b', true, 'c', 2), any); // `null`
18+
// @debug bool(('a', 'b', true, 'c', 2), first); // `true`
19+
// @debug bool(('a', 'b', false, 'c', 2, true), any); // `false`
20+
// @debug bool(('a', 'b', false, 'c', 2, true), last); // `true`
21+
// @debug bool(('a', 'b', false, 'c', 2, true), first); // `false`
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
// Functions.
2+
@use 'get.type.function' as get-type;
3+
4+
// Completed
5+
// The `get.list()` function returns any, first, last occurring list or all occurred lists from `$list`.
6+
// @param `$list` The list from which occurring lists are taken.
7+
// @param `$occurrence` All, any, first or last occurrence of list in `$list` to retrieve.
8+
// @returns The returned value is occurred any, first, last list or all occurred lists retrieved from `$list` or `null`.
9+
@function list($list, $occurrence: first) {
10+
@return get-type.type($list, $occurrence, list);
11+
}
12+
13+
// Examples.
14+
// @debug list(('a', 'b', 'c')); // `null`
15+
16+
// fist/any
17+
// @debug list(('a', 'b', 'c', ('d', 'e', 'f'))); // ("d", "e", "f")
18+
// @debug list(('a', 'b', 'c', ('d', 'e', 'f'), 132, ('a', 'd', 'f')), any); // "d", "e", "f"
19+
20+
// all
21+
// @debug list(('a', 'b', 'c', ('d', 'e', 'f'), 'g', 'h', (1, 2, 3)), all); // ("d", "e", "f"), (1, 2, 3)
22+
// @debug list(('a', 'b', 'c', ('d', (1, 2, 3, (4, 5, 6)), 'e', 'f')), all); // ("d", (1, 2, 3, (4, 5, 6)), "e", "f")
23+
24+
// last
25+
// @debug list(('a', 'b', 'c', ('d', 'e', 'f'), 132, ('a', 'd', 'f')), last); // "a", "d", "f"

src/lib/get/_get.map.function.scss

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
// Functions.
2+
@use 'get.type.function' as get-type;
3+
4+
// Completed
5+
// The `get.map()` returns retrieved any, first, last occurring map or all occured maps from `$list`.
6+
// @param `$list` The list from which occurring maps are retrieved.
7+
// @param `$occurrence` All, any, first or last occurrence of map in `$list` to retrieve.
8+
// @returns The returned value is occurred any, first, last map or all occurred maps retrieved from `$list` or `null`.
9+
@function map($list, $occurrence: first) {
10+
@return get-type.type($list, $occurrence, map);
11+
}
12+
13+
// Examples.
14+
// none
15+
// @debug map(('a', 'b', 'c')); // null
16+
17+
// first
18+
// @debug map(('a', 'b', 'c', ('d': 1, 'e': 2, 'f': 3))); // ("d": 1, "e": 2, "f": 3)
19+
// @debug map(('a', 'b', 'c', (('d': 1, 'e': 2, 'f': 3), 'g'), 'h')); // null
20+
// @debug map(('a', ('d': 1, 'e': 2, 'f': 3), 'b', 'c', ('d', ('g': 1, 'h': 2, 'i': 3), (1, 2, 3, (4, 5, 6)), 'e', 'f'))); // ("d": 1, "e": 2, "f": 3)
21+
22+
// all
23+
// @debug map(('a', (test: 2, test3: 45), 'b', 'c', ('d': 1, 'e': 2, 'f': 3)), all); // (test: 2, test3: 45), ("d": 1, "e": 2, "f": 3)
24+
// @debug map(('a', 'b', 'c', ('d': 1, 'e': 2, 'f': 3), 'g', 'h', ('1': 'd', '2': 'e', '3': 'f')), all); // ("d": 1, "e": 2, "f": 3), ("1": "d", "2": "e", "3": "f")
25+
26+
// last
27+
// @debug map(('a', (test: 2, test3: 45), 'b', 'c', ('d': 1, 'e': 2, 'f': 3)), last); // ("d": 1, "e": 2, "f": 3)
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
// Functions.
2+
@use 'get.type.function' as get-type;
3+
4+
// Completed
5+
// The `get.number()` function returns any, first or last occurring number, or all occurred numbers.
6+
// @param `$list` A list from which occurring numbers are retrieved.
7+
// @param `$occurrence` All, any, first or last occurrence of the retrieved numbers from `$list`.
8+
// @returns The returned value is occurred any, first, last number or all occurred numbers retrieved from `$list` or `null`.
9+
@function number($list, $occurrence: first) {
10+
@return get-type.type($list, $occurrence, number);
11+
}
12+
13+
// Examples.
14+
// occurrence: first by default
15+
// @debug number(('a', 'b', 'c', 2)); // `2`
16+
// @debug number(('a', 'b', 'c', 2), first); // `2`
17+
18+
// occurrence: any
19+
// @debug number(('a', 'b', 'c', (3))); // `3`
20+
// @debug number(('a', 'b', 'c', (4, ))); // `null`
21+
// @debug number(('a', 'b', 'c', ('5', (6,)))); // `null`
22+
23+
// occurrence: last
24+
// @debug number(('a', 5, 'b', 2, 'c', (4, ))); // 5
25+
26+
// occurrence: all
27+
// @debug number((3, 'a', 0, 'b', 1, 'c', 2), all); // 3, 0, 1, 2
28+
// @debug number(('a', 5, 'b', 2, 'c', (4, )), all); // 5, 2
29+
// @debug number(('a', 3, 'b', 2, 4, 2, 7, 2, 'b', 'c', 2), all); // 3, 2, 4, 2, 7, 2, 2
30+
// @debug number(('a', 3, 'b', 2, 4, 8, 2, 7, 2, 'b', 'c', 2), all); // 3, 2, 4, 8, 2, 7, 2, 2
31+
// @debug number(('a', 3, 'b', 2, 4, 8, 2, 7, 2, 'b', 'c', 2), first); // 3
32+
// @debug number(('a', 3, 'b', 2, 4, 8, 2, 7, 2, 'b', 'c', 2), last); // 2

src/lib/get/_get.spec.scss

Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
@use '../get/get.bool.function' as bool;
2+
@use '../get/get.list.function' as list;
3+
@use '../get/get.map.function' as map;
4+
@use '../get/get.number.function' as number;
5+
@use '../get/get.string.function' as string;
6+
@use '../get/get.type.function' as type;
7+
8+
//
9+
@use '../get';
10+
11+
// Examples `bool`.
12+
@debug get.bool(('a', 'b', false, 'c', 2)); // `false`
13+
@debug get.bool(('a', 'b', true, 'c', 2)); // `true`
14+
@debug get.bool(('a', 'b', 'c', (true, '5', (6,)))); // `null`
15+
@debug get.bool(('a', 'b', true, 'c', 2), any); // `true`
16+
@debug get.bool(('a', 'b', true, 'c', 2), first); // `true`
17+
@debug get.bool(('a', 'b', false, 'c', 2, true), any); // `false`
18+
@debug get.bool(('a', 'b', false, 'c', 2, true), last); // `true`
19+
@debug get.bool(('a', 'b', false, 'c', 2, true), first); // `false`
20+
21+
22+
// Examples `list`.
23+
@debug get.list(('a', 'b', 'c')); // `null`
24+
25+
// fist/any
26+
@debug get.list(('a', 'b', 'c', ('d', 'e', 'f'))); // ("d", "e", "f")
27+
@debug get.list(('a', 'b', 'c', ('d', 'e', 'f'), 132, ('a', 'd', 'f')), any); // "d", "e", "f"
28+
29+
// all
30+
@debug get.list(('a', 'b', 'c', ('d', 'e', 'f'), 'g', 'h', (1, 2, 3)), all); // ("d", "e", "f"), (1, 2, 3)
31+
@debug get.list(('a', 'b', 'c', ('d', (1, 2, 3, (4, 5, 6)), 'e', 'f')), all); // ("d", (1, 2, 3, (4, 5, 6)), "e", "f")
32+
33+
// last
34+
@debug get.list(('a', 'b', 'c', ('d', 'e', 'f'), 132, ('a', 'd', 'f')), last); // "a", "d", "f"
35+
36+
37+
// Examples `map`.
38+
// none
39+
@debug get.map(('a', 'b', 'c')); // null
40+
41+
// first
42+
@debug get.map(('a', 'b', 'c', ('d': 1, 'e': 2, 'f': 3))); // ("d": 1, "e": 2, "f": 3)
43+
@debug get.map(('a', 'b', 'c', (('d': 1, 'e': 2, 'f': 3), 'g'), 'h')); // null
44+
@debug get.map(('a', ('d': 1, 'e': 2, 'f': 3), 'b', 'c', ('d', ('g': 1, 'h': 2, 'i': 3), (1, 2, 3, (4, 5, 6)), 'e', 'f'))); // ("d": 1, "e": 2, "f": 3)
45+
46+
// all
47+
@debug get.map(('a', (test: 2, test3: 45), 'b', 'c', ('d': 1, 'e': 2, 'f': 3)), all); // (test: 2, test3: 45), ("d": 1, "e": 2, "f": 3)
48+
@debug get.map(('a', 'b', 'c', ('d': 1, 'e': 2, 'f': 3), 'g', 'h', ('1': 'd', '2': 'e', '3': 'f')), all); // ("d": 1, "e": 2, "f": 3), ("1": "d", "2": "e", "3": "f")
49+
50+
// last
51+
@debug get.map(('a', (test: 2, test3: 45), 'b', 'c', ('d': 1, 'e': 2, 'f': 3)), last); // ("d": 1, "e": 2, "f": 3)
52+
53+
54+
55+
// Examples `number`.
56+
// occurrence: first by default
57+
@debug get.number(('a', 'b', 'c', 2)); // `2`
58+
@debug get.number(('a', 'b', 'c', 2), first); // `2`
59+
60+
// occurrence: any
61+
@debug get.number(('a', 'b', 'c', (3))); // `3`
62+
@debug get.number(('a', 'b', 'c', (4, ))); // `null`
63+
@debug get.number(('a', 'b', 'c', ('5', (6,)))); // `null`
64+
65+
// occurrence: last
66+
@debug get.number(('a', 5, 'b', 2, 'c', (4, ))); // 5
67+
68+
// occurrence: all
69+
@debug get.number((3, 'a', 0, 'b', 1, 'c', 2), all); // 3, 0, 1, 2
70+
@debug get.number(('a', 5, 'b', 2, 'c', (4, )), all); // 5, 2
71+
@debug get.number(('a', 3, 'b', 2, 4, 2, 7, 2, 'b', 'c', 2), all); // 3, 2, 4, 2, 7, 2, 2
72+
@debug get.number(('a', 3, 'b', 2, 4, 8, 2, 7, 2, 'b', 'c', 2), all); // 3, 2, 4, 8, 2, 7, 2, 2
73+
@debug get.number(('a', 3, 'b', 2, 4, 8, 2, 7, 2, 'b', 'c', 2), first); // 3
74+
@debug get.number(('a', 3, 'b', 2, 4, 8, 2, 7, 2, 'b', 'c', 2), last); // 2
75+
76+
77+
78+
// Examples `string`.
79+
// occurrence: first by default
80+
@debug get.string((1, 'a', 'b', 'c', 2)); // a
81+
@debug get.string((1, 2, 3, 'a', 'b', 'c', 2), first); // a
82+
@debug get.string(('a', 3, 'b', 2, 4, 8, 2, 7, 2, 'b', 'c', 2), first); // a
83+
84+
// occurrence: any
85+
@debug get.string((1, 2, 3, 'a', 'b', 'c', (3)), any); // a
86+
87+
// occurrence: last
88+
@debug get.string(('a', 5, 'b', 2, 'c', (4, )), last); // c
89+
@debug get.string(('a', 3, 'b', 2, 4, 8, 2, 7, 2, 'b', 'c', 2), last); // c
90+
91+
// occurrence: all
92+
@debug get.string((3, 'a', 0, 'b', 1, 'c', 2), all); // "a", "b", "c"
93+
@debug get.string(('a', 'the', 5, 'sun', 'b', 2, 'c', (4, )), all); // "a", "the", "sun", "b", "c"
94+
@debug get.string(('a', 3, 'b', 2, 4, 8, 2, 7, 2, 'b', 'c', 2), all); // "a", "b", "b", "c"
95+
@debug get.string(('a', 3, 'b', 2, 'c', 4, 2, 7, 2, 'b', 'c', 2), all); // "a", "b", "c", "b", "c"
96+
@debug get.string(('a', 3, 'b', 2, 'c', 4, 'd', 8, 'e', 2, 7, 2, 'b', 'c', 2), all); // "a", "b", "c", "d", "e", "b", "c"
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
// Functions.
2+
@use 'get.type.function' as get-type;
3+
4+
// Completed
5+
// The `get.string()` function returns any, first, last string or all occurring strings, or all occurred strings.
6+
// @param `$list` A list from which the occurring strings are retrieved.
7+
// @param `$occurrence` All, any, first or last occurrence of retrieved strings from `$list`.
8+
// @returns The returned value is occurred any, first, last string or all occurred strings retrieved from `$list` or `null`.
9+
@function string($list, $occurrence: first) {
10+
@return get-type.type($list, $occurrence, string);
11+
}
12+
13+
// Examples.
14+
// occurrence: first by default
15+
// @debug string((1, 'a', 'b', 'c', 2)); // a
16+
// @debug string((1, 2, 3, 'a', 'b', 'c', 2), first); // a
17+
// @debug string(('a', 3, 'b', 2, 4, 8, 2, 7, 2, 'b', 'c', 2), first); // a
18+
19+
// occurrence: any
20+
// @debug string((1, 2, 3, 'a', 'b', 'c', (3)), any); // a
21+
22+
// occurrence: last
23+
// @debug string(('a', 5, 'b', 2, 'c', (4, )), last); // c
24+
// @debug string(('a', 3, 'b', 2, 4, 8, 2, 7, 2, 'b', 'c', 2), last); // c
25+
26+
// occurrence: all
27+
// @debug string((3, 'a', 0, 'b', 1, 'c', 2), all); // "a", "b", "c"
28+
// @debug string(('a', 'the', 5, 'sun', 'b', 2, 'c', (4, )), all); // "a", "the", "sun", "b", "c"
29+
// @debug string(('a', 3, 'b', 2, 4, 8, 2, 7, 2, 'b', 'c', 2), all); // "a", "b", "b", "c"
30+
// @debug string(('a', 3, 'b', 2, 'c', 4, 2, 7, 2, 'b', 'c', 2), all); // "a", "b", "c", "b", "c"
31+
// @debug string(('a', 3, 'b', 2, 'c', 4, 'd', 8, 'e', 2, 7, 2, 'b', 'c', 2), all); // "a", "b", "c", "d", "e", "b", "c"
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
// Sass.
2+
@use 'sass:list';
3+
@use 'sass:map';
4+
5+
// Completed
6+
// The `list.get-type()` or `get.type()` function returns the list with any, first, last or all occurred elements of `$type` and optional `$types`.
7+
// @param `$list` The list from which occurred elements of required `$type` and optional `$types` are taken.
8+
// @param `$occurrence` Any, first, last or all occurrence of `$type` and optional `$types` in `$list`.
9+
// @param `$type` Required type to retrieve from `$list`.
10+
// @arbitrary `$types...` Additional types to retrieve from `$list`.
11+
// @returns The returned value is occurred any, first, last or all element or list of the elements of required `$type` and optional `$types`.
12+
@function type($list, $occurrence: any, $type, $types...) {
13+
@if not list.index(arglist list, type-of($list)) {
14+
@error "$list: #{$list} is not arglist or list";
15+
}
16+
$result: ();
17+
@each $element in $list {
18+
$result: list.index($type, type-of($element))
19+
and list.append($result, $element, comma)
20+
or $result;
21+
}
22+
$first: list.length($result) > 0 and list.nth($result, 1) or null;
23+
$last: list.length($result) > 0 and list.nth($result, list.length($result)) or null;
24+
@return map.get((
25+
any: $first,
26+
first: $first,
27+
last: $last,
28+
all: $result,
29+
), $occurrence);
30+
}
31+
32+
// Examples.
33+
// @debug type(a 1 true, any, string); // a
34+
// @debug type(a 1 true, any, number); // 1
35+
// @debug type(a 1 true, any, bool); // true

src/lib/get/_get.type.spec.scss

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
// Functions.
2+
@use 'get.type.function' as *;
3+
4+
// Examples.
5+
// any or first
6+
// @debug type(('a',), any, string); // ("a",)
7+
// @debug type(('a',), any, string, number); // "a", null
8+
// @debug type(('a', 'b', 'c'), any, string); // ("a",)
9+
// @debug type(('a', 12, 'b', 54, 'c', 45), any, number, string); // 12, "a"
10+
// @debug type(('a', null, 'b', 'c'), any, null, string); // null, "a"
11+
12+
// all
13+
// @debug type(('a',), all, string, number); // ("a",), null
14+
// @debug type(('a', 'b', 'c'), all, string); // (("a", "b", "c"),)
15+
// @debug type(('a', 12, 'b', 54, 'c', 45), all, number, string); // (12, 54, 45), ("a", "b", "c")
16+
// @debug type(('a', 12, 'b', 54, 'c', 45), all, number, string, bool); // (12, 54, 45), ("a", "b", "c"), null
17+
18+
// last
19+
// @debug type(('a', null, 'b', 'c'), last, string); // ("c",)
20+
// @debug type(('a', null, 2, 'b', 4, 'c', 132), last, number); // (132,)
21+
// @debug type(('a', null, 2, 'b', 4, 'c', 132, 'bold king', 27), last, number); // (27,)

src/lib/get/_index.scss

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
@forward 'get.bool.function';
2+
@forward 'get.list.function';
3+
@forward 'get.map.function';
4+
@forward 'get.number.function';
5+
@forward 'get.string.function';
6+
@forward 'get.type.function';

0 commit comments

Comments
 (0)