// Sass.
@use 'sass:list';
@use 'sass:map';
@use 'sass:meta';
// Status: DONE
// The `map.set()` function sets `$allowed` `$value` under `$key` in `$map`.
// @param `$map` A map to set `$value` at `$key`.
// @param `$key` A key under which `$value` is set.
// @param `$value` The value to set in `$map` under `key`.
// @arbitrary `$allowed...` Allowed value types and/or values to set in `$map`.
// @returns The return value is updated `$map` with `$value` at `$key`.
@function set($map, $key, $value, $allowed...) {
@return if(
if(
list.length($allowed) > 0,
if(list.index($allowed, meta.type-of($value)) or list.index($allowed, $value), true, false),
true
),
map.set(
$map,
list.append(
if(meta.type-of($key) == list and list.separator($key) == comma, $key, ($key,)),
$value,
comma
)...
),
$map
);
}
// Examples.
// Equivalents.
// @debug map.set((), (test, 1), 2222); // ((test, 1): 2222)
// @debug set((), ((test, 1),), 2222); // ((test, 1): 2222)
// @debug map.set((), test 1, (test, 2), 2222); // (test 1: ((test, 2): 2222))
// @debug set((), (test 1, (test, 2)), 2222); // (test 1: ((test, 2): 2222))
// single type
// @debug set((a: 1, b: 2), test string, 27, string ); // (a: 1, b: 2)
// @debug set((a: 1, b: 2), test number, 27, number); // (a: 1, b: 2, test: (number: 27))
// @debug set((a: 1, b: 2), test map, (map: 22,), map); // (a: 1, b: 2, test: (map: (map: 22)))
// @debug set((a: 1, b: 2), test list, (22, 44), list); // (a: 1, b: 2, test: (list: (22, 44)))
// @debug set((a: 1, b: 2), test color, #ffffff, color); // (a: 1, b: 2, test: (color: #ffffff))
// @debug set((a: 1, b: 2), test null, null, null); // (a: 1, b: 2, test: (null: null))
// multiple types
// @debug set((a: 1, b: 2), test number, 27, number, string, bool, list, map); // (a: 1, b: 2, test: (number: 27))
// @debug set((a: 1, b: 2), test string, twenty seven, number, string, bool, list, map); // (a: 1, b: 2, test: (string: twenty seven))
// @debug set((a: 1, b: 2), test bool, false, number, string, bool, list, map); // (a: 1, b: 2, test: (bool: false))
// @debug set((a: 1, b: 2), test null, null, number, null, string, bool, list, map); // (a: 1, b: 2, test: (null: null))