// Sass.
@use 'sass:list';
@use 'sass:map';
// Functions.
@use '../../list/list.append.function' as *;
@use 'pick.key-substring.function' as *;
@use 'pick.type.function' as *;
// Modules.
@use '../../string';
// Status: DONE
// The `pick.pattern()` function returns a copy of `$map` filtered by using pattern.
// @param `$map` A map from which properties are picked.
// @param `$pattern` A pattern to pick property from `$map`.
// @arbitrary `$patterns...` Additional patterns to pick properties from `$map`.
// @return The return value is a copy of `$map` with properties associated with patterns.
@function pattern($map, $pattern, $patterns...) {
$result: ();
@each $pattern in append((), $pattern, comma, $patterns...) {
@if type-of($pattern) == string {
@if list.index(string.index($pattern, '^', '$'), 1) {
$position: string.slice($pattern, 1, 1);
$pattern: string.replace($pattern, first, $position, '');
$result: map.deep-merge($result, key-substring($map, ($position: $pattern)));
} @else if string.index($pattern, '*') {
$result: map.deep-merge($result, key-substring($map, string.replace($pattern, first, '*', '')));
} @else if string.index($pattern, 'value:') or string.index($pattern, 'key:') {
$result: map.deep-merge($result, type($map, $pattern));
}
}
}
@return $result;
}
// Alias function name.
@function by-pattern($map, $pattern, $patterns...) {
@return pattern($map, $pattern, $patterns);
}
// Examples.
// $-theme: (
// dark: (
// 'dark.palette': (),
// basic large: (
// '': border,
// dark: border dark,
// light: border light
// ),
// ),
// basic: (
// '9971test': 1,
// '2test': 2,
// '3test': 3,
// '4test': 4,
// '5test': 5,
// 'test9971': 1,
// 'test2': 2,
// 'test3': 3,
// 'test4': 4,
// 'test5': 5,
// 'default.palette': (),
// 'palette.gray': (
// 'gray': 'gray' color,
// 'gray' dark: 'gray' dark,
// 'gray' light: 'gray' light
// ),
// '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
// ),
// basic large: (
// '': border,
// dark: border dark,
// light: border light
// ),
// extended small: (
// '': primary,
// dark: primary dark,
// light: primary light
// ),
// [primary]: (
// primary dark: primary dark,
// ),
// (key1, key2): (
// primary: primary1
// ),
// key1: primary,
// // calculation
// calc(400px + 10%): calculation,
// calculation: calc(400px + 10%),
// // bool
// false: bool,
// bool: false,
// // color
// #fff: color,
// color: #fff,
// // function
// get-function(append): function,
// function: get-function(append),
// // map
// (a: 1, b: 2): map,
// map: (a: 1, b: 2),
// // null
// 'null': null,
// null: 'null',
// // number
// 15: number,
// number: 15,
// // string
// firstname: string,
// string: firstname,
// )
// );
// pattern: any
// @debug pattern(map.get($-theme, basic), '*palette');
// @debug pattern(map.get($-theme, basic), '*.palette');
// @debug pattern(map.get($-theme, basic), 'palette.*');
// pattern: key and/or value type
// @debug pattern(map.get($-theme, basic), 'key:list', 'value:string,');
// @debug pattern(map.get($-theme, basic), 'value:string,list');
// @debug pattern(map.get($-theme, basic), 'key:string,list');
// @debug pattern(map.get($-theme, basic), 'key,value:list,string'); // TODO
// @debug pattern(map.get($-theme, basic), '^test');
// @debug pattern(map.get($-theme, basic), '$test');