1 | /**
|
2 | * Removes a key and all it's referencies form a map. This function oeprates withous side-effects
|
3 | * and returns a new map.
|
4 | *
|
5 | * @param {Map} map - Map to remove the key from
|
6 | * @param {string} key - Key to remove from the map
|
7 | * @return {Map} New map without the passed key
|
8 | */
|
9 | function removeDeepFromMap(map, key) {
|
10 | const newMap = new Map();
|
11 |
|
12 | for (const [aKey, val] of map) {
|
13 | if (aKey !== key && val instanceof Map) {
|
14 | newMap.set(aKey, removeDeepFromMap(val, key));
|
15 | } else if (aKey !== key) {
|
16 | newMap.set(aKey, val);
|
17 | }
|
18 | }
|
19 |
|
20 | return newMap;
|
21 | }
|
22 |
|
23 | module.exports = removeDeepFromMap;
|