将post数据字符串化
function toObj(params) {
let str = "";
Object.keys(params).forEach((key) => {
str += `.${key.toString()}${judge(params[key])}`;
});
return str;
}
function toArr(params) {
let str = "";
params.forEach((item, index) => {
str += `[${index.toString()}]${judge(item)}`;
});
return str;
}
function judge(data) {
if (data instanceof Array) {
return toArr(data);
} else if (data instanceof Object) {
return toObj(data);
}
return `=${encodeURI(data.toString())}&`;
}
function frame(params) {
params = params.replace(/\&\.+/gim, "&");
params = params.replace(/^\./, "");
params = params.replace(/\=\&$/i, "");
params = params.replace(/\&$/i, "");
return params;
}
function objToUrl(params) {
return frame(judge(params));
}
module.exports = {
objToUrl,
};