Skip to content

Commit b1b03c1

Browse files
committed
Added Object.Merge and Object.MergeRight.
1 parent bd462e2 commit b1b03c1

3 files changed

Lines changed: 47 additions & 1 deletion

File tree

v3/src/utils/object/Merge.js

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
// Creates a new Object using all values from obj1 and obj2.
2+
// If a value exists in both obj1 and obj2, the value in obj1 is used.
3+
4+
var Clone = require('./Clone');
5+
6+
var Merge = function (obj1, obj2)
7+
{
8+
var clone = Clone(obj1);
9+
10+
for (var key in obj2)
11+
{
12+
if (!clone.hasOwnProperty(key))
13+
{
14+
clone[key] = obj2[key];
15+
}
16+
}
17+
18+
return clone;
19+
};
20+
21+
module.exports = Merge;

v3/src/utils/object/MergeRight.js

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
// Creates a new Object using all values from obj1.
2+
//
3+
// Then scans obj2. If a property is found in obj2 that *also* exists in obj1,
4+
// the value from obj2 is used, otherwise the property is skipped.
5+
6+
var Clone = require('./Clone');
7+
8+
var MergeRight = function (obj1, obj2)
9+
{
10+
var clone = Clone(obj1);
11+
12+
for (var key in obj2)
13+
{
14+
if (clone.hasOwnProperty(key))
15+
{
16+
clone[key] = obj2[key];
17+
}
18+
}
19+
20+
return clone;
21+
};
22+
23+
module.exports = MergeRight;

v3/src/utils/object/index.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ module.exports = {
77
GetAdvancedValue: require('./GetAdvancedValue'),
88
GetMinMaxValue: require('./GetMinMaxValue'),
99
GetValue: require('./GetValue'),
10-
IsPlainObject: require('./IsPlainObject')
10+
IsPlainObject: require('./IsPlainObject'),
11+
Merge: require('./Merge'),
12+
MergeRight: require('./MergeRight')
1113

1214
};

0 commit comments

Comments
 (0)