Skip to content

Commit 0d90f5d

Browse files
feat(map.keys-pattern()): returns keys / nested keys with pattern where '*' is substring, '^' is start, and '$' is end of string, add patterns to variables.
1 parent 35e3547 commit 0d90f5d

File tree

3 files changed

+189
-2
lines changed

3 files changed

+189
-2
lines changed

map/_index.scss

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
@forward 'map.get.function';
66
@forward 'map.has-keys.function';
77
@forward 'map.key-replace.function';
8+
@forward 'map.keys-pattern.function';
9+
@forward 'map.keys.function';
810
@forward 'map.merge-allowed.function';
911
@forward 'map.pick.function';
1012
@forward 'map.remove-type.function';
@@ -14,4 +16,4 @@
1416
@forward 'map.update.function';
1517
@forward 'map.values.function';
1618
@forward 'pick' as pick-*;
17-
@forward 'sass:map' hide deep-merge, get, remove, set, values;
19+
@forward 'sass:map' hide deep-merge, get, keys, remove, set, values;

map/_map.keys-pattern.function.scss

Lines changed: 183 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,183 @@
1+
// Sass.
2+
@use 'sass:list';
3+
@use 'sass:map';
4+
5+
// Modules.
6+
@use 'pattern';
7+
@use 'pick';
8+
9+
// Functions.
10+
@use '../list/list.append.function' as *;
11+
@use '../list/list.join.function' as *;
12+
@use '../list/list.range.function' as *;
13+
@use '../list/remove/remove.nth.function' as remove;
14+
@use '../map/map.get.function' as *;
15+
@use '../map/map.has-keys.function' as *;
16+
17+
// Status: DONE
18+
// The `map.keys-pattern()` function retrieves keys of `$key-pattern` or/and `$keys-pattern` with pattern.
19+
// @param `$map` A map to retrieve `$key-pattern` or `$keys-pattern`.
20+
// @param `$key-pattern` Required key or nested key with pattern where '*' is substring, '^' is start, and '$' is end of `string`.
21+
// @arbitrary `$keys-pattern...` Additional keys or nested keys with pattern to retrieve from `$map`.
22+
// @returns The returned value is a list of keys with pattern.
23+
@function keys-pattern($map, $key-pattern, $keys-pattern...) {
24+
$keys-pattern: append((), $key-pattern, comma, $keys-pattern...);
25+
$result: ();
26+
@if list.length($keys-pattern) > 0 {
27+
@each $key in $keys-pattern {
28+
// Check whether pattern exists.
29+
$pattern-index: null;
30+
$i: 1;
31+
@each $k in $key {
32+
@if pattern.is($k) {
33+
$pattern-index: $i;
34+
}
35+
36+
$i: $i + 1;
37+
}
38+
39+
// If pattern exists retrieve key to `map.get()`.
40+
@if $pattern-index {
41+
$resolved-key: range($key, 1, $pattern-index - 1);
42+
@if $resolved-key {
43+
$resolved-keys: null;
44+
@if list.length($key) == 1 {
45+
$resolved-keys: map.keys(pick.pattern($map, $resolved-key));
46+
} @else {
47+
$resolved-keys: map.keys(pick.pattern(get($map, $resolved-key), list.nth($key, $pattern-index)));
48+
$i: 1;
49+
@each $k in $resolved-keys {
50+
$append: if(
51+
list.length($key) > $pattern-index,
52+
($resolved-key, $k, comma, range($key, $pattern-index + 1, list.length($key))),
53+
($resolved-key, $k, comma),
54+
);
55+
$resolved-keys: list.set-nth(
56+
$resolved-keys,
57+
$i,
58+
if(list.length($key) == 1, $k, append($append...))
59+
);
60+
$i: $i + 1;
61+
}
62+
}
63+
64+
// Map has keys.
65+
@if $resolved-keys {
66+
$result: join($result, $resolved-keys);
67+
}
68+
69+
// Check whether map has keys.
70+
@if list.length($key) > $pattern-index {
71+
$i: 1;
72+
$-result: ();
73+
@each $key in $result {
74+
@if has-keys($map, $key) {
75+
$-result: list.append($-result, $key, comma);
76+
}
77+
78+
$i: $i + 1;
79+
}
80+
81+
$result: $-result;
82+
}
83+
}
84+
}
85+
}
86+
}
87+
@return $result;
88+
}
89+
90+
// Examples.
91+
// $-map: (
92+
// key: (
93+
// key1: 1,
94+
// config: (
95+
// config1: 1,
96+
// setting1: 1,
97+
// config2: 2,
98+
// setting2: 2,
99+
// config3: (
100+
// pattern1: 1,
101+
// test1: (),
102+
// pattern2: 2,
103+
// test2: (),
104+
// pattern3: 3,
105+
// test3: (),
106+
// pattern4: 4,
107+
// test4: (),
108+
// pattern5: 5,
109+
// ),
110+
// setting3: 3,
111+
// config4: 4,
112+
// setting4: 4,
113+
// config5: 5,
114+
// setting5: 5,
115+
// ),
116+
// test1: 1,
117+
// 1test: 1,
118+
// key2: 2,
119+
// config3: (
120+
// display: true
121+
// ),
122+
// test2: 2,
123+
// 2test: 2,
124+
// key3: 3,
125+
// config4: (
126+
// display4: true
127+
// ),
128+
// test3: 3,
129+
// 3test: 3,
130+
// key4: 4,
131+
// config1: (
132+
// display1: true
133+
// ),
134+
// test4: (
135+
// settings1: (
136+
// platform1: 1,
137+
// platform2: 2,
138+
// ),
139+
// settings2: (
140+
// platform3: 1,
141+
// platform4: 2,
142+
// ),
143+
// ),
144+
// 4test: (
145+
// settings3: (
146+
// platform5: 5,
147+
// platform6: 6,
148+
// ),
149+
// settings4: (
150+
// platform7: 7,
151+
// platform8: 8,
152+
// ),
153+
// ),
154+
// key5: 5,
155+
// test5: 5,
156+
// 5test: 5,
157+
// config2: (
158+
// display2: true
159+
// ),
160+
// ),
161+
// list1: (),
162+
// list2: (),
163+
// a b c d e f: value,
164+
// a: (b: (c: (d: e))),
165+
// any: true,
166+
// all: false,
167+
// separator: auto,
168+
// bracketed: false,
169+
// method: join,
170+
// null: false
171+
// );
172+
173+
// @debug keys-pattern($-map, '*key');
174+
// @debug keys-pattern($-map, '*list');
175+
// @debug keys-pattern($-map, '*key', '*list');
176+
// @debug keys-pattern($-map, (key, config, '*config'), (key, '*test'));
177+
178+
// Key between
179+
// @debug keys-pattern($-map, (key, '*config', display)); // ((key, config3, display),)
180+
// @debug keys-pattern($-map, (key, '^test')); // (key, test1), (key, test2), (key, test3), (key, test4), (key, test5)
181+
// @debug keys-pattern($-map, (key, '$test')); // (key, 1test), (key, 2test), (key, 3test), (key, 4test), (key, 5test)
182+
183+
// @debug keys-pattern($-map, (key, '*config', display), (key, '^test', settings2)); // (key, config3, display), (key, test4, settings2)

map/pattern/_pattern.variables.scss

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,7 @@ $pattern: (
22
// "By default quantifiers like * and + are "greedy",
33
// meaning that they try to match as much of the string as possible."
44
any: '*', // Select all characters.
5-
field-type: ('value:', 'key:') // Select
5+
field-type: ('value:', 'key:'), // Select
6+
start: '^', // String start with
7+
end: '$', // String end with
68
);

0 commit comments

Comments
 (0)