Skip to content

Instantly share code, notes, and snippets.

@Kreozot
Last active August 29, 2015 14:21
Show Gist options
  • Save Kreozot/c7396a0fe93ae7c29e53 to your computer and use it in GitHub Desktop.
Save Kreozot/c7396a0fe93ae7c29e53 to your computer and use it in GitHub Desktop.
Adds the function to an object that returns a parameter value or a default value (in case of parameter is missing in object)

Adds the function to an object that returns a parameter value or a default value (in case of parameter is missing in object)

Use cases:

  • Options backward compatability
  • Local and global parameters (global as defaults)
  • Getting missing localization strings

Example:

	var obj = {
		param1: "a",
		param2: 10,
		param3: {
			param4: {
				param5: 25
			}
		}
	};
	var defaultObj = {
		param1: "a1",
		param2: 11,
		param3: {
			param4: {
				param5: 25,
				param6: 25
			}
		},
		param7: "b"
	};

	initDefaultParams(obj, defaultObj);

	var param1 = obj.getParamOrDefault('param1'); //param1 = "a"
	var param7 = obj.getParamOrDefault('param7'); //param7 = "b"
	var param345 = obj.getParamOrDefault('param3.param4.param5'); //param345 = 25
	var param346 = obj.getParamOrDefault('param3.param4.param6'); //param346 = 25
/**
* Initialize default params for object and method to get them
* @param {Object} obj Object with params
* @param {Object} defaultParams Default params object
* @return {Object} Object with "getParamOrDefault" method
*/
function initDefaultParams(obj, defaultParams) {
/**
* Get parameter from options or from default options (in case of missing parameter)
* @param {String} param Parameter name (e.g. "chart.title.font.size")
* @return {Object} Parameter value
*/
var getParamOrDefault = function(param) {
/**
* Get parameter from options natively or return undefined
* @param {Array} params Array of param names chain ("title.font.size" must be sent as ["title", "font", "size"])
* @param {Object} parentParam Parent parameter value
* @return {Object} Parameter value or undefined in case of parameter missing
*/
var getParams = function(params, parentParam) {
var firstParam = params.shift();
var firstParamValue = parentParam[firstParam];
if (firstParamValue != undefined) {
if (params.length == 0) {
return firstParamValue;
} else {
return getParams(params, firstParamValue);
}
} else {
return undefined;
}
};
var params = param.split('.');
var paramValue = getParams(params, this);
if (paramValue == undefined) {
params = param.split('.');
paramValue = getParams(params, defaultParams);
}
return paramValue;
};
obj.getParamOrDefault = getParamOrDefault;
return obj;
}
'use strict';
describe('Default params', function() {
var obj = {
param1: "a",
param2: 10,
param3: {
param4: {
param5: 25
}
}
};
var defaultObj = {
param1: "a1",
param2: 11,
param3: {
param4: {
param5: 25,
param6: 25
}
},
param7: "b"
};
initDefaultParams(obj, defaultObj);
it("Getting params from object", function() {
var param1 = obj.getParamOrDefault('param1');
expect(param1).toBe(obj.param1);
});
it("Getting nesting params from object", function() {
var param345 = obj.getParamOrDefault('param3.param4.param5');
expect(param345).toBe(obj.param3.param4.param5);
});
it("Getting missing params from default object", function() {
var param7 = obj.getParamOrDefault('param7');
expect(param7).toBe(defaultObj.param7);
});
it("Getting missing nesting params from default object", function() {
var param346 = obj.getParamOrDefault('param3.param4.param6');
expect(param346).toBe(defaultObj.param3.param4.param6);
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment