Skip to content

Commit

Permalink
refactor(map.pick-key-substring()): add '^' start and '$' end `string…
Browse files Browse the repository at this point in the history
…` pattern to pick values.
  • Loading branch information
sciborrudnicki committed Nov 7, 2023
1 parent 0d90f5d commit a603ea4
Showing 1 changed file with 51 additions and 3 deletions.
54 changes: 51 additions & 3 deletions map/pick/_pick.key-substring.function.scss
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
// Sass.
@use 'sass:list';
@use 'sass:map';

// Modules.
@use '../../meta';
@use '../../string';

// Functions.
Expand All @@ -16,10 +18,34 @@
@function key-substring($map, $substring, $substrings...) {
$result: ();
@each $substring in append((), $substring, comma, $substrings...) {
@if type-of($substring) == string {
@if type-of($substring) == string or type-of($substring) == map {
@each $key in map.keys($map) {
@if type-of($key) == string and string.index($key, $substring) {
$result: map.deep-merge($result, ($key: map.get($map, $key)));
$key: if(meta.of-type(color number string, $key), #{$key}, $key);
@if type-of($key) == string {
@if type-of($substring) == map {
$position: map.keys($substring); // Get substring position.
$-substring: list.nth(map.values($substring), 1);

// Substring start.
@if list.index($position, start) or list.index($position, '^') {
@if string.index($key, $-substring) == 1 {
$result: map.deep-merge($result, ($key: map.get($map, $key)));
}

// Substring end.
} @else if list.index($position, end) or list.index($position, '$') {
@if string.index($key, $-substring) {
$true: (string.index($key, $-substring) - 1) + string.length($-substring) == string.length($key);
@if $true {
$result: map.deep-merge($result, ($key: map.get($map, $key)));
}
}
}
} @else {
@if string.index($key, $substring) {
$result: map.deep-merge($result, ($key: map.get($map, $key)));
}
}
}
}
}
Expand All @@ -39,6 +65,18 @@
// ),
// ),
// basic: (
// '9971test': 1,
// '2test': 2,
// '3test': 3,
// '4test': 4,
// '5test': 5,

// 'test9971': 1,
// 'test2': 2,
// 'test3': 3,
// 'test4': 4,
// 'test5': 5,

// 'default.palette': (),

// 'gray.palette': (
Expand Down Expand Up @@ -109,5 +147,15 @@
// )
// );

// FEATURE:
// @debug key-substring(map.get($-theme, basic), '.palette'); // ("default.palette": (), "gray.palette": ("gray": "gray" color, "gray" dark: "gray" dark, "gray" light: "gray" light), "bg.palette": (bg: bg color, bg dark: bg dark, bg light: bg light))
// @debug key-substring(map.get($-theme, basic), 'key1'); // (key1: primary)

// FEATURE: key as map where (position: name)
// @debug key-substring(map.get($-theme, basic), (start: 'test'));
// @debug key-substring(map.get($-theme, basic), (end: 'test'));
// @debug key-substring(map.get($-theme, basic), (start: 'test'), (end: 'test'));

// @debug key-substring(map.get($-theme, basic), ('^': 'test'));
// @debug key-substring(map.get($-theme, basic), ('$': 'test'));
// @debug key-substring(map.get($-theme, basic), ('^': 'test'), ('$': 'test'));

0 comments on commit a603ea4

Please sign in to comment.