map.set()

The map.set() function sets $allowed $value under $key.

Original sass map.set() function modified, by adding $allowed... arbitrary argument to check if $value is allowed to be set.

// Sass.
@use 'sass:list';
@use 'sass:map';
@use 'sass:meta';

// The `map.set()` function.
@function update($map, $key-value, $allowed...) {
  @each $key, $value in $key-value {
    $map: set($map, $key, $value, $allowed...);
  }
  @return $map;
}
https://github.com/angular-package/sass/blob/main/map/_map.set.function.scss
// 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))

Parameters

$map

A map to set $value at $key.

$key

A key under which $value is set.

$value

The value to set in $map under $key.

$allowed...

Allowed value types and/or values to set in $map.

Return

The return value is updated $map with $value at $key.

Examples

// Use.
@use '@angular-package/sass/map';

// Examples.
@debug map.set((), ((test, 1),), 2222); // ((test, 1): 2222)
@debug map.set((), (test 1, (test, 2)), 2222); // (test 1: ((test, 2): 2222))

// single type
@debug map.set((a: 1, b: 2), test string, 27, string ); // (a: 1, b: 2)
@debug map.set((a: 1, b: 2), test number, 27, number); // (a: 1, b: 2, test: (number: 27))
@debug map.set((a: 1, b: 2), test map, (map: 22,), map); // (a: 1, b: 2, test: (map: (map: 22)))
@debug map.set((a: 1, b: 2), test list, (22, 44), list); // (a: 1, b: 2, test: (list: (22, 44)))
@debug map.set((a: 1, b: 2), test color, #ffffff, color); // (a: 1, b: 2, test: (color: #ffffff))
@debug map.set((a: 1, b: 2), test null, null, null); // (a: 1, b: 2, test: (null: null))

// multiple types
@debug map.set((a: 1, b: 2), test number, 27, number, string, bool, list, map); // (a: 1, b: 2, test: (number: 27))
@debug map.set((a: 1, b: 2), test string, twenty seven, number, string, bool, list, map); // (a: 1, b: 2, test: (string: twenty seven))
@debug map.set((a: 1, b: 2), test bool, false, number, string, bool, list, map); // (a: 1, b: 2, test: (bool: false))
@debug map.set((a: 1, b: 2), test null, null, number, null, string, bool, list, map); // (a: 1, b: 2, test: (null: null))

Last updated

Was this helpful?