Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Download
81158 views
1
/**
2
* Copyright 2013-2014, Facebook, Inc.
3
* All rights reserved.
4
*
5
* This source code is licensed under the BSD-style license found in the
6
* LICENSE file in the root directory of this source tree. An additional grant
7
* of patent rights can be found in the PATENTS file in the same directory.
8
*
9
* @providesModule mergeDeep
10
*/
11
12
"use strict";
13
14
var mergeHelpers = require('mergeHelpers');
15
var mergeDeepInto = require('mergeDeepInto');
16
17
var checkArrayStrategy = mergeHelpers.checkArrayStrategy;
18
var checkMergeObjectArgs = mergeHelpers.checkMergeObjectArgs;
19
var normalizeMergeArg = mergeHelpers.normalizeMergeArg;
20
21
/**
22
* @see mergeDeepImpl comments.
23
*
24
* @param {!Object} one returned will be an structural extension of this.
25
* @param {!Object} two values from two take precedence over values in one.
26
* @param {?Enum=} arrayStrategy One of `arrayStrategies`.
27
* @return {!Object} the extension of one by two.
28
*/
29
var mergeDeep = function(oneParam, twoParam, arrayStrategy) {
30
var one = normalizeMergeArg(oneParam);
31
var two = normalizeMergeArg(twoParam);
32
checkMergeObjectArgs(one, two);
33
checkArrayStrategy(arrayStrategy);
34
var newObj = {};
35
// TODO: This is horribly inefficient. Even when some deep object in `one`
36
// will be clobbered by another entity in `two`, we perform a clone of that
37
// entire structure. To make this code more efficient, we'll need to either
38
// enhance `mergeDeepInto` to accept multiple arguments or mirror that code
39
// here.
40
mergeDeepInto(newObj, one, arrayStrategy);
41
mergeDeepInto(newObj, two, arrayStrategy);
42
return newObj;
43
};
44
45
46
module.exports = mergeDeep;
47
48