map.key-replace()
Last updated
Was this helpful?
Last updated
Was this helpful?
The map.key-replace()
function returns the map with key replaced by .
// Sass.
@use 'sass:map';
// Functions.
@use '../string';
// The `map.key-replace()` function.
@function key-replace($map, $replace, $replacement) {
@if type-of($map) == map {
@each $key, $value in $map {
$map: map.remove($map, $key);
@if type-of($key) ==
string and
type-of($replace) ==
string and
type-of($replacement) ==
string
{
@if string.index($key, $replace) {
$key: string.replace($key, first, $replace, $replacement);
}
} @else if $key == $replace {
$key: $replacement;
}
$map: map.merge(
$map,
(
$key: $value,
)
);
}
}
@return $map;
}
$map
$replace
$replacement
// Use.
@use '@angular-package/sass/map';
// Examples.
$-map: (
key replace: (map: 1),
key-replace: (map: 2),
);
// replace string key
@debug map.key-replace($-map, key-replace, new-key); // (key replace: (map: 1), new-key: (map: 2))
@debug map.key-replace(('bold king': 1, b: 2, c: 3), 'bold', 'hairy'); // ("hairy king": 1, b: 2, c: 3)
@debug map.key-replace(('bold king': 1, b: 2, c: 3), 'o', 'ooo'); // ("booold king": 1, b: 2, c: 3)
@debug map.key-replace((a: 1, b: 2, c: 3), 'a', 'd'); // (d: 1, b: 2, c: 3)
// replace string key with a list key
@debug map.key-replace($-map, key-replace, new key); // (key replace: (map: 1), new key: (map: 2))
// replace list key
@debug map.key-replace($-map, key replace, 'd'); // (d: 1, b: 2, c: 3)
A map in which a key is replaced with .
A key, or if string
substring to replace with in .
The value to replace a key in .
The return value is a with replaced key by .
// Sass.
@use 'sass:map';
// Functions.
@use '../string';
// Status: TODO: Check substring.
// The `map.key-replace()` function returns the map with key `$replace` replaced by `$replacement`.
// @param `$map` A map in which a `$replace` key is replaced with a `$replacement`.
// @param `$replace` A key, or if `string` substring to replace with `$replacement` in `$map`.
// @param `$replacement` The value to replace a `$replace` key in `$map`.
// @return The return value is a `$map` with replaced key `$replace` by `$replacement`.
@function key-replace($map, $replace, $replacement) {
@if type-of($map) == map {
@each $key, $value in $map {
$merge: false;
@if (type-of($key) == string and type-of($replace) == string and type-of($replacement) == string) or $key == $replace {
$map: map.remove($map, $key);
$merge: true;
}
@if type-of($key) == string and type-of($replace) == string and type-of($replacement) == string {
@if string.index($key, $replace) {
$key: string.replace($key, first, $replace, $replacement);
}
} @else if $key == $replace {
$key: $replacement;
}
@if $merge {
$map: map.merge($map, ($key: $value));
}
}
}
@return $map;
}
// Examples.
// $-map: (
// key replace: (map: 1),
// key-replace: (map: 2),
// );
// replace string key
// @debug key-replace($-map, key-replace, new-key); // (key replace: (map: 1), new-key: (map: 2))
// @debug key-replace(('bold king': 1, b: 2, c: 3), 'bold', 'hairy'); // ("hairy king": 1, b: 2, c: 3)
// @debug key-replace(('bold king': 1, b: 2, c: 3), 'o', 'ooo'); // ("booold king": 1, b: 2, c: 3)
// @debug key-replace((a: 1, b: 2, c: 3), 'a', 'd'); // (d: 1, b: 2, c: 3)
// replace string key with a list key
// @debug key-replace($-map, key-replace, new key); // (key replace: (map: 1), new key: (map: 2))
// replace list key
// @debug key-replace($-map, key replace, 'd'); // (d: 1, b: 2, c: 3)