-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
_list.to-map.function.scss
138 lines (118 loc) · 5.59 KB
/
_list.to-map.function.scss
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
// Sass.
@use 'sass:list';
// - list.
@use 'list.from.function' as _list;
// - map.
@use '../map';
// - string.
@use '../string';
// Status: REVIEW: Split into small functions.
// The `list.to-map()` function returns map built from `$list` of specified `$keys` in respective order.
// @param `$list` The values of list type to pass into `$keys` in respective order.
// @param `$keys...` Keys of the values in respective order.
// @returns The return value is a map built from `$list` of specified `$keys` in respective order.
@function to-map($list, $keys...) {
$map: ();
@if list.length($list) > 0 {
$index: 1;
@each $key in $keys {
// FEATURE: Key name relative on the $list length.
$length-key: null;
@if type-of($key) == map {
@each $length, $value in $key {
@if type-of($length) == number and list.length($list) >= $length {
$length-key: $value;
}
}
}
@if $length-key {
$key: $length-key;
}
@if list.length($list) >= $index {
$value: list.nth($list, $index);
@if type-of($key) == list {
$map: -map-set($map, $key, $value);
} @else {
@each $name, $type in $key {
// TODO: support list.
@if $type == map {
@if type-of($value) == map {
@each $map-name, $map-type in $name {
@each $map-key in $map-name {
$map: -map-set($map, $map-key, map.get($value, $map-key), $map-type...);
}
}
}
} @else {
@if type-of($key) == map {
$map: -map-set($map, $name, map.get($value, $name, $value), $type...);
} @else if type-of($name) == list or type-of($name) == string {
$map: -map-set($map, $name, $value);
}
}
}
}
}
// FEATURE: Arbitrary parameter with the three dots 'name...' indicates to set map rest of the values under the `name` key.
@if list.length($keys) == $index and list.length($list) >= $index {
@each $k in $key {
@if type-of($k) == string and string.index($k, '...') {
$key: list.set-nth($key, list.length($key), string.replace($k, first, '...', ''));
@if type-of(list.nth($list, $index)) == map {
$list: map.get(list.nth($list, $index), string.replace($k, first, '...', ''), $list);
} @else {
$list: _list.from($list, $index);
}
$map: map.set($map, $key, $list);
}
}
}
$index: $index + 1;
}
}
@return $map;
}
@function -map-set($map, $key, $value, $type...) {
@return if(not string.index(string.join('', $key...), '...'), map.set($map, $key, $value, $type...), $map);
}
// Examples.
// string key
// @debug to-map(a b c, key1, key2, key3); // (key1: a, key2: b, key3: c)
// string key as list
// @debug to-map(a b c, (key1, key1-2), key2, key3); // (key1: (key1-2: a), key2: b, key3: c) // TODO
// map to determine the type
// @debug to-map(a b c, (key1: string), (key2: number), key3); // (key1: a, key3: c)
// pick from the map.
// @debug to-map((key1: a, key2: 2) b c, (key1: string, key2: number), (key2: number), key3); // (key1: a, key3: c)
// map to obtain key of the type
// @debug to-map(a '1' b 2 c 3 d 4 e 5 f 6 g 7 h i j k l m n o p r s t u w y z, key, (key-string: string, key-number: number)); // (key: a, key-string: "1")
// @debug to-map(a 1 b 2 c 3 d 4 e 5 f 6 g 7 h i j k l m n o p r s t u w y z, key, (key-string: string, key-number: number)); // (key: a, key-number: 1)
// @debug to-map(a b c d e f g h i j k l m n o p r s t u w y z, ('1', 2), ((1, '2'): (string, number)), '3'); // ("1": (2: a), 1: ("2": b), "3": c)
// query
// @debug to-map(nth (a b c d) any '==' 1 2 3, select, from, occurrence, operator, 'operand...'); // (select: nth, from: a b c d, occurrence: any, operator: "==", "operand": 1 2 3)
// @debug to-map(
// nth (a b c d) any '==' 1 2 3,
// (select: index nth quantity type), (from: list), (occurrence: any all first last), (operator: string), 'operand...'
// ); // (select: nth, from: a b c d, occurrence: any, operator: "==", "operand": 1 2 3)
// @debug to-map(
// nth (a b c d) any '==' 1 2 3,
// (select: index nth quantity type), (from, list), ((where, occurrence): any all first last), ((where, operator): string), (where, 'operand...')
// ); // (select: nth, from: (list: a b c d), where: (occurrence: any, operator: "==", "operand": 1 2 3))
// list with the map
// @debug to-map(
// (select: nth) (from: a b c d) (occurrence: any) (operator: '==') (operand: 1 2 3),
// (select: index nth quantity type), (from: list), (occurrence: any all first last), (operator: string), 'operand...'
// ); // (select: nth, from: a b c d, occurrence: any, operator: "==", "operand": 1 2 3)
// @debug to-map(
// nth (a b c d) any '==' (1 2 3) 5,
// (select: index nth quantity type),
// (from, list),
// ((where, occurrence): any all first last),
// ((where, operator): string),
// (operand: list),
// (limit: number list)
// ); // (select: nth, from: (list: a b c d), where: (occurrence: any, operator: "=="), operand: 1 2 3, limit: 5)
// (2: class value, 4: class property value-class value, 5: class property attribute value-class value)
// depending on the list length
// @debug to-map(one two three four, class, (2: value, 4: property), (4: value-class, 5: attribute), (4: value, 5: value-class), (5: value));
// @debug to-map(one two three four five six, class, (2: value, 4: property), (4: value-class, 5: attribute), (4: value, 5: value-class), value, variant);