Skip to content

Instantly share code, notes, and snippets.

@samad-aghaei
Created May 4, 2017 23:30
Show Gist options
  • Save samad-aghaei/2029770bcef9722414463a4161619925 to your computer and use it in GitHub Desktop.
Save samad-aghaei/2029770bcef9722414463a4161619925 to your computer and use it in GitHub Desktop.
This script can merge two multi dimensional associative array/objects in javascript by comparing given object with its reference and will remove additional given keys, adding missed parameteres and also validating values without overhead. Also it will return the default values if no input presented with re-usable reference! Has been tested on IE…
<!doctype html>
<html>
<head>
<meta charset="UTF-8">
<title>Untitled Document</title>
</head>
<body>
<script>
/**
This script can merge two multi dimensional associative array/objects in javascript by comparing given object with its reference and
will remove additional given keys, adding missed parameteres and also validating values without overhead. Also it will return the default values if no input presented with re-usable reference!
Has been tested on IE8 and greater.
**/
var module = (function(){
//To make our reference variable onchangable, have to put it into a function which is fster and more efficient than "JSON.parse(JSON.stringify(VARIABLE))"
var _defs = function(){
return {
//string, number and boolean are actually regex based validation keys on input values.
a: ["string", 'Defaul value for "a"'],
b: ["number", 300],
c: ["boolean", true],
d: {
da: ["boolean", true],
db: ["string", 'Defaul value for "db"'],
dc: {
dca: ["number", 200],
dcb: ["string", 'Default value for "dcb"'],
dcc: ["number", 500],
dcd: ["boolean", true]
},
dce: ["string", 'Default value for "dce"'],
},
e: ["number", 200],
f: ["boolean", 0],
g: ["", 'This is an internal extra parameter']
}
}
var _validation = {
number: function (defaultValue, userValue) {
if(/^[0-9]+$/.test(userValue)) //Only numbers allowed
return userValue;
else return defaultValue;
},
string: function (defaultValue, userValue) {
if(/^[a-zA-Z\s]*$/.test(userValue)) //Only A to Z case insentitive with space aloowed.
return userValue;
else return defaultValue;
},
boolean: function (defaultValue, userValue) {
if(typeof userValue === 'boolean') //True or False or 0 ,1
return userValue;
else return defaultValue;
}
}
var _uniqueMerge = function(opts, _ref){
for(var key in _ref)
if (_ref && _ref[key] && _ref[key].constructor && _ref[key].constructor === Object)
_ref[key] = _uniqueMerge((opts ? opts[key] : null ), _ref[key] );
else if(opts && opts.hasOwnProperty(key))
_ref[key] = _validation[_ref[key][0]](_ref[key][1], opts[key]); //or without validation on user enties => ref[key] = obj[key]
else _ref[key] = _ref[key][1];
return _ref;
}
var _get = function(inputs){
return _uniqueMerge(inputs, _defs());
}
return {
options: function(){
return _get(arguments[0] || null); // for more safety and control on number of input variables! used --> ( arguments[0] || null )
}
}
})();
//How to use it:
input_one = {
a : "Hello World",
//b : ["number", 400], //User missed this parameter
c: "Hi",
d : {
da : false,
db : "Hellow! World", // ! is not allowed
dc : {
dca : 10,
dcb : "My String",
dcc: "3thString",
dcd : false
},
dce: "ANOTHER STRING",
},
e: 40,
f: true,
z: 'x'
};
console.log( JSON.stringify( module.options(input_one), null ,2 ) );
//Output:
/*
{
"a": "Hello World",
"b": 300,
"c": true,
"d": {
"da": false,
"db": "Defaul value for \"db\"",
"dc": {
"dca": 10,
"dcb": "My String",
"dcc": 500,
"dcd": false
},
"dce": "ANOTHER STRING"
},
"e": 40,
"f": true,
"g": "This is an internal extra parameter"
}
*/
input_two = {
a : 32,
//b : ["number", 400], //User missed this parameter
c: "Hi",
d : {
da : false,
db : "HelloWorld",
dc : {
dca : 10,
dcb : "My String",
dcd : false
},
dce: 73,
}
};
console.log( JSON.stringify( module.options(input_two), null ,2 ) );
//output
/*
{
"a": "Defaul value for \"a\"",
"b": 300,
"c": true,
"d": {
"da": false,
"db": "HelloWorld",
"dc": {
"dca": 10,
"dcb": "My String",
"dcc": 500,
"dcd": false
},
"dce": "Default value for \"dce\""
},
"e": 200,
"f": 0,
"g": "This is an internal extra parameter"
}
*/
//Empty input will return the default values!
console.log( JSON.stringify( module.options(), null ,2 ) );
//Output
/*
{
"a": "Defaul value for \"a\"",
"b": 300,
"c": true,
"d": {
"da": true,
"db": "Defaul value for \"db\"",
"dc": {
"dca": 200,
"dcb": "Default value for \"dcb\"",
"dcc": 500,
"dcd": true
},
"dce": "Default value for \"dce\""
},
"e": 200,
"f": 0,
"g": "This is an internal extra parameter"
}
*/
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment