/**
* 格式化请求对象
* @param obj
* @param skipEmpty 是否忽略空值
* @param trim 如果是字符串,是否需要trim,默认执行
*/
formatRequest(obj: object, skipEmpty: boolean= true, trim: boolean= true) {
if (!obj) { return; }
const resoult = {};
Object.keys(obj).forEach(key => {
if (obj[key] !== undefined && obj[key] !== null) {
if (obj[key] === '') {
if (!skipEmpty) { resoult[key] = ''; }
} else if (obj[key] instanceof Array) {
resoult[key] = obj[key].map(item => this.formatRequest(item));
else if (obj[key] instanceof Object) {
resoult[key] = this.formatRequest(obj[key]);
} else {
if (typeof obj[key] === 'string' && trim) {
const trimedStr = (obj[key] as string).trim();
if (trimedStr === '' && skipEmpty) {
return;
} else {
obj[key] = trimedStr;
}
}
resoult[key] = obj[key];
}
}
});
return resoult;
}
网友评论