Skip to content

Commit

Permalink
refactor(function/call): handle map type arguments with additional `$…
Browse files Browse the repository at this point in the history
…arguments` parameter and add spec.
  • Loading branch information
sciborrudnicki committed Aug 29, 2023
1 parent f125923 commit dc6d444
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 10 deletions.
14 changes: 14 additions & 0 deletions function/_function.spec.scss
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,19 @@ var.$var: map.merge(var.$var, (prefix: s, delimiter: '-'));
// @debug function.call-arglist(selector-nest, ((top, left), color)); // top color, left color
// @debug function.call-arglist(selector-nest, (border, (bottom, right))); // border bottom, border right

// arguments as map
// @debug function.call-arglist(--var-get, (unit 5, 1px, (args: (dictionary: (unit: u)))));
// @debug function.call-arglist(--var-get, (unit 5, 1px, (arguments: (dictionary: (unit: u)))));

// `$arguments` parameter
// @debug function.call-arglist(--var-get, (unit 5, 1px), $arguments: (true, 10px));

// pass dictionary in by `$arguments`
// @debug function.call-arglist(--var-get, (unit 5, 1px), $arguments: (dictionary: (unit: u)));




// `function.call-by-list()`
// function name + arguments
// @debug function.call-by-list(--var-get (unit 5, 15, rem));
Expand All @@ -109,6 +122,7 @@ var.$var: map.merge(var.$var, (prefix: s, delimiter: '-'));
// @debug function.call-by-list(function.insert((ut 1) (), $type-function: (list: var-get)));
// @debug function.call-by-list(function.insert(((ut 1),), $type-function: (list: var-get)));

// TODO: Check.
// `function.call-nested-list()`
// @debug function.call-nested-list((px solid (primary dark)), $function: var-get);
// @debug function.call-nested-list((ut 5) 5px (ut 1) 10px, $function: var-get);
Expand Down
37 changes: 29 additions & 8 deletions function/call/_call.arglist.function.scss
Original file line number Diff line number Diff line change
@@ -1,34 +1,55 @@
// Sass.
@use 'sass:list';
@use 'sass:map';
@use 'sass:meta';

// Functions.
@use '../../list/get';
@use '../../list/remove';
@use '../function.get.function';

// Status: DONE
// The `call.arglist()` function calls function of `$name` on `$arglist` with a separator comma where elements are arguments or space where the whole
// list is an argument.
// list is an argument. Arguments can be passed as list or map by `$arguments` parameter.
// @param `$name` Function name to call arguments from `$arglist` as comma-separated arguments or argument.
// @param `$arglist` List on which to call function of `$name`.
// @param `$prefix` Allowed prefixes of the function name.
// @param `$separator` Allowed separators of the function name.
// @param `$functions` Functions instead of global functions retrieved from `function.$functions`.
// @param `$function` Parameter to retrieve function from global `function.$functions`.
// @param `$function` Function name to retrieve from `$functions` if not `null` otherwise from global `function.$functions`.
// @param `$arguments` Additional arguments as `list` or `map` following `$arglist` to pass.
// @returns The returned value is the result of invoked function.
@function arglist(
$name,
$arglist,
$prefix: null,
$separator: null,
$functions: null,
$function: function.get($name, $prefix, $separator, $functions)
$function: function.get($name, $prefix, $separator, $functions),
$arguments: null
) {
@if $name {
@return if(
list.separator($arglist) == comma,
meta.call($function, $arglist...),
meta.call($function, $arglist)
);
@if list.separator($arglist) == comma {
@if $arguments {
$arglist: if(
meta.type-of($arguments) == map,
list.append($arglist, (arguments: $arguments), comma),
list.join($arglist, $arguments, comma)
);
}
@if get.map($arglist) {
// Map type arguments
@if map.has-key(get.map($arglist), arguments) or map.has-key(get.map($arglist), args) {
@return meta.call(
$function,
remove.map($arglist)...,
map.get(get.map($arglist), args) or map.get(get.map($arglist), arguments)...
);
}
}
@return meta.call($function, $arglist...);
}
@return meta.call($function, $arglist);
}
@return null;
}
13 changes: 11 additions & 2 deletions function/call/_call.by-list.function.scss
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,15 @@
// @param `$separator` Allowed separators to pick function name from nested lists of `$list`.
// @param `$functions` Functions instead of global functions retrieved from `function.$functions`.
// @param `$execute` Executes the function.
// @param `$arguments` Additional arguments as `list` or `map` to pass into `$list`.
// @returns The returned value is `$list` with nested lists invoked by function.
@function by-list(
$list,
$prefix: null,
$separator: null,
$functions: null,
$execute: true
$execute: true,
$arguments: null,
) {
@if type-of($list) == list {
$result: ();
Expand All @@ -53,7 +55,14 @@
@if $function-arguments {
$result: list.append(
$result,
call.arglist(nth($function-arguments, 1), nth($function-arguments, 2), $prefix, $separator, $functions),
call.arglist(
nth($function-arguments, 1),
nth($function-arguments, 2),
$prefix,
$separator,
$functions,
$arguments: $arguments
),
list.separator($list)
);
$list: remove($list, $prefix, $separator, $functions);
Expand Down

0 comments on commit dc6d444

Please sign in to comment.