-
Notifications
You must be signed in to change notification settings - Fork 7.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
71 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
/** | ||
* Utility functions namespace | ||
* @namespace | ||
* @type {Object} | ||
*/ | ||
vjs.util = {}; | ||
|
||
/** | ||
* Merge two options objects, | ||
* recursively merging any plain object properties as well. | ||
* Previously `deepMerge` | ||
* | ||
* @param {Object} obj1 Object to override values in | ||
* @param {Object} obj2 Overriding object | ||
* @return {Object} New object -- obj1 and obj2 will be untouched | ||
*/ | ||
vjs.util.mergeOptions = function(obj1, obj2){ | ||
var key, val1, val2; | ||
|
||
// make a copy of obj1 so we're not ovewriting original values. | ||
// like prototype.options_ and all sub options objects | ||
obj1 = vjs.obj.copy(obj1); | ||
|
||
for (key in obj2){ | ||
if (obj2.hasOwnProperty(key)) { | ||
val1 = obj1[key]; | ||
val2 = obj2[key]; | ||
|
||
// Check if both properties are pure objects and do a deep merge if so | ||
if (vjs.obj.isPlain(val1) && vjs.obj.isPlain(val2)) { | ||
obj1[key] = vjs.util.mergeOptions(val1, val2); | ||
} else { | ||
obj1[key] = obj2[key]; | ||
} | ||
} | ||
} | ||
return obj1; | ||
}; | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
module('util'); | ||
|
||
test('should merge options objects', function(){ | ||
var ob1, ob2, ob3; | ||
|
||
ob1 = { | ||
a: true, | ||
b: { b1: true, b2: true, b3: true }, | ||
c: true | ||
}; | ||
|
||
ob2 = { | ||
// override value | ||
a: false, | ||
// merge sub-option values | ||
b: { b1: true, b2: false, b4: true }, | ||
// add new option | ||
d: true | ||
}; | ||
|
||
ob3 = vjs.util.mergeOptions(ob1, ob2); | ||
|
||
deepEqual(ob3, { | ||
a: false, | ||
b: { b1: true, b2: false, b3: true, b4: true }, | ||
c: true, | ||
d: true | ||
}, 'options objects merged correctly'); | ||
}); |