There is a function to convert object to the GET parameters for the ASP.Net controller. Is it possible to minimize the redundancy of the code, but not to the detriment of its readability?
function getUrlPairs(obj, prefix = "") {
var getTypeOf = function (obj) {
if (typeof obj == "undefined") {
return "undefined";
}
if (obj === null) {
return "null";
}
return Object.getPrototypeOf(obj).constructor.name;
};
var urlPairs = [],
objType = getTypeOf(obj),
propType;
if (objType === "Object") {
for (var propName in obj) {
if (obj.hasOwnProperty(propName)) {
propType = getTypeOf(obj[propName]);
switch (propType) {
case "String":
urlPairs.push(prefix + propName + "=" + encodeURIComponent(obj[propName]));
break;
case "Number":
case "Boolean":
case "null":
urlPairs.push(prefix + propName + "=" + obj[propName]);
break;
case "Date":
urlPairs.push(prefix + propName + "=" + encodeURIComponent(obj[propName].toJSON()));
break;
case "Object":
urlPairs = urlPairs.concat(getUrlPairs(obj[propName], prefix + propName + "."));
break;
case "Array":
urlPairs = urlPairs.concat(getUrlPairs(obj[propName], prefix + propName));
break;
default:
break;
}
}
}
} else if (objType === "Array") {
for (var i = 0; i < obj.length; i++) {
propType = getTypeOf(obj[i]);
switch (propType) {
case "String":
urlPairs.push(prefix + "[" + i + "]" + "=" + encodeURIComponent(obj[i]));
break;
case "Number":
case "Boolean":
case "null":
urlPairs.push(prefix + "[" + i + "]=" + obj[i]);
break;
case "Date":
urlPairs.push(prefix + "[" + i + "]=" + encodeURIComponent(obj[i].toJSON()));
break;
case "Object":
urlPairs = urlPairs.concat(getUrlPairs(obj[i], prefix + "[" + i + "]" + "."));
break;
case "Array":
urlPairs = urlPairs.concat(getUrlPairs(obj[i], prefix + "[" + i + "]"));
break;
default:
break;
}
}
} else {
throw "support only arrays and objects";
}
return urlPairs;
}
var obj = {
dirty: "&%[]?",
nullable: null,
numberProp: 123,
now: new Date(),
boolProp: true,
obj: {
objProp1: "objStr",
objProp2: false,
nullable: null,
objInObj: {
aa:"aa",
bb: true
}
},
arr: [{
arrProp11: "str",
arrProp12: 321,
arrProp13: true,
arrProp14: {
a: "absdefg",
b: true
}
},
{
arrProp21: "rts",
arrProp22: 987,
arrProp23: false
},
["arrInArr1", "arrInArr2"],
[{test:"str"}],
null,
new Date()
]
},
getTypeOf = function(obj) {
if (typeof obj == "undefined") { return "undefined"; }
if (obj === null) { return "null"; }
return Object.getPrototypeOf(obj).constructor.name;
},
getUrlPairs = function(obj, prefix = "") {
var urlPairs = [],
objType = getTypeOf(obj),
propType;
if (objType === "Object") {
for (var propName in obj) {
if (obj.hasOwnProperty(propName)) {
propType = getTypeOf(obj[propName]);
switch (propType) {
case "String":
urlPairs.push(prefix + propName + "=" + encodeURIComponent(obj[propName]));
break;
case "Number":
case "Boolean":
case "null":
urlPairs.push(prefix + propName + "=" + obj[propName]);
break;
case "Date":
urlPairs.push(prefix + propName + "=" + encodeURIComponent(obj[propName].toJSON()));
break;
case "Object":
urlPairs = urlPairs.concat(getUrlPairs(obj[propName], prefix + propName + "."));
break;
case "Array":
urlPairs = urlPairs.concat(getUrlPairs(obj[propName], prefix + propName));
break;
default:
break;
}
}
}
} else if (objType === "Array") {
for (var i = 0; i < obj.length; i++) {
propType = getTypeOf(obj[i]);
switch (propType) {
case "String":
urlPairs.push(prefix + "["+i+"]" + "=" + encodeURIComponent(obj[i]));
break;
case "Number":
case "Boolean":
case "null":
urlPairs.push(prefix + "["+i+"]=" + obj[i]);
break;
case "Date":
urlPairs.push(prefix + "["+i+"]=" + encodeURIComponent(obj[i].toJSON()));
break;
case "Object":
urlPairs = urlPairs.concat(getUrlPairs(obj[i], prefix + "["+i+"]" + "."));
break;
case "Array":
urlPairs = urlPairs.concat(getUrlPairs(obj[i], prefix + "["+i+"]"));
break;
default: break;
}
}
}
else { throw "support only arrays and objects"; }
return urlPairs;
};
var res = getUrlPairs(obj);
document.getElementById("output").innerHTML = res.join("\n") + "\n\n" + res.join("&");
body, textarea {
margin: 0;
overflow:hidden;
}
textarea {
width: 100vw;
height: 100vh;
overflow:hidden;
resize: none;
}
<textarea id="output"></textarea>
obj
in example above. \$\endgroup\$JSON.stringify()
, this could be written in a few lines of code with the JSON object. \$\endgroup\$