Welcome to mirror list, hosted at ThFree Co, Russian Federation.

ObjectMerger.js « Utility « src - git.mdns.eu/nextcloud/passwords-client.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: e1d31f5f409032588521e211a1e53b9c760dfeb8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
import ObjectClone from './ObjectClone';

class ObjectMerger {
    merge(target, source) {
        for(let key in source) {
            if(!source.hasOwnProperty(key)) continue;

            if(!target.hasOwnProperty(key) || target[key] === null) {
                target[key] = ObjectClone.clone(source[key]);
            }

            let targetValue = target[key],
                sourceValue = source[key];

            if(typeof targetValue === 'object' && typeof sourceValue === 'object') {
                target[key] = this.merge(targetValue, sourceValue);
            } else if(Array.isArray(targetValue) && Array.isArray(sourceValue)) {
                target[key] = targetValue.concat(sourceValue);
            } else {
                target[key] = ObjectClone.clone(sourceValue);
            }
        }

        return target;
    }
}

export default new ObjectMerger();