map.deep-merge-key()

The map.deep-merge-key() function merges $map with retrieved values from $keys of this $map. The properties under $keys are removed.

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

// Functions.
@use 'map.get.function' as *;

// The `map.deep-merge-key()` function.
@function deep-merge-key($map, $keys...) {
  @each $key in $keys {
    $map: map.deep-remove(map.deep-merge($map, get($map, $key, ())), $key...);
  }
  @return $map;
}
https://github.com/angular-package/sass/blob/main/map/_map.deep-merge-key.function.scss
// Sass.
@use 'sass:map';

// Functions.
@use '../list/list.append.function' as *;
@use 'map.get.function' as *;

// Status: DONE
// The `map.deep-merge-key()` function merges `$map` with retrieved values from `$keys` of this `$map`.
// The properties under `$keys` are removed.
// @param `$map` A map to merge with retrieved value from `$keys` of this map.
// @param `$key` Required key to merge with `$map`.
// @arbitrary `$keys...` The keys to get the values to merge with `$map`.
// @return The return value is a map merged with the values retrieved from `$keys`.
@function deep-merge-key($map, $key, $keys...) {
  @each $key in append((), $key, comma, $keys...) {
    $map: map.deep-remove(map.deep-merge($map, get($map, $key, ())), $key...);
  }
  @return $map;
}

// Examples.
// $example: (
//   "name": first name,
//   "age": 25,
//   "Helvetica": (
//     "weights": (
//       "regular": 400,
//       "medium": 500,
//       "bold": 700
//     ),
//     "size": (
//       "regular": small,
//       "medium": big,
//       "high": extra big
//     )
//   )
// );

// key of string
// @debug deep-merge-key((a: 1, b: 2, key: (c: 3, d: 5)), key); // (a: 1, b: 2, c: 3, d: 5)

// nested key
// @debug deep-merge-key($example, ("Helvetica", "size")); // ("name": first name, "age": 25, "Helvetica": ("weights": ("regular": 400, "medium": 500, "bold": 700)), "regular": small, "medium": big, "high": extra big)
// @debug deep-merge-key($example, ("Helvetica", "weights")); // ("name": first name, "age": 25, "Helvetica": ("size": ("regular": small, "medium": big, "high": extra big)), "regular": 400, "medium": 500, "bold": 700)
// @debug deep-merge-key($example, ("Helvetica", "weights"), ("Helvetica", "size")); // ("name": first name, "age": 25, "Helvetica": (), "regular": small, "medium": big, "bold": 700, "high": extra big)
// @debug deep-merge-key($example, ("Helvetica", "size"), ("Helvetica", "weights")); // ("name": first name, "age": 25, "Helvetica": (), "regular": 400, "medium": 500, "high": extra big, "bold": 700)

// key of comma separated list
// @debug deep-merge-key((a: 1, b: 2, (key, key1): (c: 3, d: 5)), ((key, key1), )); // (a: 1, b: 2, c: 3, d: 5)

// key of space separated list
// @debug deep-merge-key((a: 1, b: 2, key key1: (c: 3, d: 5)), (key key1, )); // (a: 1, b: 2, c: 3, d: 5)

// key
// @debug deep-merge-key((a: 1, b: 2, key key1: (c: 3, d: 5)), (key key1, )); // (a: 1, b: 2, c: 3, d: 5)

Parameters

$map

A map to merge with retrieved values from $keys of this map.

$keys...

The keys to get the values to merge with $map.

Return

The return value is a map merged with the values retrieved from $keys.

Examples

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

// Examples.
$example: (
  "name": first name,
  "age": 25,
  "Helvetica": (
    "weights": (
      "regular": 400,
      "medium": 500,
      "bold": 700
    ),
    "size": (
      "regular": small,
      "medium": big,
      "high": extra big
    )
  )
);

// key of string
@debug map.deep-merge-key((a: 1, b: 2, key: (c: 3, d: 5)), key); // (a: 1, b: 2, c: 3, d: 5)

// nested key
@debug map.deep-merge-key($example, ("Helvetica", "size")); // ("name": first name, "age": 25, "Helvetica": ("weights": ("regular": 400, "medium": 500, "bold": 700)), "regular": small, "medium": big, "high": extra big)
@debug map.deep-merge-key($example, ("Helvetica", "weights")); // ("name": first name, "age": 25, "Helvetica": ("size": ("regular": small, "medium": big, "high": extra big)), "regular": 400, "medium": 500, "bold": 700)
@debug map.deep-merge-key($example, ("Helvetica", "weights"), ("Helvetica", "size")); // ("name": first name, "age": 25, "Helvetica": (), "regular": small, "medium": big, "bold": 700, "high": extra big)
@debug map.deep-merge-key($example, ("Helvetica", "size"), ("Helvetica", "weights")); // ("name": first name, "age": 25, "Helvetica": (), "regular": 400, "medium": 500, "high": extra big, "bold": 700)

// key of comma separated list
@debug map.deep-merge-key((a: 1, b: 2, (key, key1): (c: 3, d: 5)), ((key, key1), )); // (a: 1, b: 2, c: 3, d: 5)

// key of space separated list
@debug map.deep-merge-key((a: 1, b: 2, key key1: (c: 3, d: 5)), (key key1, )); // (a: 1, b: 2, c: 3, d: 5)

// key
@debug map.deep-merge-key((a: 1, b: 2, key key1: (c: 3, d: 5)), (key key1, )); // (a: 1, b: 2, c: 3, d: 5)

Last updated

Was this helpful?