0

This questions merges :P the two following questions:

How can I deep merge a Map?

(the excellent deepmerge library does not seem to support maps)

lhk
  • 27,458
  • 30
  • 122
  • 201
  • in my scenario it's nested maps up to a final level of objects. if the objects are not deep-merged it's fine for me. but it would be nice to get an answer that is as general as possible. – lhk May 12 '20 at 15:58
  • Just join the two approaches you found? Please post the code you tried. – Bergi May 12 '20 at 16:13

1 Answers1

0

Well, @Bergi's comment is right of course, I should provide my attempt at a solution. So far my best version is the following. I'll update when I find something nicer. (EDIT: updated, now with overloads and typing):

function merge<K>(a:K, b:K):K;
function merge<T, K>(a: Map<T, K>, b: Map<T, K>): Map<T, K>;
function merge<T, K>(a: Map<T, K>, b: Map<T, K>):any{

    // overload 1
    if (!(typeof (a) == "object")) {
        return b;
    }
    if (!(typeof (b) == "object")) {
        return a;
    }

    // overload 2
    let merged = new Map();
    a.forEach((a_value, key)=>{
        const b_value = b.get(key);
        if(b_value){
            merged.set(key, merge(a_value, b_value));
        }
        else{
            merged.set(key, a_value);
        }
    })
    b.forEach((b_value, key)=>{
        const a_value = a.get(key);
        if(!a_value){
            merged.set(key, b_value);
        }
    })
    return merged;
}



let a = new Map<string, Map<string, number>>();
a.set('a', new Map(Object.entries({ 'c': 1 })));
a.set('b', new Map(Object.entries({ 'c': 1 })));
let b = new Map<string, Map<string, number>>();
b.set('b', new Map(Object.entries({ 'd': 2 })));

let merged = merge(a,b);
console.log(merged)
lhk
  • 27,458
  • 30
  • 122
  • 201