f12e7371e7d186727eb968a686a1d9576787b123
[platform/framework/web/crosswalk-tizen.git] /
1 var arrayEach = require('./arrayEach'),
2     baseMergeDeep = require('./baseMergeDeep'),
3     getSymbols = require('./getSymbols'),
4     isArray = require('../lang/isArray'),
5     isLength = require('./isLength'),
6     isObject = require('../lang/isObject'),
7     isObjectLike = require('./isObjectLike'),
8     isTypedArray = require('../lang/isTypedArray'),
9     keys = require('../object/keys');
10
11 /** Used for native method references. */
12 var arrayProto = Array.prototype;
13
14 /** Native method references. */
15 var push = arrayProto.push;
16
17 /**
18  * The base implementation of `_.merge` without support for argument juggling,
19  * multiple sources, and `this` binding `customizer` functions.
20  *
21  * @private
22  * @param {Object} object The destination object.
23  * @param {Object} source The source object.
24  * @param {Function} [customizer] The function to customize merging properties.
25  * @param {Array} [stackA=[]] Tracks traversed source objects.
26  * @param {Array} [stackB=[]] Associates values with source counterparts.
27  * @returns {Object} Returns `object`.
28  */
29 function baseMerge(object, source, customizer, stackA, stackB) {
30   if (!isObject(object)) {
31     return object;
32   }
33   var isSrcArr = isLength(source.length) && (isArray(source) || isTypedArray(source));
34   if (!isSrcArr) {
35     var props = keys(source);
36     push.apply(props, getSymbols(source));
37   }
38   arrayEach(props || source, function(srcValue, key) {
39     if (props) {
40       key = srcValue;
41       srcValue = source[key];
42     }
43     if (isObjectLike(srcValue)) {
44       stackA || (stackA = []);
45       stackB || (stackB = []);
46       baseMergeDeep(object, source, key, baseMerge, customizer, stackA, stackB);
47     }
48     else {
49       var value = object[key],
50           result = customizer ? customizer(value, srcValue, key, object, source) : undefined,
51           isCommon = result === undefined;
52
53       if (isCommon) {
54         result = srcValue;
55       }
56       if ((isSrcArr || result !== undefined) &&
57           (isCommon || (result === result ? (result !== value) : (value === value)))) {
58         object[key] = result;
59       }
60     }
61   });
62   return object;
63 }
64
65 module.exports = baseMerge;