function serialize(arg) {
var res = '';
var replaceNamePop = {
modules: 'module',
categories: 'category',
searchFieldsList: 'searchFieldList',
};
Object.keys(arg).forEach(function(key) {
if(Array.isArray(this[key])) {
for(var j = 0; j < this[key].length; j++) {
if(typeof replaceNamePop[key] != 'undefined')
res += '&' + replaceNamePop[key] + '=' + this[key][j];
else
res += '&' + key + '=' + this[key][j];
}
}else{
if(typeof replaceNamePop[key] != 'undefined')
res += '&' + replaceNamePop[key] + '=' + this[key];
else
res += '&' + key + '=' + this[key];
}
}, arg);
return res;
}
serialize({
"modules": [20],
"categories" : [20],
'keyword': "keyword",
'time':"time",
'catRelation':"cat",
'sortField':"sort",
'sortDirection':"ASC",
'searchFieldsList':"list",
});
serialize({
"modules": [20, 25],
"categories" : [20, 58],
'keyword': "keyword",
'time':"time",
'catRelation':"cat",
'sortField':"sort",
'sortDirection':"ASC",
'searchFieldsList':"list",
});
serialize({
"modules": [20, 25],
'keyword': "keyword",
});
interface Dictionary<T> {
[key: string]: T
}
function serialize<T extends Dictionary<string | number[]>> (arg: T) {
var res = '';
var replaceNamePop: Dictionary<string> = {
modules: 'module',
categories: 'category',
searchFieldsList: 'searchFieldList'
};
Object.keys(arg).forEach(function (this: T, key) {
if (Array.isArray(this[key])) {
for (var j = 0; j < this[key].length; j++) {
if (typeof replaceNamePop[key] != 'undefined')
res += '&' + replaceNamePop[key] + '=' + this[key][j];
else
res += '&' + key + '=' + this[key][j];
}
} else {
if (typeof replaceNamePop[key] != 'undefined')
res += '&' + replaceNamePop[key] + '=' + this[key];
else
res += '&' + key + '=' + this[key];
}
}, arg);
return res;
}
serialize({
'modules': [20],
'categories': [20],
'keyword': 'keyword',
'time': 'time',
'catRelation': 'cat',
'sortField': 'sort',
'sortDirection': 'ASC',
'searchFieldsList': 'list'
});
serialize({
'modules': [20, 25],
'categories': [20, 58],
'keyword': 'keyword',
'time': 'time',
'catRelation': 'cat',
'sortField': 'sort',
'sortDirection': 'ASC',
'searchFieldsList': 'list'
});
serialize({
'modules': [20, 25],
'keyword': 'keyword'
});