我正在寻找删除重复代码的许多行。问题基本上是生成JSON。
搜索条件的最后一行具有“ hasValue”属性或“ value”属性。它是用TypeScript编写的,我不能在其中使用{}标记来编写JavaScript代码。
该代码按原样工作,但是95%是重复代码,因为在hasValue = false的情况下需要使用不同的属性。
有没有更简洁的书写方式?
export const valueSearch = (value: string, hasEmptyValue: boolean, profile: ProfileHolder, attributeId: string, objectTypeId: string, typeName: string = "ValueSearch") => {
if (!hasValue) {
return {
"__typename": "SearchFromProfile",
"profileHolder": {
"__typename": profile.__typename,
"Id": profile.Id
},
"search": {
"__typename": "HasValue",
"objectTypeId": objectTypeId,
"attributeId": attributeId,
"hasValue": false
}
};
}
else {
return {
"__typename": "SearchFromProfile",
"profileHolder": {
"__typename": profile.__typename,
"Id": profile.Id
},
"search": {
"__typename": typeName,
"objectTypeId": objectTypeId,
"attributeId": attributeId,
"value": value
}
};
}
};
相关分类