chuck.js/app/Lib/Utilities/Options.js
logsol 05c4e4de81 Refactoring of Options. Renamed to OptionsHelper
Because that is what it is.
2016-10-10 23:10:43 +02:00

56 lines
No EOL
1.2 KiB
JavaScript

define([
"Lib/Utilities/Exception"
],
function (Exception) {
"use strict";
function OptionsHelper() {
}
OptionsHelper.prototype.merge = function(options, preset) {
if(!preset && !options) {
throw new Exception("OptionsHelper requires objects");
}
if(preset.constructor !== Object && options.constructor !== Object) {
throw new Exception("OptionsHelper requires objects");
}
if(!preset || preset.constructor !== Object) {
return options;
}
if(!options || options.constructor !== Object) {
return preset;
}
// FIXME there is a bad bug here, the preset is being manipulated by reference, so no config can be used!
// hotfix for the bug
preset = JSON.parse(JSON.stringify(preset));
for (var key in options) {
if(!preset.hasOwnProperty(key)) {
preset[key] = options[key];
} else {
if(options[key] === undefined) {
continue;
}
if(options[key].constructor !== Object) {
preset[key] = options[key];
} else {
preset[key] = OptionsHelper.prototype.merge.call(this, options[key], preset[key]);
}
}
}
return preset;
}
return new OptionsHelper();
});