From 2aa09fd15aecdf366f355725e6c788d73f7aab97 Mon Sep 17 00:00:00 2001 From: Steve Heffernan Date: Mon, 2 Dec 2013 17:03:32 -0800 Subject: [PATCH] Close GH-862: Add util namespace. --- build/source-loader.js | 1 + src/js/component.js | 2 +- src/js/util.js | 40 ++++++++++++++++++++++++++++++++++++++++ test/unit/util.js | 29 +++++++++++++++++++++++++++++ 4 files changed, 71 insertions(+), 1 deletion(-) create mode 100644 src/js/util.js create mode 100644 test/unit/util.js diff --git a/build/source-loader.js b/build/source-loader.js index af58d8ad64..a1890929bb 100644 --- a/build/source-loader.js +++ b/build/source-loader.js @@ -19,6 +19,7 @@ var sourceFiles = [ "src/js/core-object.js", "src/js/events.js", "src/js/lib.js", + "src/js/util.js", "src/js/component.js", "src/js/button.js", "src/js/slider.js", diff --git a/src/js/component.js b/src/js/component.js index 60d72de735..a1b14948ca 100644 --- a/src/js/component.js +++ b/src/js/component.js @@ -169,7 +169,7 @@ vjs.Component.prototype.options_; vjs.Component.prototype.options = function(obj){ if (obj === undefined) return this.options_; - return this.options_ = vjs.obj.deepMerge(this.options_, obj); + return this.options_ = vjs.util.mergeOptions(this.options_, obj); }; /** diff --git a/src/js/util.js b/src/js/util.js new file mode 100644 index 0000000000..eba095fb20 --- /dev/null +++ b/src/js/util.js @@ -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; +}; + + diff --git a/test/unit/util.js b/test/unit/util.js new file mode 100644 index 0000000000..4fe49c8468 --- /dev/null +++ b/test/unit/util.js @@ -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'); +}); \ No newline at end of file