作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我的代码运行良好,我只是想继续通过良好的编码实践来解决此搜索功能。我觉得我重复相同的代码太多次了。我有两种不同类型的对象“程序”和“租赁”,如果用户在搜索菜单中选择其中之一,我只想在我的数据库中搜索指定的对象,但是如果他选择两者或都不选择,我想执行这两个搜索功能。为了实现这一目标,我有一个条件 if 来检查这些搜索选项,如果只选择一个,我将在相应的架构上执行相应的 find(),
if((queryObj.selectedSearchObjectType.indexOf("rentals") > -1))
{
let foundRentalsByTitle = await Promise.all(titleSplited.map((value) =>
getRentalsByQuery({title: { $regex: new RegExp('\\b' + value.toLowerCase() + '\\b', 'i') } } )
));
searchResults.rentals = searchResults.rentals.concat(foundRentalsByTitle);
}
但是,您可能会注意到,我重复相同的代码 4 次只是为了实现此目的,我想制定良好的编码实践,因此我认为这看起来丑陋且效率低下。
if(queryObj.title) //If we have a title seacrh
{
queryObj.title = queryObj.title.trim();
const titleSplited = queryObj.title.split(' ');//transform it to an array
if(titleSplited.length > 0)
{
// Check if selectedSearchObjectType is defined to execute queryes selectively
if(queryObj.selectedSearchObjectType)
{
// Check if selectedSearchObjectType contains 'rentals' in order to add the result to finalResults Array
if((queryObj.selectedSearchObjectType.indexOf("rentals") > -1))
{
let foundRentalsByTitle = await Promise.all(titleSplited.map((value) =>
getRentalsByQuery({title: { $regex: new RegExp('\\b' + value.toLowerCase() + '\\b', 'i') } } )
));
searchResults.rentals = searchResults.rentals.concat(foundRentalsByTitle);
}
// Check if selectedSearchObjectType contains 'programs' in order to add the result to finalResults Array
if((queryObj.selectedSearchObjectType.indexOf("programs") > -1))
{
let foundProgramsByTitle = await Promise.all(titleSplited.map((value) =>
getRentalsByQuery({title: { $regex: new RegExp('\\b' + value.toLowerCase() + '\\b', 'i') } } )
));
searchResults.programs = searchResults.programs.concat(foundProgramsByTitle);
}
}else{
//If no selectedSearchObjectType was selected then execute both
let foundProgramsByTitle = await Promise.all(titleSplited.map((value) =>
getRentalsByQuery({title: { $regex: new RegExp('\\b' + value.toLowerCase() + '\\b', 'i') } } )
));
let foundRentalsByTitle = await Promise.all(titleSplited.map((value) =>
getRentalsByQuery({title: { $regex: new RegExp('\\b' + value.toLowerCase() + '\\b', 'i') } } )
));
searchResults.rentals = searchResults.rentals.concat(foundRentalsByTitle);
searchResults.programs = searchResults.programs.concat(foundProgramsByTitle);
}
// Filter results code ...
// .........................
// End of Function
}
关于如何将其重构为更好的版本有什么想法吗?
最佳答案
可以是这样的:
if(!queryObj.title) return; // just exit from the function or return representation of an empty result
queryObj.title = queryObj.title.trim();
const titleSplited = queryObj.title.split(' ');//transform it to an array
if(titleSplited.length <= 0) return; // same as above
// define the holder for search operations
const searchPromises = {};
if(!queryObj.selectedSearchObjectType || queryObj.selectedSearchObjectType.indexOf("rentals") > -1)
{
searchPromises.rentals = Promise.all(titleSplited.map((value) =>
getRentalsByQuery({title: { $regex: new RegExp('\\b' + value.toLowerCase() + '\\b', 'i') } } )
));
}
if(!queryObj.selectedSearchObjectType || queryObj.selectedSearchObjectType.indexOf("programs") > -1)
{
searchPromises.programs = Promise.all(titleSplited.map((value) =>
getProgramsByQuery({title: { $regex: new RegExp('\\b' + value.toLowerCase() + '\\b', 'i') } } )
));
}
if (searchPromises.rentals) {
searchResults.rentals = searchResults.rentals.concat(await searchPromises.rentals);
}
if (searchPromises.programs) {
searchResults.programs = searchResults.programs.concat(await searchPromises.programs);
}
// Filter results code ...
// .........................
// End of Function
稍后我们可以定义搜索类型并提取搜索功能:
const RENTALS = 'rentals';
const PROGRAMS = 'programs';
const searchFns = {
[RENTALS]: (value) => getRentalsByQuery({title: { $regex: new RegExp('\\b' + value.toLowerCase() + '\\b', 'i') } } ),
[PROGRAMS]: (value) => getProgramsByQuery({title: { $regex: new RegExp('\\b' + value.toLowerCase() + '\\b', 'i') } } )
};
所以你可以将你的函数更改为:
if(!queryObj.title) return; // just exit from the function or return representation of an empty result
queryObj.title = queryObj.title.trim();
const titleSplited = queryObj.title.split(' ');//transform it to an array
if(titleSplited.length <= 0) return; // same as above
// define the holder for search operations
const searchPromises = {};
const searchTypes = [RENTALS, PROGRAMS];
searchTypes.forEach((type) => {
if(!queryObj.selectedSearchObjectType || queryObj.selectedSearchObjectType.indexOf(type) > -1)
{
searchPromises[type] = Promise.all(titleSplited.map(searchFns[type]));
}
});
// iterate over created promises
for (let type in searchPromises) {
searchResults[type] = searchResults[type].concat(await searchPromises[type])
}
// Filter results code ...
// .........................
// End of Function
关于javascript - 有没有办法将此 Promises 调用重构为 "best coding practices"版本?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54386647/
我是一名优秀的程序员,十分优秀!